webgl_lights_spotlights.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. const camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 2000 );
  33. const controls = new OrbitControls( camera, renderer.domElement );
  34. const scene = new THREE.Scene();
  35. const matFloor = new THREE.MeshPhongMaterial( { color: 0x808080 } );
  36. const matBox = new THREE.MeshPhongMaterial( { color: 0xaaaaaa } );
  37. const geoFloor = new THREE.PlaneGeometry( 2000, 2000 );
  38. const geoBox = new THREE.BoxGeometry( 3, 1, 2 );
  39. const mshFloor = new THREE.Mesh( geoFloor, matFloor );
  40. mshFloor.rotation.x = - Math.PI * 0.5;
  41. const mshBox = new THREE.Mesh( geoBox, matBox );
  42. const ambient = new THREE.AmbientLight( 0x111111 );
  43. const spotLight1 = createSpotlight( 0xFF7F00 );
  44. const spotLight2 = createSpotlight( 0x00FF7F );
  45. const spotLight3 = createSpotlight( 0x7F00FF );
  46. let lightHelper1, lightHelper2, lightHelper3;
  47. function init() {
  48. renderer.shadowMap.enabled = true;
  49. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  50. camera.position.set( 46, 22, - 21 );
  51. spotLight1.position.set( 15, 40, 45 );
  52. spotLight2.position.set( 0, 40, 35 );
  53. spotLight3.position.set( - 15, 40, 45 );
  54. lightHelper1 = new THREE.SpotLightHelper( spotLight1 );
  55. lightHelper2 = new THREE.SpotLightHelper( spotLight2 );
  56. lightHelper3 = new THREE.SpotLightHelper( spotLight3 );
  57. mshFloor.receiveShadow = true;
  58. mshFloor.position.set( 0, - 0.05, 0 );
  59. mshBox.castShadow = true;
  60. mshBox.receiveShadow = true;
  61. mshBox.position.set( 0, 5, 0 );
  62. scene.add( mshFloor );
  63. scene.add( mshBox );
  64. scene.add( ambient );
  65. scene.add( spotLight1, spotLight2, spotLight3 );
  66. scene.add( lightHelper1, lightHelper2, lightHelper3 );
  67. document.body.appendChild( renderer.domElement );
  68. window.addEventListener( 'resize', onWindowResize );
  69. controls.target.set( 0, 7, 0 );
  70. controls.maxPolarAngle = Math.PI / 2;
  71. controls.update();
  72. }
  73. function createSpotlight( color ) {
  74. const newObj = new THREE.SpotLight( color, 2 );
  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() * 30 ) - 15,
  95. y: ( Math.random() * 10 ) + 15,
  96. z: ( Math.random() * 30 ) - 15
  97. }, Math.random() * 3000 + 2000 )
  98. .easing( TWEEN.Easing.Quadratic.Out ).start();
  99. }
  100. function animate() {
  101. tween( spotLight1 );
  102. tween( spotLight2 );
  103. tween( spotLight3 );
  104. setTimeout( animate, 5000 );
  105. }
  106. function render() {
  107. TWEEN.update();
  108. if ( lightHelper1 ) lightHelper1.update();
  109. if ( lightHelper2 ) lightHelper2.update();
  110. if ( lightHelper3 ) lightHelper3.update();
  111. renderer.render( scene, camera );
  112. requestAnimationFrame( render );
  113. }
  114. init();
  115. render();
  116. animate();
  117. </script>
  118. </body>
  119. </html>