webgl_shadowmap_progressive.html 7.9 KB

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