webgl_lights_spotlights.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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="info">
  33. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - This animates 3 Spot Lights - by <a href="http://master-domain.com" target="_blank" rel="noopener">Master James</a><br />
  34. Orbit Controls are available to navigate.<br />
  35. Where the lights converge to make white light the shadows will appear as C M Y from light color pairing.
  36. </div>
  37. <script src="../build/three.js"></script>
  38. <script src="js/libs/dat.gui.min.js"></script>
  39. <script src="js/libs/tween.min.js"></script>
  40. <script src="js/controls/OrbitControls.js"></script>
  41. <script src="js/Detector.js"></script>
  42. <script>
  43. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  44. var renderer = new THREE.WebGLRenderer();
  45. renderer.setPixelRatio( window.devicePixelRatio );
  46. var camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 2000 );
  47. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  48. var scene = new THREE.Scene();
  49. var matFloor = new THREE.MeshPhongMaterial();
  50. var matBox = new THREE.MeshPhongMaterial( { color: 0xaaaaaa } );
  51. var geoFloor = new THREE.PlaneBufferGeometry( 2000, 2000 );
  52. var geoBox = new THREE.BoxBufferGeometry( 3, 1, 2 );
  53. var mshFloor = new THREE.Mesh( geoFloor, matFloor );
  54. mshFloor.rotation.x = - Math.PI * 0.5;
  55. var mshBox = new THREE.Mesh( geoBox, matBox );
  56. var ambient = new THREE.AmbientLight( 0x111111 );
  57. var spotLight1 = createSpotlight( 0xFF7F00 );
  58. var spotLight2 = createSpotlight( 0x00FF7F );
  59. var spotLight3 = createSpotlight( 0x7F00FF );
  60. var lightHelper1, lightHelper2, lightHelper3;
  61. function init() {
  62. renderer.shadowMap.enabled = true;
  63. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  64. renderer.gammaInput = true;
  65. renderer.gammaOutput = true;
  66. camera.position.set( 46, 22, - 21 );
  67. spotLight1.position.set( 15, 40, 45 );
  68. spotLight2.position.set( 0, 40, 35 );
  69. spotLight3.position.set( - 15, 40, 45 );
  70. lightHelper1 = new THREE.SpotLightHelper( spotLight1 );
  71. lightHelper2 = new THREE.SpotLightHelper( spotLight2 );
  72. lightHelper3 = new THREE.SpotLightHelper( spotLight3 );
  73. matFloor.color.set( 0x808080 );
  74. mshFloor.receiveShadow = true;
  75. mshFloor.position.set( 0, - 0.05, 0 );
  76. mshBox.castShadow = true;
  77. mshBox.receiveShadow = true;
  78. mshBox.position.set( 0, 5, 0 );
  79. scene.add( mshFloor );
  80. scene.add( mshBox );
  81. scene.add( ambient );
  82. scene.add( spotLight1, spotLight2, spotLight3 );
  83. scene.add( lightHelper1, lightHelper2, lightHelper3 );
  84. document.body.appendChild( renderer.domElement );
  85. onResize();
  86. window.addEventListener( 'resize', onResize, false );
  87. controls.target.set( 0, 7, 0 );
  88. controls.maxPolarAngle = Math.PI / 2;
  89. controls.update();
  90. }
  91. function createSpotlight( color ) {
  92. var newObj = new THREE.SpotLight( color, 2 );
  93. newObj.castShadow = true;
  94. newObj.angle = 0.3;
  95. newObj.penumbra = 0.2;
  96. newObj.decay = 2;
  97. newObj.distance = 50;
  98. newObj.shadow.mapSize.width = 1024;
  99. newObj.shadow.mapSize.height = 1024;
  100. return newObj;
  101. }
  102. function onResize() {
  103. camera.aspect = window.innerWidth / window.innerHeight;
  104. camera.updateProjectionMatrix();
  105. renderer.setSize( window.innerWidth, window.innerHeight );
  106. }
  107. function tween( light ) {
  108. new TWEEN.Tween( light ).to( {
  109. angle: ( Math.random() * 0.7 ) + 0.1,
  110. penumbra: Math.random() + 1
  111. }, Math.random() * 3000 + 2000 )
  112. .easing( TWEEN.Easing.Quadratic.Out ).start();
  113. new TWEEN.Tween( light.position ).to( {
  114. x: ( Math.random() * 30 ) - 15,
  115. y: ( Math.random() * 10 ) + 15,
  116. z: ( Math.random() * 30 ) - 15
  117. }, Math.random() * 3000 + 2000 )
  118. .easing( TWEEN.Easing.Quadratic.Out ).start();
  119. }
  120. function animate() {
  121. tween( spotLight1 );
  122. tween( spotLight2 );
  123. tween( spotLight3 );
  124. setTimeout( animate, 5000 );
  125. }
  126. function render() {
  127. TWEEN.update();
  128. if ( lightHelper1 ) lightHelper1.update();
  129. if ( lightHelper2 ) lightHelper2.update();
  130. if ( lightHelper3 ) lightHelper3.update();
  131. renderer.render( scene, camera );
  132. requestAnimationFrame( render );
  133. }
  134. init();
  135. render();
  136. animate();
  137. </script>
  138. </body>
  139. </html>