webgpu_lights_dynamic.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - dynamic lights</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="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Dynamic Lights</span>
  14. </div>
  15. <small>
  16. Opt-in DynamicLighting avoids shader recompilation when adding/removing supported lights.<br />
  17. 100 meshes with 50 unique PBR materials. Use the Inspector to explore the scene.
  18. </small>
  19. </div>
  20. <script type="importmap">
  21. {
  22. "imports": {
  23. "three": "../build/three.webgpu.js",
  24. "three/webgpu": "../build/three.webgpu.js",
  25. "three/tsl": "../build/three.tsl.js",
  26. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three/webgpu';
  32. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  33. import { DynamicLighting } from 'three/addons/lighting/DynamicLighting.js';
  34. import { Inspector } from 'three/addons/inspector/Inspector.js';
  35. import Stats from 'three/addons/libs/stats.module.js';
  36. let camera, scene, renderer, controls, timer, stats;
  37. const pointLights = [];
  38. let autoAddInterval = null;
  39. const params = {
  40. dynamic: true,
  41. autoAdd: false,
  42. lightCount: 2,
  43. addLight() {
  44. addLight();
  45. },
  46. removeLight() {
  47. removeLight();
  48. },
  49. removeAll() {
  50. removeAllLights();
  51. }
  52. };
  53. init();
  54. function init() {
  55. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 200 );
  56. camera.position.set( 0, 15, 30 );
  57. scene = new THREE.Scene();
  58. timer = new THREE.Timer();
  59. timer.connect( document );
  60. // Stats
  61. stats = new Stats();
  62. document.body.appendChild( stats.dom );
  63. // Floor
  64. const floorGeometry = new THREE.PlaneGeometry( 120, 120 );
  65. const floorMaterial = new THREE.MeshStandardMaterial( { color: 0x444444, roughness: 0.8 } );
  66. const floor = new THREE.Mesh( floorGeometry, floorMaterial );
  67. floor.rotation.x = - Math.PI / 2;
  68. scene.add( floor );
  69. // Shared geometries
  70. const sphereGeometry = new THREE.SphereGeometry( 0.8, 128, 128 );
  71. const boxGeometry = new THREE.BoxGeometry( 1.2, 1.2, 1.2, 64, 64, 64 );
  72. const torusGeometry = new THREE.TorusGeometry( 0.6, 0.25, 128, 128 );
  73. const cylinderGeometry = new THREE.CylinderGeometry( 0.5, 0.5, 1.4, 128, 64 );
  74. const coneGeometry = new THREE.ConeGeometry( 0.6, 1.4, 128, 64 );
  75. const geometries = [ sphereGeometry, boxGeometry, torusGeometry, cylinderGeometry, coneGeometry ];
  76. // 100 meshes — first 50 with unique PBR materials, remaining 50 reuse them
  77. const uniqueMaterials = [];
  78. for ( let i = 0; i < 50; i ++ ) {
  79. const material = new THREE.MeshStandardMaterial( {
  80. color: new THREE.Color().setHSL( i / 50, 0.6 + Math.random() * 0.4, 0.35 + Math.random() * 0.3 ),
  81. roughness: Math.random(),
  82. metalness: Math.random(),
  83. } );
  84. material.name = 'Standard_' + i;
  85. uniqueMaterials.push( material );
  86. }
  87. const meshesPerRing = 10;
  88. for ( let i = 0; i < 100; i ++ ) {
  89. const material = i < 50 ? uniqueMaterials[ i ] : uniqueMaterials[ i - 50 ];
  90. const geometry = geometries[ i % geometries.length ];
  91. const mesh = new THREE.Mesh( geometry, material );
  92. const ring = Math.floor( i / meshesPerRing );
  93. const indexInRing = i % meshesPerRing;
  94. const angle = ( indexInRing / meshesPerRing ) * Math.PI * 2 + ring * 0.3;
  95. const radius = 6 + ring * 4;
  96. mesh.position.set(
  97. Math.cos( angle ) * radius,
  98. 0.7 + Math.random() * 2,
  99. Math.sin( angle ) * radius
  100. );
  101. mesh.rotation.set(
  102. Math.random() * Math.PI,
  103. Math.random() * Math.PI,
  104. 0
  105. );
  106. scene.add( mesh );
  107. }
  108. // Center sphere
  109. const centerMaterial = new THREE.MeshStandardMaterial( { color: 0xffffff, roughness: 0.1, metalness: 0.9 } );
  110. const centerSphere = new THREE.Mesh( new THREE.SphereGeometry( 2, 128, 128 ), centerMaterial );
  111. centerSphere.position.y = 2;
  112. scene.add( centerSphere );
  113. // Ambient light
  114. scene.add( new THREE.AmbientLight( 0x404040, 0.5 ) );
  115. // Start with a couple point lights
  116. addLight();
  117. addLight();
  118. // Renderer
  119. createRenderer();
  120. // Inspector GUI
  121. window.addEventListener( 'resize', onWindowResize );
  122. }
  123. function createRenderer() {
  124. renderer = new THREE.WebGPURenderer( { antialias: true } );
  125. renderer.setPixelRatio( window.devicePixelRatio );
  126. renderer.setSize( window.innerWidth, window.innerHeight );
  127. renderer.setAnimationLoop( animate );
  128. renderer.inspector = new Inspector();
  129. document.body.appendChild( renderer.domElement );
  130. if ( params.dynamic ) renderer.lighting = new DynamicLighting();
  131. // Inspector GUI
  132. const gui = renderer.inspector.createParameters( 'Dynamic Lights' );
  133. gui.add( params, 'dynamic' ).name( 'dynamic mode' ).onChange( () => {
  134. renderer.dispose();
  135. document.body.removeChild( renderer.domElement );
  136. clearInterval( autoAddInterval );
  137. autoAddInterval = null;
  138. params.autoAdd = false;
  139. createRenderer();
  140. } );
  141. gui.add( params, 'autoAdd' ).name( 'auto-add lights' ).onChange( ( value ) => {
  142. if ( value ) {
  143. autoAddInterval = setInterval( () => {
  144. addLight();
  145. }, 500 );
  146. } else {
  147. clearInterval( autoAddInterval );
  148. autoAddInterval = null;
  149. }
  150. } );
  151. gui.add( params, 'addLight' ).name( 'add light' );
  152. gui.add( params, 'removeLight' ).name( 'remove light' );
  153. gui.add( params, 'removeAll' ).name( 'remove all lights' );
  154. gui.add( params, 'lightCount' ).name( 'point lights' ).listen();
  155. controls = new OrbitControls( camera, renderer.domElement );
  156. controls.enableDamping = true;
  157. controls.target.set( 0, 2, 0 );
  158. controls.update();
  159. }
  160. function addLight() {
  161. const color = new THREE.Color().setHSL( Math.random(), 0.8, 0.5 );
  162. const light = new THREE.PointLight( color, 1000 );
  163. const angle = Math.random() * Math.PI * 2;
  164. const radius = 5 + Math.random() * 20;
  165. light.position.set(
  166. Math.cos( angle ) * radius,
  167. 1 + Math.random() * 6,
  168. Math.sin( angle ) * radius
  169. );
  170. light.userData.angle = angle;
  171. light.userData.radius = radius;
  172. light.userData.speed = 0.2 + Math.random() * 0.8;
  173. light.userData.baseY = light.position.y;
  174. // Visual indicator
  175. const sphere = new THREE.Mesh(
  176. new THREE.SphereGeometry( 0.15, 8, 8 ),
  177. new THREE.MeshBasicMaterial( { color: color } )
  178. );
  179. light.add( sphere );
  180. scene.add( light );
  181. pointLights.push( light );
  182. params.lightCount = pointLights.length;
  183. }
  184. function removeLight() {
  185. if ( pointLights.length === 0 ) return;
  186. const light = pointLights.pop();
  187. scene.remove( light );
  188. light.dispose();
  189. params.lightCount = pointLights.length;
  190. }
  191. function removeAllLights() {
  192. while ( pointLights.length > 0 ) {
  193. const light = pointLights.pop();
  194. scene.remove( light );
  195. light.dispose();
  196. }
  197. params.lightCount = 0;
  198. }
  199. function onWindowResize() {
  200. camera.aspect = window.innerWidth / window.innerHeight;
  201. camera.updateProjectionMatrix();
  202. renderer.setSize( window.innerWidth, window.innerHeight );
  203. }
  204. function animate() {
  205. timer.update();
  206. const time = timer.getElapsed();
  207. controls.update();
  208. // Animate lights
  209. for ( let i = 0; i < pointLights.length; i ++ ) {
  210. const light = pointLights[ i ];
  211. const d = light.userData;
  212. const t = time * d.speed + d.angle;
  213. light.position.x = Math.cos( t ) * d.radius;
  214. light.position.z = Math.sin( t ) * d.radius;
  215. light.position.y = d.baseY + Math.sin( t * 2 ) * 0.5;
  216. }
  217. renderer.render( scene, camera );
  218. stats.update();
  219. }
  220. </script>
  221. </body>
  222. </html>
粤ICP备19079148号