-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replaces ssao dependency by local TS implementation of the same techn…
…ique (#80)
- Loading branch information
1 parent
a65c0e7
commit 9545514
Showing
11 changed files
with
1,488 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
packages/3d-web-client-core/src/rendering/post-effects/n8-ssao/BlueNoise.ts
Large diffs are not rendered by default.
Oops, something went wrong.
138 changes: 138 additions & 0 deletions
138
packages/3d-web-client-core/src/rendering/post-effects/n8-ssao/DepthDownSample.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Original code from: https://github.com/N8python/n8ao | ||
// ported to TypeScript | ||
|
||
import { Matrix4, Uniform, Vector2 } from "three"; | ||
|
||
const DepthDownSample = { | ||
uniforms: { | ||
sceneDepth: new Uniform(null), | ||
resolution: new Uniform(new Vector2()), | ||
near: new Uniform(0.1), | ||
far: new Uniform(1000.0), | ||
viewMatrixInv: new Uniform(new Matrix4()), | ||
projectionMatrixInv: new Uniform(new Matrix4()), | ||
logDepth: new Uniform(false), | ||
}, | ||
|
||
depthWrite: false, | ||
|
||
depthTest: false, | ||
|
||
vertexShader: /* glsl */ ` | ||
varying vec2 vUv; | ||
void main(void) { | ||
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; | ||
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(void) { | ||
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; | ||
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 | ||
); | ||
}`, | ||
}; | ||
|
||
export { DepthDownSample }; |
Oops, something went wrong.