webgl_shadowmap_progressive.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. <!-- Import maps polyfill -->
  16. <!-- Remove this when import maps will be widely supported -->
  17. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  28. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  29. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  30. import { TransformControls } from './jsm/controls/TransformControls.js';
  31. import { ProgressiveLightMap } from './jsm/misc/ProgressiveLightMap.js';
  32. // ShadowMap + LightMap Res and Number of Directional Lights
  33. const shadowMapRes = 512, lightMapRes = 1024, lightCount = 8;
  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. animate();
  42. function init() {
  43. // renderer
  44. renderer = new THREE.WebGLRenderer( { antialias: true } );
  45. renderer.setPixelRatio( window.devicePixelRatio );
  46. renderer.setSize( window.innerWidth, window.innerHeight );
  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 );
  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, 1.0 / 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. lightmapObjects.push( dirLight );
  85. dirLights.push( dirLight );
  86. }
  87. // ground
  88. const groundMesh = new THREE.Mesh(
  89. new THREE.PlaneBufferGeometry( 600, 600 ),
  90. new THREE.MeshPhongMaterial( { color: 0xffffff, depthWrite: true } )
  91. );
  92. groundMesh.position.y = - 0.1;
  93. groundMesh.rotation.x = - Math.PI / 2;
  94. groundMesh.name = 'Ground Mesh';
  95. lightmapObjects.push( groundMesh );
  96. scene.add( groundMesh );
  97. // model
  98. function loadModel() {
  99. object.traverse( function ( child ) {
  100. if ( child.isMesh ) {
  101. child.name = 'Loaded Mesh';
  102. child.castShadow = true;
  103. child.receiveShadow = true;
  104. child.material = new THREE.MeshPhongMaterial();
  105. // This adds the model to the lightmap
  106. lightmapObjects.push( child );
  107. progressiveSurfacemap.addObjectsToLightMap( lightmapObjects );
  108. } else {
  109. child.layers.disableAll(); // Disable Rendering for this
  110. }
  111. } );
  112. scene.add( object );
  113. object.scale.set( 2, 2, 2 );
  114. object.position.set( 0, - 16, 0 );
  115. control2 = new TransformControls( camera, renderer.domElement );
  116. control2.addEventListener( 'dragging-changed', ( event ) => {
  117. controls.enabled = ! event.value;
  118. } );
  119. control2.attach( object );
  120. scene.add( control2 );
  121. const lightTarget = new THREE.Group();
  122. lightTarget.position.set( 0, 20, 0 );
  123. for ( let l = 0; l < dirLights.length; l ++ ) {
  124. dirLights[ l ].target = lightTarget;
  125. }
  126. object.add( lightTarget );
  127. if ( typeof TESTING !== 'undefined' ) {
  128. for ( let i = 0; i < 300; i ++ ) {
  129. render();
  130. }
  131. }
  132. }
  133. const manager = new THREE.LoadingManager( loadModel );
  134. const loader = new GLTFLoader( manager );
  135. loader.load( 'models/gltf/ShadowmappableMesh.glb', function ( obj ) {
  136. object = obj.scene.children[ 0 ];
  137. } );
  138. // controls
  139. controls = new OrbitControls( camera, renderer.domElement );
  140. controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
  141. controls.dampingFactor = 0.05;
  142. controls.screenSpacePanning = true;
  143. controls.minDistance = 100;
  144. controls.maxDistance = 500;
  145. controls.maxPolarAngle = Math.PI / 1.5;
  146. controls.target.set( 0, 100, 0 );
  147. window.addEventListener( 'resize', onWindowResize );
  148. }
  149. function createGUI() {
  150. const gui = new GUI( { name: 'Accumulation Settings' } );
  151. gui.add( params, 'Enable' );
  152. gui.add( params, 'Blur Edges' );
  153. gui.add( params, 'Blend Window', 1, 500 ).step( 1 );
  154. gui.add( params, 'Light Radius', 0, 200 ).step( 10 );
  155. gui.add( params, 'Ambient Weight', 0, 1 ).step( 0.1 );
  156. gui.add( params, 'Debug Lightmap' );
  157. }
  158. function onWindowResize() {
  159. camera.aspect = window.innerWidth / window.innerHeight;
  160. camera.updateProjectionMatrix();
  161. renderer.setSize( window.innerWidth, window.innerHeight );
  162. }
  163. function render() {
  164. // Update the inertia on the orbit controls
  165. controls.update();
  166. // Accumulate Surface Maps
  167. if ( params[ 'Enable' ] ) {
  168. progressiveSurfacemap.update( camera, params[ 'Blend Window' ], params[ 'Blur Edges' ] );
  169. if ( ! progressiveSurfacemap.firstUpdate ) {
  170. progressiveSurfacemap.showDebugLightmap( params[ 'Debug Lightmap' ] );
  171. }
  172. }
  173. // Manually Update the Directional Lights
  174. for ( let l = 0; l < dirLights.length; l ++ ) {
  175. // Sometimes they will be sampled from the target direction
  176. // Sometimes they will be uniformly sampled from the upper hemisphere
  177. if ( Math.random() > params[ 'Ambient Weight' ] ) {
  178. dirLights[ l ].position.set(
  179. lightOrigin.position.x + ( Math.random() * params[ 'Light Radius' ] ),
  180. lightOrigin.position.y + ( Math.random() * params[ 'Light Radius' ] ),
  181. lightOrigin.position.z + ( Math.random() * params[ 'Light Radius' ] ) );
  182. } else {
  183. // Uniform Hemispherical Surface Distribution for Ambient Occlusion
  184. const lambda = Math.acos( 2 * Math.random() - 1 ) - ( 3.14159 / 2.0 );
  185. const phi = 2 * 3.14159 * Math.random();
  186. dirLights[ l ].position.set(
  187. ( ( Math.cos( lambda ) * Math.cos( phi ) ) * 300 ) + object.position.x,
  188. Math.abs( ( Math.cos( lambda ) * Math.sin( phi ) ) * 300 ) + object.position.y + 20,
  189. ( Math.sin( lambda ) * 300 ) + object.position.z
  190. );
  191. }
  192. }
  193. // Render Scene
  194. renderer.render( scene, camera );
  195. }
  196. function animate() {
  197. requestAnimationFrame( animate );
  198. render();
  199. }
  200. </script>
  201. </body>
  202. </html>