Skip to content

Commit

Permalink
spotColorMap
Browse files Browse the repository at this point in the history
  • Loading branch information
mbredif committed Aug 3, 2018
1 parent f2a52bc commit 08dbde2
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 7 deletions.
25 changes: 22 additions & 3 deletions examples/webgl_shadowmap_viewer.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

var camera, scene, renderer, clock, stats;
var dirLight, spotLight;
var dirLight, spotLight, spotLight2;
var torusKnot, cube;
var dirLightShadowMapViewer, spotLightShadowMapViewer;

Expand All @@ -65,15 +65,17 @@
camera.position.set( 0, 15, 35 );

scene = new THREE.Scene();
var textureLoader = new THREE.TextureLoader();

// Lights

scene.add( new THREE.AmbientLight( 0x404040 ) );
scene.add( new THREE.AmbientLight( 0x202020 ) );

spotLight = new THREE.SpotLight( 0xffffff );
spotLight.name = 'Spot Light';
spotLight.intensity = 0.3;
spotLight.angle = Math.PI / 5;
spotLight.penumbra = 0.3;
spotLight.penumbra = 0.2;
spotLight.position.set( 10, 10, 5 );
spotLight.castShadow = true;
spotLight.shadow.camera.near = 8;
Expand All @@ -84,8 +86,25 @@

scene.add( new THREE.CameraHelper( spotLight.shadow.camera ) );

spotLight2 = new THREE.SpotLight( 0xffffff );
spotLight2.name = 'Textured Spot Light';
spotLight2.intensity = 0.3;
spotLight2.angle = Math.PI / 5;
spotLight2.penumbra = 0.3;
spotLight2.position.set( -10, 5, 0 );
spotLight2.castShadow = true;
spotLight2.shadow.camera.near = 8;
spotLight2.shadow.camera.far = 25;
spotLight2.shadow.mapSize.width = 1024;
spotLight2.shadow.mapSize.height = 1024;
spotLight2.colorMap = textureLoader.load( 'textures/UV_Grid_Sm.jpg' );
scene.add( spotLight2 );

scene.add( new THREE.CameraHelper( spotLight2.shadow.camera ) );

dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
dirLight.name = 'Dir. Light';
dirLight.intensity = 0.3;
dirLight.position.set( 0, 10, 0 );
dirLight.castShadow = true;
dirLight.shadow.camera.near = 1;
Expand Down
3 changes: 2 additions & 1 deletion src/lights/SpotLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Object3D } from '../core/Object3D.js';
* @author alteredq / http://alteredqualia.com/
*/

function SpotLight( color, intensity, distance, angle, penumbra, decay ) {
function SpotLight( color, intensity, distance, angle, penumbra, decay, colorMap ) {

Light.call( this, color, intensity );

Expand Down Expand Up @@ -40,6 +40,7 @@ function SpotLight( color, intensity, distance, angle, penumbra, decay ) {
this.decay = ( decay !== undefined ) ? decay : 1; // for physically correct lights, should be 2.

this.shadow = new SpotLightShadow();
this.colorMap = colorMap || null;

}

Expand Down
1 change: 1 addition & 0 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,7 @@ function WebGLRenderer( parameters ) {

uniforms.directionalShadowMap.value = lights.state.directionalShadowMap;
uniforms.directionalShadowMatrix.value = lights.state.directionalShadowMatrix;
uniforms.spotColorMap.value = lights.state.spotColorMap;
uniforms.spotShadowMap.value = lights.state.spotShadowMap;
uniforms.spotShadowMatrix.value = lights.state.spotShadowMatrix;
uniforms.pointShadowMap.value = lights.state.pointShadowMap;
Expand Down
8 changes: 8 additions & 0 deletions src/renderers/shaders/ShaderChunk/lights_fragment_begin.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ IncidentLight directLight;
#if ( NUM_SPOT_LIGHTS > 0 ) && defined( RE_Direct )

SpotLight spotLight;
vec4 spotColor;
vec2 spotShadowCoord;
bvec4 inFrustumVec;

#pragma unroll_loop
for ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {
Expand All @@ -53,6 +56,11 @@ IncidentLight directLight;

getSpotDirectLightIrradiance( spotLight, geometry, directLight );

spotShadowCoord = vSpotShadowCoord[ i ].xy / vSpotShadowCoord[ i ].w;
inFrustumVec = bvec4( spotShadowCoord.x >= 0.0, spotShadowCoord.x <= 1.0, spotShadowCoord.y >= 0.0, spotShadowCoord.y <= 1.0 );
spotColor = texture2D( spotColorMap[ i ], spotShadowCoord );
directLight.color = all( inFrustumVec ) ? mix( directLight.color, spotColor.rgb, spotColor.a ) : directLight.color;

#ifdef USE_SHADOWMAP
directLight.color *= all( bvec2( spotLight.shadow, directLight.visible ) ) ? getShadow( spotShadowMap[ i ], spotLight.shadowMapSize, spotLight.shadowBias, spotLight.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#if NUM_SPOT_LIGHTS > 0

uniform sampler2D spotColorMap[ NUM_SPOT_LIGHTS ];
uniform sampler2D spotShadowMap[ NUM_SPOT_LIGHTS ];
varying vec4 vSpotShadowCoord[ NUM_SPOT_LIGHTS ];

Expand Down
1 change: 1 addition & 0 deletions src/renderers/shaders/UniformsLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ var UniformsLib = {
shadowMapSize: {}
} },

spotColorMap: { value: [] },
spotShadowMap: { value: [] },
spotShadowMatrix: { value: [] },

Expand Down
3 changes: 3 additions & 0 deletions src/renderers/webgl/WebGLLights.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function WebGLLights() {
directionalShadowMap: [],
directionalShadowMatrix: [],
spot: [],
spotColorMap: [],
spotShadowMap: [],
spotShadowMatrix: [],
rectArea: [],
Expand Down Expand Up @@ -159,6 +160,7 @@ function WebGLLights() {
var intensity = light.intensity;
var distance = light.distance;

var colorMap = light.colorMap || null;
var shadowMap = ( light.shadow && light.shadow.map ) ? light.shadow.map.texture : null;

if ( light.isAmbientLight ) {
Expand Down Expand Up @@ -226,6 +228,7 @@ function WebGLLights() {

}

state.spotColorMap[ spotLength ] = colorMap;
state.spotShadowMap[ spotLength ] = shadowMap;
state.spotShadowMatrix[ spotLength ] = light.shadow.matrix;
state.spot[ spotLength ] = uniforms;
Expand Down
10 changes: 7 additions & 3 deletions src/renderers/webgl/WebGLShadowMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,13 @@ function WebGLShadowMap( _renderer, _objects, maxTextureSize ) {

faceCount = 1;

_lookTarget.setFromMatrixPosition( light.target.matrixWorld );
shadowCamera.lookAt( _lookTarget );
shadowCamera.updateMatrixWorld();
if ( light.target ) {

_lookTarget.setFromMatrixPosition( light.target.matrixWorld );
shadowCamera.lookAt( _lookTarget );
shadowCamera.updateMatrixWorld();

}

// compute shadow matrix

Expand Down

0 comments on commit 08dbde2

Please sign in to comment.