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?