import { Document, NodeIO, type Primitive } from '@gltf-transform/core'; import { type Ktx2Mode } from './ktx2'; /** * LOD chain and preview rendition for a converted VRM. * * The converter produces ONE finished scene (LOD0: spring chains, face, constraints, hide lists, atlased materials). * Everything here derives the other levels FROM that scene, so the invariants the VRM conversion fought for hold in * every level by construction rather than by a second implementation of each: * * - every level is a self-contained copy of the same skeleton, same joint NAMES. The engine rebinds levels 1..n onto * level 0's skeleton by name (`assembleCharacterLod` -> `attachSkinnedPart`), so a hair strand weighted to * `HairJoint-x` swings in every level from the one simulation that drives level 0's bones; * - every level shares level 0's materials and textures. They are one decoded image set in GPU memory whichever level * is drawn (the file is parsed once), so a separate smaller far texture would add memory, not save it, and would * count against the publish gate's eight-material cap once per level; * - mesh and primitive names are kept, so the first-/third-person hide lists (substring matches on names) and the face * runtime (which binds every skinned mesh carrying the manifest's mesh name and morph name) reach every level. */ /** One level below LOD0. */ export interface VrmLodLevel { /** Total triangle target for the level. */ tris: number; /** Keep the face's blend shapes. The face runtime binds every level that carries them; a far level can do without. */ keepMorphs: boolean; /** Fraction of the face (morph-carrying) triangles to keep when `keepMorphs`; the rest of the mesh pays the difference. */ faceKeep: number; /** meshopt error budget, relative to the mesh extent. */ error: number; /** * Cap on the same budget in metres, whatever the primitive's size. meshopt's error is relative to each primitive's own * extent, so on a VRM whose body is one primitive (AliciaSolid, Seed-san) 0.08 of a 1.6 m body let the simplifier * flatten 8 cm arms and hair tails into zero-area lines: at 15 m the avatar lost its sleeves and twin tails. The cap * keeps the deviation near a pixel or two at the distance the level is drawn (about 1.1 cm per pixel at 15 m on a * 1000 px tall 40 degree view); a level that cannot reach its triangle target under the cap stays above it. */ maxErrorMetres?: number; } /** * The ladder, as fractions of LOD0. Roughly the generic character ladder (10000 / 5000 / 1500), kept in proportion to * whatever the budget stage left LOD0 at. Summed over the chain this is ~1.5x LOD0 against the 4x the publish gate allows. */ export declare function vrmLodLadder(lod0Tris: number): VrmLodLevel[]; /** * A level must keep at most this share of the previous kept level's triangles, or it is not shipped. A "LOD" at 98% of * the level above draws the same thing for more bytes and a bigger triangle sum under the publish gate (Punko6529: LOD0 * 24,461, LOD1 24,052, LOD2 23,867 = 72,380 against the 60,000 cap, where live shipped LOD0 alone and passed). */ export declare const LOD_MAX_SHARE_OF_PREVIOUS = 0.85; export declare const levelPaysForItself: (tris: number, previousTris: number) => boolean; export interface VrmLodReport { level: number; tris: number; primitives: number; /** Primitives that still carry blend shapes, and how many targets each carries. */ morphPrimitives: number; morphTargets: number; /** Triangles in those morph-carrying primitives. */ faceTris: number; /** meshopt error budget the level needed (it is raised when a target is out of reach). */ errorUsed: number; } /** Every target accessor must be exactly as long as the position stream it displaces, or the deltas land on the wrong vertices. */ export declare function assertMorphTargetsConsistent(document: Document, label: string): void; /** * Reduce a document to `target` triangles. The face (primitives carrying blend shapes) is reduced to `faceKeep` of * itself and the rest of the mesh takes whatever is left, because a blend shape displaces vertices the simplifier does * not know about: the fewer vertices the face loses, the less a collapsed edge can tear when the mouth opens. * * Per-primitive floor of four triangles: a hair strand of five triangles should thin out, not vanish at the first rung. */ export declare function reduceToTarget(document: Document, level: Pick): Promise; /** * After a collapse, a surviving vertex can carry the normal of a coincident vertex from the other side of a two-layer * surface (a trouser leg modelled as an outer shell and an inward-facing inner shell at the same positions): the outer * surface then shades as if lit from behind, and Seed-san's trousers went black at LOD1 and LOD2 while its texture * was untouched. Any vertex whose stored normal points against the area-weighted normal of the triangles now using it * takes that normal instead. A correctly shaded vertex, hard edge or not, never points away from its own fan, so * nothing else moves. Returns the number of vertices repaired. */ export declare function repairInvertedNormals(primitive: Primitive): number; declare function reportFor(document: Document, level: number, errorUsed: number): VrmLodReport; export declare const lodReportFor: typeof reportFor; /** * Merge LOD0 with the levels `buildVrmLodDocuments` made, as one multi-scene document that shares materials and * textures. The levels are written-and-read copies of LOD0, so LOD0 takes the same round trip first: a * HELIX_materials_toon material straight out of `attachVrmToon` never compares equal to its round-tripped copy, so * mergeLods' dedup kept a copy per level (AvatarSample_B: 11 materials against the avatar gate's 8). */ export declare function mergeVrmLodChain(io: NodeIO, lod0: Document, levels: Document[]): Promise; /** * LOD documents 1..n derived from the finished LOD0 document, each a deep copy so nothing is shared until `mergeLods` * folds identical accessors, textures and materials back together. */ export declare function buildVrmLodDocuments(io: NodeIO, lod0: Document, ladder: readonly VrmLodLevel[]): Promise<{ documents: Document[]; reports: VrmLodReport[]; }>; export interface VrmPreviewOptions { tris: number; textureEdge: number; /** Names of the canonical skeleton's joints (`helix-humanoid@1`); every other joint is folded into its nearest canonical ancestor. */ canonicalJoints: ReadonlySet; } export interface VrmPreviewReport { tris: number; primitives: number; joints: number; materials: number; textureEdge: number; } /** * The small first-paint rendition: the converted body at ~2.5k triangles, 128 px textures, the canonical skeleton only and * none of the avatar's extras (no face, dynamics or hide lists; the only constraints are the ones a folded joint cannot follow, see `livePreviewConstraints`). * A crowd shows this while the real file * downloads; it is a separate file because the real file's first bytes are its 1.5 MB-plus LOD0. */ export declare function buildVrmPreview(io: NodeIO, lod0: Document, options: VrmPreviewOptions): Promise<{ document: Document; report: VrmPreviewReport; }>; export interface WebEncodeOptions { ktx2?: boolean; ktxMode?: Ktx2Mode; meshopt?: boolean; } export interface WebEncodeReport { ktx2: { mode: Ktx2Mode; textures: number; bytesBefore: number; bytesAfter: number; } | null; meshopt: boolean; } /** * KTX2 textures and meshopt geometry, in place. * * Meshopt here is COMPRESSION ONLY: the vertex streams are reordered for the codec and stored with EXT_meshopt_compression, * but never quantized (no KHR_mesh_quantization). The backend's rig-fix and draw-call passes run on this file afterwards, * read positions as floats, bake arm re-binds into them and join primitives by their accessor shape; quantized skinned * positions carry a per-mesh decode transform those passes do not know about, so joining two meshes would corrupt one. */ export declare function encodeForWeb(document: Document, options?: WebEncodeOptions): Promise; /** * Resize every PNG/JPEG texture whose edges are not multiples of 4 to the nearest multiples of 4. Basis transcodes to * 4x4-block GPU formats (BC7/ETC/ASTC), and WebGL refuses `texStorage2D` for a block-compressed base level that is not a * multiple of 4 (GL_INVALID_OPERATION, the texture never uploads, the avatar draws black). An aspect-preserving resize * makes such edges routinely: the 128 px preview of a 1280x1024 or 1536x1024 source came out 128x102 and 128x85. */ export declare function snapTexturesToBlocks(document: Document): Promise; /** An IO that can write what `encodeForWeb` prepared. */ export declare function webWriterIo(io: NodeIO): NodeIO; export {};