mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-12-04 23:32:54 +01:00
polyfill both tank model and armor
This commit is contained in:
5 files changed
+61
-62
No files matched your search
@@ -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<string, Accessor>();
|
||||
|
||||
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),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user