INNER CODE UNIT · Python
mip_count_for
untoldengine/UntoldEngine · scripts/texbake.py:234
def mip_count_for(width: int, height: int) -> int:
"""Number of mip levels for a texture of the given base dimensions."""
return int(math.floor(math.log2(max(width, height)))) + 1
def generate_mip_chain(img: "Image.Image") -> list["Image.Image"]:
"""
Return a list of PIL Images from mip 0 (full-res) down to mip N (1×1 or smallest).
Downsampling uses LANCZOS in the encoded gamma space, which is the standard
approach. For sRGB textures this introduces a small linearisation error at mip
boundaries; acceptable for production game content.
"""
# Ensure RGBA so the ASTC encoder always receives a 4-channel input.
img = img.convert("RGBA")
base_w, base_h = img.size
levels = mip_count_for(base_w, base_h)
mips: list[Image.Image] = [img]
for level in range(1, levels):