- Home /
 
How to generate Cubemap with color and depth values?
I am looking for the best way to generate a Cubemap with color + depth values in Unity 5.3.
I already have a shader that writes color in the RGB channels and Depth in the alpha channel, but how do I use-it for a CubeMap?
Possible solutions I see:
A) Use a reflection probe + the replacement shader mentioned above...but is it possible at all? I couldn't find any information about how to configure the camera used by the reflection probe so that it uses a replacement shader.
B) Without reflection Probe: use a secondary camera and call Camera.GenerateCubemap, having first called Camera.SetReplacementShader with the shader mentioned above.
Or maybe there's a better approach? Any help / comment / feedback will be appreciated, thanks! :)
Did you manage to solve this by any chance? I'm also facing this issue. I attempted to use a replacement shader, however camera's depth capture doesn't seem to capture all six faces of the cube.
Answer by Kik24 · Sep 08, 2017 at 06:56 AM
Yes Alamir, apparently there's no way to use a replacement shader with a reflection probe, but the B) variant is working for me.
I am generating the depth values myself in the fragment shader, so I don't use the camera's depth buffer at all, like this:
 CGPROGRAM
         #pragma surface surf Standard fullforwardshadows nolightmap vertex:vert keepalpha
         #pragma target 3.0
 
         sampler2D     _MainTex;
         half         _Glossiness;
         half         _Metallic;
         fixed4         _Color;
 
         struct Input 
         {
             float2 uv_MainTex;
             float4 screenPos;                // Position in screen-space
         };
             void vert(inout appdata_base v, out Input o)
             {
                     UNITY_INITIALIZE_OUTPUT(Input, o);
             }
             void surf (Input IN, inout SurfaceOutputStandard o) 
             {
                 fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
                 o.Albedo = c.rgb;
                 o.Metallic = _Metallic;
                 o.Smoothness = _Glossiness;
     
                 // Calculate depth and store it into the alpha-channel
                 // => Use built-in function to convert to Linear [0; 1] range
                 //-----------------------------------------------------------------------
                 float fragmentDepth = Linear01Depth((0.5 * IN.screenPos.z / IN.screenPos.w) + 0.5);
                 o.Alpha             = fragmentDepth;
             }
 
 #ENDCG
 
               NOTE: it's very important to specify keepalpha for the surface shader, otherwise the alpha value, which should contain the depth, will be overwritten and set to 1.
Your answer