1 line
16 KiB
Plaintext
1 line
16 KiB
Plaintext
|
|
{"version":3,"file":"ProgressiveLightmap.cjs","sources":["../../src/misc/ProgressiveLightmap.js"],"sourcesContent":["import {\n Scene,\n WebGLRenderTarget,\n FloatType,\n MeshBasicMaterial,\n MeshPhongMaterial,\n DoubleSide,\n PlaneGeometry,\n Mesh,\n} from 'three'\nimport potpack from 'potpack'\nimport { UV1 } from '../_polyfill/uv1'\n\n/**\n * Progressive Light Map Accumulator, by [zalo](https://github.com/zalo/)\n *\n * To use, simply construct a `ProgressiveLightMap` object,\n * `plmap.addObjectsToLightMap(object)` an array of semi-static\n * objects and lights to the class once, and then call\n * `plmap.update(camera)` every frame to begin accumulating\n * lighting samples.\n *\n * This should begin accumulating lightmaps which apply to\n * your objects, so you can start jittering lighting to achieve\n * the texture-space effect you're looking for.\n *\n * @param {WebGLRenderer} renderer A WebGL Rendering Context\n * @param {number} res The side-long dimension of you total lightmap\n */\nclass ProgressiveLightMap {\n constructor(renderer, res = 1024) {\n this.renderer = renderer\n this.res = res\n this.lightMapContainers = []\n this.compiled = false\n this.scene = new Scene()\n this.scene.background = null\n this.tinyTarget = new WebGLRenderTarget(1, 1)\n this.buffer1Active = false\n this.firstUpdate = true\n this.warned = false\n\n // Create the Progressive LightMap Texture\n const format = /(Android|iPad|iPhone|iPod)/g.test(navigator.userAgent) ? alfFloatType : FloatType\n this.progressiveLightMap1 = new WebGLRenderTarget(this.res, this.res, { type: format })\n this.progressiveLightMap2 = new WebGLRenderTarget(this.res, this.res, { type: format })\n\n // Inject some spicy new logic into a standard phong material\n this.uvMat = new MeshPhongMaterial()\n this.uvMat.uniforms = {}\n this.uvMat.onBeforeCompile = (shader) => {\n // Vertex Shader: Set Vertex Positions to the Unwrapped UV Positions\n shader.vertexShader =\n '#define USE_LIGHTMAP\\n' +\n shader.vertexShader.slice(0, -1) +\n `\tgl_Position = vec4((${UV1} - 0.5) * 2.0, 1.0, 1.0); }`\n\n // Fragment Shader: Set Pixels to average in the Previous frame's Shadows\n const bodyStart = shader.fragmentShader.indexOf('void main() {')\n shader.fragmentShader =\n `varying vec2 v${UV1 === 'uv1' ? UV1 : 'Uv2'};\\n` +\n shader.fragmentShader.slice(0, bodyStart) +\n '\tuniform sampler2D previousShadowMap;\\n\tuniform float averagingWindow;\\n' +\n shader.fragmentShader.slice(bodyStart - 1, -1) +\n `\\nvec3 texelOld = texture2D(previousShadowMap, v${UV1 === 'uv1' ? UV1 : 'Uv2'}).rgb;\n\t\t\t\tgl_FragColor.rgb = mix(texelOld, gl_FragColor.rgb, 1.0/averagingWindow);\n\t\t\t}`\n\n // Set the Previous Frame's Texture Buffer and Averaging Window\n shader.uniforms.previousShadowMap = { value: this.progressiveLightMap1.texture }\n shader.uniforms.averagingWindow = { value: 100 }\n\n this.uvMat.uniforms = shader.uniforms\n\n // Set the new Shader to this\n this.uvMat.userData.shader = shader\n\n this.compiled = true\n }\n }\n\n /**\n * Sets these objects' materials' lightmaps and modifies their uv1's.\n * @param {Object3D} objects An array of objects and lights to set up your lightmap.\n */\n addObjectsToLightMap(objects) {\n // Prepare list of UV bounding boxes for packing later...\n this.uv_boxes = []\n const padding = 3 / this.res\n\n for (let ob = 0; ob < objects.length; ob++) {\n const object = objects[ob]\n\n // If this object is a light, simply add it to the internal scene\n if (object.isLight) {\n this.scene.attach(object)\n continue\n }\n\n if (!object.geometry.hasAttribute('uv')) {\n console.warn('All lightmap objects need UVs!')\n continue\n }\n\n if (this.blurringPlane == null) {\n this._initializeBlurPlane(this.res, this.progressiveLightMap1)\n }\n\n // Apply the lightmap to the object\n
|