webgl_lights_spotlights.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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( 0xFF7F00 );
  58. var spt2 = createSpotlight( 0x00FF7F );
  59. var spt3 = createSpotlight( 0x7F00FF );
  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( color ) {
  109. var newObj = new THREE.SpotLight( color, 2 );
  110. newObj.castShadow = true;
  111. newObj.angle = 0.3;
  112. newObj.penumbra = 0.2;
  113. newObj.decay = 2;
  114. newObj.distance = 50;
  115. newObj.shadow.mapSize.width = 1024;
  116. newObj.shadow.mapSize.height = 1024;
  117. return newObj;
  118. };
  119. function onResize() {
  120. rnd.setSize(window.innerWidth, window.innerHeight);
  121. cam.aspect = (window.innerWidth / window.innerHeight);
  122. cam.updateProjectionMatrix();
  123. };
  124. function animate(rate) {
  125. rate = rate || 6;
  126. if ( rate < 0.01 ) rate = 0.01;
  127. else if ( rate > 1000 ) rate = 1000;
  128. var targ1 = { x: ((Math.random() * 30) - 15), y: ((Math.random() * 10) + 15), z: ((Math.random() * 30) - 15) };
  129. TweenMax.to(spt1.position, rate / 3, targ1);
  130. TweenMax.to(spt1, rate / 2, { angle: ( (Math.random() * 0.7) + 0.1 ), penumbra: ( Math.random() + 1 ), position: targ1 } );
  131. var targ2 = { x: ((Math.random() * 30) - 15), y: ((Math.random() * 10) + 15), z: ((Math.random() * 30) - 15) };
  132. TweenMax.to(spt2.position, rate, targ2);
  133. TweenMax.to(spt2, rate / 3, { angle: ( (Math.random() * 0.7) + 0.1 ), penumbra: ( Math.random() + 1 ), position: targ2 } );
  134. var targ3 = { x: ((Math.random() * 30) - 15), y: ((Math.random() * 10) + 15), z: ((Math.random() * 30) - 15) };
  135. TweenMax.to(spt3.position, rate / 2, targ3);
  136. TweenMax.to(spt3, rate, { angle: ( (Math.random() * 0.7) + 0.1 ), penumbra: ( Math.random() + 1 ), position: targ3 } );
  137. setTimeout(function() { animate(rate); }, rate * 1000);
  138. };
  139. function render( /* time */ ) {
  140. if ( lightHelper1 ) lightHelper1.update();
  141. if ( lightHelper2 ) lightHelper2.update();
  142. if ( lightHelper3 ) lightHelper3.update();
  143. rnd.render(scn, cam);
  144. window.requestAnimationFrame(render);
  145. };
  146. function onDocumentClick( event ) {
  147. event.preventDefault();
  148. var rndDom = rnd.domElement;
  149. if( event.type === 'mousedown' ) {
  150. mouseDown.x = ( event.clientX / rndDom.clientWidth ) * 2 - 1;
  151. mouseDown.y = - ( event.clientY / rndDom.clientHeight ) * 2 + 1;
  152. }
  153. else {
  154. mouse.x = ( event.clientX / rndDom.clientWidth ) * 2 - 1;
  155. mouse.y = - ( event.clientY / rndDom.clientHeight ) * 2 + 1;
  156. if (mouseDown.distanceTo(mouse) < 0.0075) {
  157. ray.setFromCamera( mouse, cam );
  158. var found = ray.intersectObjects( [ mshBox, mshFloor ] );
  159. if ( found.length > 0 ) {
  160. if( event.ctrlKey === false ) randomColor( found[0].object );
  161. else found[0].object.material.color.set( 0xffffff );
  162. render();
  163. }
  164. }
  165. }
  166. }
  167. function randomColor( target ) {
  168. if ( target !== undefined ) {
  169. if ( target.material !== undefined ) target = target.material;
  170. if ( target.color !== undefined ) {
  171. target.color.setHex( 0xffffff * Math.random() );
  172. }
  173. }
  174. }
  175. init();
  176. render();
  177. animate(4.5);
  178. </script>
  179. </body>
  180. </html>