webgl_shadowmap_viewer.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. var camera, scene, renderer, clock, stats;
  19. var dirLight, spotLight;
  20. var torusKnot, cube;
  21. var 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, false );
  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. var geometry = new THREE.TorusKnotBufferGeometry( 25, 8, 75, 20 );
  65. var 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. var geometry = new THREE.BoxBufferGeometry( 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. var geometry = new THREE.BoxBufferGeometry( 10, 0.15, 10 );
  83. var material = new THREE.MeshPhongMaterial( {
  84. color: 0xa0adaf,
  85. shininess: 150,
  86. specular: 0x111111
  87. } );
  88. var 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. dirLightShadowMapViewer.position.x = 10;
  97. dirLightShadowMapViewer.position.y = 10;
  98. dirLightShadowMapViewer.size.width = 256;
  99. dirLightShadowMapViewer.size.height = 256;
  100. dirLightShadowMapViewer.update(); //Required when setting position or size directly
  101. spotLightShadowMapViewer = new ShadowMapViewer( spotLight );
  102. spotLightShadowMapViewer.size.set( 256, 256 );
  103. spotLightShadowMapViewer.position.set( 276, 10 );
  104. // spotLightShadowMapViewer.update(); //NOT required because .set updates automatically
  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.shadowMap.enabled = true;
  111. renderer.shadowMap.type = THREE.BasicShadowMap;
  112. // Mouse control
  113. var controls = new OrbitControls( camera, renderer.domElement );
  114. controls.target.set( 0, 2, 0 );
  115. controls.update();
  116. clock = new THREE.Clock();
  117. stats = new Stats();
  118. document.body.appendChild( stats.dom );
  119. }
  120. function onWindowResize() {
  121. camera.aspect = window.innerWidth / window.innerHeight;
  122. camera.updateProjectionMatrix();
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. dirLightShadowMapViewer.updateForWindowResize();
  125. spotLightShadowMapViewer.updateForWindowResize();
  126. }
  127. function animate() {
  128. requestAnimationFrame( animate );
  129. render();
  130. stats.update();
  131. }
  132. function renderScene() {
  133. renderer.render( scene, camera );
  134. }
  135. function renderShadowMapViewers() {
  136. dirLightShadowMapViewer.render( renderer );
  137. spotLightShadowMapViewer.render( renderer );
  138. }
  139. function render() {
  140. var delta = clock.getDelta();
  141. renderScene();
  142. renderShadowMapViewers();
  143. torusKnot.rotation.x += 0.25 * delta;
  144. torusKnot.rotation.y += 2 * delta;
  145. torusKnot.rotation.z += 1 * delta;
  146. cube.rotation.x += 0.25 * delta;
  147. cube.rotation.y += 2 * delta;
  148. cube.rotation.z += 1 * delta;
  149. }
  150. </script>
  151. </body>
  152. </html>