webgpu_shadowmap_progressive.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - progressive lightmap accumulation</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="example.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  13. <div class="title-wrapper">
  14. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Progressive Lightmaps</span>
  15. </div>
  16. <small>
  17. By <a href="https://github.com/zalo" target="_blank" rel="noopener">zalo</a>. Inspired by <a href="http://madebyevan.com/shaders/lightmap/" target="_blank" rel="noopener">evanw's Lightmap Generation</a>.
  18. </small>
  19. </div>
  20. <script type="importmap">
  21. {
  22. "imports": {
  23. "three": "../build/three.webgpu.js",
  24. "three/webgpu": "../build/three.webgpu.js",
  25. "three/tsl": "../build/three.tsl.js",
  26. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three/webgpu';
  32. import { Inspector } from 'three/addons/inspector/Inspector.js';
  33. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  34. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  35. import { TransformControls } from 'three/addons/controls/TransformControls.js';
  36. import { ProgressiveLightMap } from 'three/addons/misc/ProgressiveLightMapGPU.js';
  37. // ShadowMap + LightMap Res and Number of Directional Lights
  38. const shadowMapRes = 1024, lightMapRes = 1024, lightCount = 4;
  39. let camera, scene, renderer, controls, control, control2,
  40. object = new THREE.Mesh(), lightOrigin = null, progressiveSurfacemap;
  41. const dirLights = [], lightmapObjects = [];
  42. const params = { 'Enable': true, 'Blur Edges': true, 'Blend Window': 200,
  43. 'Light Radius': 50, 'Ambient Weight': 0.5, 'Debug Lightmap': false };
  44. init();
  45. createGUI();
  46. function init() {
  47. // renderer
  48. renderer = new THREE.WebGPURenderer( { antialias: true } );
  49. renderer.setPixelRatio( window.devicePixelRatio );
  50. renderer.setSize( window.innerWidth, window.innerHeight );
  51. renderer.setAnimationLoop( animate );
  52. renderer.shadowMap.enabled = true;
  53. renderer.inspector = new Inspector();
  54. document.body.appendChild( renderer.domElement );
  55. // camera
  56. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  57. camera.position.set( 0, 100, 200 );
  58. camera.name = 'Camera';
  59. // scene
  60. scene = new THREE.Scene();
  61. scene.background = new THREE.Color( 0x949494 );
  62. scene.fog = new THREE.Fog( 0x949494, 1000, 3000 );
  63. // progressive lightmap
  64. progressiveSurfacemap = new ProgressiveLightMap( renderer, lightMapRes );
  65. // directional lighting "origin"
  66. lightOrigin = new THREE.Group();
  67. lightOrigin.position.set( 60, 150, 100 );
  68. scene.add( lightOrigin );
  69. // transform gizmo
  70. control = new TransformControls( camera, renderer.domElement );
  71. control.addEventListener( 'dragging-changed', ( event ) => {
  72. controls.enabled = ! event.value;
  73. } );
  74. control.attach( lightOrigin );
  75. scene.add( control.getHelper() );
  76. // create 8 directional lights to speed up the convergence
  77. for ( let l = 0; l < lightCount; l ++ ) {
  78. const dirLight = new THREE.DirectionalLight( 0xffffff, Math.PI / lightCount );
  79. dirLight.name = 'Dir. Light ' + l;
  80. dirLight.position.set( 200, 200, 200 );
  81. dirLight.castShadow = true;
  82. dirLight.shadow.camera.near = 100;
  83. dirLight.shadow.camera.far = 5000;
  84. dirLight.shadow.camera.right = 150;
  85. dirLight.shadow.camera.left = - 150;
  86. dirLight.shadow.camera.top = 150;
  87. dirLight.shadow.camera.bottom = - 150;
  88. dirLight.shadow.mapSize.width = shadowMapRes;
  89. dirLight.shadow.mapSize.height = shadowMapRes;
  90. dirLight.shadow.bias = - 0.001;
  91. lightmapObjects.push( dirLight );
  92. dirLights.push( dirLight );
  93. }
  94. // ground
  95. const groundMesh = new THREE.Mesh(
  96. new THREE.PlaneGeometry( 600, 600 ),
  97. new THREE.MeshPhongMaterial( { color: 0xffffff, depthWrite: true } )
  98. );
  99. groundMesh.position.y = - 0.1;
  100. groundMesh.rotation.x = - Math.PI / 2;
  101. groundMesh.name = 'Ground Mesh';
  102. lightmapObjects.push( groundMesh );
  103. scene.add( groundMesh );
  104. // model
  105. function loadModel() {
  106. object.traverse( function ( child ) {
  107. if ( child.isMesh ) {
  108. child.name = 'Loaded Mesh';
  109. child.castShadow = true;
  110. child.receiveShadow = true;
  111. child.material = new THREE.MeshPhongMaterial();
  112. // This adds the model to the lightmap
  113. lightmapObjects.push( child );
  114. progressiveSurfacemap.addObjectsToLightMap( lightmapObjects );
  115. } else {
  116. child.layers.disableAll(); // Disable Rendering for this
  117. }
  118. } );
  119. scene.add( object );
  120. object.scale.set( 2, 2, 2 );
  121. object.position.set( 0, - 16, 0 );
  122. control2 = new TransformControls( camera, renderer.domElement );
  123. control2.addEventListener( 'dragging-changed', ( event ) => {
  124. controls.enabled = ! event.value;
  125. } );
  126. control2.attach( object );
  127. scene.add( control2.getHelper() );
  128. const lightTarget = new THREE.Group();
  129. lightTarget.position.set( 0, 20, 0 );
  130. for ( let l = 0; l < dirLights.length; l ++ ) {
  131. dirLights[ l ].target = lightTarget;
  132. }
  133. object.add( lightTarget );
  134. }
  135. const manager = new THREE.LoadingManager( loadModel );
  136. const loader = new GLTFLoader( manager );
  137. loader.load( 'models/gltf/ShadowmappableMesh.glb', function ( obj ) {
  138. object = obj.scene.children[ 0 ];
  139. } );
  140. // controls
  141. controls = new OrbitControls( camera, renderer.domElement );
  142. controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
  143. controls.dampingFactor = 0.05;
  144. controls.screenSpacePanning = true;
  145. controls.minDistance = 100;
  146. controls.maxDistance = 500;
  147. controls.maxPolarAngle = Math.PI / 1.5;
  148. controls.target.set( 0, 100, 0 );
  149. window.addEventListener( 'resize', onWindowResize );
  150. }
  151. function createGUI() {
  152. const gui = renderer.inspector.createParameters( 'Accumulation Settings' );
  153. gui.add( params, 'Enable' );
  154. gui.add( params, 'Blur Edges' );
  155. gui.add( params, 'Blend Window', 1, 500, 1 );
  156. gui.add( params, 'Light Radius', 0, 200, 10 );
  157. gui.add( params, 'Ambient Weight', 0, 1, 0.1 );
  158. gui.add( params, 'Debug Lightmap' ).onChange( ( value ) => progressiveSurfacemap.showDebugLightmap( value ) );
  159. }
  160. function onWindowResize() {
  161. camera.aspect = window.innerWidth / window.innerHeight;
  162. camera.updateProjectionMatrix();
  163. renderer.setSize( window.innerWidth, window.innerHeight );
  164. }
  165. function animate() {
  166. // Update the inertia on the orbit controls
  167. controls.update();
  168. // Accumulate Surface Maps
  169. if ( params[ 'Enable' ] ) {
  170. progressiveSurfacemap.update( camera, params[ 'Blend Window' ], params[ 'Blur Edges' ] );
  171. }
  172. // Manually Update the Directional Lights
  173. for ( let l = 0; l < dirLights.length; l ++ ) {
  174. // Sometimes they will be sampled from the target direction
  175. // Sometimes they will be uniformly sampled from the upper hemisphere
  176. if ( Math.random() > params[ 'Ambient Weight' ] ) {
  177. dirLights[ l ].position.set(
  178. lightOrigin.position.x + ( Math.random() * params[ 'Light Radius' ] ),
  179. lightOrigin.position.y + ( Math.random() * params[ 'Light Radius' ] ),
  180. lightOrigin.position.z + ( Math.random() * params[ 'Light Radius' ] ) );
  181. } else {
  182. // Uniform Hemispherical Surface Distribution for Ambient Occlusion
  183. const lambda = Math.acos( 2 * Math.random() - 1 ) - ( 3.14159 / 2.0 );
  184. const phi = 2 * 3.14159 * Math.random();
  185. dirLights[ l ].position.set(
  186. ( ( Math.cos( lambda ) * Math.cos( phi ) ) * 300 ) + object.position.x,
  187. Math.abs( ( Math.cos( lambda ) * Math.sin( phi ) ) * 300 ) + object.position.y + 20,
  188. ( Math.sin( lambda ) * 300 ) + object.position.z
  189. );
  190. }
  191. }
  192. // Render Scene
  193. renderer.render( scene, camera );
  194. }
  195. </script>
  196. </body>
  197. </html>
粤ICP备19079148号