webgl_shadowmap_pointlight.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - PointLight ShadowMap </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> - PointLight ShadowMap by <a href="https://github.com/mkkellogg">mkkellogg</a>
  28. </div>
  29. <script src="../build/three.min.js"></script>
  30. <script src="js/controls/OrbitControls.js"></script>
  31. <script src="js/Detector.js"></script>
  32. <script src="js/libs/stats.min.js"></script>
  33. <script>
  34. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  35. var camera, scene, renderer, stats;
  36. var dirLight, pointLight;
  37. var torusKnot;
  38. var cubeMaterial;
  39. var wallMaterial;
  40. var ground;
  41. init();
  42. animate();
  43. function init() {
  44. initScene();
  45. initMisc();
  46. document.body.appendChild( renderer.domElement );
  47. window.addEventListener( 'resize', onWindowResize, false );
  48. }
  49. function initScene() {
  50. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  51. camera.position.set( 0, 10, 40 );
  52. scene = new THREE.Scene();
  53. // Lights
  54. var ambient = new THREE.AmbientLight( 0x404040 );
  55. scene.add( ambient );
  56. pointLight = new THREE.PointLight( 0xffffff );
  57. pointLight.castShadow = true;
  58. pointLight.shadowCameraNear = 1;
  59. pointLight.shadowCameraFar = 30;
  60. pointLight.shadowDarkness = 0.5;
  61. // pointLight.shadowCameraVisible = true;
  62. pointLight.shadowMapWidth = 2048;
  63. pointLight.shadowMapHeight = 1024;
  64. pointLight.shadowBias = 0.01;
  65. scene.add( pointLight );
  66. var geometry = new THREE.SphereGeometry( 0.3, 32, 32 );
  67. var material = new THREE.MeshBasicMaterial( { color: 0xffffff } );
  68. var sphere = new THREE.Mesh( geometry, material );
  69. sphere.castShadow = false;
  70. sphere.receiveShadow = false;
  71. pointLight.add( sphere );
  72. /*
  73. dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
  74. dirLight.position.set( 0, 50, 0 );
  75. dirLight.castShadow = true;
  76. dirLight.shadowCameraNear = 1;
  77. dirLight.shadowCameraFar = 100;
  78. dirLight.shadowCameraRight = 15;
  79. dirLight.shadowCameraLeft = -15;
  80. dirLight.shadowCameraTop = 15;
  81. dirLight.shadowCameraBottom = -15;
  82. dirLight.shadowDarkness = 0.5;
  83. dirLight.shadowCameraVisible = true;
  84. dirLight.shadowMapWidth = 1024;
  85. dirLight.shadowMapHeight = 1024;
  86. scene.add( dirLight );
  87. */
  88. cubeMaterial = new THREE.MeshPhongMaterial( {
  89. color: 0xff0000,
  90. shininess: 50,
  91. specular: 0x222222
  92. } );
  93. var torusGeometry = new THREE.TorusKnotGeometry( 14, 1, 150, 20 );
  94. torusKnot = new THREE.Mesh( torusGeometry, cubeMaterial );
  95. torusKnot.position.set( 0, 5, 0 );
  96. torusKnot.castShadow = true;
  97. torusKnot.receiveShadow = true;
  98. scene.add( torusKnot );
  99. wallMaterial = new THREE.MeshPhongMaterial( {
  100. color: 0xa0adaf,
  101. shininess: 10,
  102. specular: 0x111111,
  103. shading: THREE.SmoothShading
  104. } );
  105. var wallGeometry = new THREE.BoxGeometry( 10, 0.15, 10 );
  106. ground = new THREE.Mesh( wallGeometry, wallMaterial );
  107. ground.scale.multiplyScalar( 3 );
  108. ground.castShadow = false;
  109. ground.receiveShadow = true;
  110. scene.add( ground );
  111. ground.position.set( 0, -5, 0 );
  112. var ceiling = new THREE.Mesh( wallGeometry, wallMaterial );
  113. ceiling.scale.multiplyScalar( 3 );
  114. ceiling.castShadow = false;
  115. ceiling.receiveShadow = true;
  116. scene.add( ceiling );
  117. ceiling.position.set( 0, 24, 0 );
  118. var wall = new THREE.Mesh( wallGeometry, wallMaterial );
  119. wall.scale.multiplyScalar( 3 );
  120. wall.castShadow = false;
  121. wall.receiveShadow = true;
  122. scene.add( wall );
  123. wall.position.set( -14, 10, 0 );
  124. wall.rotation.z = Math.PI / 2;
  125. wall = new THREE.Mesh( wallGeometry, wallMaterial );
  126. wall.scale.multiplyScalar( 3 );
  127. wall.castShadow = false;
  128. wall.receiveShadow = true;
  129. scene.add( wall );
  130. wall.position.set(14,10,0);
  131. wall.rotation.z = Math.PI / 2;
  132. wall = new THREE.Mesh( wallGeometry, wallMaterial );
  133. wall.scale.multiplyScalar( 3 );
  134. wall.castShadow = false;
  135. wall.receiveShadow = true;
  136. scene.add( wall );
  137. wall.position.set( 0, 10, -14 );
  138. wall.rotation.y = Math.PI / 2;
  139. wall.rotation.z = Math.PI / 2;
  140. /*
  141. wall = new THREE.Mesh( wallGeometry, wallMaterial );
  142. wall.scale.multiplyScalar( 3 );
  143. wall.castShadow = false;
  144. wall.receiveShadow = true;
  145. scene.add( wall );
  146. wall.position.set( 0, 10, 14 );
  147. wall.rotation.y = Math.PI / 2;
  148. wall.rotation.z = Math.PI / 2;
  149. */
  150. }
  151. function initMisc() {
  152. renderer = new THREE.WebGLRenderer();
  153. renderer.setSize( window.innerWidth, window.innerHeight );
  154. renderer.setClearColor( 0x000000 );
  155. renderer.shadowMap.enabled = true;
  156. renderer.shadowMap.type = THREE.BasicShadowMap;
  157. // Mouse control
  158. controls = new THREE.OrbitControls( camera, renderer.domElement );
  159. controls.target.set( 0, 10, 0 );
  160. controls.update();
  161. stats = new Stats();
  162. stats.domElement.style.position = 'absolute';
  163. stats.domElement.style.right = '0px';
  164. stats.domElement.style.top = '0px';
  165. document.body.appendChild( stats.domElement );
  166. }
  167. function onWindowResize() {
  168. camera.aspect = window.innerWidth / window.innerHeight;
  169. camera.updateProjectionMatrix();
  170. renderer.setSize( window.innerWidth, window.innerHeight );
  171. }
  172. function animate() {
  173. requestAnimationFrame( animate );
  174. render();
  175. stats.update();
  176. }
  177. function renderScene() {
  178. renderer.render( scene, camera );
  179. }
  180. function render() {
  181. var time = performance.now() * 0.001;
  182. pointLight.position.x = Math.sin( time ) * 9;
  183. pointLight.position.y = Math.sin( time * 1.1 ) * 9 + 5;
  184. pointLight.position.z = Math.sin( time * 1.2 ) * 9;
  185. renderScene();
  186. torusKnot.rotation.y = time * 0.1;
  187. }
  188. </script>
  189. </body>
  190. </html>