webgl_lights_spotlights.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import TWEEN from 'three/addons/libs/tween.module.js';
  25. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  26. const renderer = new THREE.WebGLRenderer( { antialias: true } );
  27. renderer.setPixelRatio( window.devicePixelRatio );
  28. renderer.setSize( window.innerWidth, window.innerHeight );
  29. renderer.setAnimationLoop( animate );
  30. const camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 100 );
  31. const controls = new OrbitControls( camera, renderer.domElement );
  32. const scene = new THREE.Scene();
  33. const matFloor = new THREE.MeshPhongMaterial( { color: 0x808080 } );
  34. const matBox = new THREE.MeshPhongMaterial( { color: 0xaaaaaa } );
  35. const geoFloor = new THREE.PlaneGeometry( 100, 100 );
  36. const geoBox = new THREE.BoxGeometry( 0.3, 0.1, 0.2 );
  37. const mshFloor = new THREE.Mesh( geoFloor, matFloor );
  38. mshFloor.rotation.x = - Math.PI * 0.5;
  39. const mshBox = new THREE.Mesh( geoBox, matBox );
  40. const ambient = new THREE.AmbientLight( 0x444444 );
  41. const spotLight1 = createSpotlight( 0xFF7F00 );
  42. const spotLight2 = createSpotlight( 0x00FF7F );
  43. const spotLight3 = createSpotlight( 0x7F00FF );
  44. let lightHelper1, lightHelper2, lightHelper3;
  45. function init() {
  46. renderer.shadowMap.enabled = true;
  47. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  48. camera.position.set( 4.6, 2.2, - 2.1 );
  49. spotLight1.position.set( 1.5, 4, 4.5 );
  50. spotLight2.position.set( 0, 4, 3.5 );
  51. spotLight3.position.set( - 1.5, 4, 4.5 );
  52. lightHelper1 = new THREE.SpotLightHelper( spotLight1 );
  53. lightHelper2 = new THREE.SpotLightHelper( spotLight2 );
  54. lightHelper3 = new THREE.SpotLightHelper( spotLight3 );
  55. mshFloor.receiveShadow = true;
  56. mshFloor.position.set( 0, - 0.05, 0 );
  57. mshBox.castShadow = true;
  58. mshBox.receiveShadow = true;
  59. mshBox.position.set( 0, 0.5, 0 );
  60. scene.add( mshFloor );
  61. scene.add( mshBox );
  62. scene.add( ambient );
  63. scene.add( spotLight1, spotLight2, spotLight3 );
  64. scene.add( lightHelper1, lightHelper2, lightHelper3 );
  65. document.body.appendChild( renderer.domElement );
  66. window.addEventListener( 'resize', onWindowResize );
  67. controls.target.set( 0, 0.5, 0 );
  68. controls.maxPolarAngle = Math.PI / 2;
  69. controls.minDistance = 1;
  70. controls.maxDistance = 10;
  71. controls.update();
  72. }
  73. function createSpotlight( color ) {
  74. const newObj = new THREE.SpotLight( color, 10 );
  75. newObj.castShadow = true;
  76. newObj.angle = 0.3;
  77. newObj.penumbra = 0.2;
  78. newObj.decay = 2;
  79. newObj.distance = 50;
  80. return newObj;
  81. }
  82. function onWindowResize() {
  83. camera.aspect = window.innerWidth / window.innerHeight;
  84. camera.updateProjectionMatrix();
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. }
  87. function tween( light ) {
  88. new TWEEN.Tween( light ).to( {
  89. angle: ( Math.random() * 0.7 ) + 0.1,
  90. penumbra: Math.random() + 1
  91. }, Math.random() * 3000 + 2000 )
  92. .easing( TWEEN.Easing.Quadratic.Out ).start();
  93. new TWEEN.Tween( light.position ).to( {
  94. x: ( Math.random() * 3 ) - 1.5,
  95. y: ( Math.random() * 1 ) + 1.5,
  96. z: ( Math.random() * 3 ) - 1.5
  97. }, Math.random() * 3000 + 2000 )
  98. .easing( TWEEN.Easing.Quadratic.Out ).start();
  99. }
  100. function updateTweens() {
  101. tween( spotLight1 );
  102. tween( spotLight2 );
  103. tween( spotLight3 );
  104. setTimeout( updateTweens, 5000 );
  105. }
  106. function animate() {
  107. TWEEN.update();
  108. if ( lightHelper1 ) lightHelper1.update();
  109. if ( lightHelper2 ) lightHelper2.update();
  110. if ( lightHelper3 ) lightHelper3.update();
  111. renderer.render( scene, camera );
  112. }
  113. init();
  114. updateTweens();
  115. </script>
  116. </body>
  117. </html>