webgl_shadowmap_viewer.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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/WebGL.js"></script>
  35. <script src="js/libs/stats.min.js"></script>
  36. <script>
  37. if ( WEBGL.isWebGLAvailable() === false ) {
  38. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  39. }
  40. var camera, scene, renderer, clock, stats;
  41. var dirLight, spotLight;
  42. var torusKnot, cube;
  43. var dirLightShadowMapViewer, spotLightShadowMapViewer;
  44. init();
  45. animate();
  46. function init() {
  47. initScene();
  48. initShadowMapViewers();
  49. initMisc();
  50. document.body.appendChild( renderer.domElement );
  51. window.addEventListener( 'resize', onWindowResize, false );
  52. }
  53. function initScene() {
  54. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  55. camera.position.set( 0, 15, 35 );
  56. scene = new THREE.Scene();
  57. // Lights
  58. scene.add( new THREE.AmbientLight( 0x404040 ) );
  59. spotLight = new THREE.SpotLight( 0xffffff );
  60. spotLight.name = 'Spot Light';
  61. spotLight.angle = Math.PI / 5;
  62. spotLight.penumbra = 0.3;
  63. spotLight.position.set( 10, 10, 5 );
  64. spotLight.castShadow = true;
  65. spotLight.shadow.camera.near = 8;
  66. spotLight.shadow.camera.far = 30;
  67. spotLight.shadow.mapSize.width = 1024;
  68. spotLight.shadow.mapSize.height = 1024;
  69. scene.add( spotLight );
  70. scene.add( new THREE.CameraHelper( spotLight.shadow.camera ) );
  71. dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
  72. dirLight.name = 'Dir. Light';
  73. dirLight.position.set( 0, 10, 0 );
  74. dirLight.castShadow = true;
  75. dirLight.shadow.camera.near = 1;
  76. dirLight.shadow.camera.far = 10;
  77. dirLight.shadow.camera.right = 15;
  78. dirLight.shadow.camera.left = - 15;
  79. dirLight.shadow.camera.top = 15;
  80. dirLight.shadow.camera.bottom = - 15;
  81. dirLight.shadow.mapSize.width = 1024;
  82. dirLight.shadow.mapSize.height = 1024;
  83. scene.add( dirLight );
  84. scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
  85. // Geometry
  86. var geometry = new THREE.TorusKnotBufferGeometry( 25, 8, 75, 20 );
  87. var material = new THREE.MeshPhongMaterial( {
  88. color: 0xff0000,
  89. shininess: 150,
  90. specular: 0x222222
  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.BoxBufferGeometry( 3, 3, 3 );
  99. cube = new THREE.Mesh( geometry, material );
  100. cube.position.set( 8, 3, 8 );
  101. cube.castShadow = true;
  102. cube.receiveShadow = true;
  103. scene.add( cube );
  104. var geometry = new THREE.BoxBufferGeometry( 10, 0.15, 10 );
  105. var material = new THREE.MeshPhongMaterial( {
  106. color: 0xa0adaf,
  107. shininess: 150,
  108. specular: 0x111111
  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( { antialias: true } );
  130. renderer.setPixelRatio( window.devicePixelRatio );
  131. renderer.setSize( window.innerWidth, window.innerHeight );
  132. renderer.shadowMap.enabled = true;
  133. renderer.shadowMap.type = THREE.BasicShadowMap;
  134. // Mouse control
  135. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  136. controls.target.set( 0, 2, 0 );
  137. controls.update();
  138. clock = new THREE.Clock();
  139. stats = new Stats();
  140. document.body.appendChild( stats.dom );
  141. }
  142. function onWindowResize() {
  143. camera.aspect = window.innerWidth / window.innerHeight;
  144. camera.updateProjectionMatrix();
  145. renderer.setSize( window.innerWidth, window.innerHeight );
  146. dirLightShadowMapViewer.updateForWindowResize();
  147. spotLightShadowMapViewer.updateForWindowResize();
  148. }
  149. function animate() {
  150. requestAnimationFrame( animate );
  151. render();
  152. stats.update();
  153. }
  154. function renderScene() {
  155. renderer.render( scene, camera );
  156. }
  157. function renderShadowMapViewers() {
  158. dirLightShadowMapViewer.render( renderer );
  159. spotLightShadowMapViewer.render( renderer );
  160. }
  161. function render() {
  162. var delta = clock.getDelta();
  163. renderScene();
  164. renderShadowMapViewers();
  165. torusKnot.rotation.x += 0.25 * delta;
  166. torusKnot.rotation.y += 2 * delta;
  167. torusKnot.rotation.z += 1 * delta;
  168. cube.rotation.x += 0.25 * delta;
  169. cube.rotation.y += 2 * delta;
  170. cube.rotation.z += 1 * delta;
  171. }
  172. </script>
  173. </body>
  174. </html>