webgl_lightprobes_sponza_baked.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - light probe volume (Sponza, baked)</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 (Sponza, baked)">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_lightprobes_sponza_baked.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_lightprobes_sponza_baked.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 (Sponza, baked)<br/>
  16. WASD to move, mouse to look
  17. </div>
  18. <progress id="progressBar" value="0" max="100" style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%)"></progress>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.module.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three';
  29. import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js';
  30. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  31. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  32. import { Sky } from 'three/addons/objects/Sky.js';
  33. import { LightProbeGrid } from 'three/addons/lighting/LightProbeGrid.js';
  34. import { LightProbeGridHelper } from 'three/addons/helpers/LightProbeGridHelper.js';
  35. const MODEL_INDEX_URL = 'https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/model-index.json';
  36. const SAMPLE_ASSETS_BASE_URL = 'https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/';
  37. const PROBES_URL = './probes/sponza-lightprobes-bake.json';
  38. // Light settings that matched the bake.
  39. const LIGHT_AZIMUTH = - 165;
  40. const LIGHT_ELEVATION = 55;
  41. const LIGHT_INTENSITY = 100.0;
  42. let camera, scene, renderer, controls, timer;
  43. const _box = new THREE.Box3();
  44. const _size = new THREE.Vector3();
  45. const _center = new THREE.Vector3();
  46. init();
  47. async function init() {
  48. timer = new THREE.Timer();
  49. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 1000 );
  50. camera.position.set( - 10.25, 4.99, 0.40 );
  51. camera.rotation.set( 1.6505, - 1.5008, 1.6507 );
  52. scene = new THREE.Scene();
  53. const sky = new Sky();
  54. sky.scale.setScalar( 450000 );
  55. scene.add( sky );
  56. const skyUniforms = sky.material.uniforms;
  57. skyUniforms[ 'turbidity' ].value = 10;
  58. skyUniforms[ 'rayleigh' ].value = 2;
  59. skyUniforms[ 'mieCoefficient' ].value = 0.005;
  60. skyUniforms[ 'mieDirectionalG' ].value = 0.8;
  61. renderer = new THREE.WebGLRenderer( { antialias: true } );
  62. renderer.setPixelRatio( Math.min( window.devicePixelRatio, 1.5 ) );
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. renderer.setAnimationLoop( animate );
  65. renderer.shadowMap.enabled = true;
  66. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  67. renderer.toneMappingExposure = 1.0;
  68. document.body.appendChild( renderer.domElement );
  69. controls = new FirstPersonControls( camera, renderer.domElement );
  70. controls.movementSpeed = 2.0;
  71. controls.lookSpeed = 0.16;
  72. const progressBar = document.getElementById( 'progressBar' );
  73. const manager = new THREE.LoadingManager();
  74. manager.onProgress = function ( url, loaded, total ) {
  75. progressBar.value = loaded / total * 100;
  76. };
  77. manager.onLoad = function () {
  78. progressBar.remove();
  79. };
  80. const loader = new GLTFLoader( manager );
  81. const modelURL = await getSponzaModelURL();
  82. const [ gltf, probesJSON ] = await Promise.all( [
  83. loader.loadAsync( modelURL ),
  84. fetch( PROBES_URL ).then( ( r ) => r.json() )
  85. ] );
  86. const model = gltf.scene;
  87. const embeddedLights = [];
  88. model.traverse( ( child ) => {
  89. if ( child.isMesh ) {
  90. child.castShadow = true;
  91. child.receiveShadow = true;
  92. } else if ( child.isLight ) {
  93. embeddedLights.push( child );
  94. }
  95. } );
  96. for ( const light of embeddedLights ) {
  97. if ( light.parent ) light.parent.remove( light );
  98. }
  99. scene.add( model );
  100. _box.setFromObject( model );
  101. const modelSize = _box.getSize( _size ).clone();
  102. const modelCenter = _box.getCenter( _center ).clone();
  103. const targetY = modelCenter.y + modelSize.y * 0.2;
  104. const lightBaseDistance = Math.max( modelSize.x, modelSize.z );
  105. const azimuth = THREE.MathUtils.degToRad( LIGHT_AZIMUTH );
  106. const elevation = THREE.MathUtils.degToRad( LIGHT_ELEVATION );
  107. const horizontal = Math.cos( elevation ) * lightBaseDistance;
  108. const vertical = Math.sin( elevation ) * lightBaseDistance;
  109. const dirLight = new THREE.DirectionalLight( 0xfff2dc, LIGHT_INTENSITY );
  110. dirLight.position.set(
  111. modelCenter.x + Math.cos( azimuth ) * horizontal,
  112. targetY + vertical,
  113. modelCenter.z + Math.sin( azimuth ) * horizontal
  114. );
  115. dirLight.target.position.set( modelCenter.x, targetY, modelCenter.z );
  116. scene.add( dirLight.target );
  117. dirLight.castShadow = true;
  118. dirLight.shadow.mapSize.setScalar( 2048 );
  119. const shadowExtent = Math.max( modelSize.x, modelSize.z ) * 0.7;
  120. dirLight.shadow.camera.left = - shadowExtent;
  121. dirLight.shadow.camera.right = shadowExtent;
  122. dirLight.shadow.camera.top = shadowExtent;
  123. dirLight.shadow.camera.bottom = - shadowExtent;
  124. dirLight.shadow.camera.near = 0.1;
  125. dirLight.shadow.camera.far = modelSize.y * 4.0;
  126. scene.add( dirLight );
  127. const sun = new THREE.Vector3();
  128. const phi = THREE.MathUtils.degToRad( 90 - LIGHT_ELEVATION );
  129. const theta = THREE.MathUtils.degToRad( LIGHT_AZIMUTH );
  130. sun.setFromSphericalCoords( 1, phi, theta );
  131. sky.material.uniforms[ 'sunPosition' ].value.copy( sun );
  132. const probes = LightProbeGrid.fromJSON( probesJSON );
  133. scene.add( probes );
  134. const params = {
  135. enabled: true,
  136. showProbes: false
  137. };
  138. const probesHelper = new LightProbeGridHelper( probes, 0.1 );
  139. probesHelper.visible = params.showProbes;
  140. scene.add( probesHelper );
  141. const gui = new GUI();
  142. gui.add( params, 'enabled' ).name( 'GI' ).onChange( ( value ) => {
  143. probes.visible = value;
  144. } );
  145. gui.add( params, 'showProbes' ).name( 'Show Probes' ).onChange( ( value ) => {
  146. probesHelper.visible = value;
  147. } );
  148. window.addEventListener( 'resize', onWindowResize );
  149. }
  150. async function getSponzaModelURL() {
  151. const response = await fetch( MODEL_INDEX_URL );
  152. const models = await response.json();
  153. const sponzaInfo = models.find( ( model ) => model.name === 'Sponza' );
  154. if ( ! sponzaInfo ) {
  155. throw new Error( 'Sponza entry was not found in the glTF sample model index.' );
  156. }
  157. const variants = sponzaInfo.variants || {};
  158. const variantName = variants[ 'glTF-Binary' ] || variants[ 'glTF' ] || variants[ 'glTF-Embedded' ] || Object.values( variants )[ 0 ];
  159. if ( ! variantName ) {
  160. throw new Error( 'Sponza has no supported glTF variant in the model index.' );
  161. }
  162. const variantFolder = variantName.endsWith( '.glb' ) ? 'glTF-Binary' : 'glTF';
  163. return `${ SAMPLE_ASSETS_BASE_URL }${ sponzaInfo.name }/${ variantFolder }/${ variantName }`;
  164. }
  165. function onWindowResize() {
  166. camera.aspect = window.innerWidth / window.innerHeight;
  167. camera.updateProjectionMatrix();
  168. renderer.setSize( window.innerWidth, window.innerHeight );
  169. }
  170. function animate( timestamp ) {
  171. timer.update( timestamp );
  172. controls.update( timer.getDelta() );
  173. renderer.render( scene, camera );
  174. }
  175. </script>
  176. </body>
  177. </html>
粤ICP备19079148号