webgl_lightprobes_complex.html 10 KB

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