import type sharpFactory from 'sharp'; import { Document, type Texture } from '@gltf-transform/core'; export interface BakeResult { size: number; /** Texels rasterized from the source surface. */ sampled: number; /** Gutter texels filled by dilation. */ dilated: number; /** Sampled texels whose closest source point was suspiciously far (> 2% of the bbox diagonal). */ far: number; /** Which atlases were baked; each target material binds only the slots its source material had. */ slots: BakedSlot[]; /** * Full-resolution bakes (HELIX-120 S-SRC-REBAKE): per slot whose SOURCE textures were larger than `outSize`, the PNG at the * source edge (see `BakeOptions.retain`). The web texture of that slot is exactly this image downscaled to `outSize` * (lanczos3), so the retained image is the true original of the web rendition by construction. */ retained: Partial>; } export interface RetainedBake { texture: Texture; bytes: Uint8Array; width: number; height: number; } export interface BakeOptions { /** * Bake at the source resolution and keep it. A slot's edge is the largest edge among the source images feeding it, * right-shifted by `shift` (one halving per LOD level, like the web mip chain) and capped at `maxEdge`; a slot whose edge * does not exceed `outSize` has nothing to retain. */ retain?: { shift: number; maxEdge: number; }; } export type BakedSlot = 'baseColor' | 'emissive' | 'orm' | 'normal'; /** * Re-bakes the SOURCE document's material textures onto the (decimated, freshly unwrapped) target * document. For every texel of every target triangle: barycentric -> 3D point -> closest point on the * SOURCE triangles of the SAME material (BVH per material) -> that material's own UVs -> bilinear * sample of that material's own textures. * * Up to four atlases share the new layout: base colour (sRGB), an emissive map (sRGB, only when a source * material has a genuine one), a packed ORM (R = occlusion, G/B = metallic-roughness, linear), and a * tangent-space normal map. Normals are transferred through object * space: decoded in the source triangle's UV-derived tangent frame and re-encoded in the target * triangle's, so detail survives the new UV layout. Each target material binds only the slots its * source material had; every other texture binding is cleared, because the old images no longer match * the new UVs. * * EMISSIVE: an emissiveFactor must never outlive its map. A genuine (non-black) emissive map is rebaked with its * factor multiplied in and the target factor becomes [1,1,1]; a black map (Meshy ships one beside factor [1,1,1]) * or a zero factor contributes nothing, so the texture is dropped and the factor zeroed. Clearing the map and * keeping the factor rendered the whole avatar flat white. * * Layered surfaces (hair shell over scalp) are disambiguated by preferring source triangles whose * facing agrees with the target triangle when their distances are comparable. Unwritten texels are * dilated outward into gutters so bilinear/mip filtering never bleeds background into charts. */ export declare function bakeMaterials(target: Document, source: Document, outSize: number, options?: BakeOptions): Promise; /** @deprecated Kept for callers of the original base-colour-only bake; bakes every material slot. */ export declare const bakeBaseColor: typeof bakeMaterials; /** * Fills every texel that no chart wrote (`written === 0`) with a smooth blend of the nearest chart colours (pyramid * push-pull): the coverage-weighted mean is pulled down a mip pyramid, then each empty texel takes the bilinear sample of its * parent. Written texels are left exactly as they are. Works on all four channels, alpha included. */ export declare function pushPullFill(atlas: Uint8ClampedArray, written: Uint8Array, size: number): void; /** True only when the rebake stage has actual embedded pixels to sample. */ export declare function hasBakeableBaseColorTexture(document: Document): boolean; export declare function fillCutoutThreadColour(atlas: Uint8ClampedArray, cutout: Uint8Array, size: number): void; /** * Web colour of the cutout layer: sharp's resize premultiplies, so the thread colour under alpha 0 is lost at the downscale. * Where the web texel is mostly cutout layer, take the straight-alpha (colour and coverage resized separately) colour; * alpha and every other texel stay exactly as the premultiplied downscale produced them. */ export declare function downscaleAtlas(sharp: typeof sharpFactory, atlas: Uint8Array, atlasEdge: number, outSize: number, cutout: { cutout: Uint8Array; edge: number; } | null): Promise;