webgl_shadowmap_viewer.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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">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.TorusKnotGeometry( 25, 8, 75, 20 );
  85. var material = new THREE.MeshPhongMaterial( {
  86. color: 0xff0000,
  87. shininess: 150,
  88. specular: 0x222222,
  89. shading: THREE.SmoothShading
  90. } );
  91. torusKnot = new THREE.Mesh( geometry, material );
  92. torusKnot.scale.multiplyScalar( 1 / 18 );
  93. torusKnot.position.y = 3;
  94. torusKnot.castShadow = true;
  95. torusKnot.receiveShadow = true;
  96. scene.add( torusKnot );
  97. var geometry = new THREE.BoxGeometry( 3, 3, 3 );
  98. cube = new THREE.Mesh( geometry, material );
  99. cube.position.set( 8, 3, 8 );
  100. cube.castShadow = true;
  101. cube.receiveShadow = true;
  102. scene.add( cube );
  103. var geometry = new THREE.BoxGeometry( 10, 0.15, 10 );
  104. var material = new THREE.MeshPhongMaterial( {
  105. color: 0xa0adaf,
  106. shininess: 150,
  107. specular: 0xffffff,
  108. shading: THREE.SmoothShading
  109. } );
  110. var ground = new THREE.Mesh( geometry, material );
  111. ground.scale.multiplyScalar( 3 );
  112. ground.castShadow = false;
  113. ground.receiveShadow = true;
  114. scene.add( ground );
  115. }
  116. function initShadowMapViewers() {
  117. dirLightShadowMapViewer = new THREE.ShadowMapViewer( dirLight );
  118. dirLightShadowMapViewer.position.x = 10;
  119. dirLightShadowMapViewer.position.y = 10;
  120. dirLightShadowMapViewer.size.width = 256;
  121. dirLightShadowMapViewer.size.height = 256;
  122. dirLightShadowMapViewer.update(); //Required when setting position or size directly
  123. spotLightShadowMapViewer = new THREE.ShadowMapViewer( spotLight );
  124. spotLightShadowMapViewer.size.set( 256, 256 );
  125. spotLightShadowMapViewer.position.set( 276, 10 );
  126. // spotLightShadowMapViewer.update(); //NOT required because .set updates automatically
  127. }
  128. function initMisc() {
  129. renderer = new THREE.WebGLRenderer();
  130. renderer.setClearColor( 0x000000 );
  131. renderer.setPixelRatio( window.devicePixelRatio );
  132. renderer.setSize( window.innerWidth, window.innerHeight );
  133. renderer.shadowMap.enabled = true;
  134. renderer.shadowMap.type = THREE.BasicShadowMap;
  135. // Mouse control
  136. controls = new THREE.OrbitControls( camera, renderer.domElement );
  137. controls.target.set( 0, 2, 0 );
  138. controls.update();
  139. clock = new THREE.Clock();
  140. stats = new Stats();
  141. document.body.appendChild( stats.dom );
  142. }
  143. function onWindowResize() {
  144. camera.aspect = window.innerWidth / window.innerHeight;
  145. camera.updateProjectionMatrix();
  146. renderer.setSize( window.innerWidth, window.innerHeight );
  147. dirLightShadowMapViewer.updateForWindowResize();
  148. spotLightShadowMapViewer.updateForWindowResize();
  149. }
  150. function animate() {
  151. requestAnimationFrame( animate );
  152. render();
  153. stats.update();
  154. }
  155. function renderScene() {
  156. renderer.render( scene, camera );
  157. }
  158. function renderShadowMapViewers() {
  159. dirLightShadowMapViewer.render( renderer );
  160. spotLightShadowMapViewer.render( renderer );
  161. }
  162. function render() {
  163. var delta = clock.getDelta();
  164. renderScene();
  165. renderShadowMapViewers();
  166. torusKnot.rotation.x += 0.25 * delta;
  167. torusKnot.rotation.y += 2 * delta;
  168. torusKnot.rotation.z += 1 * delta;
  169. cube.rotation.x += 0.25 * delta;
  170. cube.rotation.y += 2 * delta;
  171. cube.rotation.z += 1 * delta;
  172. }
  173. </script>
  174. </body>
  175. </html>