- Home /
Mesh quad rendering order
Hi, I'm working on creating a little custom tilemap system in which I use a single mesh to contain a grid of quads that displays the desired sprites. The issue arises when I have sprites that extend the size of one tile and therefore overlap. When this happens the quads higher on the y-axis always draw on top of quads lower on the y axis. I've tried flipping the order in which they're drawn and such but the issue persists. Is there any way to control the sorting order individual quads are rendered on in a mesh?
A single mesh? I think all of the usual solutions involve multiple meshes and playing with the shader layer
As it stands right now it's one mesh. I'm starting out small though, I'll probably separate bigger meshes in the future. Though there has to be a way to set the rendering order of quads in a mesh, otherwise that would entail each quad being an individual mesh
Answer by Eno-Khaon · Jan 25, 2021 at 09:23 AM
In most circumstances, when you have a transparent-capable shader for your mesh, the lowest-index vertices will draw on top of the higher-index vertices of a single mesh. I reversed the order I generated vertices in a dynamic mesh a few weeks ago for this very reason.
(Drawing a mesh based on faded-to-opaque gradient vertex colors, the faded portions would draw on top of opaque, so I reversed the vertex order to draw from the opaque end first, toward the faded end)
By contrast, an opaque shader type will instead sort more reliably by depth.
As a general rule, transparent shaders will draw a mesh like this to save on draw calls. A single draw call for a single mesh is far more sane than an indeterminate number of them (i.e. per triangle?).
I'$$anonymous$$ a bit confused if you are talking about object sorting or per triangle sorting. Unity does not do per triangle sorting within a single mesh. The rendering order within a mesh is deter$$anonymous$$ed by the order of the triangles / primitives within the mesh. The vertex order is irrelevant as triangles could be made up of arbitrary vertices.
So when you procedurally generate a mesh with quads, just changing the order of the triangles / quads in the triangle / index buffer should be enough. Though of course spatial overlap could still cause issues when you use a shader that performs depth tests and writes to the depth buffer. Old isometric games did not use the depth buffer at all. they controlled everything with the rendering order.
Ah, whoops. I did forget to think that part through as I typed it out. By reversing vertex order in my own experiments, I effectively reversed triangle order by extension, so that would make perfect sense to be the more important element here (It was easier to reverse vertex order than just triangles due to how I organized and used that data anyway).
At any rate, the question specifically addresses self-overlapping within a single mesh, and my own experiments I related were based around each of a 3D-space and UI-space mesh generation at a single depth, where self-overlapping rules varied depending on shader properties.
Considering the manner in which rendering rules can change (in Unity, at least) with multiple meshes (Z-fighting, for example), it's important to also distinguish why the rules could/would be different when a mesh overlaps itself. Granted, the shader will still control the final appearance, where the triangle order can influence it in the case of self-overlap.
Your answer
Follow this Question
Related Questions
What order are a mesh's triangles rendered in? 0 Answers
How do you use submeshes correctly? 3 Answers
How do I apply a tilesheet to a procedural mesh and prevent bleeding? 0 Answers
3D Terrain or Mesh Grid 1 Answer
UV Tiling/repeat 1 Answer