webgl_lights_circlearealight.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lights - circle 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.CircleAreaLight with Shadows<br/>
  12. CircleAreaLight approximates circular light sources using LTC and PCSS
  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 { CircleAreaLightHelper } from 'three/addons/helpers/CircleAreaLightHelper.js';
  28. import { RectAreaLightUniformsLib } from 'three/addons/lights/RectAreaLightUniformsLib.js';
  29. let renderer, scene, camera;
  30. let stats, meshKnot, meshFloor;
  31. let circleLight1, circleLight2, circleLight3;
  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. // CircleAreaLight uses the same LTC textures as RectAreaLight
  46. RectAreaLightUniformsLib.init();
  47. circleLight1 = new THREE.CircleAreaLight( 0xff0000, 5, 4 );
  48. circleLight1.position.set( - 5, 5, 5 );
  49. circleLight1.lookAt( 0, 0, 0 );
  50. circleLight1.castShadow = true;
  51. circleLight1.shadow.mapSize.width = 2048;
  52. circleLight1.shadow.mapSize.height = 2048;
  53. circleLight1.shadow.bias = - 0.001;
  54. scene.add( circleLight1 );
  55. circleLight2 = new THREE.CircleAreaLight( 0x00ff00, 5, 4 );
  56. circleLight2.position.set( 0, 5, 5 );
  57. circleLight2.lookAt( 0, 0, 0 );
  58. circleLight2.castShadow = true;
  59. circleLight2.shadow.mapSize.width = 2048;
  60. circleLight2.shadow.mapSize.height = 2048;
  61. circleLight2.shadow.bias = - 0.001;
  62. scene.add( circleLight2 );
  63. circleLight3 = new THREE.CircleAreaLight( 0x0000ff, 5, 4 );
  64. circleLight3.position.set( 5, 5, 5 );
  65. circleLight3.lookAt( 0, 0, 0 );
  66. circleLight3.castShadow = true;
  67. circleLight3.shadow.mapSize.width = 2048;
  68. circleLight3.shadow.mapSize.height = 2048;
  69. circleLight3.shadow.bias = - 0.001;
  70. scene.add( circleLight3 );
  71. scene.add( new CircleAreaLightHelper( circleLight1 ) );
  72. scene.add( new CircleAreaLightHelper( circleLight2 ) );
  73. scene.add( new CircleAreaLightHelper( circleLight3 ) );
  74. const geoFloor = new THREE.BoxGeometry( 2000, 0.1, 2000 );
  75. const matStdFloor = new THREE.MeshStandardMaterial( { color: 0xbcbcbc, roughness: 0.1, metalness: 0 } );
  76. meshFloor = new THREE.Mesh( geoFloor, matStdFloor );
  77. meshFloor.receiveShadow = true;
  78. scene.add( meshFloor );
  79. const geoKnot = new THREE.TorusKnotGeometry( 1.5, 0.5, 200, 16 );
  80. const matKnot = new THREE.MeshStandardMaterial( { color: 0xffffff, roughness: 0, metalness: 0 } );
  81. meshKnot = new THREE.Mesh( geoKnot, matKnot );
  82. meshKnot.position.set( 0, 5, 0 );
  83. meshKnot.castShadow = true;
  84. meshKnot.receiveShadow = true;
  85. scene.add( meshKnot );
  86. const controls = new OrbitControls( camera, renderer.domElement );
  87. controls.target.copy( meshKnot.position );
  88. controls.update();
  89. //
  90. window.addEventListener( 'resize', onWindowResize );
  91. stats = new Stats();
  92. document.body.appendChild( stats.dom );
  93. // GUI
  94. const gui = new GUI();
  95. const params = {
  96. animateLights: animateLights
  97. };
  98. gui.add( params, 'animateLights' ).name( 'Animate Lights' ).onChange( ( value ) => {
  99. animateLights = value;
  100. } );
  101. const knotFolder = gui.addFolder( 'Torus Knot Material' );
  102. knotFolder.add( meshKnot.material, 'roughness', 0, 1, 0.01 ).name( 'Roughness' );
  103. knotFolder.add( meshKnot.material, 'metalness', 0, 1, 0.01 ).name( 'Metalness' );
  104. knotFolder.open();
  105. const floorFolder = gui.addFolder( 'Floor Material' );
  106. floorFolder.add( meshFloor.material, 'roughness', 0, 1, 0.01 ).name( 'Roughness' );
  107. floorFolder.add( meshFloor.material, 'metalness', 0, 1, 0.01 ).name( 'Metalness' );
  108. floorFolder.open();
  109. }
  110. function onWindowResize() {
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. camera.aspect = ( window.innerWidth / window.innerHeight );
  113. camera.updateProjectionMatrix();
  114. }
  115. function animation( time ) {
  116. meshKnot.rotation.y = time / 1000;
  117. if ( animateLights ) {
  118. // Animate the lights in a circular pattern
  119. const t = time / 1000;
  120. const radius = 6;
  121. const height = 5;
  122. // Light 1 - Red
  123. circleLight1.position.x = Math.cos( t ) * radius;
  124. circleLight1.position.z = Math.sin( t ) * radius;
  125. circleLight1.position.y = height + Math.sin( t * 2 ) * 2;
  126. circleLight1.radius = 3 + Math.sin( t * 1.5 ) * 1.5;
  127. circleLight1.lookAt( 0, 0, 0 );
  128. // Light 2 - Green (120 degrees offset)
  129. circleLight2.position.x = Math.cos( t + Math.PI * 2 / 3 ) * radius;
  130. circleLight2.position.z = Math.sin( t + Math.PI * 2 / 3 ) * radius;
  131. circleLight2.position.y = height + Math.sin( ( t + Math.PI * 2 / 3 ) * 2 ) * 2;
  132. circleLight2.radius = 3 + Math.sin( ( t + Math.PI * 2 / 3 ) * 1.5 ) * 1.5;
  133. circleLight2.lookAt( 0, 0, 0 );
  134. // Light 3 - Blue (240 degrees offset)
  135. circleLight3.position.x = Math.cos( t + Math.PI * 4 / 3 ) * radius;
  136. circleLight3.position.z = Math.sin( t + Math.PI * 4 / 3 ) * radius;
  137. circleLight3.position.y = height + Math.sin( ( t + Math.PI * 4 / 3 ) * 2 ) * 2;
  138. circleLight3.radius = 3 + Math.sin( ( t + Math.PI * 4 / 3 ) * 1.5 ) * 1.5;
  139. circleLight3.lookAt( 0, 0, 0 );
  140. }
  141. renderer.render( scene, camera );
  142. stats.update();
  143. }
  144. </script>
  145. </body>
  146. </html>
粤ICP备19079148号