Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 28 additions & 24 deletions build/three-csm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
const lightParsBeginInitial = three.ShaderChunk.lights_pars_begin;
const CSMShader = {
lights_fragment_begin: (cascades) => /* glsl */ `
GeometricContext geometry;
vec3 geometryPosition = - vViewPosition;
vec3 geometryNormal = normal;
vec3 geometryViewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );

geometry.position = - vViewPosition;
geometry.normal = normal;
geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
vec3 geometryClearcoatNormal;

#ifdef CLEARCOAT

geometry.clearcoatNormal = clearcoatNormal;
geometryClearcoatNormal = clearcoatNormal;

#endif

Expand All @@ -33,14 +33,14 @@ IncidentLight directLight;

pointLight = pointLights[ i ];

getPointLightInfo( pointLight, geometry, directLight );
getPointLightInfo( pointLight, geometryPosition, directLight );

#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_POINT_LIGHT_SHADOWS )
pointLightShadow = pointLightShadows[ i ];
directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
#endif

RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );

}
#pragma unroll_loop_end
Expand All @@ -59,14 +59,14 @@ IncidentLight directLight;

spotLight = spotLights[ i ];

getSpotLightInfo( spotLight, geometry, directLight );
getSpotLightInfo( spotLight, geometryPosition, directLight );

#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS )
spotLightShadow = spotLightShadows[ i ];
directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ i ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;
#endif

RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );

}
#pragma unroll_loop_end
Expand All @@ -93,7 +93,7 @@ IncidentLight directLight;
for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {

directionalLight = directionalLights[ i ];
getDirectionalLightInfo( directionalLight, geometry, directLight );
getDirectionalLightInfo( directionalLight, directLight );

#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
// NOTE: Depth gets larger away from the camera.
Expand Down Expand Up @@ -122,7 +122,7 @@ IncidentLight directLight;
directLight.color = mix( prevColor, directLight.color, shouldFadeLastCascade ? ratio : 1.0 );

ReflectedLight prevLight = reflectedLight;
RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );

bool shouldBlend = UNROLLED_LOOP_INDEX != CSM_CASCADES - 1 || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1 && linearDepth < cascadeCenter;
float blendRatio = shouldBlend ? ratio : 1.0;
Expand All @@ -141,7 +141,7 @@ IncidentLight directLight;
directionalLightShadow = directionalLightShadows[ i ];
directLight.color *= ( directLight.visible && receiveShadow ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;

RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal,, material, reflectedLight );

#endif

Expand All @@ -155,9 +155,9 @@ IncidentLight directLight;
for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {

directionalLight = directionalLights[ i ];
getDirectionalLightInfo( directionalLight, geometry, directLight );
getDirectionalLightInfo( directionalLight, directLight );

#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )

#if ( UNROLLED_LOOP_INDEX < ${cascades} )

Expand All @@ -166,16 +166,16 @@ IncidentLight directLight;
directionalLightShadow = directionalLightShadows[ i ];
if(linearDepth >= CSM_cascades[UNROLLED_LOOP_INDEX].x && linearDepth < CSM_cascades[UNROLLED_LOOP_INDEX].y) directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;

if(linearDepth >= CSM_cascades[UNROLLED_LOOP_INDEX].x && (linearDepth < CSM_cascades[UNROLLED_LOOP_INDEX].y || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1)) RE_Direct( directLight, geometry, material, reflectedLight );
if(linearDepth >= CSM_cascades[UNROLLED_LOOP_INDEX].x && (linearDepth < CSM_cascades[UNROLLED_LOOP_INDEX].y || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1)) RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );

#else

// NOTE: Apply the reminder of directional lights

directionalLightShadow = directionalLightShadows[ i ];
directLight.color *= ( directLight.visible && receiveShadow ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
RE_Direct( directLight, geometry, material, reflectedLight );

RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );

#endif

Expand All @@ -194,9 +194,9 @@ IncidentLight directLight;

directionalLight = directionalLights[ i ];

getDirectionalLightInfo( directionalLight, geometry, directLight );
getDirectionalLightInfo( directionalLight, directLight );

RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );

}
#pragma unroll_loop_end
Expand All @@ -218,14 +218,14 @@ IncidentLight directLight;

directionalLight = directionalLights[ i ];

getDirectionalLightInfo( directionalLight, geometry, directLight );
getDirectionalLightInfo( directionalLight, directLight );

#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
directionalLightShadow = directionalLightShadows[ i ];
directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
#endif

RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );

}
#pragma unroll_loop_end
Expand All @@ -240,7 +240,7 @@ IncidentLight directLight;
for ( int i = 0; i < NUM_RECT_AREA_LIGHTS; i ++ ) {

rectAreaLight = rectAreaLights[ i ];
RE_Direct_RectArea( rectAreaLight, geometry, material, reflectedLight );
RE_Direct_RectArea( rectAreaLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );

}
#pragma unroll_loop_end
Expand All @@ -253,14 +253,18 @@ IncidentLight directLight;

vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );

irradiance += getLightProbeIrradiance( lightProbe, geometry.normal );
#if defined( USE_LIGHT_PROBES )

irradiance += getLightProbeIrradiance( lightProbe, geometryNormal );

#endif

#if ( NUM_HEMI_LIGHTS > 0 )

#pragma unroll_loop_start
for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {

irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry.normal );
irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometryNormal );

}
#pragma unroll_loop_end
Expand Down
52 changes: 28 additions & 24 deletions build/three-csm.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { ShaderChunk, Group, BufferGeometry, BufferAttribute, LineSegments, Line
const lightParsBeginInitial = ShaderChunk.lights_pars_begin;
const CSMShader = {
lights_fragment_begin: (cascades) => /* glsl */ `
GeometricContext geometry;
vec3 geometryPosition = - vViewPosition;
vec3 geometryNormal = normal;
vec3 geometryViewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );

geometry.position = - vViewPosition;
geometry.normal = normal;
geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
vec3 geometryClearcoatNormal;

#ifdef CLEARCOAT

geometry.clearcoatNormal = clearcoatNormal;
geometryClearcoatNormal = clearcoatNormal;

#endif

Expand All @@ -29,14 +29,14 @@ IncidentLight directLight;

pointLight = pointLights[ i ];

getPointLightInfo( pointLight, geometry, directLight );
getPointLightInfo( pointLight, geometryPosition, directLight );

#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_POINT_LIGHT_SHADOWS )
pointLightShadow = pointLightShadows[ i ];
directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
#endif

RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );

}
#pragma unroll_loop_end
Expand All @@ -55,14 +55,14 @@ IncidentLight directLight;

spotLight = spotLights[ i ];

getSpotLightInfo( spotLight, geometry, directLight );
getSpotLightInfo( spotLight, geometryPosition, directLight );

#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS )
spotLightShadow = spotLightShadows[ i ];
directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ i ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;
#endif

RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );

}
#pragma unroll_loop_end
Expand All @@ -89,7 +89,7 @@ IncidentLight directLight;
for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {

directionalLight = directionalLights[ i ];
getDirectionalLightInfo( directionalLight, geometry, directLight );
getDirectionalLightInfo( directionalLight, directLight );

#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
// NOTE: Depth gets larger away from the camera.
Expand Down Expand Up @@ -118,7 +118,7 @@ IncidentLight directLight;
directLight.color = mix( prevColor, directLight.color, shouldFadeLastCascade ? ratio : 1.0 );

ReflectedLight prevLight = reflectedLight;
RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );

bool shouldBlend = UNROLLED_LOOP_INDEX != CSM_CASCADES - 1 || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1 && linearDepth < cascadeCenter;
float blendRatio = shouldBlend ? ratio : 1.0;
Expand All @@ -137,7 +137,7 @@ IncidentLight directLight;
directionalLightShadow = directionalLightShadows[ i ];
directLight.color *= ( directLight.visible && receiveShadow ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;

RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal,, material, reflectedLight );

#endif

Expand All @@ -151,9 +151,9 @@ IncidentLight directLight;
for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {

directionalLight = directionalLights[ i ];
getDirectionalLightInfo( directionalLight, geometry, directLight );
getDirectionalLightInfo( directionalLight, directLight );

#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )

#if ( UNROLLED_LOOP_INDEX < ${cascades} )

Expand All @@ -162,16 +162,16 @@ IncidentLight directLight;
directionalLightShadow = directionalLightShadows[ i ];
if(linearDepth >= CSM_cascades[UNROLLED_LOOP_INDEX].x && linearDepth < CSM_cascades[UNROLLED_LOOP_INDEX].y) directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;

if(linearDepth >= CSM_cascades[UNROLLED_LOOP_INDEX].x && (linearDepth < CSM_cascades[UNROLLED_LOOP_INDEX].y || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1)) RE_Direct( directLight, geometry, material, reflectedLight );
if(linearDepth >= CSM_cascades[UNROLLED_LOOP_INDEX].x && (linearDepth < CSM_cascades[UNROLLED_LOOP_INDEX].y || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1)) RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );

#else

// NOTE: Apply the reminder of directional lights

directionalLightShadow = directionalLightShadows[ i ];
directLight.color *= ( directLight.visible && receiveShadow ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
RE_Direct( directLight, geometry, material, reflectedLight );

RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );

#endif

Expand All @@ -190,9 +190,9 @@ IncidentLight directLight;

directionalLight = directionalLights[ i ];

getDirectionalLightInfo( directionalLight, geometry, directLight );
getDirectionalLightInfo( directionalLight, directLight );

RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );

}
#pragma unroll_loop_end
Expand All @@ -214,14 +214,14 @@ IncidentLight directLight;

directionalLight = directionalLights[ i ];

getDirectionalLightInfo( directionalLight, geometry, directLight );
getDirectionalLightInfo( directionalLight, directLight );

#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
directionalLightShadow = directionalLightShadows[ i ];
directLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
#endif

RE_Direct( directLight, geometry, material, reflectedLight );
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );

}
#pragma unroll_loop_end
Expand All @@ -236,7 +236,7 @@ IncidentLight directLight;
for ( int i = 0; i < NUM_RECT_AREA_LIGHTS; i ++ ) {

rectAreaLight = rectAreaLights[ i ];
RE_Direct_RectArea( rectAreaLight, geometry, material, reflectedLight );
RE_Direct_RectArea( rectAreaLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );

}
#pragma unroll_loop_end
Expand All @@ -249,14 +249,18 @@ IncidentLight directLight;

vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );

irradiance += getLightProbeIrradiance( lightProbe, geometry.normal );
#if defined( USE_LIGHT_PROBES )

irradiance += getLightProbeIrradiance( lightProbe, geometryNormal );

#endif

#if ( NUM_HEMI_LIGHTS > 0 )

#pragma unroll_loop_start
for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {

irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry.normal );
irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometryNormal );

}
#pragma unroll_loop_end
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</head>

<body>
<script src="https://unpkg.com/three@0.155.0/build/three.js"></script>
<script src="https://unpkg.com/three@0.157.0/build/three.js"></script>
<script src="OrbitControls.js"></script>
<script src="dat.gui.min.js"></script>
<script src="../../build/three-csm.js"></script>
Expand Down
Loading