webgl_lights_spotlights.html 4.7 KB

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