webgpu_shadowmap_progressive.html 8.0 KB

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