I’m trying to implement Depth Peeling(I know it’s slow but I need the accuracy). Is it possible to do this within Unity?
I found this post on GameDev
http://www.gamedev.net/community/forums/topic.asp?topic_id=468106
(Initialization)
(1) Create 2 offscreen render targets (R32F works well)
→ note, I’ve found you need 2 of them for ping-ponging purposes
→ these two render targets are referred to as “RT_1” and “RT_2” in the below pseudo-code
(Render Code)
(1) Set ping RT = RT_1, pong RT = RT_2
(2) Clear the main DB to 0.0f
(3) Clear the 2 offscreen RT’s to 1.0f
(3) Set ZFunc to Greater (want the pixel furthest from the eye-pt)
do{
(4) SetRenderTarget(0, ping RT)
(5) SetTexture(0, pong RT)
(6) DrawScene()
(7) swap( ping RT, pong RT )
}while( Pixels are drawn );
(In your Depth Peeling shader)
(1) Compare fragments depth to depth in the ‘pong’ render target
(2) If ‘this fragments depth’ >= ‘depth stored in pong RT’
discard
Else
light and shade pixel
This could be completely wrong but this is my guess at implementing it.
I create two RenderTextures in depth format. I make a shader to set all the values of the rendertextures to 1.0f.
Clear the depth buffer(Using GL.Clear?)
Set Camera target texture to “ping” and pass in the “pong” texture into the shader for sampling.
Do RenderWithShader compare the depths values of pong with current depth values and use shader discard; to kill any pixels.
I think I have to hard code (Pixels are drawn) because I don’t think I can do a occlusion query in Unity.
The problem is how do I bind the updated depth texture to the z buffer so that when it loops it will render using that depth info or am I approaching this incorrectly.
Thanks!