Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var KERNEL_SIZE=2;
- var KERNEL_LOOP=KERNEL_SIZE/2-0.5;
- var KERNEL_CONTRIBUTIONS=KERNEL_SIZE*KERNEL_SIZE;
- //#GLSLSource
- pub.buildShader=function(features) {
- var shadowMapSampler = "\n"+
- "const float shadowTexelSize=1.0/2048.0;\n"+
- //When dot-producted with the packed values in the shadow map will produce a Z value in the range 0-1
- "const vec4 bitShifts=vec4(1.0/(256.0*256.0*256.0),1.0/(256.0*256.0),1.0/256.0,1.0);\n"+
- "float rand(vec2 co){\n"+
- " return fract(sin(dot(co.xy,vec2(12.9898,78.233)))*43758.5453);\n"+
- "}\n"+
- "float ShadowMapSamplerStochastic(vec4 sunShadowMapPosition, sampler2D sunShadowMap, float shadowStrength) {\n"+
- " float shadowEffect;\n"+
- " vec3 shadowMapTexel=sunShadowMapPosition.xyz/sunShadowMapPosition.w;\n"+
- " shadowMapTexel.z-=shadowMapTexel.z*0.003;\n"+
- " if (shadowMapTexel.x<0.0 || shadowMapTexel.x>1.0 || shadowMapTexel.y<0.0 || shadowMapTexel.y>1.0 || shadowMapTexel.z<0.0 || shadowMapTexel.z>0.45) shadowEffect=1.0;\n"+
- " else {\n"+
- " float shadowTotal=0.0, shadowMapDepth=0.0;\n"+
- " vec2 lookupTexel;\n";
- for (var pixelX=-KERNEL_LOOP; pixelX<=KERNEL_LOOP; pixelX+=1.0) {
- for (var pixelY=-KERNEL_LOOP; pixelY<=KERNEL_LOOP; pixelY+=1.0) {
- shadowMapSampler +=
- " lookupTexel=shadowMapTexel.xy+shadowTexelSize*( vec2(cos(rand(sunShadowMapPosition.xy))+("+pixelX+"),sin(rand(sunShadowMapPosition.yx))+("+pixelY+")) );\n"+
- " shadowMapDepth=dot(texture2D(sunShadowMap,lookupTexel),bitShifts);\n"+
- " shadowTotal+=step(shadowMapDepth,shadowMapTexel.z)*shadowStrength;\n";
- }
- }
- shadowMapSampler +=
- " shadowEffect=max(1.0-(shadowTotal/"+KERNEL_CONTRIBUTIONS+".0),0.0);\n"+
- " }\n"+
- " return shadowEffect;\n"+
- "}\n"+
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement