2
0

webgl_shadowmap_progressive.html 7.8 KB

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