webgl_lights_rectarealight.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lights - rect area 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> - THREE.RectAreaLight with Shadows<br/>
  12. by <a href="http://github.com/abelnation" target="_blank" rel="noopener">abelnation</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 Stats from 'three/addons/libs/stats.module.js';
  25. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. import { RectAreaLightHelper } from 'three/addons/helpers/RectAreaLightHelper.js';
  28. import { RectAreaLightUniformsLib } from 'three/addons/lights/RectAreaLightUniformsLib.js';
  29. let renderer, scene, camera;
  30. let stats, meshKnot, meshFloor;
  31. let rectLight1, rectLight2, rectLight3;
  32. let animateLights = true;
  33. init();
  34. function init() {
  35. renderer = new THREE.WebGLRenderer( { antialias: true } );
  36. renderer.setPixelRatio( window.devicePixelRatio );
  37. renderer.setSize( window.innerWidth, window.innerHeight );
  38. renderer.setAnimationLoop( animation );
  39. renderer.shadowMap.enabled = true;
  40. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  41. document.body.appendChild( renderer.domElement );
  42. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  43. camera.position.set( 0, 5, - 15 );
  44. scene = new THREE.Scene();
  45. RectAreaLightUniformsLib.init();
  46. rectLight1 = new THREE.RectAreaLight( 0xff0000, 5, 4, 10 );
  47. rectLight1.position.set( - 5, 5, 5 );
  48. rectLight1.lookAt( 0, 0, 0 );
  49. rectLight1.castShadow = true;
  50. rectLight1.shadow.mapSize.width = 2048;
  51. rectLight1.shadow.mapSize.height = 2048;
  52. rectLight1.shadow.bias = 0;
  53. scene.add( rectLight1 );
  54. rectLight2 = new THREE.RectAreaLight( 0x00ff00, 5, 4, 10 );
  55. rectLight2.position.set( 0, 5, 5 );
  56. rectLight2.lookAt( 0, 0, 0 );
  57. rectLight2.castShadow = true;
  58. rectLight2.shadow.mapSize.width = 2048;
  59. rectLight2.shadow.mapSize.height = 2048;
  60. rectLight2.shadow.bias = 0;
  61. scene.add( rectLight2 );
  62. rectLight3 = new THREE.RectAreaLight( 0x0000ff, 5, 4, 10 );
  63. rectLight3.position.set( 5, 5, 5 );
  64. rectLight3.lookAt( 0, 0, 0 );
  65. rectLight3.castShadow = true;
  66. rectLight3.shadow.mapSize.width = 2048;
  67. rectLight3.shadow.mapSize.height = 2048;
  68. rectLight3.shadow.bias = 0;
  69. scene.add( rectLight3 );
  70. scene.add( new RectAreaLightHelper( rectLight1 ) );
  71. scene.add( new RectAreaLightHelper( rectLight2 ) );
  72. scene.add( new RectAreaLightHelper( rectLight3 ) );
  73. const geoFloor = new THREE.BoxGeometry( 2000, 0.1, 2000 );
  74. const matStdFloor = new THREE.MeshStandardMaterial( { color: 0xbcbcbc, roughness: 0.1, metalness: 0 } );
  75. meshFloor = new THREE.Mesh( geoFloor, matStdFloor );
  76. meshFloor.receiveShadow = true;
  77. scene.add( meshFloor );
  78. // Add static cubes on the floor
  79. const geoCube = new THREE.BoxGeometry( 1, 1, 1 );
  80. const matCube = new THREE.MeshStandardMaterial( { color: 0x808080, roughness: 0.5, metalness: 0.2 } );
  81. const cubePositions = [
  82. { x: - 4, z: - 4 },
  83. { x: 4, z: - 4 },
  84. { x: - 4, z: 4 },
  85. { x: 4, z: 4 },
  86. { x: 0, z: - 6 },
  87. { x: 6, z: 0 },
  88. { x: - 6, z: 0 }
  89. ];
  90. cubePositions.forEach( pos => {
  91. const cube = new THREE.Mesh( geoCube, matCube );
  92. cube.position.set( pos.x, 0.55, pos.z );
  93. cube.castShadow = true;
  94. cube.receiveShadow = true;
  95. scene.add( cube );
  96. } );
  97. const geoKnot = new THREE.TorusKnotGeometry( 1.5, 0.5, 200, 16 );
  98. const matKnot = new THREE.MeshStandardMaterial( { color: 0xffffff, roughness: 0, metalness: 0 } );
  99. meshKnot = new THREE.Mesh( geoKnot, matKnot );
  100. meshKnot.position.set( 0, 5, 0 );
  101. meshKnot.castShadow = true;
  102. meshKnot.receiveShadow = true;
  103. scene.add( meshKnot );
  104. const controls = new OrbitControls( camera, renderer.domElement );
  105. controls.target.copy( meshKnot.position );
  106. controls.update();
  107. //
  108. window.addEventListener( 'resize', onWindowResize );
  109. stats = new Stats();
  110. document.body.appendChild( stats.dom );
  111. // GUI
  112. const gui = new GUI();
  113. const params = {
  114. animateLights: animateLights
  115. };
  116. gui.add( params, 'animateLights' ).name( 'Animate Lights' ).onChange( ( value ) => {
  117. animateLights = value;
  118. } );
  119. const knotFolder = gui.addFolder( 'Torus Knot Material' );
  120. knotFolder.add( meshKnot.material, 'roughness', 0, 1, 0.01 ).name( 'Roughness' );
  121. knotFolder.add( meshKnot.material, 'metalness', 0, 1, 0.01 ).name( 'Metalness' );
  122. knotFolder.open();
  123. const floorFolder = gui.addFolder( 'Floor Material' );
  124. floorFolder.add( meshFloor.material, 'roughness', 0, 1, 0.01 ).name( 'Roughness' );
  125. floorFolder.add( meshFloor.material, 'metalness', 0, 1, 0.01 ).name( 'Metalness' );
  126. floorFolder.open();
  127. }
  128. function onWindowResize() {
  129. renderer.setSize( window.innerWidth, window.innerHeight );
  130. camera.aspect = ( window.innerWidth / window.innerHeight );
  131. camera.updateProjectionMatrix();
  132. }
  133. function animation( time ) {
  134. meshKnot.rotation.y = time / 1000;
  135. if ( animateLights ) {
  136. // Animate the lights in a circular pattern
  137. const t = time / 1000;
  138. const radius = 6;
  139. const height = 5;
  140. // Light 1 - Red
  141. rectLight1.position.x = Math.cos( t ) * radius;
  142. rectLight1.position.z = Math.sin( t ) * radius;
  143. rectLight1.position.y = height + Math.sin( t * 2 ) * 2;
  144. rectLight1.width = 4 + Math.sin( t * 1.5 ) * 2;
  145. rectLight1.height = 10 + Math.cos( t * 1.3 ) * 4;
  146. rectLight1.lookAt( 0, 0, 0 );
  147. // Light 2 - Green (120 degrees offset)
  148. rectLight2.position.x = Math.cos( t + Math.PI * 2 / 3 ) * radius;
  149. rectLight2.position.z = Math.sin( t + Math.PI * 2 / 3 ) * radius;
  150. rectLight2.position.y = height + Math.sin( ( t + Math.PI * 2 / 3 ) * 2 ) * 2;
  151. rectLight2.width = 4 + Math.sin( ( t + Math.PI * 2 / 3 ) * 1.5 ) * 2;
  152. rectLight2.height = 10 + Math.cos( ( t + Math.PI * 2 / 3 ) * 1.3 ) * 4;
  153. rectLight2.lookAt( 0, 0, 0 );
  154. // Light 3 - Blue (240 degrees offset)
  155. rectLight3.position.x = Math.cos( t + Math.PI * 4 / 3 ) * radius;
  156. rectLight3.position.z = Math.sin( t + Math.PI * 4 / 3 ) * radius;
  157. rectLight3.position.y = height + Math.sin( ( t + Math.PI * 4 / 3 ) * 2 ) * 2;
  158. rectLight3.width = 4 + Math.sin( ( t + Math.PI * 4 / 3 ) * 1.5 ) * 2;
  159. rectLight3.height = 10 + Math.cos( ( t + Math.PI * 4 / 3 ) * 1.3 ) * 4;
  160. rectLight3.lookAt( 0, 0, 0 );
  161. }
  162. renderer.render( scene, camera );
  163. stats.update();
  164. }
  165. </script>
  166. </body>
  167. </html>
粤ICP备19079148号