webgl_shadowmap_vsm.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - VSM Shadows example </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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - VSM Shadows example by <a href="https://github.com/supereggbert">Paul Brunt</a>
  12. </div>
  13. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import Stats from 'three/addons/libs/stats.module.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. let camera, scene, renderer, clock, stats;
  30. let dirLight, spotLight;
  31. let torusKnot, dirGroup;
  32. init();
  33. animate();
  34. function init() {
  35. initScene();
  36. initMisc();
  37. // Init gui
  38. const gui = new GUI();
  39. const config = {
  40. spotlightRadius: 4,
  41. spotlightSamples: 8,
  42. dirlightRadius: 4,
  43. dirlightSamples: 8
  44. };
  45. const spotlightFolder = gui.addFolder( 'Spotlight' );
  46. spotlightFolder.add( config, 'spotlightRadius' ).name( 'radius' ).min( 0 ).max( 25 ).onChange( function ( value ) {
  47. spotLight.shadow.radius = value;
  48. } );
  49. spotlightFolder.add( config, 'spotlightSamples', 1, 25, 1 ).name( 'samples' ).onChange( function ( value ) {
  50. spotLight.shadow.blurSamples = value;
  51. } );
  52. spotlightFolder.open();
  53. const dirlightFolder = gui.addFolder( 'Directional Light' );
  54. dirlightFolder.add( config, 'dirlightRadius' ).name( 'radius' ).min( 0 ).max( 25 ).onChange( function ( value ) {
  55. dirLight.shadow.radius = value;
  56. } );
  57. dirlightFolder.add( config, 'dirlightSamples', 1, 25, 1 ).name( 'samples' ).onChange( function ( value ) {
  58. dirLight.shadow.blurSamples = value;
  59. } );
  60. dirlightFolder.open();
  61. document.body.appendChild( renderer.domElement );
  62. window.addEventListener( 'resize', onWindowResize );
  63. }
  64. function initScene() {
  65. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  66. camera.position.set( 0, 10, 30 );
  67. scene = new THREE.Scene();
  68. scene.background = new THREE.Color( 0x222244 );
  69. scene.fog = new THREE.Fog( 0x222244, 50, 100 );
  70. // Lights
  71. scene.add( new THREE.AmbientLight( 0x444444 ) );
  72. spotLight = new THREE.SpotLight( 0xff8888 );
  73. spotLight.angle = Math.PI / 5;
  74. spotLight.penumbra = 0.3;
  75. spotLight.position.set( 8, 10, 5 );
  76. spotLight.castShadow = true;
  77. spotLight.shadow.camera.near = 8;
  78. spotLight.shadow.camera.far = 200;
  79. spotLight.shadow.mapSize.width = 256;
  80. spotLight.shadow.mapSize.height = 256;
  81. spotLight.shadow.bias = - 0.002;
  82. spotLight.shadow.radius = 4;
  83. scene.add( spotLight );
  84. dirLight = new THREE.DirectionalLight( 0x8888ff );
  85. dirLight.position.set( 3, 12, 17 );
  86. dirLight.castShadow = true;
  87. dirLight.shadow.camera.near = 0.1;
  88. dirLight.shadow.camera.far = 500;
  89. dirLight.shadow.camera.right = 17;
  90. dirLight.shadow.camera.left = - 17;
  91. dirLight.shadow.camera.top = 17;
  92. dirLight.shadow.camera.bottom = - 17;
  93. dirLight.shadow.mapSize.width = 512;
  94. dirLight.shadow.mapSize.height = 512;
  95. dirLight.shadow.radius = 4;
  96. dirLight.shadow.bias = - 0.0005;
  97. dirGroup = new THREE.Group();
  98. dirGroup.add( dirLight );
  99. scene.add( dirGroup );
  100. // Geometry
  101. const geometry = new THREE.TorusKnotGeometry( 25, 8, 75, 20 );
  102. const material = new THREE.MeshPhongMaterial( {
  103. color: 0x999999,
  104. shininess: 0,
  105. specular: 0x222222
  106. } );
  107. torusKnot = new THREE.Mesh( geometry, material );
  108. torusKnot.scale.multiplyScalar( 1 / 18 );
  109. torusKnot.position.y = 3;
  110. torusKnot.castShadow = true;
  111. torusKnot.receiveShadow = true;
  112. scene.add( torusKnot );
  113. const cylinderGeometry = new THREE.CylinderGeometry( 0.75, 0.75, 7, 32 );
  114. const pillar1 = new THREE.Mesh( cylinderGeometry, material );
  115. pillar1.position.set( 8, 3.5, 8 );
  116. pillar1.castShadow = true;
  117. pillar1.receiveShadow = true;
  118. const pillar2 = pillar1.clone();
  119. pillar2.position.set( 8, 3.5, - 8 );
  120. const pillar3 = pillar1.clone();
  121. pillar3.position.set( - 8, 3.5, 8 );
  122. const pillar4 = pillar1.clone();
  123. pillar4.position.set( - 8, 3.5, - 8 );
  124. scene.add( pillar1 );
  125. scene.add( pillar2 );
  126. scene.add( pillar3 );
  127. scene.add( pillar4 );
  128. const planeGeometry = new THREE.PlaneGeometry( 200, 200 );
  129. const planeMaterial = new THREE.MeshPhongMaterial( {
  130. color: 0x999999,
  131. shininess: 0,
  132. specular: 0x111111
  133. } );
  134. const ground = new THREE.Mesh( planeGeometry, planeMaterial );
  135. ground.rotation.x = - Math.PI / 2;
  136. ground.scale.multiplyScalar( 3 );
  137. ground.castShadow = true;
  138. ground.receiveShadow = true;
  139. scene.add( ground );
  140. }
  141. function initMisc() {
  142. renderer = new THREE.WebGLRenderer( { antialias: true } );
  143. renderer.setPixelRatio( window.devicePixelRatio );
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. renderer.shadowMap.enabled = true;
  146. renderer.shadowMap.type = THREE.VSMShadowMap;
  147. // Mouse control
  148. const controls = new OrbitControls( camera, renderer.domElement );
  149. controls.target.set( 0, 2, 0 );
  150. controls.update();
  151. clock = new THREE.Clock();
  152. stats = new Stats();
  153. document.body.appendChild( stats.dom );
  154. }
  155. function onWindowResize() {
  156. camera.aspect = window.innerWidth / window.innerHeight;
  157. camera.updateProjectionMatrix();
  158. renderer.setSize( window.innerWidth, window.innerHeight );
  159. }
  160. function animate( time ) {
  161. requestAnimationFrame( animate );
  162. const delta = clock.getDelta();
  163. torusKnot.rotation.x += 0.25 * delta;
  164. torusKnot.rotation.y += 0.5 * delta;
  165. torusKnot.rotation.z += 1 * delta;
  166. dirGroup.rotation.y += 0.7 * delta;
  167. dirLight.position.z = 17 + Math.sin( time * 0.001 ) * 5;
  168. renderer.render( scene, camera );
  169. stats.update();
  170. }
  171. </script>
  172. </body>
  173. </html>