Skip to content

Commit

Permalink
fix(N8AO): vendor for code-splitting, react-native (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett authored Oct 21, 2023
1 parent fbb89ad commit 1f4c6c8
Show file tree
Hide file tree
Showing 11 changed files with 1,394 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"trailingComma": "es5",
"singleQuote": true,
"tabWidth": 2,
"printWidth": 120
"printWidth": 120,
"endOfLine": "auto"
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"build-storybook": "storybook build"
},
"dependencies": {
"buffer": "^6.0.3",
"maath": "^0.6.0",
"n8ao": "^1.6.6",
"postprocessing": "^6.32.1",
Expand Down
1 change: 1 addition & 0 deletions src/effects/N8AO/BlueNoise.js

Large diffs are not rendered by default.

160 changes: 160 additions & 0 deletions src/effects/N8AO/DepthDownSample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import * as THREE from 'three'

const DepthDownSample = {
uniforms: {
sceneDepth: { value: null },
resolution: { value: new THREE.Vector2() },
near: { value: 0.1 },
far: { value: 1000.0 },
viewMatrixInv: { value: new THREE.Matrix4() },
projectionMatrixInv: { value: new THREE.Matrix4() },
logDepth: { 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 mat4 viewMatrixInv;
uniform mat4 projectionMatrixInv;
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) {
return getWorldPosLog(vec3(coord, depth));
}
float z = depth * 2.0 - 1.0;
vec4 clipSpacePosition = vec4(coord * 2.0 - 1.0, z, 1.0);
vec4 viewSpacePosition = projectionMatrixInv * clipSpacePosition;
// Perspective division
vec4 worldSpacePosition = viewSpacePosition;
worldSpacePosition.xyz /= worldSpacePosition.w;
return worldSpacePosition.xyz;
}
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));
}
void main() {
vec2 uv = vUv - vec2(0.5) / resolution;
vec2 pixelSize = vec2(1.0) / resolution;
vec2[] uvSamples = vec2[4](
uv,
uv + vec2(pixelSize.x, 0.0),
uv + vec2(0.0, pixelSize.y),
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[] samples = float[4](depth00, depth10, depth01, depth11);
for(int i = 0; i < 4; ++i) {
if (samples[i] == targetDepth) {
chosenIndex = i;
break;
}
}
gl_FragColor = vec4(samples[chosenIndex], 0.0, 0.0, 1.0);
gNormal = vec4(computeNormal(
getWorldPos(samples[chosenIndex], uvSamples[chosenIndex]), uvSamples[chosenIndex]
), 0.0);
/* float[] samples = float[4](depth00, depth10, depth01, depth11);
float c = 0.25 * (depth00 + depth10 + depth01 + depth11);
float[] distances = float[4](depth00, depth10, depth01, depth11);
float maxDistance = max(max(distances[0], distances[1]), max(distances[2], distances[3]));
int remaining[3];
int rejected[3];
int i, j, k;
for(i = 0, j = 0, k = 0; i < 4; ++i) {
if (distances[i] < maxDistance) {
remaining[j++] = i;
} else {
rejected[k++] = i;
}
}
for(;j < 3;++j) {
remaining[j] = rejected[--k];
}
vec3 s = vec3(
samples[remaining[0]],
samples[remaining[1]],
samples[remaining[2]]
);
c = (s.x + s.y + s.z) / 3.0;
distances[0] = abs(c - s.x);
distances[1] = abs(c - s.y);
distances[2] = abs(c - s.z);
float minDistance = min(min(distances[0], distances[1]), distances[2]);
for(i = 0; i < 3; ++i) {
if (distances[i] == minDistance) {
break;
}
}*/
/* gl_FragColor = vec4(samples[remaining[i]], 0.0, 0.0, 0.0);
gNormal = vec4(computeNormal(
getWorldPos(samples[remaining[i]], uvSamples[remaining[i]]), uvSamples[remaining[i]]
), 0.0);*/
}`,
}

export { DepthDownSample }
Loading

0 comments on commit 1f4c6c8

Please sign in to comment.