Shader "Custom/DepthOnly" { SubShader { Tags { "RenderType"="Opaque" } Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct v2f { float4 pos : SV_POSITION; float depth : TEXCOORD0; float4 pos2 : TEXCOORD2; }; v2f vert(appdata_base v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); // o.depth = (o.pos.z + 1.) / 2. / o.pos.w; // o.depth = (o.pos.z + 1.) / 2.; // o.depth = o.pos.z; // o.pos.x = o.pos.x / o.pos.w; // o.pos.y = o.pos.y / o.pos.w; // o.pos.z = o.pos.z / o.pos.w; o.depth = o.pos.z / o.pos.w; //o.depth = mul(UNITY_MATRIX_M, v.vertex).x; // o.pos2 = o.pos; return o; } fixed4 frag(v2f i) : SV_Target { // Outputs depth in RGB (2^24 precision) // ****We had to change Unity's color space to Gamma such that it doesn't add its own gamma correction, // for this to work ******* // uint depth24b = (i.depth * ((1<<24)-1)); // maps depth to be from 0 --- 2^24 // uint rEncode = 0; // uint gEncode = 0; // uint bEncode = 0; // // This loop encodes depth into a 24 bit value split across all three color channels. // // The least significant digit is the last blue value. // // The most significant digit is the first red value. // for (int i = 0; i < 8; i++){ // bEncode |= ((depth24b) & 1) << i; // depth24b >>= 1; // gEncode |= ((depth24b) & 1) << i; // depth24b >>= 1; // rEncode |= ((depth24b) & 1) << i; // depth24b >>= 1; // } // return fixed4( // rEncode/255., // gEncode/255., // bEncode/255., // 1.0 // ); // Output depth as grayscale (2^8 precision) return fixed4((i.depth), (i.depth), (i.depth), 1.0); // return fixed4( // (i.pos2.x) * .5 + .5, // (i.pos2.x) * .5 + .5, // (i.pos2.x) * .5 + .5, 1.0); } ENDCG } } FallBack "Diffuse" }