playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
54 lines (53 loc) • 1.67 kB
JavaScript
var screenDepth_default = `
var uSceneDepthMap: texture_2d<uff>;
uniform uScreenSize: vec4f;
uniform matrix_view: mat4x4f;
uniform camera_params: vec4f;
fn linearizeDepth(z: f32) -> f32 {
if (uniform.camera_params.w == 0.0) {
return (uniform.camera_params.z * uniform.camera_params.y) / (uniform.camera_params.y + z * (uniform.camera_params.z - uniform.camera_params.y));
} else {
return uniform.camera_params.z + z * (uniform.camera_params.y - uniform.camera_params.z);
}
}
fn delinearizeDepth(linearDepth: f32) -> f32 {
if (uniform.camera_params.w == 0.0) {
return (uniform.camera_params.y * (uniform.camera_params.z - linearDepth)) / (linearDepth * (uniform.camera_params.z - uniform.camera_params.y));
} else {
return (linearDepth - uniform.camera_params.z) / (uniform.camera_params.y - uniform.camera_params.z);
}
}
fn getLinearScreenDepth(uv: vec2f) -> f32 {
let textureSize = textureDimensions(uSceneDepthMap, 0);
let texel: vec2i = vec2i(uv * vec2f(textureSize));
return textureLoad(uSceneDepthMap, texel, 0).r;
return linearizeDepth(textureLoad(uSceneDepthMap, texel, 0).r);
}
fn getLinearScreenDepthFrag() -> f32 {
let uv: vec2f = pcPosition.xy * uniform.uScreenSize.zw;
return getLinearScreenDepth(uv);
}
fn getLinearDepth(pos: vec3f) -> f32 {
return -(uniform.matrix_view * vec4f(pos, 1.0)).z;
}
`;
export {
screenDepth_default as default
};