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