Hey everyone!
I have 2 objects in my scene: a Hand model as well as a Cube model. The goal is to achieve a specific post processing effect using separate depth images of both models.
What have I done so far:
I set up two cameras in my scene: The mainCamera (depth: 0) can see the Hand as well as the Cube. The helperCamera (depth -1) sees only the Hand. I achieved this by culling: I defined the layer of the Hand as "Hand" and the CullingMask of the helperCamera is only set to "Hand". Then I added a short script to the helperCamera:
(Script on helperCamera)
void Start()
{
transform.GetComponent().depthTextureMode = DepthTextureMode.Depth;
}
My understanding is that this command sets up a global texture “_CameraDepthTexture” or “_LastCameraDepthTexture” which is accessible by all shaders. Then I added another script to the mainCamera with a blit and a public material:
(Script on mainCamera)
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Graphics.Blit(source, destination, postProcessingMaterial);
}
I put an image effect shader on the postProcessingMaterial which should simply output the depth image generated by the helperCamera with this code:
sampler2D _MainTex;
sampler2D _LastCameraDepthTexture;
sampler2D _CameraDepthTexture;
fixed4 frag (v2f i) : SV_Target
{
float depth = tex2D(_LastCameraDepthTexture , i.uv).r;
//float depth = tex2D(_CameraDepthTexture , i.uv).r; doesn't work either!
return depth;
}
What happens is: I get a depth texture of all objects in the scene on the mainCamera. I am confused as I expected to only see the depth image of the helpercamera which should only depict the Hand. Instead, the Hand as well as the Cube are greyed out.
What can I do to get a depth image of only the Hand model by the second camera? What am I doing wrong?
Kind regards and thanks in advance for your help!
↧