Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
2
Question by pmeade · Mar 06, 2012 at 11:27 PM · shaderkinectdepth-buffershader-replacement

Problems with depth values in _CameraDepthTexture

I'm new to shader programming, so apologies if I am making some hugely noobish mistakes.

I'm trying to use a camera to simulate a Primesense style depth camera. My goal is to use the camera to capture the geometry and then have the fragment shader output a greyscale image where white = max distance and black = min distance. Distance is determined by looking up the fragment in the _CameraDepthTexture.

What I am actually seeing is that all fragments seem to have the same value -- a value of 1. I would be super appreciative if someone could explain how I am misusing the depth texture.

I will attempt to include all relevant pieces here:

The camera: FOV 53, Near plane 0.5, Far 10.

I am attaching a script that contains the following OnEnable, which sets a replacement shader to use for all objects in the scene (my depth shader):

 void OnEnable()
 {
     Cam = GetComponentInChildren<Camera>();
     if (Cam != null)
     {
         Cam.depthTextureMode = DepthTextureMode.Depth;
         Cam.SetReplacementShader(ReplacementShader, "");
     }
 }

I modified the code of my shader to it's most basic form. We do the standard vertex bit, and then try to render the depth directly as a shade of grey.

 Shader "Custom/DepthShader" {
     Properties 
     {
     }
     
     SubShader
     {
         Pass 
         {
             cull off
             ZWrite On
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"            
             sampler2D _CameraDepthTexture;
             
             v2f_img vert( appdata_img v )
             {
                 return vert_img(v);
             }
             
             float4 frag (v2f_img i) : COLOR
             {
                 float d = tex2D(_CameraDepthTexture, i.uv.xy);
                 return float4(d,d,d,1);
             }
             ENDCG
         }
     } 
     FallBack "Diffuse"
 }
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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by JoeyK · Jun 12, 2012 at 09:33 PM

I've been trying to do essentially the same thing. Your code helped me get started so thank you. I eventually tried the demo shader on this page...and it worked. I feel kinda silly for spending so much time trying to get different combinations of other shaders to work when the answer was on that page the whole time.

Oh well, you've probably figured it out or given up by now but yeah... for the next guy.

I also changed the image effect script too:

 #pragma strict
 
 @script ExecuteInEditMode
 @script RequireComponent (Camera)
 
 
 class DepthCamEffect extends PostEffectsBase {    
     
     public var cam : Camera;
     public var depth : Shader;
     private var depthMaterial : Material = null;
 
     function OnDisable()
     {
         if (depthMaterial)
             DestroyImmediate(depthMaterial);
     }
 
     function CheckResources () : boolean {    
         CheckSupport (true);
     
         depthMaterial = CheckShaderAndCreateMaterial (depth,depthMaterial);
         
         if(!isSupported)
             ReportAutoDisable ();
         return isSupported;                
     }
     
     function OnEnable(){
 
        cam.depthTextureMode = DepthTextureMode.Depth;
        cam.SetReplacementShader(depth, "");
     }
 }

And then just drag the camera and shaders you want to use into the spaces in the inspector.

Comment
Add comment · 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 mysteryDate · Feb 11, 2015 at 07:32 PM 0
Share

What is the "image effect script" to which you are referring?

avatar image
1

Answer by imangitarrowood · Feb 25, 2016 at 10:22 PM

I was having the same problem, so here's a years later answer for anyone else who comes looking for this: if you are using custom shaders, you need to make sure that you define a "ShadowCaster" pass for any that you want to write into the Unity depth texture (or fallback to a shader that does), even if it doesn't cast shadows (see http://docs.unity3d.com/Manual/SL-CameraDepthTexture.html under DepthTextureMode.Depth texture). Here's code to do that (copied from the old VertexLit shader).

         // Pass to render object as a shadow caster, required to write to depth texture
         Pass 
         {
             Name "ShadowCaster"
             Tags { "LightMode" = "ShadowCaster" }
             
             Fog {Mode Off}
             ZWrite On ZTest LEqual Cull Off
             Offset 1, 1
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile_shadowcaster
             #include "UnityCG.cginc"
 
             struct v2f 
             { 
                 V2F_SHADOW_CASTER;
             };
 
             v2f vert( appdata_base v )
             {
                 v2f o;
                 TRANSFER_SHADOW_CASTER(o)
                 return o;
             }
 
             float4 frag( v2f i ) : SV_Target
             {
                 SHADOW_CASTER_FRAGMENT(i)
             }
             ENDCG
 
         }
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
avatar image
0

Answer by LeaveOneBlood · Nov 01, 2016 at 03:19 PM

hi,imangitarrowood ,I use your shader code,but I can not get the z-depth,as the result is all the objects in scene are disappeared。 how should i get the depth. thanks

Comment
Add comment · 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 imangitarrowood · Nov 01, 2016 at 04:15 PM 0
Share

The code that I posted was for writing depth if you are using custom shaders that don't fall back to Unity shaders for performance reasons. Reading depth is done how pmeade did it in the original question:

 float d = tex2D(_CameraDepthTexture, i.uv.xy);

If that's not returning depth properly, make sure you have depth write enabled for the camera:

 Cam = GetComponentInChildren();
  if (Cam != null)
  {
        Cam.depthTexture$$anonymous$$ode = DepthTexture$$anonymous$$ode.Depth;
  }

If you have everything set up right, you should be able to use the Frame Debugger in the editor to see the Projected Shadow pass clear the projectedShadow target and draw calls rendering to it under RenderForwardOpaque.Render. If that render target isn't there, your camera probably isn't set up to write depth. If the clear is there but no draw calls, then nothing drawn by that camera is using a shader with the ShadowCaster pass defined.

Hope that helps!

avatar image
0

Answer by DynastyV · Dec 04, 2016 at 08:32 PM

Using

 Tags{ "RenderType" = "Opaque" }

made it work for me

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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Making a character appear through 1 layer of objects but not 2 or more 0 Answers

Is there a way to change the shader on all meshes without doing it manually one by one? 3 Answers

Additive shader that gets lit? 0 Answers

Can I change a duplicated objects shader? 1 Answer

How to Reset Shader Replacement in Scene View? 1 Answer


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