Skip to content

Commit fa6fe81

Browse files
committed
Add worldLightPosition option.
1 parent 619d4fe commit fa6fe81

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

examples/jsm/postprocessing/SSShadowPass.js

+19
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ class SSRrPass extends Pass {
6666
}
6767
} );
6868

69+
this._worldLightPosition = SSRrShader.defines.WORLD_LIGHT_POSITION;
70+
Object.defineProperty( this, 'worldLightPosition', {
71+
get() {
72+
73+
return this._worldLightPosition;
74+
75+
},
76+
set( val ) {
77+
78+
if ( this._worldLightPosition === val ) return;
79+
this._worldLightPosition = val;
80+
this.ssrrMaterial.defines.WORLD_LIGHT_POSITION = val;
81+
this.ssrrMaterial.needsUpdate = true;
82+
83+
}
84+
} );
85+
6986
// beauty render target with depth buffer
7087

7188
const depthTexture = new DepthTexture();
@@ -134,6 +151,7 @@ class SSRrPass extends Pass {
134151
this.ssrrMaterial.uniforms[ 'resolution' ].value.set( this.width, this.height );
135152
this.ssrrMaterial.uniforms[ 'cameraProjectionMatrix' ].value.copy( this.camera.projectionMatrix );
136153
this.ssrrMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.copy( this.camera.projectionMatrixInverse );
154+
this.ssrrMaterial.uniforms[ 'cameraMatrixWorldInverse' ].value.copy( this.camera.matrixWorldInverse );
137155

138156
// normal material
139157

@@ -231,6 +249,7 @@ class SSRrPass extends Pass {
231249
this.ssrrMaterial.uniforms[ 'maxDistance' ].value = this.maxDistance;
232250
this.ssrrMaterial.uniforms[ 'surfDist' ].value = this.surfDist;
233251
this.ssrrMaterial.uniforms[ 'doubleSideCheckStartFrom' ].value = this.doubleSideCheckStartFrom;
252+
this.ssrrMaterial.uniforms[ 'cameraMatrixWorldInverse' ].value.copy( this.camera.matrixWorldInverse );
234253
this.renderPass( renderer, this.ssrrMaterial, this.ssrrRenderTarget );
235254

236255
// output result to screen

examples/jsm/shaders/SSShadowShader.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const SSRrShader = {
1010
MAX_STEP: 0,
1111
PERSPECTIVE_CAMERA: true,
1212
INFINITE_THICK: false,
13+
WORLD_LIGHT_POSITION: false,
1314
},
1415

1516
uniforms: {
@@ -23,6 +24,7 @@ const SSRrShader = {
2324
'resolution': { value: new Vector2() },
2425
'cameraProjectionMatrix': { value: new Matrix4() },
2526
'cameraInverseProjectionMatrix': { value: new Matrix4() },
27+
'cameraMatrixWorldInverse': { value: new Matrix4() },
2628
'lightPosition': { value: new Vector3(1.7,1.7,0) },
2729
'cameraRange': { value: 0 },
2830
'maxDistance': { value: 180 },
@@ -60,6 +62,7 @@ const SSRrShader = {
6062
uniform vec3 lightPosition;
6163
uniform mat4 cameraProjectionMatrix;
6264
uniform mat4 cameraInverseProjectionMatrix;
65+
uniform mat4 cameraMatrixWorldInverse;
6366
uniform float maxDistance;
6467
uniform float surfDist;
6568
uniform float doubleSideCheckStartFrom;
@@ -124,7 +127,12 @@ const SSRrShader = {
124127
125128
vec3 viewNormal=getViewNormal( vUv );
126129
127-
vec3 viewRefractDir=normalize(lightPosition-viewPosition);
130+
#ifdef WORLD_LIGHT_POSITION
131+
vec3 viewLightPosition=(cameraMatrixWorldInverse*vec4(lightPosition,1.)).xyz;
132+
#else
133+
vec3 viewLightPosition=lightPosition;
134+
#endif
135+
vec3 viewRefractDir=normalize(viewLightPosition-viewPosition);
128136
129137
vec3 d1viewPosition=viewPosition+viewRefractDir*maxDistance;
130138
#ifdef PERSPECTIVE_CAMERA

examples/webgl_postprocessing_ssshadow.html

+5-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
const params = {
3535
enableSSRr: true,
36-
autoRotate: false,
36+
autoRotate: true,
3737
};
3838
let composer;
3939
let ssrrPass;
@@ -179,9 +179,10 @@
179179

180180
gui = new GUI();
181181
gui.add( params, 'enableSSRr' ).name( 'Enable SSRr' );
182-
gui.add( ssrrPass.lightPosition, 'x' ).name('lightX').min( 0 ).max( 10 ).step( .0001 );
183-
gui.add( ssrrPass.lightPosition, 'y' ).name('lightY').min( 0 ).max( 10 ).step( .0001 );
184-
gui.add( ssrrPass.lightPosition, 'z' ).name('lightZ').min( 0 ).max( 10 ).step( .0001 );
182+
gui.add( ssrrPass.lightPosition, 'x' ).name('lightX').min( -10 ).max( 10 ).step( .0001 );
183+
gui.add( ssrrPass.lightPosition, 'y' ).name('lightY').min( -10 ).max( 10 ).step( .0001 );
184+
gui.add( ssrrPass.lightPosition, 'z' ).name('lightZ').min( -10 ).max( 10 ).step( .0001 );
185+
gui.add( ssrrPass, 'worldLightPosition' );
185186
gui.add( ssrrPass, 'doubleSideCheckStartFrom' ).min( 0 ).max( .1 ).step( .0001 );
186187
gui.add( params, 'autoRotate' ).onChange( () => {
187188

0 commit comments

Comments
 (0)