I’m trying to found how to use the pre-rendered background (isometric camera) with 3D objects with the working depth testing so the 3D object are occluded by the background objects (like the pillars of the eternity). I’ve searched for some stuff how it is done. What I found, is that the the pillars of eternity is rendering some kind of depth map, but haven’t found any details what kind of the depth map are rendering and how are they using it in unity.
I found that they’re rendering the height of the each pixel in the depth map. But I don’t think the only height is sufficient. I think that each pixel of the depth map should have x, y, z position encoded. Then in unity in fragment shader reconstruct the position from this map and set the depth value for each fragment. I’ve tried this approach but somehow the depth value computed from the position (I’m multiplying the MVP with position reconstructed from depth map) is always same (doesn’t change when I move the background) and wrong (there is some small gradient).
The attached image is showing the the wrong depth (right image) and how it should like (left image).
Fragment shader:
fixed4 frag_mult(v2f_vct i, out float depth_o : SV_DEPTH) : SV_Target
{
depth_o = 1;
fixed4 col = tex2D(_MainTex, i.texcoord);
clip(col.a - 0.25f);
float4 col_depth = tex2D(_DepthTex, i.texcoord);
float4 x_depth = tex2D(_XTex, i.texcoord);
float4 y_depth = tex2D(_YTex, i.texcoord);
float x = (DecodeFloatRGBA(x_depth) * 4 - 2);
float y = (DecodeFloatRGBA(y_depth) * 4 - 2);
float z = (DecodeFloatRGBA(col_depth) * 4 - 2);
float4 vertex_p = float4(x, z, y, 0);
float4 vertex_p_cam = vertex_p;
vertex_p_cam = mul(UNITY_MATRIX_MVP, vertex_p);
float depth = vertex_p_cam.z;
depth_o = depth;
col = fixed4(depth_o, depth_o, depth_o, col.a);
return col;
}
So, does anyone has any suggestion?
Thank You
