webgl_shadowmap_viewer.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - ShadowMapViewer 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> - ShadowMapViewer example by <a href="https://github.com/arya-s">arya-s</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. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import Stats from './jsm/libs/stats.module.js';
  26. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  27. import { ShadowMapViewer } from './jsm/utils/ShadowMapViewer.js';
  28. let camera, scene, renderer, clock, stats;
  29. let dirLight, spotLight;
  30. let torusKnot, cube;
  31. let dirLightShadowMapViewer, spotLightShadowMapViewer;
  32. init();
  33. animate();
  34. function init() {
  35. initScene();
  36. initShadowMapViewers();
  37. initMisc();
  38. document.body.appendChild( renderer.domElement );
  39. window.addEventListener( 'resize', onWindowResize );
  40. }
  41. function initScene() {
  42. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  43. camera.position.set( 0, 15, 35 );
  44. scene = new THREE.Scene();
  45. // Lights
  46. scene.add( new THREE.AmbientLight( 0x404040 ) );
  47. spotLight = new THREE.SpotLight( 0xffffff );
  48. spotLight.name = 'Spot Light';
  49. spotLight.angle = Math.PI / 5;
  50. spotLight.penumbra = 0.3;
  51. spotLight.position.set( 10, 10, 5 );
  52. spotLight.castShadow = true;
  53. spotLight.shadow.camera.near = 8;
  54. spotLight.shadow.camera.far = 30;
  55. spotLight.shadow.mapSize.width = 1024;
  56. spotLight.shadow.mapSize.height = 1024;
  57. scene.add( spotLight );
  58. scene.add( new THREE.CameraHelper( spotLight.shadow.camera ) );
  59. dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
  60. dirLight.name = 'Dir. Light';
  61. dirLight.position.set( 0, 10, 0 );
  62. dirLight.castShadow = true;
  63. dirLight.shadow.camera.near = 1;
  64. dirLight.shadow.camera.far = 10;
  65. dirLight.shadow.camera.right = 15;
  66. dirLight.shadow.camera.left = - 15;
  67. dirLight.shadow.camera.top = 15;
  68. dirLight.shadow.camera.bottom = - 15;
  69. dirLight.shadow.mapSize.width = 1024;
  70. dirLight.shadow.mapSize.height = 1024;
  71. scene.add( dirLight );
  72. scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
  73. // Geometry
  74. let geometry = new THREE.TorusKnotGeometry( 25, 8, 75, 20 );
  75. let material = new THREE.MeshPhongMaterial( {
  76. color: 0xff0000,
  77. shininess: 150,
  78. specular: 0x222222
  79. } );
  80. torusKnot = new THREE.Mesh( geometry, material );
  81. torusKnot.scale.multiplyScalar( 1 / 18 );
  82. torusKnot.position.y = 3;
  83. torusKnot.castShadow = true;
  84. torusKnot.receiveShadow = true;
  85. scene.add( torusKnot );
  86. geometry = new THREE.BoxGeometry( 3, 3, 3 );
  87. cube = new THREE.Mesh( geometry, material );
  88. cube.position.set( 8, 3, 8 );
  89. cube.castShadow = true;
  90. cube.receiveShadow = true;
  91. scene.add( cube );
  92. geometry = new THREE.BoxGeometry( 10, 0.15, 10 );
  93. material = new THREE.MeshPhongMaterial( {
  94. color: 0xa0adaf,
  95. shininess: 150,
  96. specular: 0x111111
  97. } );
  98. const ground = new THREE.Mesh( geometry, material );
  99. ground.scale.multiplyScalar( 3 );
  100. ground.castShadow = false;
  101. ground.receiveShadow = true;
  102. scene.add( ground );
  103. }
  104. function initShadowMapViewers() {
  105. dirLightShadowMapViewer = new ShadowMapViewer( dirLight );
  106. spotLightShadowMapViewer = new ShadowMapViewer( spotLight );
  107. resizeShadowMapViewers();
  108. }
  109. function initMisc() {
  110. renderer = new THREE.WebGLRenderer( { antialias: true } );
  111. renderer.setPixelRatio( window.devicePixelRatio );
  112. renderer.setSize( window.innerWidth, window.innerHeight );
  113. renderer.shadowMap.enabled = true;
  114. renderer.shadowMap.type = THREE.BasicShadowMap;
  115. // Mouse control
  116. const controls = new OrbitControls( camera, renderer.domElement );
  117. controls.target.set( 0, 2, 0 );
  118. controls.update();
  119. clock = new THREE.Clock();
  120. stats = new Stats();
  121. document.body.appendChild( stats.dom );
  122. }
  123. function resizeShadowMapViewers() {
  124. const size = window.innerWidth * 0.15;
  125. dirLightShadowMapViewer.position.x = 10;
  126. dirLightShadowMapViewer.position.y = 10;
  127. dirLightShadowMapViewer.size.width = size;
  128. dirLightShadowMapViewer.size.height = size;
  129. dirLightShadowMapViewer.update(); //Required when setting position or size directly
  130. spotLightShadowMapViewer.size.set( size, size );
  131. spotLightShadowMapViewer.position.set( size + 20, 10 );
  132. // spotLightShadowMapViewer.update(); //NOT required because .set updates automatically
  133. }
  134. function onWindowResize() {
  135. camera.aspect = window.innerWidth / window.innerHeight;
  136. camera.updateProjectionMatrix();
  137. renderer.setSize( window.innerWidth, window.innerHeight );
  138. resizeShadowMapViewers();
  139. dirLightShadowMapViewer.updateForWindowResize();
  140. spotLightShadowMapViewer.updateForWindowResize();
  141. }
  142. function animate() {
  143. requestAnimationFrame( animate );
  144. render();
  145. stats.update();
  146. }
  147. function renderScene() {
  148. renderer.render( scene, camera );
  149. }
  150. function renderShadowMapViewers() {
  151. dirLightShadowMapViewer.render( renderer );
  152. spotLightShadowMapViewer.render( renderer );
  153. }
  154. function render() {
  155. const delta = clock.getDelta();
  156. renderScene();
  157. renderShadowMapViewers();
  158. torusKnot.rotation.x += 0.25 * delta;
  159. torusKnot.rotation.y += 2 * delta;
  160. torusKnot.rotation.z += 1 * delta;
  161. cube.rotation.x += 0.25 * delta;
  162. cube.rotation.y += 2 * delta;
  163. cube.rotation.z += 1 * delta;
  164. }
  165. </script>
  166. </body>
  167. </html>