webgl_shadowmap_pcss.html 8.1 KB

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