webgl_shadowmap_vsm.html 6.1 KB

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