webgpu_lights_dynamic.html 8.3 KB

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