webgl_shadowmap_progressive.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - 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.module.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  26. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import { TransformControls } from 'three/addons/controls/TransformControls.js';
  29. import { ProgressiveLightMap } from 'three/addons/misc/ProgressiveLightMap.js';
  30. // ShadowMap + LightMap Res and Number of Directional Lights
  31. const shadowMapRes = 512, lightMapRes = 1024, lightCount = 8;
  32. let camera, scene, renderer, controls, control, control2,
  33. object = new THREE.Mesh(), lightOrigin = null, progressiveSurfacemap;
  34. const dirLights = [], lightmapObjects = [];
  35. const params = { 'Enable': true, 'Blur Edges': true, 'Blend Window': 200,
  36. 'Light Radius': 50, 'Ambient Weight': 0.5, 'Debug Lightmap': false };
  37. init();
  38. createGUI();
  39. function init() {
  40. // renderer
  41. renderer = new THREE.WebGLRenderer( { antialias: true } );
  42. renderer.setPixelRatio( window.devicePixelRatio );
  43. renderer.setSize( window.innerWidth, window.innerHeight );
  44. renderer.setAnimationLoop( animate );
  45. renderer.shadowMap.enabled = true;
  46. document.body.appendChild( renderer.domElement );
  47. // camera
  48. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  49. camera.position.set( 0, 100, 200 );
  50. camera.name = 'Camera';
  51. // scene
  52. scene = new THREE.Scene();
  53. scene.background = new THREE.Color( 0x949494 );
  54. scene.fog = new THREE.Fog( 0x949494, 1000, 3000 );
  55. // progressive lightmap
  56. progressiveSurfacemap = new ProgressiveLightMap( renderer, lightMapRes );
  57. // directional lighting "origin"
  58. lightOrigin = new THREE.Group();
  59. lightOrigin.position.set( 60, 150, 100 );
  60. scene.add( lightOrigin );
  61. // transform gizmo
  62. control = new TransformControls( camera, renderer.domElement );
  63. control.addEventListener( 'dragging-changed', ( event ) => {
  64. controls.enabled = ! event.value;
  65. } );
  66. control.attach( lightOrigin );
  67. scene.add( control.getHelper() );
  68. // create 8 directional lights to speed up the convergence
  69. for ( let l = 0; l < lightCount; l ++ ) {
  70. const dirLight = new THREE.DirectionalLight( 0xffffff, 1.0 / lightCount );
  71. dirLight.name = 'Dir. Light ' + l;
  72. dirLight.position.set( 200, 200, 200 );
  73. dirLight.castShadow = true;
  74. dirLight.shadow.camera.near = 100;
  75. dirLight.shadow.camera.far = 5000;
  76. dirLight.shadow.camera.right = 150;
  77. dirLight.shadow.camera.left = - 150;
  78. dirLight.shadow.camera.top = 150;
  79. dirLight.shadow.camera.bottom = - 150;
  80. dirLight.shadow.mapSize.width = shadowMapRes;
  81. dirLight.shadow.mapSize.height = shadowMapRes;
  82. lightmapObjects.push( dirLight );
  83. dirLights.push( dirLight );
  84. }
  85. // ground
  86. const groundMesh = new THREE.Mesh(
  87. new THREE.PlaneGeometry( 600, 600 ),
  88. new THREE.MeshPhongMaterial( { color: 0xffffff, depthWrite: true } )
  89. );
  90. groundMesh.position.y = - 0.1;
  91. groundMesh.rotation.x = - Math.PI / 2;
  92. groundMesh.name = 'Ground Mesh';
  93. lightmapObjects.push( groundMesh );
  94. scene.add( groundMesh );
  95. // model
  96. function loadModel() {
  97. object.traverse( function ( child ) {
  98. if ( child.isMesh ) {
  99. child.name = 'Loaded Mesh';
  100. child.castShadow = true;
  101. child.receiveShadow = true;
  102. child.material = new THREE.MeshPhongMaterial();
  103. // This adds the model to the lightmap
  104. lightmapObjects.push( child );
  105. progressiveSurfacemap.addObjectsToLightMap( lightmapObjects );
  106. } else {
  107. child.layers.disableAll(); // Disable Rendering for this
  108. }
  109. } );
  110. scene.add( object );
  111. object.scale.set( 2, 2, 2 );
  112. object.position.set( 0, - 16, 0 );
  113. control2 = new TransformControls( camera, renderer.domElement );
  114. control2.addEventListener( 'dragging-changed', ( event ) => {
  115. controls.enabled = ! event.value;
  116. } );
  117. control2.attach( object );
  118. scene.add( control2.getHelper() );
  119. const lightTarget = new THREE.Group();
  120. lightTarget.position.set( 0, 20, 0 );
  121. for ( let l = 0; l < dirLights.length; l ++ ) {
  122. dirLights[ l ].target = lightTarget;
  123. }
  124. object.add( lightTarget );
  125. }
  126. const manager = new THREE.LoadingManager( loadModel );
  127. const loader = new GLTFLoader( manager );
  128. loader.load( 'models/gltf/ShadowmappableMesh.glb', function ( obj ) {
  129. object = obj.scene.children[ 0 ];
  130. } );
  131. // controls
  132. controls = new OrbitControls( camera, renderer.domElement );
  133. controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
  134. controls.dampingFactor = 0.05;
  135. controls.screenSpacePanning = true;
  136. controls.minDistance = 100;
  137. controls.maxDistance = 500;
  138. controls.maxPolarAngle = Math.PI / 1.5;
  139. controls.target.set( 0, 100, 0 );
  140. window.addEventListener( 'resize', onWindowResize );
  141. }
  142. function createGUI() {
  143. const gui = new GUI( { title: 'Accumulation Settings' } );
  144. gui.add( params, 'Enable' );
  145. gui.add( params, 'Blur Edges' );
  146. gui.add( params, 'Blend Window', 1, 500 ).step( 1 );
  147. gui.add( params, 'Light Radius', 0, 200 ).step( 10 );
  148. gui.add( params, 'Ambient Weight', 0, 1 ).step( 0.1 );
  149. gui.add( params, 'Debug Lightmap' );
  150. }
  151. function onWindowResize() {
  152. camera.aspect = window.innerWidth / window.innerHeight;
  153. camera.updateProjectionMatrix();
  154. renderer.setSize( window.innerWidth, window.innerHeight );
  155. }
  156. function animate() {
  157. // Update the inertia on the orbit controls
  158. controls.update();
  159. // Accumulate Surface Maps
  160. if ( params[ 'Enable' ] ) {
  161. progressiveSurfacemap.update( camera, params[ 'Blend Window' ], params[ 'Blur Edges' ] );
  162. if ( ! progressiveSurfacemap.firstUpdate ) {
  163. progressiveSurfacemap.showDebugLightmap( params[ 'Debug Lightmap' ] );
  164. }
  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>