webgl_lights_spotlights.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.<br />
  36. Where the lights converge to make white light the shadows will appear as R G B from pairs of lights.<br />
  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. let stats, container = document.getElementById( 'container' );
  45. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  46. let rnd = new THREE.WebGLRenderer();
  47. let cam = new THREE.PerspectiveCamera(34, window.innerWidth / window.innerHeight, 0.1, 20000);
  48. let orb = new THREE.OrbitControls(cam, document);
  49. let scn = new THREE.Scene();
  50. let matFloor = new THREE.MeshPhongMaterial();
  51. let matBox = new THREE.MeshPhongMaterial();
  52. let geoFloor = new THREE.BoxGeometry(2000, 0.1, 2000);
  53. let geoBox = new THREE.BoxGeometry(Math.PI, Math.sqrt(2), Math.E);
  54. let mshFloor = new THREE.Mesh(geoFloor, matFloor);
  55. let mshBox = new THREE.Mesh(geoBox, matBox);
  56. let amb = new THREE.AmbientLight(0x121422);
  57. let spt1 = createSpotlight( { color: 0xFF7F00, angle:0.3 } );
  58. let spt2 = createSpotlight( { color: 0x00FF7F, angle:0.3 } );
  59. let spt3 = createSpotlight( { color: 0x7F00FF, angle:0.3 } );
  60. let lightHelper1, lightHelper2, lightHelper3;
  61. function init() {
  62. rnd.shadowMap.enabled = true;
  63. rnd.shadowMap.type = THREE.PCFSoftShadowMap;
  64. rnd.gammaInput = true;
  65. rnd.gammaOutput = true;
  66. rnd.antialias = true;
  67. cam.position.set(38, 20, -32);
  68. spt1.position.set(15, 40, 45);
  69. spt2.position.set(0, 40, 35);
  70. spt3.position.set(-15, 40, 45);
  71. lightHelper1 = new THREE.SpotLightHelper( spt1 );
  72. lightHelper2 = new THREE.SpotLightHelper( spt2 );
  73. lightHelper3 = new THREE.SpotLightHelper( spt3 );
  74. matFloor.color.set( 0x808080 );
  75. matFloor.color.a = 1.0;
  76. mshFloor.receiveShadow = true;
  77. mshFloor.position.set(0, -0.05, 0);
  78. matBox.color = { r: Math.random(), g: Math.random(), b: Math.random(), a: 1.0 };
  79. matBox.opacity = 0.8;
  80. mshBox.castShadow = true;
  81. mshBox.receiveShadow = true;
  82. mshBox.position.set(0, 5, 0);
  83. scn.add(cam);
  84. scn.add(mshFloor);
  85. scn.add(mshBox);
  86. scn.add(amb);
  87. scn.add( spt1 );
  88. scn.add( spt1.shadowCameraHelper );
  89. scn.add( spt2 );
  90. scn.add( spt2.shadowCameraHelper );
  91. scn.add( spt3 );
  92. scn.add( spt3.shadowCameraHelper );
  93. scn.add( new THREE.AxisHelper( 7 ) );
  94. scn.add( lightHelper1, lightHelper2, lightHelper3 );
  95. document.body.appendChild(rnd.domElement);
  96. onResize();
  97. window.addEventListener('resize', onResize, false);
  98. orb.addEventListener('change', render);
  99. orb.object.position.set(46, 22, -21);
  100. orb.target.set(-6, 7, 2);
  101. orb.maxPolarAngle = (Math.PI / 2);
  102. orb.update();
  103. };
  104. function createSpotlight( object ) {
  105. let newObj = new THREE.SpotLight(object.color || 0xFFFFFF);
  106. newObj.castShadow = object.castShadow || true;
  107. newObj.angle = object.angle || 0.777;
  108. newObj.exponent = object.exponent || 2.0;
  109. newObj.penumbra = object.penumbra || 0.2;
  110. newObj.decay = object.decay || 10;
  111. newObj.distance = object.distance || 0.0;
  112. newObj.shadow.mapSize.width = object.shadowWidth || 2048;
  113. newObj.shadow.mapSize.height = object.shadowHeight || 2048;
  114. // shadow camera helper
  115. newObj.shadowCameraHelper = new THREE.CameraHelper( newObj.shadow.camera ); // colored lines
  116. newObj.shadow.camera.near = 0.1;
  117. newObj.shadow.camera.far = 20000;
  118. return newObj;
  119. };
  120. function onResize() {
  121. rnd.setSize(window.innerWidth, window.innerHeight);
  122. cam.aspect = (window.innerWidth / window.innerHeight);
  123. cam.updateProjectionMatrix();
  124. };
  125. function animate(rate) {
  126. rate = rate || 6;
  127. if ( rate < 0.01 ) rate = 0.01;
  128. else if ( rate > 1000 ) rate = 1000;
  129. let targ1 = { x: ((Math.random() * 30) - 15), y: ((Math.random() * 10) + 15), z: ((Math.random() * 30) - 15) };
  130. TweenMax.to(spt1.position, rate / 3, targ1);
  131. TweenMax.to(spt1, rate / 2, { angle: ( (Math.random() * 0.7) + 0.1 ), penumbra: ( Math.random() + 1 ), position: targ1 } );
  132. let targ2 = { x: ((Math.random() * 30) - 15), y: ((Math.random() * 10) + 15), z: ((Math.random() * 30) - 15) };
  133. TweenMax.to(spt2.position, rate, targ2);
  134. TweenMax.to(spt2, rate / 3, { angle: ( (Math.random() * 0.7) + 0.1 ), penumbra: ( Math.random() + 1 ), position: targ2 } );
  135. let targ3 = { x: ((Math.random() * 30) - 15), y: ((Math.random() * 10) + 15), z: ((Math.random() * 30) - 15) };
  136. TweenMax.to(spt3.position, rate / 2, targ3);
  137. TweenMax.to(spt3, rate, { angle: ( (Math.random() * 0.7) + 0.1 ), penumbra: ( Math.random() + 1 ), position: targ3 } );
  138. setTimeout(function() { animate(rate); }, rate * 1000);
  139. };
  140. function render( /* time */ ) {
  141. if ( lightHelper1 ) lightHelper1.update();
  142. if ( lightHelper2 ) lightHelper2.update();
  143. if ( lightHelper3 ) lightHelper3.update();
  144. if ( spt1.shadowCameraHelper ) spt1.shadowCameraHelper.update();
  145. if ( spt2.shadowCameraHelper ) spt2.shadowCameraHelper.update();
  146. if ( spt3.shadowCameraHelper ) spt3.shadowCameraHelper.update();
  147. rnd.render(scn, cam);
  148. window.requestAnimationFrame(render);
  149. };
  150. init();
  151. render();
  152. animate(4.5);
  153. </script>
  154. </body>
  155. </html>