Files
blitzkit/packages/website/src/pages/[...chunk_api]/tanks/[id]/collision.glb.ts
T

189 lines
6.1 KiB
TypeScript
Raw Normal View History

2024-09-07 21:45:00 -05:00
import {
Sc2ReadStream,
ScgReadStream,
2026-08-04 20:18:05 -05:00
toUniqueId,
2024-09-07 21:45:00 -05:00
VertexAttribute,
2026-08-04 20:18:05 -05:00
vertexAttributeVectorSizes,
type Hierarchy,
type VehicleDefinitionList,
2025-11-26 13:18:44 -06:00
} from "@blitzkit/core";
2026-08-04 20:18:05 -05:00
import type { AbstractVFS } from "@blitzkit/core/src/blitzkit/vfs/abstract";
import { Accessor, Document, Node, NodeIO, Scene } from "@gltf-transform/core";
import type { APIContext } from "astro";
2025-11-26 13:18:44 -06:00
import { times } from "lodash-es";
2026-08-06 20:10:29 -05:00
import { vfs } from "../../../../core/blitzkit/vfs";
2024-01-08 18:34:04 -06:00
2026-08-04 20:18:05 -05:00
export { getStaticPaths } from "./_index";
export async function GET({ props }: APIContext<{ id: number }>) {
const nodeIO = new NodeIO();
const nations = await vfs
.dir(`Data/XML/item_defs/vehicles`)
.then((files) => files.filter((nation) => nation !== "common"));
for (const nation of nations) {
const tanks = await vfs.xml<{ root: VehicleDefinitionList }>(
`Data/XML/item_defs/vehicles/${nation}/list.xml`,
);
for (const tankKey in tanks.root) {
const tank = tanks.root[tankKey];
if (tankKey.includes("tutorial_bot")) continue;
const id = toUniqueId(nation, tank.id);
if (id !== props.id) continue;
const model = await extractArmor(vfs, `${nation}-${tankKey}`);
const bytes = await nodeIO.writeBinary(model);
return new Response(bytes);
}
}
}
async function extractArmor(vfs: AbstractVFS, fileName: string) {
2025-11-26 13:18:44 -06:00
const sc2Path = `Data/3d/Tanks/CollisionMeshes/${fileName}.sc2`;
const scgPath = `Data/3d/Tanks/CollisionMeshes/${fileName}.scg`;
2024-06-23 20:59:03 -05:00
const sc2 = new Sc2ReadStream(
2026-02-18 16:36:12 -06:00
(await vfs.file(sc2Path)).buffer as ArrayBuffer,
2024-06-23 20:59:03 -05:00
).sc2();
const scg = new ScgReadStream(
2026-02-18 16:36:12 -06:00
(await vfs.file(scgPath)).buffer as ArrayBuffer,
2024-06-23 20:59:03 -05:00
).scg();
2024-01-08 18:34:04 -06:00
const document = new Document();
const scene = document.createScene();
const buffer = document.createBuffer();
function parseHierarchies(hierarchies: Hierarchy[], parent: Scene | Node) {
hierarchies.forEach((hierarchy) => {
const components = times(
hierarchy.components.count,
2026-02-18 16:36:12 -06:00
(index) => hierarchy.components[index.toString().padStart(4, "0")],
2024-01-08 18:34:04 -06:00
);
components.forEach((component) => {
2025-11-26 13:18:44 -06:00
switch (component["comp.typename"]) {
case "TransformComponent":
2024-01-08 18:34:04 -06:00
break;
2025-11-26 13:18:44 -06:00
case "RenderComponent": {
const batch = component["rc.renderObj"]["ro.batches"]["0000"];
const polygonGroup = scg.get(batch["rb.datasource"]);
2024-01-08 18:34:04 -06:00
if (!polygonGroup) {
console.warn(
2026-02-18 16:36:12 -06:00
`Missing polygon group ${batch["rb.datasource"]} (${hierarchy.name}); skipping...`,
2024-01-08 18:34:04 -06:00
);
break;
}
2024-01-09 13:07:27 -06:00
const hardJointIndices = new Set<number>();
const vertexHardJointIndices = new Map<number, number>();
const attributes = new Map<VertexAttribute, number[][]>();
2024-01-08 18:34:04 -06:00
2024-01-09 13:07:27 -06:00
polygonGroup.vertices.forEach((vertex, index) => {
2024-01-08 18:34:04 -06:00
vertex.forEach(({ attribute, value }) => {
2024-01-09 13:07:27 -06:00
if (!attributes.has(attribute)) {
attributes.set(attribute, []);
2024-01-08 18:34:04 -06:00
}
2024-01-09 13:07:27 -06:00
attributes.get(attribute)!.push(value);
if (attribute === VertexAttribute.HARD_JOINTINDEX) {
2024-01-09 13:07:27 -06:00
hardJointIndices.add(value[0]);
vertexHardJointIndices.set(index, value[0]);
}
2024-01-08 18:34:04 -06:00
});
});
2024-01-09 13:07:27 -06:00
if (attributes.has(VertexAttribute.TANGENT)) {
attributes.set(
VertexAttribute.TANGENT,
attributes
.get(VertexAttribute.TANGENT)!
2026-02-18 16:36:12 -06:00
.map((tangent) => [...tangent, 1]),
2024-01-09 13:07:27 -06:00
);
2024-01-08 18:34:04 -06:00
}
2024-01-09 13:07:27 -06:00
const accessors = new Map<string, Accessor>();
2024-01-08 18:34:04 -06:00
2024-01-09 13:07:27 -06:00
attributes.forEach((value, attribute) => {
const name = vertexAttributeGLTFName[attribute];
if (!name || accessors.has(name)) return;
const vertexSize = vertexAttributeGltfVectorSizes[attribute];
const accessor = document
2024-01-08 18:34:04 -06:00
.createAccessor(name)
2025-11-26 13:18:44 -06:00
.setType(vertexSize === 1 ? "SCALAR" : `VEC${vertexSize}`)
2024-01-09 13:07:27 -06:00
.setArray(new Float32Array(value.flat()))
2024-01-08 18:34:04 -06:00
.setBuffer(buffer);
2024-01-09 13:07:27 -06:00
accessors.set(name, accessor);
2024-01-08 18:34:04 -06:00
});
2024-01-09 13:07:27 -06:00
hardJointIndices.forEach((hardJointIndex) => {
2024-01-09 13:36:33 -06:00
const node = document.createNode(
2026-02-18 16:36:12 -06:00
`${hierarchy.name}_armor_${hardJointIndex}`,
2024-01-09 13:07:27 -06:00
);
2025-11-26 13:18:44 -06:00
const mesh = document.createMesh(batch["##name"]);
2024-01-09 13:07:27 -06:00
const indicesAccessor = document
.createAccessor()
2025-11-26 13:18:44 -06:00
.setType("SCALAR")
2024-01-09 13:07:27 -06:00
.setArray(
new Uint16Array(
2024-01-12 19:58:02 -06:00
polygonGroup.indices.filter(
(index) =>
2026-02-18 16:36:12 -06:00
vertexHardJointIndices.get(index) === hardJointIndex,
),
),
2024-01-09 13:07:27 -06:00
)
.setBuffer(buffer);
const primitive = document
.createPrimitive()
.setIndices(indicesAccessor);
accessors.forEach((accessor, name) => {
primitive.setAttribute(name, accessor);
});
mesh.addPrimitive(primitive);
2024-01-09 13:36:33 -06:00
node.setMesh(mesh);
parent.addChild(node);
2024-01-09 13:07:27 -06:00
});
2024-01-08 18:34:04 -06:00
break;
}
default:
throw new TypeError(
2026-02-18 16:36:12 -06:00
`Unhandled component type: ${component["comp.typename"]}`,
2024-01-08 18:34:04 -06:00
);
}
});
2025-11-26 13:18:44 -06:00
if (hierarchy["#hierarchy"]) {
parseHierarchies(hierarchy["#hierarchy"], parent);
2024-01-08 18:34:04 -06:00
}
});
}
2025-11-26 13:18:44 -06:00
parseHierarchies(sc2["#hierarchy"], scene);
2024-01-08 18:34:04 -06:00
return document;
2026-08-04 20:18:05 -05:00
}
export const vertexAttributeGLTFName: Partial<Record<VertexAttribute, string>> =
{
[VertexAttribute.VERTEX]: "POSITION",
[VertexAttribute.NORMAL]: "NORMAL",
[VertexAttribute.TEXCOORD0]: "TEXCOORD_0",
[VertexAttribute.TEXCOORD1]: "TEXCOORD_1",
[VertexAttribute.TEXCOORD2]: "TEXCOORD_2",
[VertexAttribute.TEXCOORD3]: "TEXCOORD_3",
};
export const vertexAttributeGltfVectorSizes = {
...vertexAttributeVectorSizes,
[VertexAttribute.TANGENT]: 4,
} as const;