From eda6eb280fbe14dcb922f60a74a10db45a0b4b49 Mon Sep 17 00:00:00 2001 From: tresabhi <43380238+tresabhi@users.noreply.github.com> Date: Wed, 2 Dec 2026 17:06:33 -0600 Subject: [PATCH] polyfill missing attributes across meshes --- .../[...chunk_models_tanks]/[id]/model.glb.ts | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/packages/website/src/pages/[...chunk_models_tanks]/[id]/model.glb.ts b/packages/website/src/pages/[...chunk_models_tanks]/[id]/model.glb.ts index e76e7cc8b..568e6f321 100644 --- a/packages/website/src/pages/[...chunk_models_tanks]/[id]/model.glb.ts +++ b/packages/website/src/pages/[...chunk_models_tanks]/[id]/model.glb.ts @@ -20,7 +20,10 @@ import { Node, NodeIO, Scene, + type Accessor, + type Primitive, type Skin, + type TypedArray, } from "@gltf-transform/core"; import { ALL_EXTENSIONS } from "@gltf-transform/extensions"; import { dedup, prune } from "@gltf-transform/functions"; @@ -370,6 +373,65 @@ export async function extractModel( await document.transform(prune({ keepAttributes: true }), dedup()); + /** + * The website batches a model's primitives by material, and a batch needs + * every primitive to have the same attributes. A primitive missing one that + * another of its material has (say TEXCOORD_1) gets it zero-filled, which is + * what the shader read for the missing attribute anyway. Parts aren't + * batched so their bones can animate. + */ + if (!isPart) { + const groups = new Map(); + + for (const mesh of document.getRoot().listMeshes()) { + for (const primitive of mesh.listPrimitives()) { + const material = primitive.getMaterial(); + let group = groups.get(material); + + if (!group) { + group = []; + groups.set(material, group); + } + + group.push(primitive); + } + } + + for (const primitives of groups.values()) { + const references = new Map(); + + for (const primitive of primitives) { + for (const name of primitive.listSemantics()) { + if (!references.has(name)) { + references.set(name, primitive.getAttribute(name)!); + } + } + } + + for (const primitive of primitives) { + const count = primitive.getAttribute("POSITION")!.getCount(); + + for (const [name, reference] of references) { + if (primitive.getAttribute(name) !== null) continue; + + const Array = reference.getArray()!.constructor as new ( + length: number, + ) => TypedArray; + + primitive.setAttribute( + name, + document + .createAccessor(name) + .setType(reference.getType()) + .setNormalized(reference.getNormalized()) + .setArray(new Array(count * reference.getElementSize())) + .setBuffer(buffer), + ); + } + } + } + } + return document; }