Skip to content

Commit 477e435

Browse files
authored
WebGLRenderer: Modernized shadow mapping: native cube depth textures, Vogel disk sampling, and VSM improvements (#32303)
1 parent 39b824b commit 477e435

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+642
-346
lines changed

editor/js/Sidebar.Project.Renderer.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ function SidebarProjectRenderer( editor ) {
3737
const shadowTypeSelect = new UISelect().setOptions( {
3838
0: 'Basic',
3939
1: 'PCF',
40-
2: 'PCF Soft',
4140
3: 'VSM'
4241
} ).setWidth( '125px' ).onChange( updateShadows );
4342
shadowTypeSelect.setValue( config.getKey( 'project/renderer/shadowType' ) );

examples/jsm/materials/MeshGouraudMaterial.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ const GouraudShader = {
221221
#endif
222222
223223
#include <common>
224-
#include <packing>
225224
#include <dithering_pars_fragment>
226225
#include <color_pars_fragment>
227226
#include <uv_pars_fragment>

examples/jsm/shaders/UnpackDepthRGBAShader.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
/**
7-
* Unpack RGBA depth shader that shows RGBA encoded depth as monochrome color.
7+
* Depth visualization shader that shows depth values as monochrome color.
88
*
99
* @constant
1010
* @type {ShaderMaterial~Shader}
@@ -39,11 +39,9 @@ const UnpackDepthRGBAShader = {
3939
4040
varying vec2 vUv;
4141
42-
#include <packing>
43-
4442
void main() {
4543
46-
float depth = unpackRGBAToDepth( texture2D( tDiffuse, vUv ) );
44+
float depth = texture2D( tDiffuse, vUv ).r;
4745
4846
#ifdef USE_REVERSED_DEPTH_BUFFER
4947

examples/jsm/utils/ShadowMapViewer.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import {
66
OrthographicCamera,
77
PlaneGeometry,
88
Scene,
9-
ShaderMaterial,
10-
UniformsUtils
9+
ShaderMaterial
1110
} from 'three';
12-
import { UnpackDepthRGBAShader } from '../shaders/UnpackDepthRGBAShader.js';
1311

1412
/**
1513
* This is a helper for visualising a given light's shadow map.
@@ -57,13 +55,29 @@ class ShadowMapViewer {
5755
const scene = new Scene();
5856

5957
//HUD for shadow map
60-
const shader = UnpackDepthRGBAShader;
61-
62-
const uniforms = UniformsUtils.clone( shader.uniforms );
6358
const material = new ShaderMaterial( {
64-
uniforms: uniforms,
65-
vertexShader: shader.vertexShader,
66-
fragmentShader: shader.fragmentShader
59+
uniforms: {
60+
tDiffuse: { value: null },
61+
opacity: { value: 1.0 }
62+
},
63+
vertexShader: /* glsl */`
64+
varying vec2 vUv;
65+
void main() {
66+
vUv = uv;
67+
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
68+
}`,
69+
fragmentShader: /* glsl */`
70+
uniform float opacity;
71+
uniform sampler2D tDiffuse;
72+
varying vec2 vUv;
73+
void main() {
74+
float depth = texture2D( tDiffuse, vUv ).r;
75+
#ifdef USE_REVERSED_DEPTH_BUFFER
76+
gl_FragColor = vec4( vec3( depth ), opacity );
77+
#else
78+
gl_FragColor = vec4( vec3( 1.0 - depth ), opacity );
79+
#endif
80+
}`
6781
} );
6882
const plane = new PlaneGeometry( frame.width, frame.height );
6983
const mesh = new Mesh( plane, material );
@@ -177,7 +191,7 @@ class ShadowMapViewer {
177191
//always end up with the scene's first added shadow casting light's shadowMap
178192
//in the shader
179193
//See: https://github.com/mrdoob/three.js/issues/5932
180-
uniforms.tDiffuse.value = light.shadow.map.texture;
194+
material.uniforms.tDiffuse.value = light.shadow.map.texture;
181195

182196
userAutoClearSetting = renderer.autoClear;
183197
renderer.autoClear = false; // To allow render overlay

examples/webgl_animation_walk.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
renderer.toneMapping = THREE.ACESFilmicToneMapping;
111111
renderer.toneMappingExposure = 0.5;
112112
renderer.shadowMap.enabled = true;
113-
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
113+
renderer.shadowMap.type = THREE.PCFShadowMap;
114114
container.appendChild( renderer.domElement );
115115

116116
orbitControls = new OrbitControls( camera, renderer.domElement );

examples/webgl_depth_texture.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
}
2626
</script>
2727
<script id="post-frag" type="x-shader/x-fragment">
28-
#include <packing>
29-
3028
varying vec2 vUv;
3129
uniform sampler2D tDiffuse;
3230
uniform sampler2D tDepth;
@@ -36,8 +34,8 @@
3634

3735
float readDepth( sampler2D depthSampler, vec2 coord ) {
3836
float fragCoordZ = texture2D( depthSampler, coord ).x;
39-
float viewZ = perspectiveDepthToViewZ( fragCoordZ, cameraNear, cameraFar );
40-
return viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );
37+
float viewZ = ( cameraNear * cameraFar ) / ( ( cameraFar - cameraNear ) * fragCoordZ - cameraFar );
38+
return ( viewZ + cameraNear ) / ( cameraNear - cameraFar );
4139
}
4240

4341
void main() {

examples/webgl_geometry_csg.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
renderer.setSize( window.innerWidth, window.innerHeight );
8787
renderer.setAnimationLoop( animate );
8888
renderer.shadowMap.enabled = true;
89-
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
89+
renderer.shadowMap.type = THREE.PCFShadowMap;
9090
document.body.appendChild( renderer.domElement );
9191

9292
stats = new Stats();

examples/webgl_lights_spotlight.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
renderer.toneMappingExposure = 1;
5151

5252
renderer.shadowMap.enabled = true;
53-
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
53+
renderer.shadowMap.type = THREE.PCFShadowMap;
5454

5555
scene = new THREE.Scene();
5656

examples/webgl_lights_spotlights.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
function init() {
6060

6161
renderer.shadowMap.enabled = true;
62-
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
62+
renderer.shadowMap.type = THREE.PCFShadowMap;
6363

6464
camera.position.set( 4.6, 2.2, - 2.1 );
6565

examples/webgl_loader_3mf_materials.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
renderer.setPixelRatio( window.devicePixelRatio );
109109
renderer.setSize( window.innerWidth, window.innerHeight );
110110
renderer.shadowMap.enabled = true;
111-
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
111+
renderer.shadowMap.type = THREE.PCFShadowMap;
112112
document.body.appendChild( renderer.domElement );
113113

114114
//

0 commit comments

Comments
 (0)