webgl_shadowmap_viewer.html 6.1 KB

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