webgl_shadowmap_pointlight.html 5.8 KB

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