@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
2 lines (1 loc) • 3.01 kB
JavaScript
export default "#define LIGHT_WORLD_SIZE 0.005\n// #define LIGHT_FRUSTUM_WIDTH 1.0\n// #define PCSS_FILTER_SIZE 1.0\n#define LIGHT_SIZE_UV (PCSS_FILTER_SIZE * LIGHT_WORLD_SIZE)\n#define NEAR_PLANE 9.5\n\n#define NUM_SAMPLES 17\n#define NUM_RINGS 11\n#define BLOCKER_SEARCH_NUM_SAMPLES NUM_SAMPLES\n\nvec2 poissonDisk[NUM_SAMPLES];\n\nvoid initPoissonSamples( const in vec2 randomSeed ) {\n float ANGLE_STEP = PI2 * float( NUM_RINGS ) / float( NUM_SAMPLES );\n float INV_NUM_SAMPLES = 1.0 / float( NUM_SAMPLES );\n\n // jsfiddle that shows sample pattern: https://jsfiddle.net/a16ff1p7/\n float angle = rand( randomSeed ) * PI2;\n float radius = INV_NUM_SAMPLES;\n float radiusStep = radius;\n\n for( int i = 0; i < NUM_SAMPLES; i ++ ) {\n poissonDisk[i] = vec2( cos( angle ), sin( angle ) ) * pow( radius, 0.75 );\n radius += radiusStep;\n angle += ANGLE_STEP;\n }\n}\n\nfloat penumbraSize( const in float zReceiver, const in float zBlocker ) { // Parallel plane estimation\n return (zReceiver - zBlocker) / zBlocker;\n}\n\nfloat findBlocker( sampler2D shadowMap, const in vec2 uv, const in float zReceiver ) {\n // This uses similar triangles to compute what\n // area of the shadow map we should search\n float searchRadius = LIGHT_SIZE_UV * ( zReceiver - NEAR_PLANE ) / zReceiver;\n float blockerDepthSum = 0.0;\n int numBlockers = 0;\n\n for( int i = 0; i < BLOCKER_SEARCH_NUM_SAMPLES; i++ ) {\n float shadowMapDepth = unpackRGBAToDepth(texture2D(shadowMap, uv + poissonDisk[i] * searchRadius));\n if ( shadowMapDepth < zReceiver ) {\n blockerDepthSum += shadowMapDepth;\n numBlockers ++;\n }\n }\n\n if( numBlockers == 0 ) return -1.0;\n\n return blockerDepthSum / float( numBlockers );\n}\n\nfloat PCF_Filter(sampler2D shadowMap, vec2 uv, float zReceiver, float filterRadius ) {\n float sum = 0.0;\n float depth;\n #pragma unroll_loop_start\n for( int i = 0; i < 17; i ++ ) {\n depth = unpackRGBAToDepth( texture2D( shadowMap, uv + poissonDisk[ i ] * filterRadius ) );\n if( zReceiver <= depth ) sum += 1.0;\n }\n #pragma unroll_loop_end\n #pragma unroll_loop_start\n for( int i = 0; i < 17; i ++ ) {\n depth = unpackRGBAToDepth( texture2D( shadowMap, uv + -poissonDisk[ i ].yx * filterRadius ) );\n if( zReceiver <= depth ) sum += 1.0;\n }\n #pragma unroll_loop_end\n return sum / ( 2.0 * float( 17 ) );\n}\n\nfloat PCSS ( sampler2D shadowMap, vec4 coords ) {\n vec2 uv = coords.xy;\n float zReceiver = coords.z; // Assumed to be eye-space z in this code\n\n initPoissonSamples( uv );\n // STEP 1: blocker search\n float avgBlockerDepth = findBlocker( shadowMap, uv, zReceiver );\n\n //There are no occluders so early out (this saves filtering)\n if( avgBlockerDepth == -1.0 ) return 1.0;\n\n // STEP 2: penumbra size\n float penumbraRatio = penumbraSize( zReceiver, avgBlockerDepth );\n float filterRadius = penumbraRatio * LIGHT_SIZE_UV * NEAR_PLANE / zReceiver;\n\n // STEP 3: filtering\n //return avgBlockerDepth;\n return PCF_Filter( shadowMap, uv, zReceiver, filterRadius );\n}";