|
@@ -44,209 +44,238 @@
|
|
|
shadowCoord.z += shadowBias[ i ];
|
|
|
|
|
|
#if defined( SHADOWMAP_TYPE_PCF )
|
|
|
- if( isPointLight ){
|
|
|
|
|
|
- initGridSamplingDisk();
|
|
|
+ #if defined(POINT_LIGHT_SHADOWS)
|
|
|
|
|
|
- float diskRadius = 1.5;
|
|
|
- float numSamples = 1.0;
|
|
|
- shadow = 0.0;
|
|
|
+ if( isPointLight ) {
|
|
|
|
|
|
- vec3 baseDirection = normalize( lightToPosition );
|
|
|
- float curDistance = length( lightToPosition );
|
|
|
+ initGridSamplingDisk();
|
|
|
|
|
|
- float dist = unpack1K( textureCube( shadowCube[ i ], baseDirection ) );
|
|
|
- if ( curDistance >= dist )
|
|
|
- shadow += 1.0;
|
|
|
-
|
|
|
- // evaluate each sampling direction
|
|
|
- for( int s = 0; s < 20; s++ ){
|
|
|
-
|
|
|
- vec3 offset = gridSamplingDisk[ s ] * diskRadius * cubeTexelSize;
|
|
|
- dist = unpack1K( textureCube( shadowCube[ i ], vec3( baseDirection + offset ) ) );
|
|
|
+ float diskRadius = 1.5;
|
|
|
+ float numSamples = 1.0;
|
|
|
+ shadow = 0.0;
|
|
|
+
|
|
|
+ vec3 baseDirection = normalize( lightToPosition );
|
|
|
+ float curDistance = length( lightToPosition );
|
|
|
+
|
|
|
+ float dist = unpack1K( textureCube( shadowCube[ i ], baseDirection ) );
|
|
|
if ( curDistance >= dist )
|
|
|
shadow += 1.0;
|
|
|
- numSamples += 1.0;
|
|
|
|
|
|
- }
|
|
|
+ // evaluate each sampling direction
|
|
|
+ for( int s = 0; s < 20; s++ ) {
|
|
|
+
|
|
|
+ vec3 offset = gridSamplingDisk[ s ] * diskRadius * cubeTexelSize;
|
|
|
+ dist = unpack1K( textureCube( shadowCube[ i ], vec3( baseDirection + offset ) ) );
|
|
|
+ if ( curDistance >= dist )
|
|
|
+ shadow += 1.0;
|
|
|
+ numSamples += 1.0;
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- shadow /= numSamples;
|
|
|
-
|
|
|
- } else {
|
|
|
- // Percentage-close filtering
|
|
|
- // (9 pixel kernel)
|
|
|
- // http://fabiensanglard.net/shadowmappingPCF/
|
|
|
-
|
|
|
- /*
|
|
|
- // nested loops breaks shader compiler / validator on some ATI cards when using OpenGL
|
|
|
- // must enroll loop manually
|
|
|
+ shadow /= numSamples;
|
|
|
+
|
|
|
+ } else {
|
|
|
|
|
|
- for ( float y = -1.25; y <= 1.25; y += 1.25 )
|
|
|
- for ( float x = -1.25; x <= 1.25; x += 1.25 ) {
|
|
|
+ #endif
|
|
|
|
|
|
- vec4 rgbaDepth = texture2D( shadowMap[ i ], vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy );
|
|
|
+ // Percentage-close filtering
|
|
|
+ // (9 pixel kernel)
|
|
|
+ // http://fabiensanglard.net/shadowmappingPCF/
|
|
|
+
|
|
|
+ /*
|
|
|
+ // nested loops breaks shader compiler / validator on some ATI cards when using OpenGL
|
|
|
+ // must enroll loop manually
|
|
|
|
|
|
- // doesn't seem to produce any noticeable visual difference compared to simple texture2D lookup
|
|
|
- //vec4 rgbaDepth = texture2DProj( shadowMap[ i ], vec4( vShadowCoord[ i ].w * ( vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy ), 0.05, vShadowCoord[ i ].w ) );
|
|
|
+ for ( float y = -1.25; y <= 1.25; y += 1.25 )
|
|
|
+ for ( float x = -1.25; x <= 1.25; x += 1.25 ) {
|
|
|
|
|
|
- float fDepth = unpackDepth( rgbaDepth );
|
|
|
+ vec4 rgbaDepth = texture2D( shadowMap[ i ], vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy );
|
|
|
|
|
|
- if ( fDepth < shadowCoord.z )
|
|
|
- shadow += 1.0;
|
|
|
+ // doesn't seem to produce any noticeable visual difference compared to simple texture2D lookup
|
|
|
+ //vec4 rgbaDepth = texture2DProj( shadowMap[ i ], vec4( vShadowCoord[ i ].w * ( vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy ), 0.05, vShadowCoord[ i ].w ) );
|
|
|
|
|
|
- }
|
|
|
+ float fDepth = unpackDepth( rgbaDepth );
|
|
|
+
|
|
|
+ if ( fDepth < shadowCoord.z )
|
|
|
+ shadow += 1.0;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ shadow /= 9.0;
|
|
|
|
|
|
- shadow /= 9.0;
|
|
|
+ */
|
|
|
|
|
|
- */
|
|
|
+ const float shadowDelta = 1.0 / 9.0;
|
|
|
|
|
|
- const float shadowDelta = 1.0 / 9.0;
|
|
|
+ float xPixelOffset = 1.0 / shadowMapSize[ i ].x;
|
|
|
+ float yPixelOffset = 1.0 / shadowMapSize[ i ].y;
|
|
|
|
|
|
- float xPixelOffset = 1.0 / shadowMapSize[ i ].x;
|
|
|
- float yPixelOffset = 1.0 / shadowMapSize[ i ].y;
|
|
|
+ float dx0 = -1.25 * xPixelOffset;
|
|
|
+ float dy0 = -1.25 * yPixelOffset;
|
|
|
+ float dx1 = 1.25 * xPixelOffset;
|
|
|
+ float dy1 = 1.25 * yPixelOffset;
|
|
|
|
|
|
- float dx0 = -1.25 * xPixelOffset;
|
|
|
- float dy0 = -1.25 * yPixelOffset;
|
|
|
- float dx1 = 1.25 * xPixelOffset;
|
|
|
- float dy1 = 1.25 * yPixelOffset;
|
|
|
+ fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );
|
|
|
+ if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
|
|
|
- fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );
|
|
|
- if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
+ fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );
|
|
|
+ if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
|
|
|
- fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );
|
|
|
- if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
+ fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );
|
|
|
+ if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
|
|
|
- fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );
|
|
|
- if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
+ fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );
|
|
|
+ if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
|
|
|
- fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );
|
|
|
- if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
+ fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );
|
|
|
+ if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
|
|
|
- fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );
|
|
|
- if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
+ fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );
|
|
|
+ if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
|
|
|
- fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );
|
|
|
- if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
+ fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );
|
|
|
+ if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
|
|
|
- fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );
|
|
|
- if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
+ fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );
|
|
|
+ if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
|
|
|
- fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );
|
|
|
- if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
+ fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );
|
|
|
+ if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
|
|
|
- fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );
|
|
|
- if ( fDepth < shadowCoord.z ) shadow += shadowDelta;
|
|
|
+ #if defined(POINT_LIGHT_SHADOWS)
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ #endif
|
|
|
|
|
|
- }
|
|
|
shadowColor = shadowColor * vec3( ( 1.0 - realShadowDarkness * shadow ) );
|
|
|
|
|
|
#elif defined( SHADOWMAP_TYPE_PCF_SOFT )
|
|
|
-
|
|
|
- if( isPointLight ){
|
|
|
+
|
|
|
+ #if defined(POINT_LIGHT_SHADOWS)
|
|
|
|
|
|
- initGridSamplingDisk();
|
|
|
+ if( isPointLight ) {
|
|
|
|
|
|
- float diskRadius = 2.5;
|
|
|
- float numSamples = 1.0;
|
|
|
- shadow = 0.0;
|
|
|
+ initGridSamplingDisk();
|
|
|
|
|
|
- vec3 baseDirection = normalize( lightToPosition );
|
|
|
- float curDistance = length( lightToPosition );
|
|
|
+ float diskRadius = 2.5;
|
|
|
+ float numSamples = 1.0;
|
|
|
+ shadow = 0.0;
|
|
|
|
|
|
- float dist = unpack1K( textureCube( shadowCube[ i ], baseDirection ) );
|
|
|
- if ( curDistance >= dist )
|
|
|
- shadow += 1.0;
|
|
|
-
|
|
|
- // evaluate each sampling direction
|
|
|
- for( int s = 0; s < 20; s++ ){
|
|
|
-
|
|
|
- vec3 offset = gridSamplingDisk[ s ] * diskRadius * cubeTexelSize;
|
|
|
- dist = unpack1K( textureCube( shadowCube[ i ], vec3( baseDirection + offset ) ) );
|
|
|
+ vec3 baseDirection = normalize( lightToPosition );
|
|
|
+ float curDistance = length( lightToPosition );
|
|
|
+
|
|
|
+ float dist = unpack1K( textureCube( shadowCube[ i ], baseDirection ) );
|
|
|
if ( curDistance >= dist )
|
|
|
shadow += 1.0;
|
|
|
- numSamples += 1.0;
|
|
|
|
|
|
- }
|
|
|
+ // evaluate each sampling direction
|
|
|
+ for( int s = 0; s < 20; s++ ) {
|
|
|
+
|
|
|
+ vec3 offset = gridSamplingDisk[ s ] * diskRadius * cubeTexelSize;
|
|
|
+ dist = unpack1K( textureCube( shadowCube[ i ], vec3( baseDirection + offset ) ) );
|
|
|
+ if ( curDistance >= dist )
|
|
|
+ shadow += 1.0;
|
|
|
+ numSamples += 1.0;
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- shadow /= numSamples;
|
|
|
+ shadow /= numSamples;
|
|
|
|
|
|
- } else {
|
|
|
+ } else {
|
|
|
|
|
|
- // Percentage-close filtering
|
|
|
- // (9 pixel kernel)
|
|
|
- // http://fabiensanglard.net/shadowmappingPCF/
|
|
|
+ #endif
|
|
|
|
|
|
- float xPixelOffset = 1.0 / shadowMapSize[ i ].x;
|
|
|
- float yPixelOffset = 1.0 / shadowMapSize[ i ].y;
|
|
|
+ // Percentage-close filtering
|
|
|
+ // (9 pixel kernel)
|
|
|
+ // http://fabiensanglard.net/shadowmappingPCF/
|
|
|
|
|
|
- float dx0 = -1.0 * xPixelOffset;
|
|
|
- float dy0 = -1.0 * yPixelOffset;
|
|
|
- float dx1 = 1.0 * xPixelOffset;
|
|
|
- float dy1 = 1.0 * yPixelOffset;
|
|
|
+ float xPixelOffset = 1.0 / shadowMapSize[ i ].x;
|
|
|
+ float yPixelOffset = 1.0 / shadowMapSize[ i ].y;
|
|
|
|
|
|
- mat3 shadowKernel;
|
|
|
- mat3 depthKernel;
|
|
|
+ float dx0 = -1.0 * xPixelOffset;
|
|
|
+ float dy0 = -1.0 * yPixelOffset;
|
|
|
+ float dx1 = 1.0 * xPixelOffset;
|
|
|
+ float dy1 = 1.0 * yPixelOffset;
|
|
|
|
|
|
- depthKernel[0][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );
|
|
|
- depthKernel[0][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );
|
|
|
- depthKernel[0][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );
|
|
|
- depthKernel[1][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );
|
|
|
- depthKernel[1][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );
|
|
|
- depthKernel[1][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );
|
|
|
- depthKernel[2][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );
|
|
|
- depthKernel[2][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );
|
|
|
- depthKernel[2][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );
|
|
|
+ mat3 shadowKernel;
|
|
|
+ mat3 depthKernel;
|
|
|
|
|
|
- vec3 shadowZ = vec3( shadowCoord.z );
|
|
|
- shadowKernel[0] = vec3(lessThan(depthKernel[0], shadowZ ));
|
|
|
- shadowKernel[0] *= vec3(0.25);
|
|
|
+ depthKernel[0][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );
|
|
|
+ depthKernel[0][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );
|
|
|
+ depthKernel[0][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );
|
|
|
+ depthKernel[1][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );
|
|
|
+ depthKernel[1][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );
|
|
|
+ depthKernel[1][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );
|
|
|
+ depthKernel[2][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );
|
|
|
+ depthKernel[2][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );
|
|
|
+ depthKernel[2][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );
|
|
|
|
|
|
- shadowKernel[1] = vec3(lessThan(depthKernel[1], shadowZ ));
|
|
|
- shadowKernel[1] *= vec3(0.25);
|
|
|
+ vec3 shadowZ = vec3( shadowCoord.z );
|
|
|
+ shadowKernel[0] = vec3(lessThan(depthKernel[0], shadowZ ));
|
|
|
+ shadowKernel[0] *= vec3(0.25);
|
|
|
|
|
|
- shadowKernel[2] = vec3(lessThan(depthKernel[2], shadowZ ));
|
|
|
- shadowKernel[2] *= vec3(0.25);
|
|
|
+ shadowKernel[1] = vec3(lessThan(depthKernel[1], shadowZ ));
|
|
|
+ shadowKernel[1] *= vec3(0.25);
|
|
|
|
|
|
- vec2 fractionalCoord = 1.0 - fract( shadowCoord.xy * shadowMapSize[i].xy );
|
|
|
+ shadowKernel[2] = vec3(lessThan(depthKernel[2], shadowZ ));
|
|
|
+ shadowKernel[2] *= vec3(0.25);
|
|
|
|
|
|
- shadowKernel[0] = mix( shadowKernel[1], shadowKernel[0], fractionalCoord.x );
|
|
|
- shadowKernel[1] = mix( shadowKernel[2], shadowKernel[1], fractionalCoord.x );
|
|
|
+ vec2 fractionalCoord = 1.0 - fract( shadowCoord.xy * shadowMapSize[i].xy );
|
|
|
|
|
|
- vec4 shadowValues;
|
|
|
- shadowValues.x = mix( shadowKernel[0][1], shadowKernel[0][0], fractionalCoord.y );
|
|
|
- shadowValues.y = mix( shadowKernel[0][2], shadowKernel[0][1], fractionalCoord.y );
|
|
|
- shadowValues.z = mix( shadowKernel[1][1], shadowKernel[1][0], fractionalCoord.y );
|
|
|
- shadowValues.w = mix( shadowKernel[1][2], shadowKernel[1][1], fractionalCoord.y );
|
|
|
+ shadowKernel[0] = mix( shadowKernel[1], shadowKernel[0], fractionalCoord.x );
|
|
|
+ shadowKernel[1] = mix( shadowKernel[2], shadowKernel[1], fractionalCoord.x );
|
|
|
|
|
|
- shadow = dot( shadowValues, vec4( 1.0 ) );
|
|
|
- }
|
|
|
+ vec4 shadowValues;
|
|
|
+ shadowValues.x = mix( shadowKernel[0][1], shadowKernel[0][0], fractionalCoord.y );
|
|
|
+ shadowValues.y = mix( shadowKernel[0][2], shadowKernel[0][1], fractionalCoord.y );
|
|
|
+ shadowValues.z = mix( shadowKernel[1][1], shadowKernel[1][0], fractionalCoord.y );
|
|
|
+ shadowValues.w = mix( shadowKernel[1][2], shadowKernel[1][1], fractionalCoord.y );
|
|
|
+
|
|
|
+ shadow = dot( shadowValues, vec4( 1.0 ) );
|
|
|
+
|
|
|
+ #if defined(POINT_LIGHT_SHADOWS)
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ #endif
|
|
|
|
|
|
shadowColor = shadowColor * vec3( ( 1.0 - realShadowDarkness * shadow ) );
|
|
|
|
|
|
#else
|
|
|
|
|
|
- if( isPointLight ){
|
|
|
+ #if defined(POINT_LIGHT_SHADOWS)
|
|
|
+
|
|
|
+ if( isPointLight ) {
|
|
|
+
|
|
|
+ vec4 data = textureCube( shadowCube[ i ], normalize( lightToPosition ) );
|
|
|
+ float dist = unpack1K( data );
|
|
|
+ if ( length( lightToPosition ) >= dist)
|
|
|
+ shadowColor = shadowColor * vec3( 1.0 - realShadowDarkness );
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ #endif
|
|
|
|
|
|
- vec4 data = textureCube( shadowCube[ i ], normalize( lightToPosition ) );
|
|
|
- float dist = unpack1K( data );
|
|
|
- if ( length( lightToPosition ) >= dist)
|
|
|
+ vec4 rgbaDepth = texture2D( shadowMap[ i ], shadowCoord.xy );
|
|
|
+ float fDepth = unpackDepth( rgbaDepth );
|
|
|
+
|
|
|
+ if ( fDepth < shadowCoord.z )
|
|
|
+
|
|
|
+ // spot with multiple shadows is darker
|
|
|
+
|
|
|
shadowColor = shadowColor * vec3( 1.0 - realShadowDarkness );
|
|
|
-
|
|
|
- } else {
|
|
|
-
|
|
|
- vec4 rgbaDepth = texture2D( shadowMap[ i ], shadowCoord.xy );
|
|
|
- float fDepth = unpackDepth( rgbaDepth );
|
|
|
|
|
|
- if ( fDepth < shadowCoord.z )
|
|
|
+ // spot with multiple shadows has the same color as single shadow spot
|
|
|
|
|
|
- // spot with multiple shadows is darker
|
|
|
+ // shadowColor = min( shadowColor, vec3( realShadowDarkness ) );
|
|
|
|
|
|
- shadowColor = shadowColor * vec3( 1.0 - realShadowDarkness );
|
|
|
+ #if defined(POINT_LIGHT_SHADOWS)
|
|
|
|
|
|
- // spot with multiple shadows has the same color as single shadow spot
|
|
|
+ }
|
|
|
|
|
|
- // shadowColor = min( shadowColor, vec3( realShadowDarkness ) );
|
|
|
- }
|
|
|
+ #endif
|
|
|
|
|
|
#endif
|
|
|
|