2
0

webgl_lights_spotlights.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.min.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. const renderer = new THREE.WebGLRenderer();
  30. renderer.setPixelRatio( window.devicePixelRatio );
  31. const camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 2000 );
  32. const controls = new OrbitControls( camera, renderer.domElement );
  33. const scene = new THREE.Scene();
  34. const matFloor = new THREE.MeshPhongMaterial();
  35. const matBox = new THREE.MeshPhongMaterial( { color: 0xaaaaaa } );
  36. const geoFloor = new THREE.PlaneGeometry( 2000, 2000 );
  37. const geoBox = new THREE.BoxGeometry( 3, 1, 2 );
  38. const mshFloor = new THREE.Mesh( geoFloor, matFloor );
  39. mshFloor.rotation.x = - Math.PI * 0.5;
  40. const mshBox = new THREE.Mesh( geoBox, matBox );
  41. const ambient = new THREE.AmbientLight( 0x111111 );
  42. const spotLight1 = createSpotlight( 0xFF7F00 );
  43. const spotLight2 = createSpotlight( 0x00FF7F );
  44. const spotLight3 = createSpotlight( 0x7F00FF );
  45. let lightHelper1, lightHelper2, lightHelper3;
  46. function init() {
  47. renderer.shadowMap.enabled = true;
  48. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  49. renderer.outputEncoding = THREE.sRGBEncoding;
  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. matFloor.color.set( 0x808080 );
  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, 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. onWindowResize();
  70. window.addEventListener( 'resize', onWindowResize );
  71. controls.target.set( 0, 7, 0 );
  72. controls.maxPolarAngle = Math.PI / 2;
  73. controls.update();
  74. }
  75. function createSpotlight( color ) {
  76. const newObj = new THREE.SpotLight( color, 2 );
  77. newObj.castShadow = true;
  78. newObj.angle = 0.3;
  79. newObj.penumbra = 0.2;
  80. newObj.decay = 2;
  81. newObj.distance = 50;
  82. return newObj;
  83. }
  84. function onWindowResize() {
  85. camera.aspect = window.innerWidth / window.innerHeight;
  86. camera.updateProjectionMatrix();
  87. renderer.setSize( window.innerWidth, window.innerHeight );
  88. }
  89. function tween( light ) {
  90. new TWEEN.Tween( light ).to( {
  91. angle: ( Math.random() * 0.7 ) + 0.1,
  92. penumbra: Math.random() + 1
  93. }, Math.random() * 3000 + 2000 )
  94. .easing( TWEEN.Easing.Quadratic.Out ).start();
  95. new TWEEN.Tween( light.position ).to( {
  96. x: ( Math.random() * 30 ) - 15,
  97. y: ( Math.random() * 10 ) + 15,
  98. z: ( Math.random() * 30 ) - 15
  99. }, Math.random() * 3000 + 2000 )
  100. .easing( TWEEN.Easing.Quadratic.Out ).start();
  101. }
  102. function animate() {
  103. tween( spotLight1 );
  104. tween( spotLight2 );
  105. tween( spotLight3 );
  106. setTimeout( animate, 5000 );
  107. }
  108. function render() {
  109. TWEEN.update();
  110. if ( lightHelper1 ) lightHelper1.update();
  111. if ( lightHelper2 ) lightHelper2.update();
  112. if ( lightHelper3 ) lightHelper3.update();
  113. renderer.render( scene, camera );
  114. requestAnimationFrame( render );
  115. }
  116. init();
  117. render();
  118. animate();
  119. </script>
  120. </body>
  121. </html>