webgl_lights_spotlights.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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, 2000);
  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( { color: 0xffffff * Math.random() } );
  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. cam.position.set(38, 20, -32);
  70. spt1.position.set(15, 40, 45);
  71. spt2.position.set(0, 40, 35);
  72. spt3.position.set(-15, 40, 45);
  73. lightHelper1 = new THREE.SpotLightHelper( spt1 );
  74. lightHelper2 = new THREE.SpotLightHelper( spt2 );
  75. lightHelper3 = new THREE.SpotLightHelper( spt3 );
  76. matFloor.color.set( 0x808080 );
  77. mshFloor.receiveShadow = true;
  78. mshFloor.position.set(0, -0.05, 0);
  79. mshBox.castShadow = true;
  80. mshBox.receiveShadow = true;
  81. mshBox.position.set(0, 5, 0);
  82. scn.add( cam);
  83. scn.add( mshFloor );
  84. scn.add( mshBox );
  85. scn.add( amb );
  86. scn.add( spt1, spt2, spt3 );
  87. scn.add( lightHelper1, lightHelper2, lightHelper3 );
  88. document.body.appendChild(rnd.domElement);
  89. onResize();
  90. window.addEventListener('resize', onResize, false);
  91. orb.object.position.set(46, 22, -21);
  92. orb.target.set(-6, 7, 2);
  93. orb.maxPolarAngle = (Math.PI / 2);
  94. orb.update();
  95. }
  96. function createSpotlight( color ) {
  97. var newObj = new THREE.SpotLight( color, 2 );
  98. newObj.castShadow = true;
  99. newObj.angle = 0.3;
  100. newObj.penumbra = 0.2;
  101. newObj.decay = 2;
  102. newObj.distance = 50;
  103. newObj.shadow.mapSize.width = 1024;
  104. newObj.shadow.mapSize.height = 1024;
  105. return newObj;
  106. }
  107. function onResize() {
  108. cam.aspect = window.innerWidth / window.innerHeight;
  109. cam.updateProjectionMatrix();
  110. rnd.setSize( window.innerWidth, window.innerHeight );
  111. }
  112. function animate(rate) {
  113. rate = rate || 6;
  114. if ( rate < 0.01 ) rate = 0.01;
  115. else if ( rate > 1000 ) rate = 1000;
  116. var targ1 = { x: ((Math.random() * 30) - 15), y: ((Math.random() * 10) + 15), z: ((Math.random() * 30) - 15) };
  117. TweenMax.to(spt1.position, rate / 3, targ1);
  118. TweenMax.to(spt1, rate / 2, { angle: ( (Math.random() * 0.7) + 0.1 ), penumbra: ( Math.random() + 1 ), position: targ1 } );
  119. var targ2 = { x: ((Math.random() * 30) - 15), y: ((Math.random() * 10) + 15), z: ((Math.random() * 30) - 15) };
  120. TweenMax.to(spt2.position, rate, targ2);
  121. TweenMax.to(spt2, rate / 3, { angle: ( (Math.random() * 0.7) + 0.1 ), penumbra: ( Math.random() + 1 ), position: targ2 } );
  122. var targ3 = { x: ((Math.random() * 30) - 15), y: ((Math.random() * 10) + 15), z: ((Math.random() * 30) - 15) };
  123. TweenMax.to(spt3.position, rate / 2, targ3);
  124. TweenMax.to(spt3, rate, { angle: ( (Math.random() * 0.7) + 0.1 ), penumbra: ( Math.random() + 1 ), position: targ3 } );
  125. setTimeout(function() { animate(rate); }, rate * 1000);
  126. }
  127. function render( /* time */ ) {
  128. if ( lightHelper1 ) lightHelper1.update();
  129. if ( lightHelper2 ) lightHelper2.update();
  130. if ( lightHelper3 ) lightHelper3.update();
  131. rnd.render(scn, cam);
  132. requestAnimationFrame(render);
  133. }
  134. init();
  135. render();
  136. animate(4.5);
  137. </script>
  138. </body>
  139. </html>