var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var texture_atlas_exports = {};
__export(texture_atlas_exports, {
  buildEmissiveAtlas: () => buildEmissiveAtlas,
  buildTextureAtlas: () => buildTextureAtlas
});
module.exports = __toCommonJS(texture_atlas_exports);
const toPowerOfTwo = (value) => {
  return 2 ** Math.ceil(Math.log2(Math.max(1, value)));
};
function buildTextureAtlas(THREE, textures) {
  const uniqueTextures = Array.from(new Set(textures));
  if (uniqueTextures.length === 0) {
    return null;
  }
  const maxWidth = uniqueTextures.reduce((acc, tex) => Math.max(acc, tex.image?.width ?? 0), 0);
  const totalHeight = uniqueTextures.reduce((acc, tex) => acc + (tex.image?.height ?? 0), 0);
  if (maxWidth === 0 || totalHeight === 0) {
    return null;
  }
  const atlasWidth = toPowerOfTwo(maxWidth);
  const atlasHeight = toPowerOfTwo(totalHeight);
  const atlasData = new Uint8Array(atlasWidth * atlasHeight * 4);
  const transforms = /* @__PURE__ */ new Map();
  let offsetY = 0;
  uniqueTextures.forEach((texture) => {
    const image = texture.image;
    if (!image) {
      return;
    }
    const width = image.width;
    const height = image.height;
    const data = image.data;
    const startY = offsetY;
    for (let row = 0; row < height; row += 1) {
      const srcStart = row * width * 4;
      const dstStart = (startY + row) * atlasWidth * 4;
      atlasData.set(data.subarray(srcStart, srcStart + width * 4), dstStart);
    }
    transforms.set(texture, {
      offset: [0, startY / atlasHeight],
      scale: [width / atlasWidth, height / atlasHeight]
    });
    offsetY += height;
  });
  const template = uniqueTextures[0];
  const templateTexture = template;
  const atlasTexture = new THREE.DataTexture(atlasData, atlasWidth, atlasHeight);
  atlasTexture.needsUpdate = true;
  atlasTexture.magFilter = templateTexture.magFilter;
  atlasTexture.minFilter = templateTexture.minFilter;
  atlasTexture.wrapS = templateTexture.wrapS;
  atlasTexture.wrapT = templateTexture.wrapT;
  atlasTexture.flipY = false;
  if ("colorSpace" in atlasTexture && "colorSpace" in templateTexture) {
    atlasTexture.colorSpace = templateTexture.colorSpace;
  } else if ("encoding" in atlasTexture && "encoding" in templateTexture) {
    atlasTexture.encoding = templateTexture.encoding;
  }
  return {
    texture: atlasTexture,
    transforms,
    width: atlasWidth,
    height: atlasHeight
  };
}
function buildEmissiveAtlas(THREE, width, height, transforms, pairs) {
  if (pairs.size === 0) {
    return null;
  }
  const atlasData = new Uint8Array(width * height * 4);
  let template = null;
  pairs.forEach((emissive, reference) => {
    const transform = transforms.get(reference);
    if (!transform) {
      return;
    }
    const image = emissive.image;
    const refImage = reference.image;
    if (!image || !refImage) {
      return;
    }
    const texWidth = refImage.width;
    const texHeight = refImage.height;
    const data = image.data;
    const offsetX = Math.round(transform.offset[0] * width);
    const offsetY = Math.round(transform.offset[1] * height);
    for (let row = 0; row < texHeight; row += 1) {
      const srcStart = row * texWidth * 4;
      const dstStart = ((offsetY + row) * width + offsetX) * 4;
      atlasData.set(data.subarray(srcStart, srcStart + texWidth * 4), dstStart);
    }
    template = emissive;
  });
  if (!template) {
    return null;
  }
  const templateTexture = template;
  const atlasTexture = new THREE.DataTexture(atlasData, width, height);
  atlasTexture.needsUpdate = true;
  atlasTexture.magFilter = templateTexture.magFilter;
  atlasTexture.minFilter = templateTexture.minFilter;
  atlasTexture.wrapS = templateTexture.wrapS;
  atlasTexture.wrapT = templateTexture.wrapT;
  atlasTexture.flipY = false;
  if ("colorSpace" in atlasTexture && "colorSpace" in templateTexture) {
    atlasTexture.colorSpace = templateTexture.colorSpace;
  } else if ("encoding" in atlasTexture && "encoding" in templateTexture) {
    atlasTexture.encoding = templateTexture.encoding;
  }
  return atlasTexture;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildEmissiveAtlas,
  buildTextureAtlas
});
