webgl_shadowmap_pointlight.html 5.8 KB

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