webgl_shadowmapviewer.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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, 0.01, 1000 );
  52. camera.position.set( 0, 15, 35 );
  53. scene = new THREE.Scene();
  54. // Lights
  55. var ambient = new THREE.AmbientLight( 0x404040 );
  56. scene.add( ambient );
  57. spotLight = new THREE.SpotLight( 0xffffff );
  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.shadowCameraVisible = true;
  64. spotLight.shadowMapWidth = 1024;
  65. spotLight.shadowMapHeight = 1024;
  66. spotLight.name = 'Spot Light';
  67. scene.add( spotLight );
  68. dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
  69. dirLight.position.set( 0, 10, 0 );
  70. dirLight.castShadow = true;
  71. dirLight.shadowCameraNear = 0.01;
  72. dirLight.shadowCameraFar = 10;
  73. dirLight.shadowCameraRight = 15;
  74. dirLight.shadowCameraLeft = -15;
  75. dirLight.shadowCameraTop = 15;
  76. dirLight.shadowCameraBottom = -15;
  77. spotLight.shadowDarkness = 0.5;
  78. dirLight.shadowCameraVisible = true;
  79. dirLight.shadowMapWidth = 1024;
  80. dirLight.shadowMapHeight = 1024;
  81. dirLight.name = 'Dir. Light';
  82. scene.add( dirLight );
  83. // Geometry
  84. var geometry = new THREE.TorusKnotGeometry( 25, 8, 75, 20 );
  85. var material = new THREE.MeshPhongMaterial( {
  86. ambient: 0x404040,
  87. color: 0xff0000,
  88. shininess: 150,
  89. specular: 0xffffff,
  90. shading: THREE.SmoothShading,
  91. } );
  92. torusKnot = new THREE.Mesh( geometry, material );
  93. torusKnot.scale.multiplyScalar( 1 / 18 );
  94. torusKnot.position.y = 3;
  95. torusKnot.castShadow = true;
  96. torusKnot.receiveShadow = true;
  97. scene.add( torusKnot );
  98. var geometry = new THREE.BoxGeometry( 3, 3, 3 );
  99. cube = new THREE.Mesh( geometry, material );
  100. cube.position.set( 10, 3, 10 );
  101. cube.castShadow = true;
  102. cube.receiveShadow = true;
  103. scene.add( cube );
  104. var geometry = new THREE.BoxGeometry( 10, 0.15, 10 );
  105. var material = new THREE.MeshPhongMaterial( {
  106. ambient: 0x404040,
  107. color: 0xa0adaf,
  108. shininess: 150,
  109. specular: 0xffffff,
  110. shading: THREE.SmoothShading
  111. } );
  112. var ground = new THREE.Mesh( geometry, material );
  113. ground.scale.multiplyScalar( 3 );
  114. ground.castShadow = false;
  115. ground.receiveShadow = true;
  116. scene.add( ground );
  117. }
  118. function initShadowMapViewers() {
  119. dirLightShadowMapViewer = new THREE.ShadowMapViewer( dirLight );
  120. dirLightShadowMapViewer.position.x = 10;
  121. dirLightShadowMapViewer.position.y = 10;
  122. dirLightShadowMapViewer.size.width = 256;
  123. dirLightShadowMapViewer.size.height = 256;
  124. dirLightShadowMapViewer.update(); //Required when setting position or size directly
  125. spotLightShadowMapViewer = new THREE.ShadowMapViewer( spotLight );
  126. spotLightShadowMapViewer.size.set( 256, 256 );
  127. spotLightShadowMapViewer.position.set( 276, 10 );
  128. // spotLightShadowMapViewer.update(); //NOT required because .set updates automatically
  129. }
  130. function initMisc() {
  131. renderer = new THREE.WebGLRenderer( { antialias: true } );
  132. renderer.setSize( window.innerWidth, window.innerHeight );
  133. renderer.setClearColor( 0x000000, 1 );
  134. renderer.shadowMapEnabled = true;
  135. renderer.shadowMapType = THREE.BasicShadowMap;
  136. //Mouse control
  137. controls = new THREE.OrbitControls( camera, renderer.domElement );
  138. clock = new THREE.Clock();
  139. stats = new Stats();
  140. stats.domElement.style.position = 'absolute';
  141. stats.domElement.style.right = '0px';
  142. stats.domElement.style.top = '0px';
  143. document.body.appendChild( stats.domElement );
  144. }
  145. function onWindowResize() {
  146. camera.aspect = window.innerWidth / window.innerHeight;
  147. camera.updateProjectionMatrix();
  148. renderer.setSize( window.innerWidth, window.innerHeight );
  149. dirLightShadowMapViewer.updateForWindowResize();
  150. spotLightShadowMapViewer.updateForWindowResize();
  151. }
  152. function animate() {
  153. requestAnimationFrame( animate );
  154. render();
  155. stats.update();
  156. }
  157. function renderScene() {
  158. renderer.render( scene, camera );
  159. }
  160. function renderShadowMapViewers() {
  161. dirLightShadowMapViewer.render( renderer );
  162. spotLightShadowMapViewer.render( renderer );
  163. }
  164. function render() {
  165. var delta = clock.getDelta();
  166. renderScene();
  167. renderShadowMapViewers();
  168. torusKnot.rotation.x += 0.5 * delta;
  169. torusKnot.rotation.y += 3 * delta;
  170. torusKnot.rotation.z += 2 * delta;
  171. cube.rotation.x += 0.5 * delta;
  172. cube.rotation.y += 3 * delta;
  173. cube.rotation.z += 2 * delta;
  174. }
  175. </script>
  176. </body>
  177. </html>