webgl_shadowmap_viewer.html 5.9 KB

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