- Home /
What is the best way to render groups of flat planes using a shader
In my project I have several planes (flat polygons).
Sometimes these planes are stacked on top of each other into a group. In this case I need to specify the rendering order so that they display correctly. To do this I have set the materials renderQueue for these stacked planes.
this.renderer.material.renderQueue = 2000 + groupIndex;
Using this approach the planes within a stack will render correctly.
The problem is that overriding the render queue in this manner will cause the planes which are in different groups to render incorrectly. By overriding the render order I have reintroduced the problems associated with the Painter's algorithm.
For example if I have group1 containing 5 planes and group2 containing 3 planes, I know in which order the planes within a group must be rendered (in group1 [4,3,5,1,2]), but not the order in which the planes in different groups are rendered (group1[4,3] then group2 then group1[5,1,2]).
I tried sorting the groups by the z distance of each vertex, but after implementing this approach I realized it doesn't work (and didn't make sense). I think I need to use the zBuffer, but have a special case where the planes have the same z value but are in the same group.
Would it be easier to approach this problem by writing my own shader?
Essentially my question is how do I render stacked planes correctly without upsetting the z buffering.
I'm also hoping to get this project ported to iOS (once I get a mac). So my solution needs to be compatible with any special iOS rules.
UPDATE
After further research it seems that I need to write a shader to solve this approach. The shader needs to do the following:
Store depth values in the Z-buffer
When a polygon is being rendered into the Z-Buffer
if(currentZ same as Z-Buffer)
if(currentPolygon 'above' drawnPolygon)
Update pixel with new value
To determine whether a currentPolygon is 'above' the drawPolygon I need to store the current index of the drawn polygon and then compare with the index of the current polygon
How can I approach this in Unity? My understanding of shaders is unfortunately quite limited. I understand 'if' statements aren't really the way things are done. What is the approach towrite this in ShaderLab/CG?
Thanks in advance for any ideas,suggestions and solutions :D
Answer by Lemon · Oct 26, 2010 at 10:18 PM
I have opted to use a 'hack' and shift each of the parallel planes by a tiny amount along their local z-axis in order to force them to render in the correct order. By reducing the near and far clip planes this approach is working and the shift amount is tiny ( 0.001 ).
My venture into learning shaders will has been postponed for another day!
Your answer
Follow this Question
Related Questions
How would I fix this DrawMeshInstancedIndirect and Translucent object problem? 0 Answers
Why is this clipping/overlap occurring between game objects? 1 Answer
HDRP Change color on touch ground on a custom pass shader 0 Answers
Unity System Fog Access 0 Answers
Standard Shader Still Visible through Stencil Shader 0 Answers