2
0

webgl_shadowmap_progressive.html 8.2 KB

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