I am currently maintaining a vertex shader contained within a custom material class (originally inherited from ShaderMaterial
but now from MeshStandardMaterial
) which converts 3D coordinates to Normalized Device Coordinates (NDC) as usual:
vec4 ndcPos = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
ndcPos /= ndcPos.w;
//...other transformations to ndcPos
Next, a series of transformations are applied to ndcPos
. These transformations are carried out in NDC space. To return the resulting coordinate back to camera (eye) space, it appears that the steps need to be reversed, as shown below:
vec4 mvPos = ndcPos * ndcPos.w;
mvPos *= inverse of projectionMatrix;
Expected outcome: mvPos
should only have the modelView transformation applied.
Queries:
- Is this approach correct?
- How can the inverse of
projectionMatrix
be computed? It would be convenient and efficient to passcamera.projectionMatrixInverse
as a uniform to the vertex shader, but I have not been able to figure out a way to do so in three.js: neither through ancestor material classes, nor throughonBeforeCompile
which can access the camera.