diff --git a/docs/changelogs/1.33.2.md b/docs/changelogs/1.33.2.md new file mode 100644 index 000000000..b423baba7 --- /dev/null +++ b/docs/changelogs/1.33.2.md @@ -0,0 +1,3 @@ +# BlitzKit 1.33.2 + +Fixed an issue that caused the armor of some tanks like the Type 71 to crash BlitzKit. diff --git a/package.json b/package.json index 1f0e85697..838927f68 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "blitzkit", - "version": "1.33.1", + "version": "1.33.2", "license": "BlitzKit License", "private": true, "type": "module", diff --git a/packages/website/src/core/blitzkit/polyfillAttributes.ts b/packages/website/src/core/blitzkit/polyfillAttributes.ts new file mode 100644 index 000000000..7e585b350 --- /dev/null +++ b/packages/website/src/core/blitzkit/polyfillAttributes.ts @@ -0,0 +1,51 @@ +import type { + Accessor, + Buffer, + Document, + TypedArray, +} from "@gltf-transform/core"; + +/** + * The website batches primitives into BatchedMeshes, which need every + * primitive in them to have the same attributes. Batches cross materials (the + * armor view batches wheels with tracks), so a primitive missing an attribute + * any other in the document has (say TEXCOORD_1) gets it zero-filled, which is + * what the shader read for the missing attribute anyway. + */ +export function polyfillAttributes(document: Document, buffer: Buffer) { + const primitives = document + .getRoot() + .listMeshes() + .flatMap((mesh) => mesh.listPrimitives()); + 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), + ); + } + } +} diff --git a/packages/website/src/pages/[...chunk_models_tanks]/[id]/collision.glb.ts b/packages/website/src/pages/[...chunk_models_tanks]/[id]/collision.glb.ts index afe75bccc..e4b8793a5 100644 --- a/packages/website/src/pages/[...chunk_models_tanks]/[id]/collision.glb.ts +++ b/packages/website/src/pages/[...chunk_models_tanks]/[id]/collision.glb.ts @@ -8,6 +8,7 @@ import { import type { AbstractVFS } from "@blitzkit/vfs"; import { Document, Node, NodeIO, Scene } from "@gltf-transform/core"; import type { APIContext } from "astro"; +import { polyfillAttributes } from "../../../core/blitzkit/polyfillAttributes"; import { Sc2ComponentHandler } from "../../../core/blitzkit/sc2Components"; import { vfs } from "../../../core/blitzkit/vfs"; @@ -80,5 +81,7 @@ async function extractArmor(vfs: AbstractVFS, fileName: string) { }, ); + polyfillAttributes(document, buffer); + return document; } 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 8a49a6cab..05b83e2f4 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 @@ -19,9 +19,6 @@ import { Node, NodeIO, Scene, - type Accessor, - type Primitive, - type TypedArray, } from "@gltf-transform/core"; import { ALL_EXTENSIONS } from "@gltf-transform/extensions"; import { dedup, prune } from "@gltf-transform/functions"; @@ -29,6 +26,7 @@ import type { APIContext } from "astro"; import { isEqual, times } from "lodash-es"; import { dirname } from "path"; import sharp from "sharp"; +import { polyfillAttributes } from "../../../core/blitzkit/polyfillAttributes"; import { loadHierarchyMotions, Sc2ComponentHandler, @@ -140,64 +138,8 @@ 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), - ); - } - } - } - } + // parts aren't batched so their bones can animate + if (!isPart) polyfillAttributes(document, buffer); return document; }