webgl_lightprobes.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - light probe volume</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<br/>
  12. Position-dependent diffuse global illumination via L1 SH probe grid
  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 probes, probesHelper;
  30. init();
  31. async function init() {
  32. // Camera
  33. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  34. camera.position.set( 0, 2.5, 8 );
  35. // Scene
  36. scene = new THREE.Scene();
  37. scene.background = new THREE.Color( 0x111111 );
  38. // Cornell box
  39. const wallMaterial = new THREE.MeshStandardMaterial( { color: 0xcccccc } );
  40. const redMaterial = new THREE.MeshStandardMaterial( { color: 0xff0000 } );
  41. const greenMaterial = new THREE.MeshStandardMaterial( { color: 0x00ff00 } );
  42. // Floor
  43. const floor = new THREE.Mesh( new THREE.PlaneGeometry( 6, 6 ), wallMaterial );
  44. floor.rotation.x = - Math.PI / 2;
  45. floor.receiveShadow = true;
  46. scene.add( floor );
  47. // Ceiling
  48. const ceiling = new THREE.Mesh( new THREE.PlaneGeometry( 6, 6 ), wallMaterial );
  49. ceiling.rotation.x = Math.PI / 2;
  50. ceiling.position.y = 5;
  51. ceiling.receiveShadow = true;
  52. scene.add( ceiling );
  53. // Back wall
  54. const backWall = new THREE.Mesh( new THREE.PlaneGeometry( 6, 5 ), wallMaterial );
  55. backWall.position.set( 0, 2.5, - 3 );
  56. backWall.receiveShadow = true;
  57. scene.add( backWall );
  58. // Front wall
  59. const frontWall = new THREE.Mesh( new THREE.PlaneGeometry( 6, 5 ), wallMaterial );
  60. frontWall.rotation.y = Math.PI;
  61. frontWall.position.set( 0, 2.5, 3 );
  62. frontWall.receiveShadow = true;
  63. scene.add( frontWall );
  64. // Left wall (red)
  65. const leftWall = new THREE.Mesh( new THREE.PlaneGeometry( 6, 5 ), redMaterial );
  66. leftWall.rotation.y = Math.PI / 2;
  67. leftWall.position.set( - 3, 2.5, 0 );
  68. leftWall.receiveShadow = true;
  69. scene.add( leftWall );
  70. // Right wall (green)
  71. const rightWall = new THREE.Mesh( new THREE.PlaneGeometry( 6, 5 ), greenMaterial );
  72. rightWall.rotation.y = - Math.PI / 2;
  73. rightWall.position.set( 3, 2.5, 0 );
  74. rightWall.receiveShadow = true;
  75. scene.add( rightWall );
  76. // Objects inside the box
  77. const objectMaterial = new THREE.MeshStandardMaterial( { color: 0xeeeeee } );
  78. // Tall box
  79. const tallBox = new THREE.Mesh( new THREE.BoxGeometry( 1.2, 2.5, 1.2 ), objectMaterial );
  80. tallBox.position.set( - 0.8, 1.25, - 0.8 );
  81. tallBox.rotation.y = Math.PI / 8;
  82. tallBox.castShadow = true;
  83. tallBox.receiveShadow = true;
  84. scene.add( tallBox );
  85. // Short box
  86. const shortBox = new THREE.Mesh( new THREE.BoxGeometry( 1.2, 1.2, 1.2 ), objectMaterial );
  87. shortBox.position.set( 1, 0.6, 0.5 );
  88. shortBox.rotation.y = - Math.PI / 6;
  89. shortBox.castShadow = true;
  90. shortBox.receiveShadow = true;
  91. scene.add( shortBox );
  92. // Sphere (to show smooth GI variation)
  93. const sphere = new THREE.Mesh( new THREE.SphereGeometry( 0.5, 32, 32 ), objectMaterial.clone() );
  94. sphere.position.set( 1, 1.9, 0.5 );
  95. sphere.castShadow = true;
  96. sphere.receiveShadow = true;
  97. scene.add( sphere );
  98. // Light
  99. const light = new THREE.PointLight( 0xffffff, 40 );
  100. light.position.set( 0, 4.5, 0 );
  101. light.castShadow = true;
  102. light.shadow.mapSize.setScalar( 256 );
  103. light.shadow.radius = 10;
  104. light.shadow.normalBias = - 0.02;
  105. scene.add( light );
  106. // Dim ambient to see GI effect better
  107. const ambient = new THREE.AmbientLight( 0xffffff, 0.05 );
  108. scene.add( ambient );
  109. // Renderer
  110. renderer = new THREE.WebGLRenderer( { antialias: true } );
  111. renderer.setPixelRatio( window.devicePixelRatio );
  112. renderer.setSize( window.innerWidth, window.innerHeight );
  113. renderer.setAnimationLoop( animate );
  114. renderer.shadowMap.enabled = true;
  115. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  116. renderer.toneMappingExposure = 1.0;
  117. document.body.appendChild( renderer.domElement );
  118. // Controls
  119. controls = new OrbitControls( camera, renderer.domElement );
  120. controls.target.set( 0, 2.5, 0 );
  121. controls.update();
  122. // Bake light probe volume
  123. async function bakeWithResolution( resolution ) {
  124. if ( probes ) {
  125. scene.remove( probes );
  126. probes.dispose();
  127. }
  128. probes = new LightProbeGrid( 5.6, 4.7, 5.6, resolution, resolution, resolution );
  129. probes.position.set( 0, 2.45, 0 );
  130. probes.bake( renderer, scene, { cubemapSize: 32, near: 0.05, far: 20 } );
  131. probes.visible = params.enabled;
  132. scene.add( probes );
  133. // Update debug visualization
  134. if ( ! probesHelper ) {
  135. probesHelper = new LightProbeGridHelper( probes );
  136. probesHelper.visible = params.showProbes;
  137. scene.add( probesHelper );
  138. } else {
  139. probesHelper.probes = probes;
  140. probesHelper.update();
  141. }
  142. }
  143. const params = {
  144. enabled: true,
  145. showProbes: false,
  146. resolution: 6
  147. };
  148. await bakeWithResolution( params.resolution );
  149. // GUI
  150. const gui = new GUI();
  151. gui.add( params, 'enabled' ).name( 'GI' ).onChange( ( value ) => {
  152. probes.visible = value;
  153. } );
  154. gui.add( params, 'resolution', 2, 12, 1 ).name( 'Resolution' ).onFinishChange( ( value ) => {
  155. bakeWithResolution( value );
  156. } );
  157. gui.add( params, 'showProbes' ).name( 'Show Probes' ).onChange( ( value ) => {
  158. probesHelper.visible = value;
  159. } );
  160. //
  161. window.addEventListener( 'resize', onWindowResize );
  162. }
  163. function onWindowResize() {
  164. camera.aspect = window.innerWidth / window.innerHeight;
  165. camera.updateProjectionMatrix();
  166. renderer.setSize( window.innerWidth, window.innerHeight );
  167. }
  168. function animate() {
  169. renderer.render( scene, camera );
  170. }
  171. </script>
  172. </body>
  173. </html>
粤ICP备19079148号