webgl_lightprobes.html 6.8 KB

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