So I’ve been trying to reconstruct the position from depth, with no success …
This is how I construct inverse projection matrix:
var vm = this.camera.camera.viewMatrix.clone();
var pm = this.camera.camera.projectionMatrix.clone();
var f = new pc.Mat4();
f.mul2(pm, vm);
f.invert();
scope.resolve("invViewProj").setValue( f.data );
These are my shader functions:
float unpackFloat(vec4 rgbaDepth) {
const vec4 bitShift = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);
return dot(rgbaDepth, bitShift);
}
vec3 depthToWorld(float depth, vec2 texcoord) {
// get screen-space position
vec4 pos;
pos.xy = texcoord * 2.0 - 1.0;
pos.z = depth * 2.0 - 1.0;
pos.w = 1.0;
// get world-space position
pos = invViewProj * pos; // or pos * mat, depends on the matrix-representation
pos /= pos.w;
vec3 worldPos = pos.xyz;
return worldPos;
}
And the output:
gl_FragColor = vec4( depthToWorld(unpackFloat(depth), vUv0), 1 );
Tried many different combinations / inversions, all leads to rainbows, but not the correct result.
Any ideas?
link: https://playcanvas.com/editor/scene/425968