webgpu_lightprobes_sponza.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - light probe volume (Sponza)</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 (Sponza)">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_lightprobes_sponza.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_lightprobes_sponza.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 (Sponza)</span>
  18. </div>
  19. <small>Position-dependent diffuse global illumination in Sponza. WASD to move, mouse to look.</small>
  20. </div>
  21. <progress id="progressBar" value="0" max="100" style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%)"></progress>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.webgpu.js",
  26. "three/webgpu": "../build/three.webgpu.js",
  27. "three/tsl": "../build/three.tsl.js",
  28. "three/addons/": "./jsm/"
  29. }
  30. }
  31. </script>
  32. <script type="module">
  33. import * as THREE from 'three/webgpu';
  34. import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js';
  35. import { Inspector } from 'three/addons/inspector/Inspector.js';
  36. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  37. import { SkyMesh } from 'three/addons/objects/SkyMesh.js';
  38. import { LightProbeGrid } from 'three/addons/lighting/LightProbeGrid.js';
  39. import { LightProbeGridHelper } from 'three/addons/helpers/LightProbeGridHelper.js';
  40. const MODEL_INDEX_URL = 'https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/model-index.json';
  41. const SAMPLE_ASSETS_BASE_URL = 'https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/';
  42. let camera, scene, renderer, controls, timer;
  43. let probes = null, probesHelper = null;
  44. let modelSize = null;
  45. let dirLight = null, sky = null;
  46. const sun = new THREE.Vector3();
  47. const _box = new THREE.Box3();
  48. const _size = new THREE.Vector3();
  49. const _center = new THREE.Vector3();
  50. init();
  51. async function init() {
  52. timer = new THREE.Timer();
  53. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 1000 );
  54. camera.position.set( - 10.25, 4.99, 0.40 );
  55. camera.rotation.set( 1.6505, - 1.5008, 1.6507 );
  56. scene = new THREE.Scene();
  57. sky = new SkyMesh();
  58. sky.scale.setScalar( 450000 );
  59. sky.turbidity.value = 10;
  60. sky.rayleigh.value = 2;
  61. sky.mieCoefficient.value = 0.005;
  62. sky.mieDirectionalG.value = 0.8;
  63. scene.add( sky );
  64. renderer = new THREE.WebGPURenderer( { antialias: true } );
  65. renderer.setPixelRatio( Math.min( window.devicePixelRatio, 1.5 ) );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. renderer.setAnimationLoop( animate );
  68. renderer.shadowMap.enabled = true;
  69. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  70. renderer.toneMappingExposure = 1.0;
  71. renderer.inspector = new Inspector();
  72. document.body.appendChild( renderer.domElement );
  73. await renderer.init();
  74. controls = new FirstPersonControls( camera, renderer.domElement );
  75. controls.movementSpeed = 2.0;
  76. controls.lookSpeed = 0.16;
  77. const progressBar = document.getElementById( 'progressBar' );
  78. const manager = new THREE.LoadingManager();
  79. manager.onProgress = function ( url, loaded, total ) {
  80. progressBar.value = loaded / total * 100;
  81. };
  82. manager.onLoad = function () {
  83. progressBar.remove();
  84. };
  85. const loader = new GLTFLoader( manager );
  86. const modelURL = await getSponzaModelURL();
  87. const gltf = await loader.loadAsync( modelURL );
  88. const model = gltf.scene;
  89. const embeddedLights = [];
  90. model.traverse( ( child ) => {
  91. if ( child.isMesh ) {
  92. child.castShadow = true;
  93. child.receiveShadow = true;
  94. } else if ( child.isLight ) {
  95. embeddedLights.push( child );
  96. }
  97. } );
  98. for ( const light of embeddedLights ) {
  99. if ( light.parent ) light.parent.remove( light );
  100. }
  101. scene.add( model );
  102. _box.setFromObject( model );
  103. modelSize = _box.getSize( _size ).clone();
  104. const modelCenter = _box.getCenter( _center ).clone();
  105. const targetY = modelCenter.y + modelSize.y * 0.2;
  106. const lightBaseDistance = Math.max( modelSize.x, modelSize.z );
  107. const probeFar = Math.max( modelSize.x, modelSize.y, modelSize.z ) * 2.0;
  108. let rebakeTimer = null;
  109. let isBaking = false;
  110. let bakeQueued = false;
  111. dirLight = new THREE.DirectionalLight( 0xfff2dc, 100.0 );
  112. dirLight.target.position.set( modelCenter.x, targetY, modelCenter.z );
  113. scene.add( dirLight.target );
  114. dirLight.castShadow = true;
  115. dirLight.shadow.mapSize.setScalar( 2048 );
  116. const shadowExtent = Math.max( modelSize.x, modelSize.z ) * 0.7;
  117. dirLight.shadow.camera.left = - shadowExtent;
  118. dirLight.shadow.camera.right = shadowExtent;
  119. dirLight.shadow.camera.top = shadowExtent;
  120. dirLight.shadow.camera.bottom = - shadowExtent;
  121. dirLight.shadow.camera.near = 0.1;
  122. dirLight.shadow.camera.far = modelSize.y * 4.0;
  123. scene.add( dirLight );
  124. const params = {
  125. enabled: true,
  126. showProbes: false,
  127. probeSize: 0.2,
  128. boundsX: - 0.5,
  129. boundsY: 6,
  130. boundsZ: - 0.3,
  131. sizeX: 21,
  132. sizeY: 11,
  133. sizeZ: 9,
  134. countX: 10,
  135. countY: 7,
  136. countZ: 7,
  137. bounces: 1,
  138. lightAzimuth: - 45,
  139. lightElevation: 55,
  140. lightIntensity: 100.0,
  141. shadows: true
  142. };
  143. function updateLightPosition() {
  144. const azimuth = THREE.MathUtils.degToRad( params.lightAzimuth );
  145. const elevation = THREE.MathUtils.degToRad( params.lightElevation );
  146. const radius = lightBaseDistance;
  147. const horizontal = Math.cos( elevation ) * radius;
  148. const vertical = Math.sin( elevation ) * radius;
  149. dirLight.position.set(
  150. modelCenter.x + Math.cos( azimuth ) * horizontal,
  151. targetY + vertical,
  152. modelCenter.z + Math.sin( azimuth ) * horizontal
  153. );
  154. dirLight.target.position.set( modelCenter.x, targetY, modelCenter.z );
  155. dirLight.target.updateMatrixWorld();
  156. const phi = THREE.MathUtils.degToRad( 90 - params.lightElevation );
  157. const theta = THREE.MathUtils.degToRad( params.lightAzimuth );
  158. sun.setFromSphericalCoords( 1, phi, theta );
  159. sky.sunPosition.value.copy( sun );
  160. }
  161. function scheduleRebake() {
  162. if ( rebakeTimer !== null ) clearTimeout( rebakeTimer );
  163. rebakeTimer = setTimeout( () => {
  164. rebakeTimer = null;
  165. bakeWithSettings();
  166. }, 250 );
  167. }
  168. async function bakeWithSettings() {
  169. if ( isBaking ) {
  170. bakeQueued = true;
  171. return;
  172. }
  173. isBaking = true;
  174. do {
  175. bakeQueued = false;
  176. if ( probes ) {
  177. scene.remove( probes );
  178. probes.dispose();
  179. }
  180. probes = new LightProbeGrid(
  181. params.sizeX, params.sizeY, params.sizeZ,
  182. params.countX, params.countY, params.countZ
  183. );
  184. probes.position.set( params.boundsX, params.boundsY, params.boundsZ );
  185. // Add to the scene before baking so bounce passes can sample the prior pass's atlas.
  186. scene.add( probes );
  187. // Hide the helper spheres so they don't appear in the cubemap captures.
  188. if ( probesHelper ) probesHelper.visible = false;
  189. await probes.bake( renderer, scene, {
  190. cubemapSize: 32,
  191. near: 0.05,
  192. far: probeFar,
  193. bounces: params.bounces
  194. } );
  195. probes.visible = params.enabled;
  196. if ( ! probesHelper ) {
  197. probesHelper = new LightProbeGridHelper( probes, params.probeSize );
  198. probesHelper.visible = params.showProbes;
  199. scene.add( probesHelper );
  200. } else {
  201. probesHelper.probes = probes;
  202. probesHelper.update();
  203. probesHelper.visible = params.showProbes;
  204. }
  205. } while ( bakeQueued );
  206. isBaking = false;
  207. }
  208. updateLightPosition();
  209. const gui = renderer.inspector.createParameters( 'Light Probes' );
  210. gui.add( params, 'enabled' ).name( 'GI' ).onChange( ( value ) => {
  211. if ( probes ) probes.visible = value;
  212. } );
  213. gui.add( params, 'lightAzimuth', - 180, 180, 1 ).name( 'Light Azimuth' ).onChange( () => {
  214. updateLightPosition();
  215. scheduleRebake();
  216. } );
  217. gui.add( params, 'lightElevation', 5, 85, 1 ).name( 'Light Elevation' ).onChange( () => {
  218. updateLightPosition();
  219. scheduleRebake();
  220. } );
  221. gui.add( params, 'lightIntensity', 0, 100, 0.1 ).name( 'Light Intensity' ).onChange( ( value ) => {
  222. dirLight.intensity = value;
  223. scheduleRebake();
  224. } );
  225. gui.add( params, 'shadows' ).name( 'Shadows' ).onChange( ( value ) => {
  226. setShadowsEnabled( value );
  227. scheduleRebake();
  228. } );
  229. gui.add( params, 'countX', 2, 32, 1 ).name( 'Probes X' ).onChange( scheduleRebake );
  230. gui.add( params, 'countY', 2, 16, 1 ).name( 'Probes Y' ).onChange( scheduleRebake );
  231. gui.add( params, 'countZ', 2, 16, 1 ).name( 'Probes Z' ).onChange( scheduleRebake );
  232. gui.add( params, 'bounces', 0, 2, 1 ).name( 'Bounces' ).onChange( scheduleRebake );
  233. gui.add( params, 'showProbes' ).name( 'Show Probes' ).onChange( ( value ) => {
  234. if ( probesHelper ) probesHelper.visible = value;
  235. } );
  236. gui.add( params, 'probeSize', 0.05, 2.0, 0.05 ).name( 'Probe Size' ).onChange( ( value ) => {
  237. if ( probesHelper ) {
  238. scene.remove( probesHelper );
  239. probesHelper.dispose();
  240. probesHelper = new LightProbeGridHelper( probes, value );
  241. probesHelper.visible = params.showProbes;
  242. scene.add( probesHelper );
  243. }
  244. } );
  245. gui.add( { log: () => {
  246. console.log( 'position:', camera.position.x.toFixed( 2 ), camera.position.y.toFixed( 2 ), camera.position.z.toFixed( 2 ) );
  247. console.log( 'rotation:', camera.rotation.x.toFixed( 4 ), camera.rotation.y.toFixed( 4 ), camera.rotation.z.toFixed( 4 ) );
  248. } }, 'log' ).name( 'Log Camera' );
  249. setShadowsEnabled( params.shadows );
  250. await bakeWithSettings();
  251. window.addEventListener( 'resize', onWindowResize );
  252. }
  253. async function getSponzaModelURL() {
  254. const response = await fetch( MODEL_INDEX_URL );
  255. const models = await response.json();
  256. const sponzaInfo = models.find( ( model ) => model.name === 'Sponza' );
  257. if ( ! sponzaInfo ) {
  258. throw new Error( 'Sponza entry was not found in the glTF sample model index.' );
  259. }
  260. const variants = sponzaInfo.variants || {};
  261. const variantName = variants[ 'glTF-Binary' ] || variants[ 'glTF' ] || variants[ 'glTF-Embedded' ] || Object.values( variants )[ 0 ];
  262. if ( ! variantName ) {
  263. throw new Error( 'Sponza has no supported glTF variant in the model index.' );
  264. }
  265. const variantFolder = variantName.endsWith( '.glb' ) ? 'glTF-Binary' : 'glTF';
  266. return `${ SAMPLE_ASSETS_BASE_URL }${ sponzaInfo.name }/${ variantFolder }/${ variantName }`;
  267. }
  268. function setShadowsEnabled( enabled ) {
  269. if ( ! renderer || ! dirLight ) return;
  270. renderer.shadowMap.enabled = enabled;
  271. dirLight.castShadow = enabled;
  272. }
  273. function onWindowResize() {
  274. camera.aspect = window.innerWidth / window.innerHeight;
  275. camera.updateProjectionMatrix();
  276. renderer.setSize( window.innerWidth, window.innerHeight );
  277. }
  278. function animate( timestamp ) {
  279. timer.update( timestamp );
  280. controls.update( timer.getDelta() );
  281. renderer.render( scene, camera );
  282. }
  283. </script>
  284. </body>
  285. </html>
粤ICP备19079148号