(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "three"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("three")); } else { var mod = { exports: {} }; factory(mod.exports, global.three); global.LightProbeGenerator = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.LightProbeGenerator = void 0; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } var LightProbeGenerator = /*#__PURE__*/function () { function LightProbeGenerator() { _classCallCheck(this, LightProbeGenerator); } _createClass(LightProbeGenerator, null, [{ key: "fromCubeTexture", value: // https://www.ppsloan.org/publications/StupidSH36.pdf function fromCubeTexture(cubeTexture) { var totalWeight = 0; var coord = new _three.Vector3(); var dir = new _three.Vector3(); var color = new _three.Color(); var shBasis = [0, 0, 0, 0, 0, 0, 0, 0, 0]; var sh = new _three.SphericalHarmonics3(); var shCoefficients = sh.coefficients; for (var faceIndex = 0; faceIndex < 6; faceIndex++) { var image = cubeTexture.image[faceIndex]; var width = image.width; var height = image.height; var canvas = document.createElement('canvas'); canvas.width = width; canvas.height = height; var context = canvas.getContext('2d'); context.drawImage(image, 0, 0, width, height); var imageData = context.getImageData(0, 0, width, height); var data = imageData.data; var imageWidth = imageData.width; // assumed to be square var pixelSize = 2 / imageWidth; for (var i = 0, il = data.length; i < il; i += 4) { // RGBA assumed // pixel color color.setRGB(data[i] / 255, data[i + 1] / 255, data[i + 2] / 255); // convert to linear color space convertColorToLinear(color, cubeTexture.encoding); // pixel coordinate on unit cube var pixelIndex = i / 4; var col = -1 + (pixelIndex % imageWidth + 0.5) * pixelSize; var row = 1 - (Math.floor(pixelIndex / imageWidth) + 0.5) * pixelSize; switch (faceIndex) { case 0: coord.set(-1, row, -col); break; case 1: coord.set(1, row, col); break; case 2: coord.set(-col, 1, -row); break; case 3: coord.set(-col, -1, row); break; case 4: coord.set(-col, row, 1); break; case 5: coord.set(col, row, -1); break; } // weight assigned to this pixel var lengthSq = coord.lengthSq(); var weight = 4 / (Math.sqrt(lengthSq) * lengthSq); totalWeight += weight; // direction vector to this pixel dir.copy(coord).normalize(); // evaluate SH basis functions in direction dir _three.SphericalHarmonics3.getBasisAt(dir, shBasis); // accummuulate for (var j = 0; j < 9; j++) { shCoefficients[j].x += shBasis[j] * color.r * weight; shCoefficients[j].y += shBasis[j] * color.g * weight; shCoefficients[j].z += shBasis[j] * color.b * weight; } } } // normalize var norm = 4 * Math.PI / totalWeight; for (var _j = 0; _j < 9; _j++) { shCoefficients[_j].x *= norm; shCoefficients[_j].y *= norm; shCoefficients[_j].z *= norm; } return new _three.LightProbe(sh); } }, { key: "fromCubeRenderTarget", value: function fromCubeRenderTarget(renderer, cubeRenderTarget) { // The renderTarget must be set to RGBA in order to make readRenderTargetPixels works var totalWeight = 0; var coord = new _three.Vector3(); var dir = new _three.Vector3(); var color = new _three.Color(); var shBasis = [0, 0, 0, 0, 0, 0, 0, 0, 0]; var sh = new _three.SphericalHarmonics3(); var shCoefficients = sh.coefficients; for (var faceIndex = 0; faceIndex < 6; faceIndex++) { var imageWidth = cubeRenderTarget.width; // assumed to be square var data = new Uint8Array(imageWidth * imageWidth * 4); renderer.readRenderTargetPixels(cubeRenderTarget, 0, 0, imageWidth, imageWidth, data, faceIndex); var pixelSize = 2 / imageWidth; for (var i = 0, il = data.length; i < il; i += 4) { // RGBA assumed // pixel color color.setRGB(data[i] / 255, data[i + 1] / 255, data[i + 2] / 255); // convert to linear color space convertColorToLinear(color, cubeRenderTarget.texture.encoding); // pixel coordinate on unit cube var pixelIndex = i / 4; var col = -1 + (pixelIndex % imageWidth + 0.5) * pixelSize; var row = 1 - (Math.floor(pixelIndex / imageWidth) + 0.5) * pixelSize; switch (faceIndex) { case 0: coord.set(1, row, -col); break; case 1: coord.set(-1, row, col); break; case 2: coord.set(col, 1, -row); break; case 3: coord.set(col, -1, row); break; case 4: coord.set(col, row, 1); break; case 5: coord.set(-col, row, -1); break; } // weight assigned to this pixel var lengthSq = coord.lengthSq(); var weight = 4 / (Math.sqrt(lengthSq) * lengthSq); totalWeight += weight; // direction vector to this pixel dir.copy(coord).normalize(); // evaluate SH basis functions in direction dir _three.SphericalHarmonics3.getBasisAt(dir, shBasis); // accummuulate for (var j = 0; j < 9; j++) { shCoefficients[j].x += shBasis[j] * color.r * weight; shCoefficients[j].y += shBasis[j] * color.g * weight; shCoefficients[j].z += shBasis[j] * color.b * weight; } } } // normalize var norm = 4 * Math.PI / totalWeight; for (var _j2 = 0; _j2 < 9; _j2++) { shCoefficients[_j2].x *= norm; shCoefficients[_j2].y *= norm; shCoefficients[_j2].z *= norm; } return new _three.LightProbe(sh); } }]); return LightProbeGenerator; }(); _exports.LightProbeGenerator = LightProbeGenerator; function convertColorToLinear(color, encoding) { switch (encoding) { case _three.sRGBEncoding: color.convertSRGBToLinear(); break; case _three.LinearEncoding: break; default: console.warn('WARNING: LightProbeGenerator convertColorToLinear() encountered an unsupported encoding.'); break; } return color; } });