/** * Smooth vertex NORMALs for meshes that ship without any. * * glTF 2.0 obliges a renderer to fall back to FLAT per-face normals when a * primitive has no NORMAL attribute. three.js, the HELIX viewer and the * thumbnail renderer all do exactly that, so a mesh exported without normals * (Meshy/Tripo output, `trimesh` exports, hand-rolled writers) renders as a * faceted polygon soup no matter how dense it is. The fix belongs in the asset: * the renderers are correct. * * This module synthesises the missing attribute deterministically: * - vertices are grouped by POSITION (a UV seam duplicates a vertex, and a * seam must NOT become a lighting seam), * - each triangle corner gets the angle-weighted average of the face normals * around its position group that lie within `creaseAngleDeg` of the * corner's own face, so hard edges (a car's panel lines, a table's corners) * stay crisp while curved surfaces shade smooth, * - a vertex is split only where the resulting corner normals differ. * * Invariants: * - A primitive that already carries NORMAL is NEVER touched. Authored * normals are the creator's intent, flat or smooth. * - Only TRIANGLES primitives are shaded. Lines, points, strips and fans have * no meaningful surface normal here. * - Primitives with morph targets are left alone: a split vertex table would * have to be mirrored into every target, and a face rig that loses a * blendshape is a worse defect than a faceted one. They are reported. * - Primitives whose material is KHR_materials_unlit never read a normal and * are left alone, so an unlit asset round-trips byte-identical. * - A GLB with nothing to fix comes back as the SAME bytes, never a * re-serialisation: no needless change of content identity. * * The backend ships a sibling of this routine for generated (Dreamer) props * (`helix-backend-api/src/universal-items/optimization/smooth-normals.ts`); the * two share the algorithm, not the crease default. */ import type { Document } from '@gltf-transform/core'; /** * Edges sharper than this stay hard. 40 degrees keeps 45-degree bevels, * panel-line steps and box corners crisp while smoothing the 5-25 degree facets * of a tessellated curve; Dreamer's 60 is looser because generated props lean * on a baked normal map for their hard detail. */ export declare const DEFAULT_CREASE_ANGLE_DEG = 40; export interface SmoothNormalsOptions { creaseAngleDeg?: number; } /** Why a primitive without NORMAL was left without one. */ export type NormalsSkipReason = 'morph-targets' | 'non-triangles' | 'no-position' | 'no-faces' | 'unlit'; export interface NormalsAudit { /** Every primitive in the document. */ primitives: number; /** Primitives that already carry NORMAL. */ withNormal: number; /** Primitives with no NORMAL that `addSmoothNormals` can fix. */ fixable: number; /** Primitives with no NORMAL that it will not touch, by reason. */ skipped: Record; } export interface SmoothNormalsResult { primitivesProcessed: number; verticesBefore: number; verticesAfter: number; } export interface EnsureNormalsReport extends NormalsAudit { changed: boolean; creaseAngleDeg: number; verticesBefore: number; verticesAfter: number; /** Set when the file could not be read (for example Draco-compressed); nothing was changed. */ unreadable?: string; } /** Counts what a document has and what `addSmoothNormals` would do about it. */ export declare function auditNormals(doc: Document): NormalsAudit; /** * Add smooth vertex normals to every fixable triangle primitive of `doc` that * has none. Run `pruneReplacedAccessors` (or write through an optimiser that * prunes) afterwards to drop the accessors that were replaced. */ export declare function addSmoothNormals(doc: Document, options?: SmoothNormalsOptions): SmoothNormalsResult; /** * Audit a GLB's normals without changing it. `unreadable` is set instead of * counts when the file cannot be parsed here (Draco needs a decoder this * package does not ship). */ export declare function auditGlbNormals(bytes: Uint8Array): Promise<{ audit: NormalsAudit | null; unreadable?: string; }>; /** * Return `bytes` with smooth NORMALs added to every primitive that lacks them. * When there is nothing to fix (or the file cannot be read) the original bytes * come back untouched and `changed` is false. */ export declare function ensureSmoothNormalsGlb(bytes: Uint8Array, options?: SmoothNormalsOptions): Promise<{ bytes: Uint8Array; report: EnsureNormalsReport; }>;