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