webgl_shadowmap_viewer.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. position: absolute;
  17. top: 10px;
  18. width: 100%;
  19. text-align: center;
  20. z-index: 100;
  21. display:block;
  22. }
  23. #info a { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="info">
  28. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - ShadowMapViewer example by <a href="https://github.com/arya-s">arya-s</a>
  29. </div>
  30. <script src="../build/three.js"></script>
  31. <script src="js/controls/OrbitControls.js"></script>
  32. <script src="js/shaders/UnpackDepthRGBAShader.js"></script>
  33. <script src="js/utils/ShadowMapViewer.js"></script>
  34. <script src="js/Detector.js"></script>
  35. <script src="js/libs/stats.min.js"></script>
  36. <script>
  37. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  38. var camera, scene, renderer, clock, stats;
  39. var dirLight, spotLight;
  40. var torusKnot, cube;
  41. var dirLightShadowMapViewer, spotLightShadowMapViewer;
  42. init();
  43. animate();
  44. function init() {
  45. initScene();
  46. initShadowMapViewers();
  47. initMisc();
  48. document.body.appendChild( renderer.domElement );
  49. window.addEventListener( 'resize', onWindowResize, false );
  50. }
  51. function initScene() {
  52. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  53. camera.position.set( 0, 15, 35 );
  54. scene = new THREE.Scene();
  55. // Lights
  56. scene.add( new THREE.AmbientLight( 0x404040 ) );
  57. spotLight = new THREE.SpotLight( 0xffffff );
  58. spotLight.name = 'Spot Light';
  59. spotLight.angle = Math.PI / 5;
  60. spotLight.penumbra = 0.3;
  61. spotLight.position.set( 10, 10, 5 );
  62. spotLight.castShadow = true;
  63. spotLight.shadow.camera.near = 8;
  64. spotLight.shadow.camera.far = 30;
  65. spotLight.shadow.mapSize.width = 1024;
  66. spotLight.shadow.mapSize.height = 1024;
  67. scene.add( spotLight );
  68. scene.add( new THREE.CameraHelper( spotLight.shadow.camera ) );
  69. dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
  70. dirLight.name = 'Dir. Light';
  71. dirLight.position.set( 0, 10, 0 );
  72. dirLight.castShadow = true;
  73. dirLight.shadow.camera.near = 1;
  74. dirLight.shadow.camera.far = 10;
  75. dirLight.shadow.camera.right = 15;
  76. dirLight.shadow.camera.left = - 15;
  77. dirLight.shadow.camera.top = 15;
  78. dirLight.shadow.camera.bottom = - 15;
  79. dirLight.shadow.mapSize.width = 1024;
  80. dirLight.shadow.mapSize.height = 1024;
  81. scene.add( dirLight );
  82. scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
  83. // Geometry
  84. var geometry = new THREE.TorusKnotBufferGeometry( 25, 8, 75, 20 );
  85. var material = new THREE.MeshPhongMaterial( {
  86. color: 0xff0000,
  87. shininess: 150,
  88. specular: 0x222222
  89. } );
  90. torusKnot = new THREE.Mesh( geometry, material );
  91. torusKnot.scale.multiplyScalar( 1 / 18 );
  92. torusKnot.position.y = 3;
  93. torusKnot.castShadow = true;
  94. torusKnot.receiveShadow = true;
  95. scene.add( torusKnot );
  96. var geometry = new THREE.BoxBufferGeometry( 3, 3, 3 );
  97. cube = new THREE.Mesh( geometry, material );
  98. cube.position.set( 8, 3, 8 );
  99. cube.castShadow = true;
  100. cube.receiveShadow = true;
  101. scene.add( cube );
  102. var geometry = new THREE.BoxBufferGeometry( 10, 0.15, 10 );
  103. var material = new THREE.MeshPhongMaterial( {
  104. color: 0xa0adaf,
  105. shininess: 150,
  106. specular: 0x111111
  107. } );
  108. var ground = new THREE.Mesh( geometry, material );
  109. ground.scale.multiplyScalar( 3 );
  110. ground.castShadow = false;
  111. ground.receiveShadow = true;
  112. scene.add( ground );
  113. }
  114. function initShadowMapViewers() {
  115. dirLightShadowMapViewer = new THREE.ShadowMapViewer( dirLight );
  116. dirLightShadowMapViewer.position.x = 10;
  117. dirLightShadowMapViewer.position.y = 10;
  118. dirLightShadowMapViewer.size.width = 256;
  119. dirLightShadowMapViewer.size.height = 256;
  120. dirLightShadowMapViewer.update(); //Required when setting position or size directly
  121. spotLightShadowMapViewer = new THREE.ShadowMapViewer( spotLight );
  122. spotLightShadowMapViewer.size.set( 256, 256 );
  123. spotLightShadowMapViewer.position.set( 276, 10 );
  124. // spotLightShadowMapViewer.update(); //NOT required because .set updates automatically
  125. }
  126. function initMisc() {
  127. renderer = new THREE.WebGLRenderer( { antialias: true } );
  128. renderer.setPixelRatio( window.devicePixelRatio );
  129. renderer.setSize( window.innerWidth, window.innerHeight );
  130. renderer.shadowMap.enabled = true;
  131. renderer.shadowMap.type = THREE.BasicShadowMap;
  132. // Mouse control
  133. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  134. controls.target.set( 0, 2, 0 );
  135. controls.update();
  136. clock = new THREE.Clock();
  137. stats = new Stats();
  138. document.body.appendChild( stats.dom );
  139. }
  140. function onWindowResize() {
  141. camera.aspect = window.innerWidth / window.innerHeight;
  142. camera.updateProjectionMatrix();
  143. renderer.setSize( window.innerWidth, window.innerHeight );
  144. dirLightShadowMapViewer.updateForWindowResize();
  145. spotLightShadowMapViewer.updateForWindowResize();
  146. }
  147. function animate() {
  148. requestAnimationFrame( animate );
  149. render();
  150. stats.update();
  151. }
  152. function renderScene() {
  153. renderer.render( scene, camera );
  154. }
  155. function renderShadowMapViewers() {
  156. dirLightShadowMapViewer.render( renderer );
  157. spotLightShadowMapViewer.render( renderer );
  158. }
  159. function render() {
  160. var delta = clock.getDelta();
  161. renderScene();
  162. renderShadowMapViewers();
  163. torusKnot.rotation.x += 0.25 * delta;
  164. torusKnot.rotation.y += 2 * delta;
  165. torusKnot.rotation.z += 1 * delta;
  166. cube.rotation.x += 0.25 * delta;
  167. cube.rotation.y += 2 * delta;
  168. cube.rotation.z += 1 * delta;
  169. }
  170. </script>
  171. </body>
  172. </html>