2
0

webgl_shadowmap_viewer.html 5.7 KB

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