webgl_lights_spotlights.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - SpotLights<br/>
  12. by <a href="http://master-domain.com" target="_blank" rel="noopener">Master James</a>
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { TWEEN } from './jsm/libs/tween.module.min.js';
  17. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  18. var renderer = new THREE.WebGLRenderer();
  19. renderer.setPixelRatio( window.devicePixelRatio );
  20. var camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 2000 );
  21. var controls = new OrbitControls( camera, renderer.domElement );
  22. var scene = new THREE.Scene();
  23. var matFloor = new THREE.MeshPhongMaterial();
  24. var matBox = new THREE.MeshPhongMaterial( { color: 0xaaaaaa } );
  25. var geoFloor = new THREE.PlaneBufferGeometry( 2000, 2000 );
  26. var geoBox = new THREE.BoxBufferGeometry( 3, 1, 2 );
  27. var mshFloor = new THREE.Mesh( geoFloor, matFloor );
  28. mshFloor.rotation.x = - Math.PI * 0.5;
  29. var mshBox = new THREE.Mesh( geoBox, matBox );
  30. var ambient = new THREE.AmbientLight( 0x111111 );
  31. var spotLight1 = createSpotlight( 0xFF7F00 );
  32. var spotLight2 = createSpotlight( 0x00FF7F );
  33. var spotLight3 = createSpotlight( 0x7F00FF );
  34. var lightHelper1, lightHelper2, lightHelper3;
  35. function init() {
  36. renderer.shadowMap.enabled = true;
  37. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  38. renderer.outputEncoding = THREE.sRGBEncoding;
  39. camera.position.set( 46, 22, - 21 );
  40. spotLight1.position.set( 15, 40, 45 );
  41. spotLight2.position.set( 0, 40, 35 );
  42. spotLight3.position.set( - 15, 40, 45 );
  43. lightHelper1 = new THREE.SpotLightHelper( spotLight1 );
  44. lightHelper2 = new THREE.SpotLightHelper( spotLight2 );
  45. lightHelper3 = new THREE.SpotLightHelper( spotLight3 );
  46. matFloor.color.set( 0x808080 );
  47. mshFloor.receiveShadow = true;
  48. mshFloor.position.set( 0, - 0.05, 0 );
  49. mshBox.castShadow = true;
  50. mshBox.receiveShadow = true;
  51. mshBox.position.set( 0, 5, 0 );
  52. scene.add( mshFloor );
  53. scene.add( mshBox );
  54. scene.add( ambient );
  55. scene.add( spotLight1, spotLight2, spotLight3 );
  56. scene.add( lightHelper1, lightHelper2, lightHelper3 );
  57. document.body.appendChild( renderer.domElement );
  58. onResize();
  59. window.addEventListener( 'resize', onResize, false );
  60. controls.target.set( 0, 7, 0 );
  61. controls.maxPolarAngle = Math.PI / 2;
  62. controls.update();
  63. }
  64. function createSpotlight( color ) {
  65. var newObj = new THREE.SpotLight( color, 2 );
  66. newObj.castShadow = true;
  67. newObj.angle = 0.3;
  68. newObj.penumbra = 0.2;
  69. newObj.decay = 2;
  70. newObj.distance = 50;
  71. return newObj;
  72. }
  73. function onResize() {
  74. camera.aspect = window.innerWidth / window.innerHeight;
  75. camera.updateProjectionMatrix();
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. }
  78. function tween( light ) {
  79. new TWEEN.Tween( light ).to( {
  80. angle: ( Math.random() * 0.7 ) + 0.1,
  81. penumbra: Math.random() + 1
  82. }, Math.random() * 3000 + 2000 )
  83. .easing( TWEEN.Easing.Quadratic.Out ).start();
  84. new TWEEN.Tween( light.position ).to( {
  85. x: ( Math.random() * 30 ) - 15,
  86. y: ( Math.random() * 10 ) + 15,
  87. z: ( Math.random() * 30 ) - 15
  88. }, Math.random() * 3000 + 2000 )
  89. .easing( TWEEN.Easing.Quadratic.Out ).start();
  90. }
  91. function animate() {
  92. tween( spotLight1 );
  93. tween( spotLight2 );
  94. tween( spotLight3 );
  95. setTimeout( animate, 5000 );
  96. }
  97. function render() {
  98. TWEEN.update();
  99. if ( lightHelper1 ) lightHelper1.update();
  100. if ( lightHelper2 ) lightHelper2.update();
  101. if ( lightHelper3 ) lightHelper3.update();
  102. renderer.render( scene, camera );
  103. requestAnimationFrame( render );
  104. }
  105. init();
  106. render();
  107. animate();
  108. </script>
  109. </body>
  110. </html>