webgl_shadowmap_vsm.html 6.0 KB

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