- Home /
Only render objects behind specific object
I have a sphere on one layer and a bunch of objects on an other layer. I would like to only render the objects that are behind the sphere with respect to the camera. This would all be rendered to a texture for later use.
I assume the steps would have to be something like:
Render sphere layer with ZWrite on and ColorMask 0
Render objects layer using ZTest Greater or GEqual
I will also probably need to use Shader Replacement when rendering the objects layer.
I have tried a couple of things but I have failed to produce any desirable results.
Let me know if you need any more information.
Edit: After further thought I think the steps should rather be:
Render depth of sphere layer to depth texture
Render objects with a depth greater than that in the depth texture
This would involve setting depthTextureMode to Depth when rendering the sphere then using the generated _CameraDepthTexture when rendering the objects for depth testing. But I am not sure what the shader would look like that was using the _CameraDepthTexture.
Answer by miningold · Oct 31, 2012 at 09:34 PM
I ended up rendering the sphere to the depth texture at _CameraDepthTexture
Then I wrote a surface shader that draws the other objects. This shader would simply draw an alpha of 0 if it were in front of the sphere (using the objects screenPos to map to the depth texture and retrieve the depth).
There was one small caveat, I began to run into transparency render order problems. So I told unity to compile the surf shader into a vertex/fragment pair. Then I overrode the AlphaTest to write to the Z-buffer only if the alpha was greater than 0. I couldn't do this in a surface shader because Unity compiles there own alpha testing.
AlphaTest Greater 0 ZWrite on ColorMask RBG
i guess your above explanation would be enough to reproduce your solution, but since i am a shader noob... would you please share your shaders? thanks in advance :D
Your answer
Follow this Question
Related Questions
Dynamically render/hide object individually for each camera in scene 0 Answers
Prevent material from rendering behind itself 0 Answers
How to Choose Camera for Game Window 2 Answers
Seperate Shadow Strengths from different objects? 0 Answers
Apply Camera.layerCullDistances to certain/specific layers? 2 Answers