webgpu_lightprobes_complex.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - light probe volume (multi-room)</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 - light probe volume (multi-room)">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_lightprobes_complex.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_lightprobes_complex.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>Light Probe Volume (Multi-Room)</span>
  18. </div>
  19. <small>Two rooms with independent probe volumes showcasing the multi-volume scene.add() API.</small>
  20. </div>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.webgpu.js",
  25. "three/webgpu": "../build/three.webgpu.js",
  26. "three/tsl": "../build/three.tsl.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three/webgpu';
  33. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  34. import { Inspector } from 'three/addons/inspector/Inspector.js';
  35. import { LightProbeGrid } from 'three/addons/lighting/LightProbeGrid.js';
  36. import { LightProbeGridHelper } from 'three/addons/helpers/LightProbeGridHelper.js';
  37. let camera, scene, renderer, controls;
  38. let probesLeft, probesRight;
  39. let probesHelperLeft, probesHelperRight;
  40. init();
  41. async function init() {
  42. // Camera
  43. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  44. camera.position.set( 0, 2.5, 16 );
  45. // Scene
  46. scene = new THREE.Scene();
  47. scene.background = new THREE.Color( 0x111111 );
  48. // Materials
  49. const wallMaterial = new THREE.MeshStandardMaterial( { color: 0xcccccc, side: THREE.BackSide } );
  50. const whiteMat = new THREE.MeshStandardMaterial( { color: 0xeeeeee } );
  51. // Room shell: x=-8 to 8, z=-4 to 4, y=0 to 5
  52. const room = new THREE.Mesh( new THREE.BoxGeometry( 16, 5, 8 ), wallMaterial );
  53. room.position.set( 0, 2.5, 0 );
  54. room.receiveShadow = true;
  55. scene.add( room );
  56. const redWall = new THREE.Mesh(
  57. new THREE.PlaneGeometry( 8, 5 ),
  58. new THREE.MeshStandardMaterial( { color: 0xdd2200 } )
  59. );
  60. redWall.rotation.y = Math.PI / 2;
  61. redWall.position.set( - 7.99, 2.5, 0 );
  62. scene.add( redWall );
  63. const blueWall = new THREE.Mesh(
  64. new THREE.PlaneGeometry( 8, 5 ),
  65. new THREE.MeshStandardMaterial( { color: 0x0044ff } )
  66. );
  67. blueWall.rotation.y = - Math.PI / 2;
  68. blueWall.position.set( 7.99, 2.5, 0 );
  69. scene.add( blueWall );
  70. // Dividing wall at x=0 with doorway
  71. const dividerMat = new THREE.MeshStandardMaterial( { color: 0xcccccc } );
  72. const doorwayHalfGap = 1.25;
  73. // Left section of divider (z = -4 to -1.25)
  74. const dividerLeft = new THREE.Mesh(
  75. new THREE.BoxGeometry( 0.3, 5, 4 - doorwayHalfGap ),
  76. dividerMat
  77. );
  78. dividerLeft.position.set( 0, 2.5, - ( doorwayHalfGap + ( 4 - doorwayHalfGap ) / 2 ) );
  79. dividerLeft.castShadow = true;
  80. dividerLeft.receiveShadow = true;
  81. scene.add( dividerLeft );
  82. // Right section of divider (z = 1.25 to 4)
  83. const dividerRight = new THREE.Mesh(
  84. new THREE.BoxGeometry( 0.3, 5, 4 - doorwayHalfGap ),
  85. dividerMat
  86. );
  87. dividerRight.position.set( 0, 2.5, doorwayHalfGap + ( 4 - doorwayHalfGap ) / 2 );
  88. dividerRight.castShadow = true;
  89. dividerRight.receiveShadow = true;
  90. scene.add( dividerRight );
  91. // Lintel above doorway
  92. const lintel = new THREE.Mesh(
  93. new THREE.BoxGeometry( 0.3, 1.2, doorwayHalfGap * 2 ),
  94. dividerMat
  95. );
  96. lintel.position.set( 0, 4.4, 0 );
  97. lintel.castShadow = true;
  98. lintel.receiveShadow = true;
  99. scene.add( lintel );
  100. // Left room objects
  101. // Columns
  102. const columnGeom = new THREE.CylinderGeometry( 0.3, 0.3, 4, 16 );
  103. for ( const x of [ - 6, - 2 ] ) {
  104. for ( const z of [ - 2.5, 2.5 ] ) {
  105. const col = new THREE.Mesh( columnGeom, whiteMat );
  106. col.position.set( x, 2, z );
  107. col.castShadow = true;
  108. col.receiveShadow = true;
  109. scene.add( col );
  110. }
  111. }
  112. // Table with golden sphere
  113. const tableTop = new THREE.Mesh(
  114. new THREE.BoxGeometry( 2.4, 0.12, 1.4 ),
  115. new THREE.MeshStandardMaterial( { color: 0x886644 } )
  116. );
  117. tableTop.position.set( - 4, 1.0, 0 );
  118. tableTop.castShadow = true;
  119. tableTop.receiveShadow = true;
  120. scene.add( tableTop );
  121. const legGeom = new THREE.BoxGeometry( 0.1, 1, 0.1 );
  122. for ( const x of [ - 5.05, - 2.95 ] ) {
  123. for ( const z of [ - 0.55, 0.55 ] ) {
  124. const leg = new THREE.Mesh( legGeom, tableTop.material );
  125. leg.position.set( x, 0.5, z );
  126. leg.castShadow = true;
  127. scene.add( leg );
  128. }
  129. }
  130. const sphere = new THREE.Mesh(
  131. new THREE.SphereGeometry( 0.35, 32, 32 ),
  132. new THREE.MeshStandardMaterial( { color: 0xffd700, metalness: 0.3, roughness: 0.4 } )
  133. );
  134. sphere.position.set( - 4, 1.41, 0 );
  135. sphere.castShadow = true;
  136. sphere.receiveShadow = true;
  137. scene.add( sphere );
  138. // Stepped blocks near red wall
  139. const step1 = new THREE.Mesh( new THREE.BoxGeometry( 1.5, 0.5, 1 ), whiteMat );
  140. step1.position.set( - 7, 0.25, 0 );
  141. step1.castShadow = true;
  142. step1.receiveShadow = true;
  143. scene.add( step1 );
  144. const step2 = new THREE.Mesh( new THREE.BoxGeometry( 1.0, 1.0, 1 ), whiteMat );
  145. step2.position.set( - 7, 0.5, 0 );
  146. step2.castShadow = true;
  147. step2.receiveShadow = true;
  148. scene.add( step2 );
  149. const step3 = new THREE.Mesh( new THREE.BoxGeometry( 0.5, 1.5, 1 ), whiteMat );
  150. step3.position.set( - 7, 0.75, 0 );
  151. step3.castShadow = true;
  152. step3.receiveShadow = true;
  153. scene.add( step3 );
  154. // Right room objects
  155. // Columns
  156. for ( const x of [ 2, 6 ] ) {
  157. for ( const z of [ - 2.5, 2.5 ] ) {
  158. const col = new THREE.Mesh( columnGeom, whiteMat );
  159. col.position.set( x, 2, z );
  160. col.castShadow = true;
  161. col.receiveShadow = true;
  162. scene.add( col );
  163. }
  164. }
  165. // Pedestal with torus knot
  166. const pedestal = new THREE.Mesh(
  167. new THREE.BoxGeometry( 0.8, 1.2, 0.8 ),
  168. whiteMat
  169. );
  170. pedestal.position.set( 4, 0.6, 0 );
  171. pedestal.castShadow = true;
  172. pedestal.receiveShadow = true;
  173. scene.add( pedestal );
  174. const torusKnot = new THREE.Mesh(
  175. new THREE.TorusKnotGeometry( 0.3, 0.1, 64, 16 ),
  176. new THREE.MeshStandardMaterial( { color: 0xff44aa, metalness: 0.2, roughness: 0.5 } )
  177. );
  178. torusKnot.position.set( 4, 1.65, 0 );
  179. torusKnot.castShadow = true;
  180. torusKnot.receiveShadow = true;
  181. scene.add( torusKnot );
  182. // Tall cone sculpture
  183. const sculpture = new THREE.Mesh(
  184. new THREE.ConeGeometry( 0.4, 2.5, 5 ),
  185. whiteMat
  186. );
  187. sculpture.position.set( 6.5, 1.25, - 1.5 );
  188. sculpture.castShadow = true;
  189. sculpture.receiveShadow = true;
  190. scene.add( sculpture );
  191. // Lights
  192. // Warm point light in left room
  193. const warmLight = new THREE.PointLight( 0xffaa44, 30 );
  194. warmLight.position.set( - 4, 4.5, 0 );
  195. warmLight.castShadow = true;
  196. warmLight.shadow.mapSize.setScalar( 256 );
  197. warmLight.shadow.radius = 12;
  198. warmLight.shadow.bias = - 0.002;
  199. scene.add( warmLight );
  200. // Cool point light in right room
  201. const coolLight = new THREE.PointLight( 0x88bbff, 30 );
  202. coolLight.position.set( 4, 4.5, 0 );
  203. coolLight.castShadow = true;
  204. coolLight.shadow.mapSize.setScalar( 256 );
  205. coolLight.shadow.radius = 12;
  206. coolLight.shadow.bias = - 0.002;
  207. scene.add( coolLight );
  208. // Renderer
  209. renderer = new THREE.WebGPURenderer( { antialias: true } );
  210. renderer.setPixelRatio( window.devicePixelRatio );
  211. renderer.setSize( window.innerWidth, window.innerHeight );
  212. renderer.setAnimationLoop( animate );
  213. renderer.shadowMap.enabled = true;
  214. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  215. renderer.toneMappingExposure = 1.0;
  216. renderer.inspector = new Inspector();
  217. document.body.appendChild( renderer.domElement );
  218. await renderer.init();
  219. // Controls
  220. controls = new OrbitControls( camera, renderer.domElement );
  221. controls.target.set( 0, 2.5, 0 );
  222. controls.update();
  223. // Probe volumes
  224. const params = {
  225. enabled: true,
  226. showProbes: false,
  227. resolution: 6
  228. };
  229. async function bake( resolution ) {
  230. // Remove both volumes before baking to prevent feedback
  231. if ( probesLeft ) {
  232. scene.remove( probesLeft );
  233. probesLeft.dispose();
  234. }
  235. if ( probesRight ) {
  236. scene.remove( probesRight );
  237. probesRight.dispose();
  238. }
  239. // Bake both volumes. The `falloff` fades each volume past its
  240. // boundary so the two rooms blend smoothly without bleeding into
  241. // each other.
  242. probesLeft = new LightProbeGrid( 7.8, 4.7, 7.6, resolution, resolution, resolution );
  243. probesLeft.position.set( - 3.9, 2.45, 0 );
  244. probesLeft.falloff = 1.0;
  245. await probesLeft.bake( renderer, scene, { cubemapSize: 32, near: 0.05, far: 20 } );
  246. probesLeft.visible = params.enabled;
  247. probesRight = new LightProbeGrid( 7.8, 4.7, 7.6, resolution, resolution, resolution );
  248. probesRight.position.set( 3.9, 2.45, 0 );
  249. probesRight.falloff = 1.0;
  250. await probesRight.bake( renderer, scene, { cubemapSize: 32, near: 0.05, far: 20 } );
  251. probesRight.visible = params.enabled;
  252. // Add both after baking
  253. scene.add( probesLeft );
  254. scene.add( probesRight );
  255. // Update debug visualization
  256. if ( ! probesHelperLeft ) {
  257. probesHelperLeft = new LightProbeGridHelper( probesLeft );
  258. probesHelperLeft.visible = params.showProbes;
  259. scene.add( probesHelperLeft );
  260. probesHelperRight = new LightProbeGridHelper( probesRight );
  261. probesHelperRight.visible = params.showProbes;
  262. scene.add( probesHelperRight );
  263. } else {
  264. probesHelperLeft.probes = probesLeft;
  265. probesHelperLeft.update();
  266. probesHelperRight.probes = probesRight;
  267. probesHelperRight.update();
  268. }
  269. }
  270. await bake( params.resolution );
  271. // GUI
  272. const gui = renderer.inspector.createParameters( 'Light Probes' );
  273. gui.add( params, 'enabled' ).name( 'GI' ).onChange( ( value ) => {
  274. probesLeft.visible = value;
  275. probesRight.visible = value;
  276. } );
  277. let rebakeTimer = null;
  278. gui.add( params, 'resolution', 2, 12, 1 ).name( 'Resolution' ).onChange( ( value ) => {
  279. // Debounce so a slider drag rebakes once it settles, not every step.
  280. if ( rebakeTimer !== null ) clearTimeout( rebakeTimer );
  281. rebakeTimer = setTimeout( () => bake( value ), 250 );
  282. } );
  283. gui.add( params, 'showProbes' ).name( 'Show Probes' ).onChange( ( value ) => {
  284. probesHelperLeft.visible = value;
  285. probesHelperRight.visible = value;
  286. } );
  287. //
  288. window.addEventListener( 'resize', onWindowResize );
  289. }
  290. function onWindowResize() {
  291. camera.aspect = window.innerWidth / window.innerHeight;
  292. camera.updateProjectionMatrix();
  293. renderer.setSize( window.innerWidth, window.innerHeight );
  294. }
  295. function animate() {
  296. renderer.render( scene, camera );
  297. }
  298. </script>
  299. </body>
  300. </html>
粤ICP备19079148号