• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Razziel · Feb 18, 2018 at 11:05 AM · camerarendertexturedepth bufferreplacement shader

Camera Depth Texture from a second camera with replacement shader

I'm trying to access another camera's depth texture inside a shader.

The setup is like this:

The main camera has no script attached that renders to depth buffer or something. It's a simple camera. The second camera has a replacement shader, replaced by a script "ReplaceAndUseDepth.cs", that renders only some objects from the scene (those who have a shader tagged with "Focused").

The problem that I can't solve:

I want this replacement shader to make use of the second camera's depth buffer. But accessing it with uniform sampler2D _CameraDepthTexture will not get me anything.

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Razziel · Feb 20, 2018 at 09:58 PM

I managed to solve this. My second's camera target texture was set to something different than null. That seemed to cause the problem. Also, I used custom target buffers to make sure that I get the second's camera depth buffer, using :

 target = new RenderTexture(mainCamera.pixelWidth, mainCamera.pixelHeight, 16, RenderTextureFormat.Default);
         targetDepth = new RenderTexture(mainCamera.pixelWidth, mainCamera.pixelHeight, 24, RenderTextureFormat.Depth);
 
         secondCamera.SetTargetBuffers(target.colorBuffer, targetDepth.depthBuffer);
 
         //secondCamera.targetTexture = target; **COMMENTED LINE**
 
         Shader.SetGlobalTexture(targetColorName, target);
         Shader.SetGlobalTexture(targetDepthName, targetDepth);

With this code, using the uniform sampler2D [targetDepthName]will get me exactly what _CameraDepthTexture would normally get me if I had a simple setup with only one camera.

Comment
Jeph_Diel

People who like this

1 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by klemen_unity · Mar 01, 2018 at 05:08 PM

Hey @Razziel, I'm trying to do the same thing, use a secondary camera's depth in a shader. _LastCameraDepthTexture always looks the same as _CameraDepthTexture for me. Are you able to share a more detailed breakdown of your solution?

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Razziel · Mar 05, 2018 at 04:30 PM 1
Share

Hey @klemen_unity, I'm going to show you a script attached to the secondary camera:

 [ExecuteInEditMode]
 public class ReplaceCameraShader : MonoBehaviour {
     public Camera mainCamera; // main camera
     private Camera cameraComp; // second camera
     public Shader replacedWith;
     public static readonly string targetTextureName = "_TransparencyFocusMask";
     public static readonly string targetDepthName = "_DepthFocusMask";
 
     private RenderTexture target;
     private RenderTexture targetDepth;
 
     private void OnEnable() {
         cameraComp = GetComponent<Camera>();
         cameraComp.depthTextureMode = DepthTextureMode.None;
 
         //cameraComp.SetReplacementShader(replacedWith, "Focused");
 
         target = new RenderTexture(mainCamera.pixelWidth, mainCamera.pixelHeight, 24, RenderTextureFormat.ARGBFloat);
         targetDepth = new RenderTexture(mainCamera.pixelWidth, mainCamera.pixelHeight, 24, RenderTextureFormat.Depth);
 
         Shader.SetGlobalTexture(targetTextureName, target);
         Shader.SetGlobalTexture(targetDepthName, targetDepth);
 
         cameraComp.SetTargetBuffers(target.colorBuffer, targetDepth.depthBuffer);
 
     }
 
     private void OnDisable() {
         cameraComp = GetComponent<Camera>();
         //cameraComp .ResetReplacementShader();
 
         if (cameraComp.targetTexture != null) {
             RenderTexture temp = cameraComp.targetTexture;
             cameraComp.targetTexture = null;
             DestroyImmediate(temp);
         }
         target = null;
         targetDepth = null;
         Shader.SetGlobalTexture(targetDepthName, targetDepth);
         Shader.SetGlobalTexture(targetTextureName, target);
     }
 }


With this script you can definitely use the secondary's camera depth texture AND color texture (what the camera sees). And you access them, in the shader with uniform sampler2D _DepthFocusMask. You can see that i commented the lines mentioning the shader replacement. That's because i ran in a different problem. (You can see it here: https://answers.unity.com/questions/1472358/problem-with-camera-target-texture-and-replacement.html ) But, as far as getting the secondary's camera depth texture, this method works.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

123 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera.RenderWithShader not working as of Unity 5.3? 0 Answers

Problem with camera target texture and replacement shader 0 Answers

RenderTexture with image effects baked in 2 Answers

dynamic hole in layer / texture / camera 0 Answers

glReadPixels from a RenderTexture 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges