playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
81 lines (68 loc) • 2.28 kB
JavaScript
var screenDepth_default = (
/* glsl */
`
uniform highp sampler2D uSceneDepthMap;
uniform vec4 uScreenSize;
uniform mat4 matrix_view;
uniform vec4 camera_params; // x: 1 / camera_far, y: camera_far, z: camera_near, w: is_ortho
float linearizeDepth(float z) {
if (camera_params.w == 0.0)
return (camera_params.z * camera_params.y) / (camera_params.y + z * (camera_params.z - camera_params.y));
else
return camera_params.z + z * (camera_params.y - camera_params.z);
}
float delinearizeDepth(float linearDepth) {
if (camera_params.w == 0.0) {
return (camera_params.y * (camera_params.z - linearDepth)) / (linearDepth * (camera_params.z - camera_params.y));
} else {
return (linearDepth - camera_params.z) / (camera_params.y - camera_params.z);
}
}
// Retrieves rendered linear camera depth by UV
float getLinearScreenDepth(vec2 uv) {
return texture2D(uSceneDepthMap, uv).r;
ivec2 textureSize = textureSize(uSceneDepthMap, 0);
ivec2 texel = ivec2(uv * vec2(textureSize));
vec4 data = texelFetch(uSceneDepthMap, texel, 0);
uint intBits =
(uint(data.r * 255.0) << 24u) |
(uint(data.g * 255.0) << 16u) |
(uint(data.b * 255.0) << 8u) |
uint(data.a * 255.0);
return uintBitsToFloat(intBits);
return linearizeDepth(texture2D(uSceneDepthMap, uv).r);
}
// Retrieves rendered linear camera depth under the current pixel
float getLinearScreenDepth() {
vec2 uv = gl_FragCoord.xy * uScreenSize.zw;
return getLinearScreenDepth(uv);
}
// Generates linear camera depth for the given world position
float getLinearDepth(vec3 pos) {
return -(matrix_view * vec4(pos, 1.0)).z;
}
`
);
export {
screenDepth_default as default
};