webgl_shadowmap_viewer.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. <meta property="og:title" content="three.js webgl - ShadowMapViewer example ">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_shadowmap_viewer.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_shadowmap_viewer.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <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>
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { ShadowMapViewer } from 'three/addons/utils/ShadowMapViewer.js';
  30. let camera, scene, renderer, timer, stats;
  31. let dirLight, spotLight;
  32. let torusKnot, cube;
  33. let dirLightShadowMapViewer, spotLightShadowMapViewer;
  34. init();
  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.setAnimationLoop( animate );
  115. renderer.shadowMap.enabled = true;
  116. renderer.shadowMap.type = THREE.BasicShadowMap;
  117. // Mouse control
  118. const controls = new OrbitControls( camera, renderer.domElement );
  119. controls.target.set( 0, 2, 0 );
  120. controls.update();
  121. timer = new THREE.Timer();
  122. timer.connect( document );
  123. stats = new Stats();
  124. document.body.appendChild( stats.dom );
  125. }
  126. function resizeShadowMapViewers() {
  127. const size = window.innerWidth * 0.15;
  128. dirLightShadowMapViewer.position.x = 10;
  129. dirLightShadowMapViewer.position.y = 10;
  130. dirLightShadowMapViewer.size.width = size;
  131. dirLightShadowMapViewer.size.height = size;
  132. dirLightShadowMapViewer.update(); //Required when setting position or size directly
  133. spotLightShadowMapViewer.size.set( size, size );
  134. spotLightShadowMapViewer.position.set( size + 20, 10 );
  135. // spotLightShadowMapViewer.update(); //NOT required because .set updates automatically
  136. }
  137. function onWindowResize() {
  138. camera.aspect = window.innerWidth / window.innerHeight;
  139. camera.updateProjectionMatrix();
  140. renderer.setSize( window.innerWidth, window.innerHeight );
  141. resizeShadowMapViewers();
  142. dirLightShadowMapViewer.updateForWindowResize();
  143. spotLightShadowMapViewer.updateForWindowResize();
  144. }
  145. function animate() {
  146. timer.update();
  147. render();
  148. stats.update();
  149. }
  150. function renderScene() {
  151. renderer.render( scene, camera );
  152. }
  153. function renderShadowMapViewers() {
  154. dirLightShadowMapViewer.render( renderer );
  155. spotLightShadowMapViewer.render( renderer );
  156. }
  157. function render() {
  158. const delta = timer.getDelta();
  159. renderScene();
  160. renderShadowMapViewers();
  161. torusKnot.rotation.x += 0.25 * delta;
  162. torusKnot.rotation.y += 2 * delta;
  163. torusKnot.rotation.z += 1 * delta;
  164. cube.rotation.x += 0.25 * delta;
  165. cube.rotation.y += 2 * delta;
  166. cube.rotation.z += 1 * delta;
  167. }
  168. </script>
  169. </body>
  170. </html>
粤ICP备19079148号