#Draw order

51 messages · Page 1 of 1 (latest)

raven torrent
#

Hello!

I'm trying to understand how to set up my draw ordering properly. I have a tile map with a bunch of textured quads as trees/rocks etc. on top and real meshes(not flat like the quads) for players and other entities. It currently looks like this attached image. I'm trying to figure out how to:

  1. Properly sort the textured quads (this can be done by setting the Z of the transform since they're flat, only 1.0 Z). i.e. the rock is behind the tree.
  2. Have meshes stacked without z-fighting glitches. (This seems to work)
  3. Properly render the entity meshes in front/behind of trees/rocks. (This does not work currently).

Number 3 is what I'm very much struggling with. For example, if I set the Z to the Y value, it works well for the quads but an entity cannot fit in between for example Z = 50 and 51 since it isn't flat.

Is there any techniques to solve this problem? I don't know the terminology of 3D rendering enough to even understand what to search for to find possible solutions.

I can make the code available that was used to render the image if needed.

Thank you so much in advance!

#

Tiny addition: Red boxes are trees/rock tile positions. White boxes are entity tile positions.

severe tundra
#

I'm not sure what you mean when you say that an entity cannot fit

#

As far as I know, y sorting is the correct way to do this

raven torrent
#

What I meant is that if an entity isn't flat, how would I manage to fit it between the rock and the tree for example? Would have I have to:

let Z = Y * maximum_size_of_an_entity;

Like for example, the rock and the tree in the picture are textured quads but the entities (boxes) can be 32x64x32 or similar (actual 3d models).

#

The rock and the tree would need Z values that can fit a box in between, otherwise it'd protrude through the quad?

#

Maybe I'm thinking about this all wrong, but it's not like in 2D where the Z decides draw order completely

raven torrent
#

Tried to make a little recording of the issue. Here I'm not really calculating the Z value at all for the entity, I just set it to one to illustrate my problem!

#

So since it's not flat it can be partly behind and partly in front

severe tundra
#

wait so, you're merging 2d and 3d?

#

It seems to me it would be easier to just go full 3d at that point and render the base map as just one big plane

raven torrent
#

It's all 3D, The rock is one quad, the tree is one quad. The green background is a quad too (low Z though so it's always behind)

#

It's just one 3d camera, no 2d sprites or anything

#

But I guess you can kind of consider the quads to be 2d?

severe tundra
#

Oh, ok I think I get it. So the trees and rocks are just a single quad, but your boxes are full 3d models which creates the clipping we see in the video

raven torrent
#

Exactly!

severe tundra
#

So I guess the issue is that your box is larger than 1 tile

raven torrent
#

The box is "standing" on a specific tile (the white square) and that's all that matter for ordering vs the Tree and Rock (red squares)

#

so the Y position of the white/red squares is what matters and what I use to set the Z value, but since the entity box mesh isn't just a thin quad it clips through

#

I can't really wrap my head around how to solve this.. the only thing I came up with was to set the near/far on the camera to a massive number and have a lot of spacing between the objects:

let z = y * 200.0;

or similar.

#

Either that or render all the entities to textures so they're "flat" as well.. but then I guess two entities could Z-fight if they stack on the same tile?

severe tundra
#

one thing you could do is maybe play with the depth_bias of the meshes

raven torrent
severe tundra
raven torrent
#

(assuming I render entities to textures first)

severe tundra
raven torrent
#

Oh, I've never used it before, could be worth looking into.. but you think it's a good idea to render the entities to textures?

#

I can't really think of another way to solve it

severe tundra
raven torrent
#

Ouch, I'd need realtime since they're animated and can rotate while moving etc. definitely not feasible to make spritesheets for all of this

severe tundra
#

right, in that case I'm not sure. I think it might just be an issue where your quads are just not far enough apart in world space because I don't get how you'd get that clipping if the quads are at the correct distance in 3d

raven torrent
#

You mean with actual meshes (not rendered to textures)?

#

I guess I'm not really sure how "far apart" in Z I should place the quads depending on Y

severe tundra
#

It seems to me like there's some scale that might be wrong here

raven torrent
#

I mean is it feasible to have like a near 0.0 and far 1_000_000.0 on the camera and just do:

let z = y * 200.0;
#

(200 being more than the thickest entity)

#

I'm not sure how large values I can work with here, sorry I'm pretty new to this.

#

I just know I want to base my Z value on Y but I'm not really sure what the best practice is here.

severe tundra
#

I'm not sure either how to fix this to be honest. I pretty much only work with pure 3d stuff

raven torrent
#

Maybe I should give it a try then? Just make the Z difference really big between each Y and see how it works..

raven torrent
severe tundra
#

yeah, certainly worth a try, shouldn't be too hard

raven torrent
#

Yep! Will do first thing in the morning, got to catch some sleeps now 😰

severe tundra
raven torrent
#

🙏 Big thanks and I'll pop in here tomorrow and let you know how it all went haha.

raven torrent
#

@severe tundra seems like this definitely has potential. Setting Near = 0.0 and Far = f32::MAX introduced a lot of z-fighting, however setting Far = 1_000_000 seems to work?

#

This is with a very simple:

let world_z = world_y * 500.0 + world_x * 10.0;