webgl_shadowmap_pointlight.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 torusKnotMaterial;
  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, 60 );
  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.shadow.camera.near = 1;
  60. pointLight.shadow.camera.far = 30;
  61. // pointLight.shadowCameraVisible = true;
  62. pointLight.shadow.bias = 0.01;
  63. var geometry = new THREE.SphereGeometry( 0.3, 12, 6 );
  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( 0xaaffaa );
  70. scene.add( pointLight );
  71. pointLight2 = createLight( 0xaaaaff );
  72. scene.add( pointLight2 );
  73. torusKnotMaterial = new THREE.MeshPhongMaterial( {
  74. color: 0xff0000,
  75. shininess: 100,
  76. //wireframe: true,
  77. //wireframeLinewidth: 2,
  78. specular: 0x222222
  79. } );
  80. var torusKnotGeometry = new THREE.TorusKnotGeometry( 8, 1, 150, 20 );
  81. torusKnot = new THREE.Mesh( torusKnotGeometry, torusKnotMaterial );
  82. torusKnot.position.set( 0, 9, 0 );
  83. torusKnot.castShadow = true;
  84. torusKnot.receiveShadow = true;
  85. scene.add( torusKnot );
  86. wallMaterial = new THREE.MeshPhongMaterial( {
  87. color: 0xffffff,
  88. shininess: 10,
  89. specular: 0x111111,
  90. shading: THREE.SmoothShading
  91. } );
  92. var wallGeometry = new THREE.BoxGeometry( 10, 0.15, 10 );
  93. ground = new THREE.Mesh( wallGeometry, wallMaterial );
  94. ground.position.set( 0, - 5, 0 );
  95. ground.scale.multiplyScalar( 3 );
  96. ground.receiveShadow = true;
  97. scene.add( ground );
  98. var ceiling = new THREE.Mesh( wallGeometry, wallMaterial );
  99. ceiling.position.set( 0, 24, 0 );
  100. ceiling.scale.multiplyScalar( 3 );
  101. ceiling.receiveShadow = true;
  102. scene.add( ceiling );
  103. var wall = new THREE.Mesh( wallGeometry, wallMaterial );
  104. wall.position.set( - 14, 10, 0 );
  105. wall.rotation.z = Math.PI / 2;
  106. wall.scale.multiplyScalar( 3 );
  107. wall.receiveShadow = true;
  108. scene.add( wall );
  109. wall = new THREE.Mesh( wallGeometry, wallMaterial );
  110. wall.position.set( 14, 10, 0 );
  111. wall.rotation.z = Math.PI / 2;
  112. wall.scale.multiplyScalar( 3 );
  113. wall.receiveShadow = true;
  114. scene.add( wall );
  115. wall = new THREE.Mesh( wallGeometry, wallMaterial );
  116. wall.position.set( 0, 10, - 14 );
  117. wall.rotation.y = Math.PI / 2;
  118. wall.rotation.z = Math.PI / 2;
  119. wall.scale.multiplyScalar( 3 );
  120. wall.receiveShadow = true;
  121. scene.add( wall );
  122. }
  123. function initMisc() {
  124. renderer = new THREE.WebGLRenderer( { antialias: true } );
  125. renderer.setClearColor( 0x000000 );
  126. renderer.setPixelRatio( window.devicePixelRatio );
  127. renderer.setSize( window.innerWidth, window.innerHeight );
  128. renderer.shadowMap.enabled = true;
  129. renderer.shadowMap.type = THREE.BasicShadowMap;
  130. // Mouse control
  131. controls = new THREE.OrbitControls( camera, renderer.domElement );
  132. controls.minDistance = 12;
  133. controls.maxDistance = 60;
  134. controls.enablePan = false;
  135. controls.target.set( 0, 10, 0 );
  136. controls.update();
  137. stats = new Stats();
  138. stats.domElement.style.position = 'absolute';
  139. stats.domElement.style.right = '0px';
  140. stats.domElement.style.top = '0px';
  141. document.body.appendChild( stats.domElement );
  142. }
  143. function onWindowResize() {
  144. camera.aspect = window.innerWidth / window.innerHeight;
  145. camera.updateProjectionMatrix();
  146. renderer.setSize( window.innerWidth, window.innerHeight );
  147. }
  148. function animate() {
  149. requestAnimationFrame( animate );
  150. render();
  151. stats.update();
  152. }
  153. function renderScene() {
  154. renderer.render( scene, camera );
  155. }
  156. function render() {
  157. var time = performance.now() * 0.001;
  158. pointLight.position.x = Math.sin( time ) * 9;
  159. pointLight.position.y = Math.sin( time * 1.1 ) * 9 + 5;
  160. pointLight.position.z = Math.sin( time * 1.2 ) * 9;
  161. time += 10000;
  162. pointLight2.position.x = Math.sin( time ) * 9;
  163. pointLight2.position.y = Math.sin( time * 1.1 ) * 9 + 5;
  164. pointLight2.position.z = Math.sin( time * 1.2 ) * 9;
  165. renderScene();
  166. torusKnot.rotation.y = time * 0.1;
  167. }
  168. </script>
  169. </body>
  170. </html>