webgl_lights_spotlights.html 4.6 KB

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