webgl_shadowmap_pointlight.html 6.0 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 pointLight, pointLight2;
  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. function createLight( color ) {
  57. var pointLight = new THREE.PointLight( color );
  58. pointLight.castShadow = true;
  59. pointLight.shadowCameraNear = 1;
  60. pointLight.shadowCameraFar = 30;
  61. pointLight.shadowDarkness = 0.5;
  62. // pointLight.shadowCameraVisible = true;
  63. pointLight.shadowMapWidth = 2048;
  64. pointLight.shadowMapHeight = 1024;
  65. pointLight.shadowBias = 0.01;
  66. var geometry = new THREE.SphereGeometry( 0.3, 32, 32 );
  67. var material = new THREE.MeshBasicMaterial( { color: color } );
  68. var sphere = new THREE.Mesh( geometry, material );
  69. sphere.castShadow = false;
  70. sphere.receiveShadow = false;
  71. pointLight.add( sphere );
  72. return pointLight
  73. }
  74. pointLight = createLight( 0xffffff );
  75. scene.add( pointLight );
  76. pointLight2 = createLight( 0xff0000 );
  77. scene.add( pointLight2 );
  78. cubeMaterial = new THREE.MeshPhongMaterial( {
  79. color: 0xff0000,
  80. shininess: 50,
  81. specular: 0x222222
  82. } );
  83. var torusGeometry = new THREE.TorusKnotGeometry( 14, 1, 150, 20 );
  84. torusKnot = new THREE.Mesh( torusGeometry, cubeMaterial );
  85. torusKnot.position.set( 0, 5, 0 );
  86. torusKnot.castShadow = true;
  87. torusKnot.receiveShadow = true;
  88. scene.add( torusKnot );
  89. wallMaterial = new THREE.MeshPhongMaterial( {
  90. color: 0xa0adaf,
  91. shininess: 10,
  92. specular: 0x111111,
  93. shading: THREE.SmoothShading
  94. } );
  95. var wallGeometry = new THREE.BoxGeometry( 10, 0.15, 10 );
  96. ground = new THREE.Mesh( wallGeometry, wallMaterial );
  97. ground.scale.multiplyScalar( 3 );
  98. ground.castShadow = false;
  99. ground.receiveShadow = true;
  100. scene.add( ground );
  101. ground.position.set( 0, -5, 0 );
  102. var ceiling = new THREE.Mesh( wallGeometry, wallMaterial );
  103. ceiling.scale.multiplyScalar( 3 );
  104. ceiling.castShadow = false;
  105. ceiling.receiveShadow = true;
  106. scene.add( ceiling );
  107. ceiling.position.set( 0, 24, 0 );
  108. var wall = new THREE.Mesh( wallGeometry, wallMaterial );
  109. wall.scale.multiplyScalar( 3 );
  110. wall.castShadow = false;
  111. wall.receiveShadow = true;
  112. scene.add( wall );
  113. wall.position.set( -14, 10, 0 );
  114. wall.rotation.z = Math.PI / 2;
  115. wall = new THREE.Mesh( wallGeometry, wallMaterial );
  116. wall.scale.multiplyScalar( 3 );
  117. wall.castShadow = false;
  118. wall.receiveShadow = true;
  119. scene.add( wall );
  120. wall.position.set(14,10,0);
  121. wall.rotation.z = Math.PI / 2;
  122. wall = new THREE.Mesh( wallGeometry, wallMaterial );
  123. wall.scale.multiplyScalar( 3 );
  124. wall.castShadow = false;
  125. wall.receiveShadow = true;
  126. scene.add( wall );
  127. wall.position.set( 0, 10, -14 );
  128. wall.rotation.y = Math.PI / 2;
  129. wall.rotation.z = Math.PI / 2;
  130. /*
  131. wall = new THREE.Mesh( wallGeometry, wallMaterial );
  132. wall.scale.multiplyScalar( 3 );
  133. wall.castShadow = false;
  134. wall.receiveShadow = true;
  135. scene.add( wall );
  136. wall.position.set( 0, 10, 14 );
  137. wall.rotation.y = Math.PI / 2;
  138. wall.rotation.z = Math.PI / 2;
  139. */
  140. }
  141. function initMisc() {
  142. renderer = new THREE.WebGLRenderer();
  143. renderer.setSize( window.innerWidth, window.innerHeight );
  144. renderer.setClearColor( 0x000000 );
  145. renderer.shadowMap.enabled = true;
  146. renderer.shadowMap.type = THREE.BasicShadowMap;
  147. // Mouse control
  148. controls = new THREE.OrbitControls( camera, renderer.domElement );
  149. controls.target.set( 0, 10, 0 );
  150. controls.update();
  151. stats = new Stats();
  152. stats.domElement.style.position = 'absolute';
  153. stats.domElement.style.right = '0px';
  154. stats.domElement.style.top = '0px';
  155. document.body.appendChild( stats.domElement );
  156. }
  157. function onWindowResize() {
  158. camera.aspect = window.innerWidth / window.innerHeight;
  159. camera.updateProjectionMatrix();
  160. renderer.setSize( window.innerWidth, window.innerHeight );
  161. }
  162. function animate() {
  163. requestAnimationFrame( animate );
  164. render();
  165. stats.update();
  166. }
  167. function renderScene() {
  168. renderer.render( scene, camera );
  169. }
  170. function render() {
  171. var time = performance.now() * 0.001;
  172. pointLight.position.x = Math.sin( time ) * 9;
  173. pointLight.position.y = Math.sin( time * 1.1 ) * 9 + 5;
  174. pointLight.position.z = Math.sin( time * 1.2 ) * 9;
  175. time += 10000;
  176. pointLight2.position.x = Math.sin( time ) * 9;
  177. pointLight2.position.y = Math.sin( time * 1.1 ) * 9 + 5;
  178. pointLight2.position.z = Math.sin( time * 1.2 ) * 9;
  179. renderScene();
  180. torusKnot.rotation.y = time * 0.1;
  181. }
  182. </script>
  183. </body>
  184. </html>