Multiple override surface mesh flickering/Z fighting
Asked Answered
E

1

0

Hello,

I'm working on a shell-based grass shader to go with my procedurally generated terrain game. The terrain is an arrayMesh, and the grass is another arrayMesh that is just the top faces of the terrain repeated in layers, with spacing between each layer. I wanted to avoid having hundreds of duplicates of this grass meshinstance3D node in the scene, so when I generate the grass mesh I've been adding each 'shell' or layer of the grass as a surface using

grassMesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, grassDataArray);

and then setting its material with

grassObject.SetSurfaceOverrideMaterial(i,grassMaterial);

Anyway, the mesh generation is all set up and working however the surfaces are flickering and presumably z-fighting with each other when the camera moves, as well as having a very noticeable moire effect. It seems like all of the shell layers on the top half disappear, presumably getting sorted to the bottom at certain camera angles.

I know this is specifically being caused by the use of multiple overlapping override-material surfaces in one mesh, because the shader works fine if I manually create multiple meshinstance3D nodes with the same distance between vertices. It also has no flicker issue if the vertices are all on one surface however I cannot do this approach because each surface layer is floating separate from the other layers and there is no way to tell the triangle winding to have 'gaps' between triangles. So you end up with wonky triangles stretching across the mesh trying to connect every layer. I've tried changing my cameras near and far plane settings and it does not affect the flicker.

Does anyone know why the surface material overrides sort differently, and if there is any setting or anything I could change that would make them sort correctly? Can I manually tell the mesh which surfaces to draw first?

Estevez answered 17/2 at 22:2 Comment(0)
E
0

Ok I am big dumbo energy today. You can just use Material.RenderPriority on each surface to tell it the draw order. Crisis averted

Estevez answered 17/2 at 23:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.