- Home /
The meaning of the depth map value
I would like to know, the value I get from the depth map of certain camera (range : 0.0f ~ 1.0f), is it equal to the Z value of the viewport space coordinate ?
Here is part of my code : Matrix4x4 matrix = _3dCamera.projectionMatrix * _3dCamera.worldToCameraMatrix; Vector3 screenPos = matrix.MultiplyPoint(newObj.transform.position);
// (-1, 1)'s clip => (0 ,1)'s viewport screenPos= new Vector3(screenPos.x + 1f, screenPos.y + 1f, screenPos.z + 1f) / 2f;
At the end, I use the X, Y value of the screenPos (Vector3) to get the depth value of the position (x, y) of the depth map. I would like to know if the meaning of the screenPos.z is equal to the value I get from the depth map.
Answer by IgorAherne · Sep 23, 2017 at 09:57 AM
I would like to know if the meaning of the screenPos.z is equal to the value I get from the depth map.
in your example, your variable screenPos.z is equal. Also, it's important to add that the depth progression is not linear, but is logarithmic.
Also, don't forget that multiplying by projection will modify the W of the vector; You then need to divide XYZW of your vector by W to make this vector conform to the laws of perspective. Traditionally this is automatically done in between Vertex and Fragment shaders by the pipeline (optimized for the hardware). But when manually computating, this is needed to be done by hand.
After perspective projection step was done, you can then do (-1,1)=>(0,1) transformation