How to pass camera.projectionMatrixInverse as uniform to the vertex shader

Hi guys:

I’m maintaining a vertex shader encapsulated in a custom material class (inherited from ShaderMaterial but now from MeshStandardMaterial).

At some point into the shader I need the inverse of projectionMatrix which is already computed in camera.projectionMatrixInverse but camera is not accessible from a material class, nor onBeforeCompile callback.

So is there a way to pass the already-computed “projectionMatrixInverse” to the shader so that no GLSL “inverse()” function need to be used?
(I would not like overload the vertex shader with stuff that can be computed outside and passed as uniform)

SO: javascript - How to convert coordinate from NDC to camera space in vertex shader? - Stack Overflow


You can pass the camera to your material constructor as an argument and then assign camera.projectionMatrixInverse.elements to a uniform. In the shader the type is mat4, in Javascript - Array(16)