webgl_shadowmap_pcss.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - percent closer soft-shadows</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #cce0ff;
  11. color: #000;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">Percent Closer Soft-Shadows (PCSS) by <a href="http://eduperiment.com">spidersharma03</a></div>
  20. <script src="../build/three.js"></script>
  21. <script src="js/WebGL.js"></script>
  22. <script src="js/controls/OrbitControls.js"></script>
  23. <script src="js/libs/stats.min.js"></script>
  24. <script type="x-shader/x-fragment" id="PCSS">
  25. #define LIGHT_WORLD_SIZE 0.005
  26. #define LIGHT_FRUSTUM_WIDTH 3.75
  27. #define LIGHT_SIZE_UV (LIGHT_WORLD_SIZE / LIGHT_FRUSTUM_WIDTH)
  28. #define NEAR_PLANE 9.5
  29. #define NUM_SAMPLES 17
  30. #define NUM_RINGS 11
  31. #define BLOCKER_SEARCH_NUM_SAMPLES NUM_SAMPLES
  32. #define PCF_NUM_SAMPLES NUM_SAMPLES
  33. vec2 poissonDisk[NUM_SAMPLES];
  34. void initPoissonSamples( const in vec2 randomSeed ) {
  35. float ANGLE_STEP = PI2 * float( NUM_RINGS ) / float( NUM_SAMPLES );
  36. float INV_NUM_SAMPLES = 1.0 / float( NUM_SAMPLES );
  37. // jsfiddle that shows sample pattern: https://jsfiddle.net/a16ff1p7/
  38. float angle = rand( randomSeed ) * PI2;
  39. float radius = INV_NUM_SAMPLES;
  40. float radiusStep = radius;
  41. for( int i = 0; i < NUM_SAMPLES; i ++ ) {
  42. poissonDisk[i] = vec2( cos( angle ), sin( angle ) ) * pow( radius, 0.75 );
  43. radius += radiusStep;
  44. angle += ANGLE_STEP;
  45. }
  46. }
  47. float penumbraSize( const in float zReceiver, const in float zBlocker ) { // Parallel plane estimation
  48. return (zReceiver - zBlocker) / zBlocker;
  49. }
  50. float findBlocker( sampler2D shadowMap, const in vec2 uv, const in float zReceiver ) {
  51. // This uses similar triangles to compute what
  52. // area of the shadow map we should search
  53. float searchRadius = LIGHT_SIZE_UV * ( zReceiver - NEAR_PLANE ) / zReceiver;
  54. float blockerDepthSum = 0.0;
  55. int numBlockers = 0;
  56. for( int i = 0; i < BLOCKER_SEARCH_NUM_SAMPLES; i++ ) {
  57. float shadowMapDepth = unpackRGBAToDepth(texture2D(shadowMap, uv + poissonDisk[i] * searchRadius));
  58. if ( shadowMapDepth < zReceiver ) {
  59. blockerDepthSum += shadowMapDepth;
  60. numBlockers ++;
  61. }
  62. }
  63. if( numBlockers == 0 ) return -1.0;
  64. return blockerDepthSum / float( numBlockers );
  65. }
  66. float PCF_Filter(sampler2D shadowMap, vec2 uv, float zReceiver, float filterRadius ) {
  67. float sum = 0.0;
  68. for( int i = 0; i < PCF_NUM_SAMPLES; i ++ ) {
  69. float depth = unpackRGBAToDepth( texture2D( shadowMap, uv + poissonDisk[ i ] * filterRadius ) );
  70. if( zReceiver <= depth ) sum += 1.0;
  71. }
  72. for( int i = 0; i < PCF_NUM_SAMPLES; i ++ ) {
  73. float depth = unpackRGBAToDepth( texture2D( shadowMap, uv + -poissonDisk[ i ].yx * filterRadius ) );
  74. if( zReceiver <= depth ) sum += 1.0;
  75. }
  76. return sum / ( 2.0 * float( PCF_NUM_SAMPLES ) );
  77. }
  78. float PCSS ( sampler2D shadowMap, vec4 coords ) {
  79. vec2 uv = coords.xy;
  80. float zReceiver = coords.z; // Assumed to be eye-space z in this code
  81. initPoissonSamples( uv );
  82. // STEP 1: blocker search
  83. float avgBlockerDepth = findBlocker( shadowMap, uv, zReceiver );
  84. //There are no occluders so early out (this saves filtering)
  85. if( avgBlockerDepth == -1.0 ) return 1.0;
  86. // STEP 2: penumbra size
  87. float penumbraRatio = penumbraSize( zReceiver, avgBlockerDepth );
  88. float filterRadius = penumbraRatio * LIGHT_SIZE_UV * NEAR_PLANE / zReceiver;
  89. // STEP 3: filtering
  90. //return avgBlockerDepth;
  91. return PCF_Filter( shadowMap, uv, zReceiver, filterRadius );
  92. }
  93. </script>
  94. <script type="x-shader/x-fragment" id="PCSSGetShadow">
  95. return PCSS( shadowMap, shadowCoord );
  96. </script>
  97. <script>
  98. if ( WEBGL.isWebGLAvailable() === false ) {
  99. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  100. }
  101. var container, stats;
  102. var camera, scene, renderer;
  103. var group;
  104. init();
  105. animate();
  106. function init() {
  107. container = document.createElement( 'div' );
  108. document.body.appendChild( container );
  109. // scene
  110. scene = new THREE.Scene();
  111. scene.fog = new THREE.Fog( 0xcce0ff, 5, 100 );
  112. // camera
  113. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 10000 );
  114. camera.position.y = 5;
  115. camera.position.z = 15;
  116. scene.add( camera );
  117. // lights
  118. scene.add( new THREE.AmbientLight( 0x666666 ) );
  119. var light = new THREE.DirectionalLight( 0xdfebff, 1.75 );
  120. light.position.set( 2, 8, 4 );
  121. light.castShadow = true;
  122. light.shadow.mapSize.width = 1024;
  123. light.shadow.mapSize.height = 1024;
  124. light.shadow.camera.far = 20;
  125. scene.add( light );
  126. // scene.add( new THREE.DirectionalLightHelper( light ) );
  127. scene.add( new THREE.CameraHelper( light.shadow.camera ) );
  128. // group
  129. group = new THREE.Group();
  130. scene.add( group );
  131. var geometry = new THREE.SphereBufferGeometry( 0.3, 20, 20 );
  132. for ( var i = 0; i < 20; i ++ ) {
  133. var material = new THREE.MeshPhongMaterial( { color: Math.random() * 0xffffff } );
  134. var sphere = new THREE.Mesh( geometry, material );
  135. sphere.position.x = Math.random() - 0.5;
  136. sphere.position.z = Math.random() - 0.5;
  137. sphere.position.normalize();
  138. sphere.position.multiplyScalar( Math.random() * 2 + 1 );
  139. sphere.castShadow = true;
  140. sphere.receiveShadow = true;
  141. sphere.userData.phase = Math.random() * Math.PI;
  142. group.add( sphere );
  143. }
  144. // ground
  145. var groundMaterial = new THREE.MeshPhongMaterial( { color: 0x404040, specular: 0x111111 } );
  146. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 20000, 20000 ), groundMaterial );
  147. mesh.rotation.x = - Math.PI / 2;
  148. mesh.receiveShadow = true;
  149. scene.add( mesh );
  150. // column
  151. var mesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 1, 4, 1 ), groundMaterial );
  152. mesh.position.y = 2;
  153. mesh.castShadow = true;
  154. mesh.receiveShadow = true;
  155. scene.add( mesh );
  156. // overwrite shadowmap code
  157. var shader = THREE.ShaderChunk.shadowmap_pars_fragment;
  158. shader = shader.replace(
  159. '#ifdef USE_SHADOWMAP',
  160. '#ifdef USE_SHADOWMAP' +
  161. document.getElementById( 'PCSS' ).textContent
  162. );
  163. shader = shader.replace(
  164. '#if defined( SHADOWMAP_TYPE_PCF )',
  165. document.getElementById( 'PCSSGetShadow' ).textContent +
  166. '#if defined( SHADOWMAP_TYPE_PCF )'
  167. );
  168. THREE.ShaderChunk.shadowmap_pars_fragment = shader;
  169. // renderer
  170. renderer = new THREE.WebGLRenderer( { antialias: true } );
  171. renderer.setPixelRatio( window.devicePixelRatio );
  172. renderer.setSize( window.innerWidth, window.innerHeight );
  173. renderer.setClearColor( scene.fog.color );
  174. container.appendChild( renderer.domElement );
  175. renderer.gammaInput = true;
  176. renderer.gammaOutput = true;
  177. renderer.shadowMap.enabled = true;
  178. // controls
  179. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  180. controls.maxPolarAngle = Math.PI * 0.5;
  181. controls.minDistance = 10;
  182. controls.maxDistance = 75;
  183. controls.target.set( 0, 2.5, 0 );
  184. controls.update();
  185. // performance monitor
  186. stats = new Stats();
  187. container.appendChild( stats.dom );
  188. //
  189. window.addEventListener( 'resize', onWindowResize, false );
  190. }
  191. //
  192. function onWindowResize() {
  193. camera.aspect = window.innerWidth / window.innerHeight;
  194. camera.updateProjectionMatrix();
  195. renderer.setSize( window.innerWidth, window.innerHeight );
  196. }
  197. //
  198. function animate() {
  199. var time = performance.now() / 1000;
  200. group.traverse( function ( child ) {
  201. if ( 'phase' in child.userData ) {
  202. child.position.y = Math.abs( Math.sin( time + child.userData.phase ) ) * 4 + 0.3;
  203. }
  204. } );
  205. renderer.render( scene, camera );
  206. stats.update();
  207. requestAnimationFrame( animate );
  208. }
  209. </script>
  210. </body>
  211. </html>