webgl_lights_spotlights.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lights - spot light</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. background-color: #000;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. #info {
  14. position: absolute;
  15. top: 0px; width: 100%;
  16. color: #ffffff;
  17. padding: 5px;
  18. font-family: Monospace;
  19. font-size: 13px;
  20. text-align: center;
  21. }
  22. a {
  23. color: #ff0080;
  24. text-decoration: none;
  25. }
  26. a:hover {
  27. color: #0080ff;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div id="container"></div>
  33. <div id="info">
  34. <a href="http://threejs.org" target="_blank">three.js</a> - This animates 3 Spot Lights - by <a href="http://master-domain.com" target="_blank">Master James</a><br />
  35. Orbit Controls are available to navigate. Click to set random color CTRL-Click for White.<br />
  36. Where the lights converge to make white light the shadows will appear as C M Y from light color pairing.
  37. </div>
  38. <script src="../build/three.js"></script>
  39. <script src="../examples/js/libs/dat.gui.min.js"></script>
  40. <script src="../examples/js/controls/OrbitControls.js"></script>
  41. <script src="js/Detector.js"></script>
  42. <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.2/TweenMax.min.js"></script>
  43. <script>
  44. var stats, container = document.getElementById( 'container' );
  45. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  46. var rnd = new THREE.WebGLRenderer();
  47. var cam = new THREE.PerspectiveCamera(34, window.innerWidth / window.innerHeight, 0.1, 20000);
  48. var orb = new THREE.OrbitControls(cam, rnd.domElement);
  49. var scn = new THREE.Scene();
  50. var matFloor = new THREE.MeshPhongMaterial();
  51. var matBox = new THREE.MeshPhongMaterial();
  52. var geoFloor = new THREE.BoxGeometry(2000, 0.1, 2000);
  53. var geoBox = new THREE.BoxGeometry(Math.PI, Math.sqrt(2), Math.E);
  54. var mshFloor = new THREE.Mesh(geoFloor, matFloor);
  55. var mshBox = new THREE.Mesh(geoBox, matBox);
  56. var amb = new THREE.AmbientLight(0x121422);
  57. var spt1 = createSpotlight( { color: 0xFF7F00, angle:0.3 } );
  58. var spt2 = createSpotlight( { color: 0x00FF7F, angle:0.3 } );
  59. var spt3 = createSpotlight( { color: 0x7F00FF, angle:0.3 } );
  60. var lightHelper1, lightHelper2, lightHelper3;
  61. var ray = new THREE.Raycaster();
  62. var mouseDown = new THREE.Vector2();
  63. var mouse = new THREE.Vector2();
  64. function init() {
  65. rnd.shadowMap.enabled = true;
  66. rnd.shadowMap.type = THREE.PCFSoftShadowMap;
  67. rnd.gammaInput = true;
  68. rnd.gammaOutput = true;
  69. rnd.antialias = true;
  70. rnd.domElement.addEventListener('mousedown', onDocumentClick);
  71. rnd.domElement.addEventListener('mouseup', onDocumentClick);
  72. cam.position.set(38, 20, -32);
  73. spt1.position.set(15, 40, 45);
  74. spt2.position.set(0, 40, 35);
  75. spt3.position.set(-15, 40, 45);
  76. lightHelper1 = new THREE.SpotLightHelper( spt1 );
  77. lightHelper2 = new THREE.SpotLightHelper( spt2 );
  78. lightHelper3 = new THREE.SpotLightHelper( spt3 );
  79. matFloor.color.set( 0x808080 );
  80. mshFloor.receiveShadow = true;
  81. mshFloor.position.set(0, -0.05, 0);
  82. randomColor( matBox );
  83. matBox.opacity = 0.8;
  84. mshBox.castShadow = true;
  85. mshBox.receiveShadow = true;
  86. mshBox.position.set(0, 5, 0);
  87. scn.add(cam);
  88. scn.add(mshFloor);
  89. scn.add(mshBox);
  90. scn.add(amb);
  91. scn.add( spt1 );
  92. scn.add( spt1.shadowCameraHelper );
  93. scn.add( spt2 );
  94. scn.add( spt2.shadowCameraHelper );
  95. scn.add( spt3 );
  96. scn.add( spt3.shadowCameraHelper );
  97. scn.add( new THREE.AxisHelper( 7 ) );
  98. scn.add( lightHelper1, lightHelper2, lightHelper3 );
  99. document.body.appendChild(rnd.domElement);
  100. onResize();
  101. window.addEventListener('resize', onResize, false);
  102. orb.addEventListener('change', render);
  103. orb.object.position.set(46, 22, -21);
  104. orb.target.set(-6, 7, 2);
  105. orb.maxPolarAngle = (Math.PI / 2);
  106. orb.update();
  107. };
  108. function createSpotlight( object ) {
  109. var newObj = new THREE.SpotLight(object.color || 0xFFFFFF);
  110. newObj.castShadow = object.castShadow || true;
  111. newObj.angle = object.angle || 0.777;
  112. newObj.exponent = object.exponent || 2.0;
  113. newObj.penumbra = object.penumbra || 0.2;
  114. newObj.decay = object.decay || 10;
  115. newObj.distance = object.distance || 0.0;
  116. newObj.shadow.mapSize.width = object.shadowWidth || 2048;
  117. newObj.shadow.mapSize.height = object.shadowHeight || 2048;
  118. // shadow camera helper
  119. newObj.shadowCameraHelper = new THREE.CameraHelper( newObj.shadow.camera ); // colored lines
  120. newObj.shadow.camera.near = 0.1;
  121. newObj.shadow.camera.far = 20000;
  122. return newObj;
  123. };
  124. function onResize() {
  125. rnd.setSize(window.innerWidth, window.innerHeight);
  126. cam.aspect = (window.innerWidth / window.innerHeight);
  127. cam.updateProjectionMatrix();
  128. };
  129. function animate(rate) {
  130. rate = rate || 6;
  131. if ( rate < 0.01 ) rate = 0.01;
  132. else if ( rate > 1000 ) rate = 1000;
  133. var targ1 = { x: ((Math.random() * 30) - 15), y: ((Math.random() * 10) + 15), z: ((Math.random() * 30) - 15) };
  134. TweenMax.to(spt1.position, rate / 3, targ1);
  135. TweenMax.to(spt1, rate / 2, { angle: ( (Math.random() * 0.7) + 0.1 ), penumbra: ( Math.random() + 1 ), position: targ1 } );
  136. var targ2 = { x: ((Math.random() * 30) - 15), y: ((Math.random() * 10) + 15), z: ((Math.random() * 30) - 15) };
  137. TweenMax.to(spt2.position, rate, targ2);
  138. TweenMax.to(spt2, rate / 3, { angle: ( (Math.random() * 0.7) + 0.1 ), penumbra: ( Math.random() + 1 ), position: targ2 } );
  139. var targ3 = { x: ((Math.random() * 30) - 15), y: ((Math.random() * 10) + 15), z: ((Math.random() * 30) - 15) };
  140. TweenMax.to(spt3.position, rate / 2, targ3);
  141. TweenMax.to(spt3, rate, { angle: ( (Math.random() * 0.7) + 0.1 ), penumbra: ( Math.random() + 1 ), position: targ3 } );
  142. setTimeout(function() { animate(rate); }, rate * 1000);
  143. };
  144. function render( /* time */ ) {
  145. if ( lightHelper1 ) lightHelper1.update();
  146. if ( lightHelper2 ) lightHelper2.update();
  147. if ( lightHelper3 ) lightHelper3.update();
  148. if ( spt1.shadowCameraHelper ) spt1.shadowCameraHelper.update();
  149. if ( spt2.shadowCameraHelper ) spt2.shadowCameraHelper.update();
  150. if ( spt3.shadowCameraHelper ) spt3.shadowCameraHelper.update();
  151. rnd.render(scn, cam);
  152. window.requestAnimationFrame(render);
  153. };
  154. function onDocumentClick( event ) {
  155. event.preventDefault();
  156. var rndDom = rnd.domElement;
  157. if( event.type === 'mousedown' ) {
  158. mouseDown.x = ( event.clientX / rndDom.clientWidth ) * 2 - 1;
  159. mouseDown.y = - ( event.clientY / rndDom.clientHeight ) * 2 + 1;
  160. }
  161. else {
  162. mouse.x = ( event.clientX / rndDom.clientWidth ) * 2 - 1;
  163. mouse.y = - ( event.clientY / rndDom.clientHeight ) * 2 + 1;
  164. if (mouseDown.distanceTo(mouse) < 0.0075) {
  165. ray.setFromCamera( mouse, cam );
  166. var found = ray.intersectObjects( [ mshBox, mshFloor ] );
  167. if ( found.length > 0 ) {
  168. if( event.ctrlKey === false ) randomColor( found[0].object );
  169. else found[0].object.material.color.set( 0xffffff );
  170. render();
  171. }
  172. }
  173. }
  174. }
  175. function randomColor( target ) {
  176. if ( target !== undefined ) {
  177. if ( target.material !== undefined ) target = target.material;
  178. if ( target.color !== undefined ) {
  179. target.color.setHex( 0xffffff * Math.random() );
  180. }
  181. }
  182. }
  183. init();
  184. render();
  185. animate(4.5);
  186. </script>
  187. </body>
  188. </html>