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