Skip to content

Commit

Permalink
add support for depth sensing
Browse files Browse the repository at this point in the history
  • Loading branch information
cabanier committed Nov 10, 2023
1 parent d4c01b9 commit e0b2926
Show file tree
Hide file tree
Showing 31 changed files with 291 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/webxr_xr_ballshooter.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
controls.target.y = 1.6;
controls.update();

document.body.appendChild( XRButton.createButton( renderer ) );
document.body.appendChild( XRButton.createButton( renderer, { 'optionalFeatures': [ 'depth-sensing'] } ) );

// controllers

Expand Down
2 changes: 1 addition & 1 deletion examples/webxr_xr_cubes.html
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@

//

document.body.appendChild( XRButton.createButton( renderer ) );
document.body.appendChild( XRButton.createButton( renderer, { 'optionalFeatures': [ 'depth-sensing'] } ) );

}

Expand Down
2 changes: 1 addition & 1 deletion examples/webxr_xr_dragging.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
renderer.xr.enabled = true;
container.appendChild( renderer.domElement );

document.body.appendChild( XRButton.createButton( renderer ) );
document.body.appendChild( XRButton.createButton( renderer, { 'optionalFeatures': [ 'depth-sensing'] } ) );

// controllers

Expand Down
4 changes: 4 additions & 0 deletions src/materials/LineBasicMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class LineBasicMaterial extends Material {

this.fog = true;

this.occlusion = true;

this.setValues( parameters );

}
Expand All @@ -40,6 +42,8 @@ class LineBasicMaterial extends Material {

this.fog = source.fog;

this.occlusion = source.occlusion;

return this;

}
Expand Down
4 changes: 4 additions & 0 deletions src/materials/MeshBasicMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class MeshBasicMaterial extends Material {

this.fog = true;

this.occlusion = true;

this.setValues( parameters );

}
Expand Down Expand Up @@ -72,6 +74,8 @@ class MeshBasicMaterial extends Material {

this.fog = source.fog;

this.occlusion = source.occlusion;

return this;

}
Expand Down
4 changes: 4 additions & 0 deletions src/materials/MeshLambertMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class MeshLambertMaterial extends Material {

this.fog = true;

this.occlusion = true;

this.setValues( parameters );

}
Expand Down Expand Up @@ -107,6 +109,8 @@ class MeshLambertMaterial extends Material {

this.fog = source.fog;

this.occlusion = source.occlusion;

return this;

}
Expand Down
4 changes: 4 additions & 0 deletions src/materials/MeshMatcapMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class MeshMatcapMaterial extends Material {

this.fog = true;

this.occlusion = true;

this.setValues( parameters );

}
Expand Down Expand Up @@ -72,6 +74,8 @@ class MeshMatcapMaterial extends Material {

this.fog = source.fog;

this.occlusion = source.occlusion;

return this;

}
Expand Down
4 changes: 4 additions & 0 deletions src/materials/MeshPhongMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class MeshPhongMaterial extends Material {

this.fog = true;

this.occlusion = true;

this.setValues( parameters );

}
Expand Down Expand Up @@ -111,6 +113,8 @@ class MeshPhongMaterial extends Material {

this.fog = source.fog;

this.occlusion = source.occlusion;

return this;

}
Expand Down
4 changes: 4 additions & 0 deletions src/materials/MeshStandardMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class MeshStandardMaterial extends Material {

this.fog = true;

this.occlusion = true;

this.setValues( parameters );

}
Expand Down Expand Up @@ -115,6 +117,8 @@ class MeshStandardMaterial extends Material {

this.fog = source.fog;

this.occlusion = source.occlusion;

return this;

}
Expand Down
4 changes: 4 additions & 0 deletions src/materials/MeshToonMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class MeshToonMaterial extends Material {

this.fog = true;

this.occlusion = true;

this.setValues( parameters );

}
Expand Down Expand Up @@ -93,6 +95,8 @@ class MeshToonMaterial extends Material {

this.fog = source.fog;

this.occlusion = source.occlusion;

return this;

}
Expand Down
4 changes: 4 additions & 0 deletions src/materials/PointsMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class PointsMaterial extends Material {

this.fog = true;

this.occlusion = true;

this.setValues( parameters );

}
Expand All @@ -41,6 +43,8 @@ class PointsMaterial extends Material {

this.fog = source.fog;

this.occlusion = source.occlusion;

return this;

}
Expand Down
2 changes: 2 additions & 0 deletions src/materials/ShaderMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class ShaderMaterial extends Material {
this.lights = source.lights;
this.clipping = source.clipping;

this.occlusion = source.occlusion;

this.extensions = Object.assign( {}, source.extensions );

this.glslVersion = source.glslVersion;
Expand Down
27 changes: 26 additions & 1 deletion src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ class WebGLRenderer {
const _vector3 = new Vector3();

const _emptyScene = { background: null, fog: null, environment: null, overrideMaterial: null, isScene: true };
let _occlusion = null;

function getTargetPixelRatio() {

Expand Down Expand Up @@ -415,6 +416,20 @@ class WebGLRenderer {

};

this.setOcclusion = function ( value ) {

if ( value === undefined ) return;

_occlusion = value;

};

this.clearOcclusion = function () {

_occlusion = null;

};

this.getSize = function ( target ) {

return target.set( _width, _height );
Expand Down Expand Up @@ -1600,7 +1615,7 @@ class WebGLRenderer {

const lightsStateVersion = lights.state.version;

const parameters = programCache.getParameters( material, lights.state, shadowsArray, scene, object );
const parameters = programCache.getParameters( material, lights.state, shadowsArray, scene, object, _occlusion );
const programCacheKey = programCache.getProgramCacheKey( parameters );

let programs = materialProperties.programs;
Expand Down Expand Up @@ -1840,6 +1855,10 @@ class WebGLRenderer {

needsProgramChange = true;

} else if ( material.occlusion === true ) {

needsProgramChange = true;

} else if ( materialProperties.numClippingPlanes !== undefined &&
( materialProperties.numClippingPlanes !== clipping.numPlanes ||
materialProperties.numIntersection !== clipping.numIntersection ) ) {
Expand Down Expand Up @@ -2054,6 +2073,12 @@ class WebGLRenderer {

}

if ( _occlusion && material.occlusion === true ) {

materials.refreshOcclusionUniforms( m_uniforms, _occlusion );

}

materials.refreshMaterialUniforms( m_uniforms, material, _pixelRatio, _height, _transmissionRenderTarget );

WebGLUniforms.upload( _gl, getUniformList( materialProperties ), m_uniforms, textures );
Expand Down
4 changes: 4 additions & 0 deletions src/renderers/shaders/ShaderChunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ import normal_pars_fragment from './ShaderChunk/normal_pars_fragment.glsl.js';
import normal_pars_vertex from './ShaderChunk/normal_pars_vertex.glsl.js';
import normal_vertex from './ShaderChunk/normal_vertex.glsl.js';
import normalmap_pars_fragment from './ShaderChunk/normalmap_pars_fragment.glsl.js';
import occlusion_fragment from './ShaderChunk/occlusion_fragment.glsl.js';
import occlusion_pars_fragment from './ShaderChunk/occlusion_pars_fragment.glsl.js';
import clearcoat_normal_fragment_begin from './ShaderChunk/clearcoat_normal_fragment_begin.glsl.js';
import clearcoat_normal_fragment_maps from './ShaderChunk/clearcoat_normal_fragment_maps.glsl.js';
import clearcoat_pars_fragment from './ShaderChunk/clearcoat_pars_fragment.glsl.js';
Expand Down Expand Up @@ -200,6 +202,8 @@ export const ShaderChunk = {
normal_pars_vertex: normal_pars_vertex,
normal_vertex: normal_vertex,
normalmap_pars_fragment: normalmap_pars_fragment,
occlusion_fragment: occlusion_fragment,
occlusion_pars_fragment: occlusion_pars_fragment,
clearcoat_normal_fragment_begin: clearcoat_normal_fragment_begin,
clearcoat_normal_fragment_maps: clearcoat_normal_fragment_maps,
clearcoat_pars_fragment: clearcoat_pars_fragment,
Expand Down
26 changes: 26 additions & 0 deletions src/renderers/shaders/ShaderChunk/occlusion_fragment.glsl.js
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 src/renderers/shaders/ShaderChunk/occlusion_pars_fragment.glsl.js
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
`;
Loading

0 comments on commit e0b2926

Please sign in to comment.