webgl_shadowmap_viewer.html 6.0 KB

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