• 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
1
Question by sbsmith · Dec 01, 2017 at 05:17 AM · rendertexturedepth-buffer

How do you pass the depth buffer of a RenderTexture to a CommandBuffer

I have a RenderTexture with a depth component. I render objects and depth into it and verify this in RenderDoc. I then pass that texture to another shader like so

 m_blitToScreenCommand.SetGlobalTexture( "_MainTex", m_overrideTexture.colorBuffer );
 m_blitToScreenCommand.SetGlobalTexture( "_MainDepth", m_overrideTexture.depthBuffer );
 m_blitToScreenCommand.DrawMesh( screenMesh, Matrix4x4.identity, customBlit );

However, when I check in RenderDoc, the shader is receiving two copies of the colour buffer. It doesn't seem to matter if the camera is running in deferred or forward rendering. Unity version 2017.2

Here is the shader

 Shader "Test"
 {
     // Command buffers write to global keywords so "white" will always take priority because it is more specific than the global
     //Properties
     //{
     //    _MainTex ("Texture", 2D) = "white" {}
     //}
     SubShader
     {
         Tags{ "Queue" = "Geometry" "PreviewType" = "Plane" }

         CGINCLUDE
         #pragma enable_d3d11_debug_symbols
         #pragma target 3.0
         #include "UnityCG.cginc"

         struct appdata
         {
             float3 vertex : POSITION;
             float2 uv : TEXCOORD0;
         };

         struct v2f
         {
             float2 uv : TEXCOORD0;
             float4 vertex : SV_POSITION;
         };

         sampler2D _MainTex;
         sampler2D _MainDepth;            

         v2f vert(appdata v)
         {
             v2f o;
             o.vertex = float4(v.vertex, 1);
             o.uv = v.uv;
 #ifndef UNITY_UV_STARTS_AT_TOP
             o.uv.y = 1 - o.uv.y;
 #endif
             return o;
         }

         fixed4 frag(v2f i, out float out_depth : SV_Depth) : SV_Target
         {
             out_depth = UNITY_SAMPLE_DEPTH(tex2D(_MainDepth, i.uv));
             return tex2D(_MainTex, i.uv);
         }
         ENDCG

         Pass
         {
             ZWrite on
             ZTest off
             Cull off

             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             ENDCG
         }
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by sbsmith · Dec 08, 2017 at 11:36 PM

In Unity 2017.2 you cannot pass the color and depth of the same RenderTexture as separate parameters. The only thing that works is to have two separate RenderTextures where one is colour only and the other is depth only. You can then pass the colorBuffer of the first and the depthBuffer of the second and things will behave as intended.

Comment
Add comment · 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

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

73 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

Related Questions

How to get the depth values of the camera view? 1 Answer

Can't get _LastCameraDepthTexture to work 0 Answers

commandbuffer can't get depth buffer in forward rendering but correct in Deferred .Why? 1 Answer

Read depth buffer on the cpu 1 Answer

Copying depth from one RenderTexture to another 2 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