I am making a procedurally generated voxel world made of blocks. Some of the blocks have non cubic shapes (ramps), and some of the blocks are transparent (water).
All the blocks are part of the same mesh. The mesh is made out of 2 sub-meshes, both using unity's standard shader, one with the opaque rendering mode, while the other uses the transparent mode.
I want to allow for the water block to render at the same location as a ramp block, with the water correctly appearing above the ground.
Here is an image of a ramp block next to a water block:
When I combine them however, I get the following effect:
I have set the transparent Shader to write to the Z-Buffer using:
meshRenderer.materials[1].SetInt("_ZWrite", 1);
(Otherwise only the water block is visible)
Here is my code for rendering the mesh:
filter.mesh.Clear();
filter.mesh.vertices = meshData.vertices.ToArray();
filter.mesh.subMeshCount = 2;
filter.mesh.SetTriangles(meshData.triangles.ToArray(), 0);
filter.mesh.SetTriangles(meshData.transparentTriangles.ToArray(), 1);
filter.mesh.uv = meshData.uv.ToArray();
filter.mesh.RecalculateNormals();
meshData.triangles contains the triangles of the opaque blocks, and meshData.transparentTriangles contains the triangles of the transparent blocks.
I have tried to slightly offset the water block, to avoid having 2 triangles at the exact same position, and that almost gives me the effect I want, except for some reason the opaque block becomes transparent, even though it's triangles are rendered by the opaque shader:
https:// i.stack.imgur.com/zlSKl .png
(The link is weird since it does not allow me to post a 3rd image link as I dont have enough reputation, please just remove the spaces)
Any help with solving the issue would be greatly appreciated.
Thank you.