webgpu_shadowmap_progressive.html 8.5 KB

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