webgpu_lightprobes.html 7.0 KB

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