webgl_lightprobes_complex.html 11 KB

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