- Home /
Depth shaders always appear black
I can't view the depth of objects no matter what I try, they just appear black.
I've been grabbing code from forums to access depth data, both via camera render targets and material shaders, including this code directly from the manual https://docs.unity3d.com/Manual/SL-DepthTextures.html
Shader "Render Depth 1" {
SubShader{
Tags { "RenderType" = "Opaque" }
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float2 depth : TEXCOORD0;
};
v2f vert(appdata_base v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
UNITY_TRANSFER_DEPTH(o.depth);
return o;
}
half4 frag(v2f i) : SV_Target {
//UNITY_OUTPUT_DEPTH(i.depth)*10000000;
half d = i.depth.x / i.depth.y;
return half4(d * 10, d * 10, d * 10, 1);
}
ENDCG
}
}
}
It's always just black, even messing with camera render path settings, or "mainCamera.depthTextureMode = DepthTextureMode.Depth;", or multiplying in case the values are just dark. How do I get the depth in a material?
Answer by Kaldrin · Oct 16, 2020 at 07:47 PM
I don't know if you're still having this issue but for anyone who comes here some time after, I've found this tutorial to be pretty useful, especially by adding this script to the camera :
private void Start(){
Camera cam = GetComponent<Camera>();
cam.depthTextureMode = cam.depthTextureMode | DepthTextureMode.Depth;
}
Your answer
Follow this Question
Related Questions
Mesh z-fighting to itself 1 Answer
How to get the depth values of the camera view? 1 Answer
Get depth texture from RT without the extra pass? 0 Answers
Scene View Camera Depth Texture? 0 Answers
Depth Rendering Issue 0 Answers