webgl_shadowmap_vsm.html 5.6 KB

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