I've been fighting with this for a while now and I have no ideas anymore. I want to create a depth map/texture with my shader. I don't use the macros/functions from unity because they are based on the camera's matrices (I guess). I use my matrices. I get the scene drawn to a texture completely fine, but I can't create a depth texture from it. I tried multiple ways to achieve this. Here some code to get a better understanding of my problem. this will be called every update: Code (CSharp): // Remember the current texture and set our own as "active". RenderTexture prevRT = RenderTexture.active; RenderTexture.active = renderTexture; // Set material as "active". Without this, Unity editor will freeze. material.SetPass(0); // Push the projection matrix GL.PushMatrix(); // `projectionMatrix` is an ortho projection GL.LoadProjectionMatrix(projectionMatrix); // `viewMatrix` is a TRS matrix material.SetMatrix("_viewMatrix", viewMatrix); // Clear the texture GL.Clear(true, true, Color.white); // bunch of meshes foreach (var mesh in meshes){ Quaternion rotation = Quaternion.Euler(mesh.rotation); // Create the model matrix Matrix4x4 objectMatrix = Matrix4x4.TRS(mesh.position, rotation, mesh.scale); // Draw the mesh! Graphics.DrawMeshNow(mesh.mesh, objectMatrix); } // Pop the projection matrix to set it back to the previous one GL.PopMatrix(); // Re-set the RenderTexture to the last used one RenderTexture.active = prevRT; So this draws everything fine into a RenderTexture which has a shader attached. The shader is an unlit shader which is very basic. Code (CSharp): //... SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" float4x4 _viewMatrix; struct appdata { float4 vertex : POSITION; }; struct v2f { float4 vertex : SV_POSITION; float2 depth : DEPTH; }; v2f vert (appdata v) { v2f o; float4 vertex = v.vertex; float4x4 mv = mul(_viewMatrix, unity_ObjectToWorld); float4x4 mvp = mul(UNITY_MATRIX_P, mv); vertex = mul(mvp, vertex); o.vertex = vertex; o.depth = vertex.zw; return o; } fixed4 frag (v2f i) : SV_Target { half depth = i.depth.x / i.depth.y; return fixed4(depth,depth,depth,1); } ENDCG } } //... I also tried to just return 0 in the pixel shader by disabling Cull Off and ZWrite On. The result that I get is always a black or transparent texture where the meshes are. So, there is no depth. Has someone an idea how I can create a depth map with custom matrices? I'd appreciate any help.
In the meanwhile, I got help somewhere else and I wanted to post the solution in case someone else might have the same problem. See comments for the changes. Code (CSharp): struct v2f { float4 vertex : SV_POSITION; // this line below is new float depth : DEPTH; }; v2f vert (appdata v) { v2f o; float4 vertex = v.vertex; float4x4 mv = mul(_viewMatrix, unity_ObjectToWorld); float4x4 mvp = mul(UNITY_MATRIX_P, mv); vertex = mul(mvp, vertex); o.vertex = vertex; // this line below is new o.depth = -mul(mv, v.vertex).z * _ProjectionParams.w; return o; } fixed4 frag (v2f i) : SV_Target { // this line below is new half depth = i.depth; return fixed4(depth,depth,depth,1); }