import type { Document } from '@gltf-transform/core'; /** * COLLAPSING AN IMPORTED AVATAR'S MATERIALS INTO A PUBLISHABLE FEW. * * A VRoid export is one material per surface: eyes, brows, lashes, eight hair instances, ears, tail, every * garment piece. The reference avatar arrives with 28, each with its own base-colour texture, and every one is * a distinct draw call. HELIX publishes avatars at **8 materials maximum** — the same ceiling the backend's * asset verification enforces — because a runaway material count is what tanks a crowded room's frame time on * the devices this platform is trying to reach. * * The reduction is a texture atlas, and it is the only reduction that keeps the avatar looking like itself: * * 1. Primitives group by the two properties a merged material CANNOT reconcile — alpha mode and sidedness. * A cutout hair card and an opaque skin patch drawn under one material would either lose the cutout or * punch holes in the skin. This avatar sorts into four such groups, which is already under the cap. * 2. Each group's base-colour textures pack into one atlas, and every primitive's UVs are rescaled into its * own tile. Tiles keep their SOURCE's shape and size: a shelf packer places them, and only when a group's * textures cannot fit the edge cap are they all halved together. Square uniform tiles were simpler and cost * exactly the detail a blind judge noticed: VRoid hair is 1:2 and tiles along the strand, so a square tile * gave each hair texture a quarter of its pixels while the rest of the tile held repeats. * 3. UVs OUTSIDE [0,1] are the interesting case, and ignoring them is how naive atlasers ruin hair. Eight of * this avatar's primitives run to v = 1.76: the hair texture is tiled along the strand. So the tile is * as long as the range the mesh actually samples (1.76 source lengths, not 2), filled by wrapping the * source the way the sampler would, and the UVs keep their texel scale - sampling the same pixels the * original did. Every tile carries a wrapped (or clamped, per its sampler) gutter so bilinear filtering * and the first mips never read a neighbour. * * MToon (`HELIX_materials_toon`, see helixToon.ts) rides through the same packing. A toon group atlases FOUR * channels with one tile layout - base colour, shade colour (shade factor baked in), outline width and rim * multiply - so a shade map is never lost to the merge, and the scalars that differ between merged surfaces * (shading shift, toony, outline width / colour / lighting mix, rim lighting mix) move onto the vertices as * `_TOON0` / `_TOON1` when they are not uniform across the group. Surfaces whose matcap, parametric rim or outline * MODE differ are not merged (they cannot be per-vertex), unless the 8-material cap forces the merge, in which case * the key is relaxed rim -> matcap -> mode and the loss is reported in `toonRelaxed`. * * A material without toon settings behaves exactly as before: base colour only, no extra channels. */ export interface AtlasOptions { /** Hard ceiling on materials in the output. The publish gate's number, not a preference. */ readonly maxMaterials: number; /** Longest edge of a generated atlas. Textures are never allowed to grow past the platform's cap. */ readonly maxEdge: number; } export interface AtlasResult { materialsBefore: number; materialsAfter: number; /** One entry per generated atlas: its group key, tile grid and pixel size. */ atlases: Array<{ key: string; tiles: number; size: number; channels: string[]; }>; /** Primitives whose UVs were divided by a repeat factor because the source tiled. */ tiledPrimitives: number; /** Toon: what had to be merged although it differs (rim, matcap, outline mode), because of the material cap. */ toonRelaxed: Array<'outline-none' | 'rim' | 'matcap-factor' | 'outline-mode'>; /** Toon: primitives that received `_TOON0` / `_TOON1` per-vertex overrides. */ toonAttributePrimitives: number; /** Atlases that keep their sources' no-mipmap filtering (every VRoid 0.x export). */ unmippedAtlases: number; } /** * Pack every group's textures into one atlas each and rewrite the primitives onto the merged materials. * * Returns without touching the document when it is already inside the cap — an avatar that does not need this * should not pay a resample for it. */ export declare function atlasMaterials(document: Document, options: AtlasOptions): Promise;