%PDF- %PDF-
| Direktori : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/node_modules/n8ao/src/ |
| Current File : //home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/node_modules/n8ao/src/DepthDownSample.js |
import * as THREE from 'three';
const DepthDownSample = {
uniforms: {
'sceneDepth': { value: null },
'resolution': { value: /* @__PURE__ */ new THREE.Vector2() },
'near': { value: 0.1 },
'far': { value: 1000.0 },
'viewMatrixInv': { value: /* @__PURE__ */ new THREE.Matrix4() },
'projectionMatrixInv': { value: /* @__PURE__ */ new THREE.Matrix4() },
'projMat': { value: /* @__PURE__ */ new THREE.Matrix4() },
'logDepth': { value: false },
'ortho': { value: false }
},
depthWrite: false,
depthTest: false,
vertexShader: /* glsl */ `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = vec4(position, 1);
}`,
fragmentShader: /* glsl */ `
uniform highp sampler2D sceneDepth;
uniform vec2 resolution;
uniform float near;
uniform float far;
uniform bool logDepth;
uniform bool ortho;
uniform mat4 viewMatrixInv;
uniform mat4 projectionMatrixInv;
uniform mat4 projMat;
varying vec2 vUv;
layout(location = 1) out vec4 gNormal;
vec3 getWorldPosLog(vec3 posS) {
vec2 uv = posS.xy;
float z = posS.z;
float nearZ =near;
float farZ = far;
float depth = pow(2.0, z * log2(farZ + 1.0)) - 1.0;
float a = farZ / (farZ - nearZ);
float b = farZ * nearZ / (nearZ - farZ);
float linDepth = a + b / depth;
vec4 clipVec = vec4(uv, linDepth, 1.0) * 2.0 - 1.0;
vec4 wpos = projectionMatrixInv * clipVec;
return wpos.xyz / wpos.w;
}
vec3 getWorldPos(float depth, vec2 coord) {
if (logDepth && !ortho) {
return getWorldPosLog(vec3(coord, depth));
}
if (ortho) {
#ifdef REVERSEDEPTH
float z = depth;
#else
float z = depth * 2. - 1.;
#endif
vec4 clipSpacePosition = vec4(coord * 2. - 1., z, 1.);
vec4 viewSpacePosition = projectionMatrixInv * clipSpacePosition;
viewSpacePosition.xyz /= viewSpacePosition.w;
return viewSpacePosition.xyz;
}
vec2 ndc = coord * 2. - 1.;
#ifdef REVERSEDEPTH
float ndcZ = depth;
#else
float ndcZ = depth * 2. - 1.;
#endif
mat4 Q = projectionMatrixInv;
vec3 view = vec3(Q[0][0] * ndc.x + Q[3][0], Q[1][1] * ndc.y + Q[3][1], Q[3][2]);
float invW = 1. / (Q[2][3] * ndcZ + Q[3][3]);
return view * invW;
}
vec3 computeNormal(vec3 worldPos, vec2 vUv) {
ivec2 p = ivec2(vUv * resolution);
float c0 = texelFetch(sceneDepth, p, 0).x;
float l2 = texelFetch(sceneDepth, p - ivec2(2, 0), 0).x;
float l1 = texelFetch(sceneDepth, p - ivec2(1, 0), 0).x;
float r1 = texelFetch(sceneDepth, p + ivec2(1, 0), 0).x;
float r2 = texelFetch(sceneDepth, p + ivec2(2, 0), 0).x;
float b2 = texelFetch(sceneDepth, p - ivec2(0, 2), 0).x;
float b1 = texelFetch(sceneDepth, p - ivec2(0, 1), 0).x;
float t1 = texelFetch(sceneDepth, p + ivec2(0, 1), 0).x;
float t2 = texelFetch(sceneDepth, p + ivec2(0, 2), 0).x;
float dl = abs((2.0 * l1 - l2) - c0);
float dr = abs((2.0 * r1 - r2) - c0);
float db = abs((2.0 * b1 - b2) - c0);
float dt = abs((2.0 * t1 - t2) - c0);
vec3 ce = getWorldPos(c0, vUv).xyz;
vec3 dpdx = (dl < dr) ? ce - getWorldPos(l1, (vUv - vec2(1.0 / resolution.x, 0.0))).xyz
: -ce + getWorldPos(r1, (vUv + vec2(1.0 / resolution.x, 0.0))).xyz;
vec3 dpdy = (db < dt) ? ce - getWorldPos(b1, (vUv - vec2(0.0, 1.0 / resolution.y))).xyz
: -ce + getWorldPos(t1, (vUv + vec2(0.0, 1.0 / resolution.y))).xyz;
return normalize(cross(dpdx, dpdy));
}
bool isBackgroundDepth(float depth) {
#ifdef REVERSEDEPTH
return depth == 0.0;
#else
return depth == 1.0;
#endif
}
// Later passes rebuild each half-res pixel's position at the pixel's center, but the chosen
// depth belongs to one of its four full-res samples. On sloped surfaces that puts the position
// in front of or behind the surface, so the depth is moved along the sample's tangent plane to
// the center.
float depthAtCenter(float depth, vec3 position, vec3 normal) {
if (isBackgroundDepth(depth)) {
return depth;
}
// The center's view ray, through the point at the same depth.
vec3 center = getWorldPos(depth, vUv);
vec3 origin = ortho ? center : vec3(0.0);
vec3 direction = ortho ? vec3(0.0, 0.0, -1.0) : center;
float facing = dot(direction, normal);
if (abs(facing) < 1e-6 * length(direction)) {
return depth;
}
vec3 onPlane = origin + direction * (dot(position - origin, normal) / facing);
// Near grazing angles the plane runs almost along the ray; keep the sample there.
if (distance(onPlane, center) > 4.0 * distance(position, center)) {
return depth;
}
float centerDepth;
if (logDepth && !ortho) {
centerDepth = log2(1.0 - onPlane.z) / log2(far + 1.0);
} else {
vec4 clip = projMat * vec4(onPlane, 1.0);
#ifdef REVERSEDEPTH
centerDepth = clip.z / clip.w;
#else
centerDepth = clip.z / clip.w * 0.5 + 0.5;
#endif
}
// Never move a surface onto or past the near and far planes.
return centerDepth > 0.0 && centerDepth < 1.0 ? centerDepth : depth;
}
void main() {
vec2 uv = vUv - vec2(0.5) / resolution;
vec2 pixelSize = vec2(1.0) / resolution;
highp vec2[4] uvSamples;
uvSamples[0] = uv;
uvSamples[1] = uv + vec2(pixelSize.x, 0.0);
uvSamples[2] = uv + vec2(0.0, pixelSize.y);
uvSamples[3] = uv + pixelSize;
float depth00 = texture2D(sceneDepth, uvSamples[0]).r;
float depth10 = texture2D(sceneDepth, uvSamples[1]).r;
float depth01 = texture2D(sceneDepth, uvSamples[2]).r;
float depth11 = texture2D(sceneDepth, uvSamples[3]).r;
float minDepth = min(min(depth00, depth10), min(depth01, depth11));
float maxDepth = max(max(depth00, depth10), max(depth01, depth11));
float targetDepth = minDepth;
// Checkerboard pattern to avoid artifacts
if (mod(gl_FragCoord.x + gl_FragCoord.y, 2.0) > 0.5) {
targetDepth = maxDepth;
}
int chosenIndex = 0;
float[4] samples;
samples[0] = depth00;
samples[1] = depth10;
samples[2] = depth01;
samples[3] = depth11;
for(int i = 0; i < 4; ++i) {
if (samples[i] == targetDepth) {
chosenIndex = i;
break;
}
}
vec3 chosenPos = getWorldPos(samples[chosenIndex], uvSamples[chosenIndex]);
vec3 chosenNormal = computeNormal(chosenPos, uvSamples[chosenIndex]);
gl_FragColor = vec4(depthAtCenter(samples[chosenIndex], chosenPos, chosenNormal), 0.0, 0.0, 1.0);
gNormal = vec4(chosenNormal, 0.0);
}`
};
export { DepthDownSample };