I am trying to write a shader that computes the depth of a mesh from a camera with a linear scale from 0 to 1.
A vertex that is the closest to the camera would have a value of 0, whereas the vertex that is the farthest from the camera would have a value of 1. Also, if the camera is rotated, the depth values of the mesh must adapt to the change.
Here is an image to support the understanding.
From the image, you can see that the closest vertices alpha is set to 0 as compared to the value of 1 for the farthest vertices.
I would like to achieve this effect, even when the camera is rotated.
From this Shader - Calculate depth relative to Object, I can find the closest vertex, but I am finding it difficult to calculate the farthest point of the mesh from the camera.
The bounding box of the mesh could be used, but if I rotate the camera, I need to recompute the bounding box. Hence, I would like to know if there is any solution that could be used using the matrices available?
PS: I am trying to implement this effect in both three.js and Unity.