-
-
Notifications
You must be signed in to change notification settings - Fork 35.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
291 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
26 changes: 26 additions & 0 deletions
26
src/renderers/shaders/ShaderChunk/occlusion_fragment.glsl.js
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,26 @@ | ||
export default /* glsl */` | ||
#ifdef USE_OCCLUSION | ||
if (gl_FragColor.a > 0.0) { | ||
int arrayIndex = 0; | ||
vec2 depthUv; | ||
if (gl_FragCoord.x>=depthWidth) { | ||
arrayIndex = 1; | ||
depthUv = vec2((gl_FragCoord.x-depthWidth)/depthWidth, gl_FragCoord.y/depthHeight); | ||
} else { | ||
depthUv = vec2(gl_FragCoord.x/depthWidth, gl_FragCoord.y/depthHeight); | ||
} | ||
float assetDepthMm = gl_FragCoord.z * 1000.0; | ||
float occlusion = Depth_GetBlurredOcclusionAroundUV(depthColor, depthUv, assetDepthMm, arrayIndex); | ||
float objectMaskEroded = pow(occlusion, 10.0); | ||
float occlusionTransition = clamp(occlusion * (2.0 - objectMaskEroded), 0.0, 1.0); | ||
float kMaxOcclusion = 1.0; | ||
occlusionTransition = min(occlusionTransition, kMaxOcclusion); | ||
gl_FragColor = gl_FragColor * (1.0 - occlusion); | ||
} | ||
#endif | ||
`; |
97 changes: 97 additions & 0 deletions
97
src/renderers/shaders/ShaderChunk/occlusion_pars_fragment.glsl.js
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,97 @@ | ||
export default /* glsl */` | ||
#ifdef USE_OCCLUSION | ||
uniform sampler2DArray depthColor; | ||
uniform float depthWidth; | ||
uniform float depthHeight; | ||
float Depth_GetCameraDepthInMillimeters(const sampler2DArray depthTexture, | ||
const vec2 depthUv, int arrayIndex) { | ||
return texture(depthColor, vec3(depthUv.x, depthUv.y, arrayIndex)).r * 1000.0; | ||
} | ||
float Depth_GetOcclusion(const sampler2DArray depthTexture, const vec2 depthUv, float assetDepthMm, int arrayIndex) { | ||
float depthMm = Depth_GetCameraDepthInMillimeters(depthTexture, depthUv, arrayIndex); | ||
// Instead of a hard z-buffer test, allow the asset to fade into the | ||
// background along a 2 * kDepthTolerancePerMm * assetDepthMm | ||
// range centered on the background depth. | ||
const float kDepthTolerancePerMm = 0.01; | ||
return clamp(1.0 - | ||
0.5 * (depthMm - assetDepthMm) / | ||
(kDepthTolerancePerMm * assetDepthMm) + | ||
0.5, 0.0, 1.0); | ||
} | ||
float Depth_GetBlurredOcclusionAroundUV(const sampler2DArray depthTexture, const vec2 uv, float assetDepthMm, int arrayIndex) { | ||
// Kernel used: | ||
// 0 4 7 4 0 | ||
// 4 16 26 16 4 | ||
// 7 26 41 26 7 | ||
// 4 16 26 16 4 | ||
// 0 4 7 4 0 | ||
const float kKernelTotalWeights = 269.0; | ||
float sum = 0.0; | ||
const float kOcclusionBlurAmount = 0.01; | ||
vec2 blurriness = | ||
vec2(kOcclusionBlurAmount, kOcclusionBlurAmount /** u_DepthAspectRatio*/); | ||
float current = 0.0; | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(-1.0, -2.0) * blurriness, assetDepthMm, arrayIndex); | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(+1.0, -2.0) * blurriness, assetDepthMm, arrayIndex); | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(-1.0, +2.0) * blurriness, assetDepthMm, arrayIndex); | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(+1.0, +2.0) * blurriness, assetDepthMm, arrayIndex); | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(-2.0, +1.0) * blurriness, assetDepthMm, arrayIndex); | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(+2.0, +1.0) * blurriness, assetDepthMm, arrayIndex); | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(-2.0, -1.0) * blurriness, assetDepthMm, arrayIndex); | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(+2.0, -1.0) * blurriness, assetDepthMm, arrayIndex); | ||
sum += current * 4.0; | ||
current = 0.0; | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(-2.0, -0.0) * blurriness, assetDepthMm, arrayIndex); | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(+2.0, +0.0) * blurriness, assetDepthMm, arrayIndex); | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(+0.0, +2.0) * blurriness, assetDepthMm, arrayIndex); | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(-0.0, -2.0) * blurriness, assetDepthMm, arrayIndex); | ||
sum += current * 7.0; | ||
current = 0.0; | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(-1.0, -1.0) * blurriness, assetDepthMm, arrayIndex); | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(+1.0, -1.0) * blurriness, assetDepthMm, arrayIndex); | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(-1.0, +1.0) * blurriness, assetDepthMm, arrayIndex); | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(+1.0, +1.0) * blurriness, assetDepthMm, arrayIndex); | ||
sum += current * 16.0; | ||
current = 0.0; | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(+0.0, +1.0) * blurriness, assetDepthMm, arrayIndex); | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(-0.0, -1.0) * blurriness, assetDepthMm, arrayIndex); | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(-1.0, -0.0) * blurriness, assetDepthMm, arrayIndex); | ||
current += Depth_GetOcclusion( | ||
depthTexture, uv + vec2(+1.0, +0.0) * blurriness, assetDepthMm, arrayIndex); | ||
sum += current * 26.0; | ||
sum += Depth_GetOcclusion(depthTexture, uv, assetDepthMm, arrayIndex) * 41.0; | ||
return sum / kKernelTotalWeights; | ||
} | ||
#endif | ||
`; |
Oops, something went wrong.