2024-09-07 21:45:00 -05:00
|
|
|
import {
|
2026-03-11 16:50:18 -05:00
|
|
|
bufferToBigInt,
|
2026-08-04 20:18:05 -05:00
|
|
|
DdsReadStream,
|
2026-10-08 20:25:03 +13:00
|
|
|
parseHierarchies,
|
2026-08-04 20:18:05 -05:00
|
|
|
PvrReadStream,
|
2024-09-07 21:45:00 -05:00
|
|
|
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
|
|
|
type ConfigArchive,
|
2026-10-20 18:41:01 +13:00
|
|
|
type Hierarchy,
|
2026-08-04 20:18:05 -05:00
|
|
|
type TankParameters,
|
|
|
|
|
type Textures,
|
|
|
|
|
type VehicleDefinitionList,
|
2025-09-05 20:45:04 -05:00
|
|
|
} from "@blitzkit/core";
|
2026-08-04 20:18:05 -05:00
|
|
|
import type { AbstractVFS } from "@blitzkit/core/src/blitzkit/vfs/abstract";
|
2026-10-20 18:41:01 +13:00
|
|
|
import {
|
|
|
|
|
Document,
|
|
|
|
|
Material,
|
|
|
|
|
Node,
|
|
|
|
|
NodeIO,
|
|
|
|
|
Scene,
|
|
|
|
|
type Skin,
|
|
|
|
|
} from "@gltf-transform/core";
|
2026-08-04 20:18:05 -05:00
|
|
|
import { ALL_EXTENSIONS } from "@gltf-transform/extensions";
|
2025-09-05 20:45:04 -05:00
|
|
|
import { dedup, prune } from "@gltf-transform/functions";
|
2026-08-04 20:18:05 -05:00
|
|
|
import type { APIContext } from "astro";
|
2026-10-20 17:20:57 +13:00
|
|
|
import { isEqual, times } from "lodash-es";
|
2025-09-05 20:45:04 -05:00
|
|
|
import { dirname } from "path";
|
2026-08-04 20:18:05 -05:00
|
|
|
import sharp from "sharp";
|
2026-10-31 22:15:57 +13:00
|
|
|
import type { Motion } from "../../../core/blitzkit/skinAnimations/motion";
|
2026-10-20 18:41:01 +13:00
|
|
|
import {
|
|
|
|
|
bindSocket,
|
|
|
|
|
createMotionClip,
|
|
|
|
|
createSkeleton,
|
|
|
|
|
loadMotion,
|
|
|
|
|
skinPrimitive,
|
2026-10-31 22:15:57 +13:00
|
|
|
} from "../../../core/blitzkit/skinAnimations/skeleton";
|
|
|
|
|
import { partGarage } from "../../../core/blitzkit/switcherStates";
|
2026-10-21 22:56:40 -05:00
|
|
|
import { vfs } from "../../../core/blitzkit/vfs";
|
2024-03-12 23:36:56 -05:00
|
|
|
import {
|
|
|
|
|
vertexAttributeGLTFName,
|
|
|
|
|
vertexAttributeGltfVectorSizes,
|
2026-08-04 20:18:05 -05:00
|
|
|
} from "./collision.glb";
|
|
|
|
|
|
|
|
|
|
export { getStaticPaths } from "./_index";
|
|
|
|
|
|
|
|
|
|
export async function GET({ props }: APIContext<{ id: number }>) {
|
|
|
|
|
const nodeIO = new NodeIO().registerExtensions(ALL_EXTENSIONS);
|
|
|
|
|
const nations = await vfs
|
|
|
|
|
.dir(`Data/XML/item_defs/vehicles`)
|
|
|
|
|
.then((files) => files.filter((nation) => nation !== "common"));
|
|
|
|
|
|
|
|
|
|
for (const nationIndex in nations) {
|
|
|
|
|
const nation = nations[nationIndex];
|
|
|
|
|
const tanks = await vfs.xml<{ root: VehicleDefinitionList }>(
|
|
|
|
|
`Data/XML/item_defs/vehicles/${nation}/list.xml`,
|
|
|
|
|
);
|
|
|
|
|
const entries = Object.entries(tanks.root);
|
|
|
|
|
|
|
|
|
|
for (const [tankKey, tank] of entries) {
|
|
|
|
|
if (tankKey.includes("tutorial_bot")) continue;
|
|
|
|
|
|
|
|
|
|
const id = toUniqueId(nation, tank.id);
|
|
|
|
|
|
|
|
|
|
if (id !== props.id) continue;
|
|
|
|
|
|
|
|
|
|
const parameters = await vfs.yaml<TankParameters>(
|
|
|
|
|
`Data/3d/Tanks/Parameters/${nation}/${tankKey}.yaml`,
|
|
|
|
|
);
|
|
|
|
|
const model = await extractModel(
|
|
|
|
|
vfs,
|
|
|
|
|
parameters.resourcesPath.blitzModelPath.replace(/\.sc2$/, ""),
|
|
|
|
|
);
|
|
|
|
|
const bytes = await nodeIO.writeBinary(model);
|
|
|
|
|
|
|
|
|
|
return new Response(bytes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-25 12:37:37 -06:00
|
|
|
|
2024-01-20 23:20:00 -06:00
|
|
|
const ERROR_ON_UNKNOWN_COMPONENT = false;
|
2023-12-30 12:43:46 -06:00
|
|
|
|
2023-12-31 19:26:08 -06:00
|
|
|
const omitMeshNames = {
|
2026-10-28 20:49:14 +13:00
|
|
|
start: [
|
|
|
|
|
"chassis_chassis_",
|
|
|
|
|
"chassis_track_crash_",
|
|
|
|
|
"HP_",
|
|
|
|
|
// mastery marks a new tank doesn't have yet
|
|
|
|
|
"custom_gun_marks_",
|
|
|
|
|
],
|
2025-09-05 20:45:04 -05:00
|
|
|
end: ["_POINT"],
|
2023-12-31 19:26:08 -06:00
|
|
|
};
|
2023-12-30 13:02:47 -06:00
|
|
|
|
2026-10-20 17:20:57 +13:00
|
|
|
export async function extractModel(
|
|
|
|
|
vfs: AbstractVFS,
|
|
|
|
|
path: string,
|
|
|
|
|
isPart = false,
|
2026-10-31 22:15:57 +13:00
|
|
|
preset?: string,
|
2026-10-20 17:20:57 +13:00
|
|
|
) {
|
2025-11-26 13:18:44 -06:00
|
|
|
const sc2Path = `Data/3d/${path}.sc2`;
|
|
|
|
|
const scgPath = `Data/3d/${path}.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();
|
2023-12-25 12:37:37 -06:00
|
|
|
const document = new Document();
|
|
|
|
|
const scene = document.createScene();
|
|
|
|
|
const buffer = document.createBuffer();
|
2026-10-20 17:20:57 +13:00
|
|
|
const materials = await extractMaterials(document, sc2, path);
|
2026-10-20 18:41:01 +13:00
|
|
|
const skeletons = new Map<Hierarchy, ReturnType<typeof createSkeleton>>();
|
|
|
|
|
const jointBindings: [
|
|
|
|
|
owner: Node,
|
|
|
|
|
entity: string,
|
|
|
|
|
joint: Node,
|
|
|
|
|
skin: Skin,
|
|
|
|
|
][] = [];
|
|
|
|
|
const motions = new Map<Hierarchy, Motion>();
|
2026-10-31 22:15:57 +13:00
|
|
|
const garageMotions = isPart ? partGarage(sc2, preset).motions : new Set();
|
2026-10-20 18:41:01 +13:00
|
|
|
|
|
|
|
|
if (isPart) {
|
|
|
|
|
const loading: Promise<void>[] = [];
|
|
|
|
|
|
|
|
|
|
parseHierarchies(sc2["#hierarchy"], null, (hierarchy, components) => {
|
|
|
|
|
for (const component of components) {
|
|
|
|
|
if (component["comp.typename"] !== "MotionComponent") continue;
|
|
|
|
|
|
|
|
|
|
loading.push(
|
|
|
|
|
loadMotion(vfs, path, component).then((motion) => {
|
2026-10-31 22:15:57 +13:00
|
|
|
motions.set(hierarchy, motion);
|
2026-10-20 18:41:01 +13:00
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await Promise.all(loading);
|
|
|
|
|
}
|
2026-10-20 17:20:57 +13:00
|
|
|
|
|
|
|
|
parseHierarchies<Scene | Node>(
|
|
|
|
|
sc2["#hierarchy"],
|
|
|
|
|
scene,
|
|
|
|
|
(hierarchy, components, parent) => {
|
|
|
|
|
if (
|
|
|
|
|
omitMeshNames.start.some((omit) => hierarchy.name.startsWith(omit)) ||
|
|
|
|
|
omitMeshNames.end.some((omit) => hierarchy.name.endsWith(omit))
|
|
|
|
|
)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const node = document.createNode(hierarchy.name);
|
|
|
|
|
|
2026-10-20 18:41:01 +13:00
|
|
|
if (isPart) {
|
|
|
|
|
const skeleton = components.find(
|
|
|
|
|
(component) => component["comp.typename"] === "SkeletonComponent",
|
|
|
|
|
);
|
|
|
|
|
const motionComponent = components.find(
|
|
|
|
|
(component) => component["comp.typename"] === "MotionComponent",
|
|
|
|
|
);
|
2026-10-31 22:15:57 +13:00
|
|
|
const motion = motions.get(hierarchy);
|
2026-10-20 18:41:01 +13:00
|
|
|
|
|
|
|
|
if (skeleton) {
|
|
|
|
|
const created = createSkeleton(
|
|
|
|
|
document,
|
|
|
|
|
buffer,
|
|
|
|
|
node,
|
|
|
|
|
skeleton,
|
|
|
|
|
motion,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
skeletons.set(hierarchy, created);
|
|
|
|
|
|
2026-10-31 22:15:57 +13:00
|
|
|
// only what loops in the garage is exported
|
|
|
|
|
if (motion && motionComponent && garageMotions.has(hierarchy)) {
|
|
|
|
|
createMotionClip(
|
|
|
|
|
document,
|
|
|
|
|
buffer,
|
|
|
|
|
`motion${document.getRoot().listAnimations().length}`,
|
|
|
|
|
created.joints,
|
|
|
|
|
motion,
|
|
|
|
|
motionComponent,
|
|
|
|
|
);
|
2026-10-20 18:41:01 +13:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-20 17:20:57 +13:00
|
|
|
components.forEach((component) => {
|
|
|
|
|
switch (component["comp.typename"]) {
|
|
|
|
|
case "LodComponent":
|
|
|
|
|
// found and used later by transform component
|
|
|
|
|
// TODO: lod component always shows up before transform component; cache it before parsing transform
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "TransformComponent": {
|
|
|
|
|
const localTranslation = component["tc.localTranslation"];
|
|
|
|
|
// The game resets top-level node translation to [0, 0, 0] on load.
|
|
|
|
|
// Child nodes and customization parts must keep their authored
|
|
|
|
|
// local offsets.
|
|
|
|
|
node.setTranslation(
|
|
|
|
|
parent instanceof Scene && !isPart ? [0, 0, 0] : localTranslation,
|
|
|
|
|
);
|
|
|
|
|
node.setRotation(component["tc.localRotation"]);
|
|
|
|
|
node.setScale(component["tc.localScale"]);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "StateSwitcherComponent":
|
2026-10-22 16:12:03 +13:00
|
|
|
node.setExtras({
|
2026-11-01 14:44:03 +13:00
|
|
|
// a part's scenarios can pick its state as it loads
|
|
|
|
|
state:
|
|
|
|
|
component[`ssc.state${component["ssc.activeState"]}`] ??
|
|
|
|
|
(isPart ? null : undefined),
|
2026-10-20 17:20:57 +13:00
|
|
|
});
|
2026-10-20 18:41:01 +13:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "SkeletonJointBindingComponent": {
|
|
|
|
|
const skeleton = skeletons.get(hierarchy);
|
|
|
|
|
|
|
|
|
|
if (!skeleton) break;
|
|
|
|
|
|
|
|
|
|
times(component.count, (index) => {
|
|
|
|
|
const binding = component.map[
|
|
|
|
|
index.toString().padStart(4, "0")
|
|
|
|
|
] as unknown as { entityUid: string; jointUid: string };
|
|
|
|
|
const joint = skeleton.joints.get(binding.jointUid);
|
|
|
|
|
|
2026-10-31 22:15:57 +13:00
|
|
|
if (!joint) throw new Error(`Missing joint ${binding.jointUid}`);
|
|
|
|
|
|
|
|
|
|
jointBindings.push([
|
|
|
|
|
node,
|
|
|
|
|
binding.entityUid,
|
|
|
|
|
joint,
|
|
|
|
|
skeleton.skin,
|
|
|
|
|
]);
|
2026-10-20 18:41:01 +13:00
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-20 17:20:57 +13:00
|
|
|
case "RenderComponent": {
|
|
|
|
|
const renderObject = component["rc.renderObj"];
|
|
|
|
|
|
|
|
|
|
times(renderObject["ro.batchCount"], (batchIndex): void => {
|
|
|
|
|
const lodIndex = renderObject[`rb${batchIndex}.lodIndex`];
|
|
|
|
|
|
2026-10-20 18:41:01 +13:00
|
|
|
// -1 means the batch has no lods and is always drawn
|
|
|
|
|
if (lodIndex !== 0 && !(isPart && lodIndex === -1)) return;
|
2026-10-20 17:20:57 +13:00
|
|
|
|
|
|
|
|
const batchKey = batchIndex.toString().padStart(4, "0");
|
|
|
|
|
const batch = renderObject["ro.batches"][batchKey];
|
|
|
|
|
const material = materials.get(batch["rb.nmatname"]);
|
|
|
|
|
const polygonGroup = scg.get(batch["rb.datasource"]);
|
|
|
|
|
|
|
|
|
|
if (!(material instanceof Material)) {
|
|
|
|
|
// probably shadow material
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (polygonGroup === undefined) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Missing polygon group ${batch["rb.datasource"]}`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const lodNode = document.createNode(batchKey);
|
|
|
|
|
const indicesAccessor = document
|
|
|
|
|
.createAccessor()
|
|
|
|
|
.setType("SCALAR")
|
|
|
|
|
.setArray(new Uint16Array(polygonGroup.indices))
|
|
|
|
|
.setBuffer(buffer);
|
|
|
|
|
const primitive = document
|
|
|
|
|
.createPrimitive()
|
|
|
|
|
.setIndices(indicesAccessor)
|
|
|
|
|
.setMaterial(material)
|
|
|
|
|
.setName(batchKey);
|
|
|
|
|
|
|
|
|
|
const attributes = new Map<VertexAttribute, number[][]>();
|
|
|
|
|
|
|
|
|
|
polygonGroup.vertices.forEach((vertex) => {
|
|
|
|
|
vertex.forEach(({ attribute, value }) => {
|
|
|
|
|
if (!attributes.has(attribute)) {
|
|
|
|
|
attributes.set(attribute, []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attributes.get(attribute)!.push(value);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
attributes.forEach((value, attribute) => {
|
|
|
|
|
const name = vertexAttributeGLTFName[attribute];
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
name === undefined ||
|
|
|
|
|
primitive.getAttribute(name) !== null
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const vertexSize = vertexAttributeGltfVectorSizes[attribute];
|
|
|
|
|
const attributeAccessor = document
|
|
|
|
|
.createAccessor(name)
|
|
|
|
|
.setType(vertexSize === 1 ? "SCALAR" : `VEC${vertexSize}`)
|
|
|
|
|
.setArray(new Float32Array(value.flat()))
|
|
|
|
|
.setBuffer(buffer);
|
|
|
|
|
|
|
|
|
|
primitive.setAttribute(name, attributeAccessor);
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-20 18:41:01 +13:00
|
|
|
const skeleton = skeletons.get(hierarchy);
|
2026-10-20 17:20:57 +13:00
|
|
|
const mesh = document
|
|
|
|
|
.createMesh(batch["##name"])
|
|
|
|
|
.addPrimitive(primitive);
|
|
|
|
|
lodNode.setMesh(mesh);
|
2026-10-20 18:41:01 +13:00
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
skeleton &&
|
|
|
|
|
skinPrimitive(
|
|
|
|
|
document,
|
|
|
|
|
buffer,
|
|
|
|
|
primitive,
|
|
|
|
|
attributes,
|
|
|
|
|
renderObject[`skinnedObject.batch${batchIndex}.targetsData`],
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
lodNode.setSkin(skeleton.skin);
|
|
|
|
|
}
|
2026-10-20 17:20:57 +13:00
|
|
|
node.addChild(lodNode);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default: {
|
|
|
|
|
if (ERROR_ON_UNKNOWN_COMPONENT) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Unhandled component type: ${component["comp.typename"]}`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
parent.addChild(node);
|
|
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-10-20 18:41:01 +13:00
|
|
|
for (const [owner, entity, joint, skin] of jointBindings) {
|
|
|
|
|
const socket = findNode(owner, entity);
|
|
|
|
|
|
2026-10-31 22:15:57 +13:00
|
|
|
if (!socket) throw new Error(`Missing socket ${entity}`);
|
|
|
|
|
|
|
|
|
|
bindSocket(socket, joint, skin);
|
2026-10-20 18:41:01 +13:00
|
|
|
}
|
|
|
|
|
|
2026-10-20 17:20:57 +13:00
|
|
|
scene.addChild(document.createNode("test"));
|
|
|
|
|
|
|
|
|
|
await document.transform(prune({ keepAttributes: true }), dedup());
|
|
|
|
|
|
|
|
|
|
return document;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-20 18:41:01 +13:00
|
|
|
function findNode(node: Node, name: string): Node | undefined {
|
|
|
|
|
for (const child of node.listChildren()) {
|
|
|
|
|
if (child.getName() === name) return child;
|
|
|
|
|
|
|
|
|
|
const found = findNode(child, name);
|
|
|
|
|
|
|
|
|
|
if (found) return found;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-20 17:20:57 +13:00
|
|
|
export async function extractMaterials(
|
|
|
|
|
document: Document,
|
|
|
|
|
sc2: ReturnType<Sc2ReadStream["sc2"]>,
|
|
|
|
|
path: string,
|
|
|
|
|
config?: string,
|
|
|
|
|
) {
|
2024-02-20 11:21:01 -06:00
|
|
|
const materials = new Map<bigint, Material | bigint | undefined>();
|
2023-12-25 12:37:37 -06:00
|
|
|
|
2025-10-11 21:22:48 -05:00
|
|
|
for (const node of sc2["#dataNodes"]) {
|
2026-03-11 16:50:18 -05:00
|
|
|
const id = bufferToBigInt(node["#id"]);
|
2023-12-25 12:37:37 -06:00
|
|
|
|
2025-10-11 21:22:48 -05:00
|
|
|
if (node.parentMaterialKey !== undefined) {
|
|
|
|
|
/**
|
|
|
|
|
* material depends on a parent and doesn't seem to have any
|
|
|
|
|
* properties so we just point it to the parent
|
|
|
|
|
*/
|
|
|
|
|
materials.set(id, node.parentMaterialKey);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let textures: Textures | undefined = undefined;
|
|
|
|
|
|
2026-10-20 17:20:57 +13:00
|
|
|
if (config !== undefined) {
|
|
|
|
|
if (typeof node.configCount !== "number") continue;
|
|
|
|
|
|
|
|
|
|
const archive = times(
|
|
|
|
|
node.configCount,
|
|
|
|
|
(index) => node[`configArchive_${index}`],
|
|
|
|
|
).find((archive) => archive.configName === config);
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
!archive ||
|
|
|
|
|
isEqual(archive.textures, node.configArchive_0.textures)
|
|
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
textures = archive.textures;
|
|
|
|
|
} else if (node.textures) {
|
2025-10-11 21:22:48 -05:00
|
|
|
textures = node.textures;
|
|
|
|
|
} else if (typeof node.configCount === "number") {
|
|
|
|
|
textures = node.configArchive_0.textures;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-20 17:20:57 +13:00
|
|
|
const material = document.createMaterial(node.materialName);
|
|
|
|
|
|
2025-10-11 21:22:48 -05:00
|
|
|
if (textures) {
|
2026-08-05 14:17:33 -05:00
|
|
|
const baseColor = await readBaseColor(
|
2026-02-18 18:20:37 -06:00
|
|
|
`Data/3d/${dirname(path)}/${textures.baseColorMap ?? textures.albedo}`,
|
|
|
|
|
);
|
2025-10-11 21:22:48 -05:00
|
|
|
material.setBaseColorTexture(
|
|
|
|
|
document
|
|
|
|
|
.createTexture(node.materialName)
|
|
|
|
|
.setMimeType("image/webp")
|
2026-02-18 18:20:37 -06:00
|
|
|
.setImage(baseColor),
|
2025-10-11 21:22:48 -05:00
|
|
|
);
|
|
|
|
|
|
2026-04-14 11:31:16 -05:00
|
|
|
let defaultConfigArchive: ConfigArchive | undefined = undefined;
|
|
|
|
|
|
|
|
|
|
if (node.configCount) {
|
|
|
|
|
const names = times(
|
|
|
|
|
node.configCount,
|
|
|
|
|
(index) => node[`configArchive_${index}`],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
defaultConfigArchive = names.find(
|
|
|
|
|
(archive) => archive.configName === "Default",
|
|
|
|
|
);
|
|
|
|
|
defaultConfigArchive ??= node.configArchive_0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-12 22:35:55 +02:00
|
|
|
const customCullMode =
|
|
|
|
|
node.customCullMode ?? defaultConfigArchive?.customCullMode;
|
|
|
|
|
|
|
|
|
|
if (customCullMode === 0) {
|
|
|
|
|
material.setDoubleSided(true);
|
|
|
|
|
} else if (customCullMode !== undefined) {
|
|
|
|
|
material.setDoubleSided(false);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-12 22:17:55 +02:00
|
|
|
if (
|
|
|
|
|
node.enabledPresets?.AlphaTest &&
|
2026-04-12 22:35:55 +02:00
|
|
|
node.properties?.alphatestThreshold
|
2026-04-12 22:17:55 +02:00
|
|
|
) {
|
|
|
|
|
const view = new DataView(node.properties.alphatestThreshold);
|
|
|
|
|
const alphaCutoff =
|
|
|
|
|
view.byteLength < 4
|
|
|
|
|
? 0.5
|
|
|
|
|
: view.getFloat32(view.byteLength - 4, true);
|
|
|
|
|
|
|
|
|
|
material.setAlphaMode("MASK").setAlphaCutoff(alphaCutoff);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-11 21:22:48 -05:00
|
|
|
if (node.configCount) {
|
|
|
|
|
for (
|
|
|
|
|
let configIndex = 0;
|
|
|
|
|
configIndex < node.configCount;
|
|
|
|
|
configIndex++
|
|
|
|
|
) {
|
2026-03-11 16:50:18 -05:00
|
|
|
const archive = node[`configArchive_${configIndex}`];
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
archive.configName !== "Default" ||
|
2026-04-12 22:35:55 +02:00
|
|
|
!archive.enabledPresets?.AlphaTest ||
|
|
|
|
|
!archive.properties?.alphatestThreshold
|
2026-03-11 16:50:18 -05:00
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const view = new DataView(archive.properties.alphatestThreshold);
|
2026-04-12 22:17:55 +02:00
|
|
|
const alphaCutoff =
|
|
|
|
|
view.byteLength < 4
|
|
|
|
|
? 0.5
|
|
|
|
|
: view.getFloat32(view.byteLength - 4, true);
|
2026-03-11 16:50:18 -05:00
|
|
|
|
|
|
|
|
material.setAlphaMode("MASK").setAlphaCutoff(alphaCutoff);
|
2025-10-11 21:22:48 -05:00
|
|
|
}
|
2023-12-25 12:37:37 -06:00
|
|
|
}
|
|
|
|
|
|
2026-10-28 20:49:14 +13:00
|
|
|
const enabledPresets =
|
|
|
|
|
node.enabledPresets ?? defaultConfigArchive?.enabledPresets;
|
|
|
|
|
const fxName = node.fxName ?? defaultConfigArchive?.fxName;
|
|
|
|
|
|
|
|
|
|
if (enabledPresets?.AlphaBlend || enabledPresets?.AlphaBlendAdditive) {
|
|
|
|
|
material.setAlphaMode("BLEND");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (enabledPresets?.AlphaBlendAdditive) {
|
|
|
|
|
// gltf has no additive blending so the viewer applies it
|
|
|
|
|
material.setExtras({ blending: "additive" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fxName?.endsWith("/Textured.material")) {
|
2026-10-30 17:26:50 +13:00
|
|
|
// unlit in game, emissive so the viewer can fade it with the lights
|
|
|
|
|
material
|
|
|
|
|
.setEmissiveTexture(material.getBaseColorTexture())
|
|
|
|
|
.setEmissiveFactor([1, 1, 1])
|
|
|
|
|
.setBaseColorFactor([0, 0, 0, 1]);
|
2026-10-28 20:49:14 +13:00
|
|
|
}
|
|
|
|
|
|
2026-08-10 10:39:31 -05:00
|
|
|
if (textures.miscMap) {
|
|
|
|
|
material.setOcclusionTexture(
|
|
|
|
|
document
|
|
|
|
|
.createTexture(node.materialName)
|
|
|
|
|
.setMimeType("image/webp")
|
|
|
|
|
.setImage(
|
|
|
|
|
await readOcclusion(
|
|
|
|
|
`Data/3d/${dirname(path)}/${textures.miscMap}`,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-11 21:22:48 -05:00
|
|
|
if (textures.baseRMMap) {
|
|
|
|
|
material.setMetallicRoughnessTexture(
|
2023-12-25 12:37:37 -06:00
|
|
|
document
|
|
|
|
|
.createTexture(node.materialName)
|
2025-10-11 21:22:48 -05:00
|
|
|
.setMimeType("image/webp")
|
2023-12-25 12:37:37 -06:00
|
|
|
.setImage(
|
2025-10-11 21:22:48 -05:00
|
|
|
await readRoughnessMetallic(
|
2026-02-18 16:36:12 -06:00
|
|
|
`Data/3d/${dirname(path)}/${textures.baseRMMap}`,
|
|
|
|
|
),
|
|
|
|
|
),
|
2023-12-25 12:37:37 -06:00
|
|
|
);
|
2025-10-11 21:22:48 -05:00
|
|
|
}
|
2023-12-25 12:37:37 -06:00
|
|
|
|
2025-10-11 21:22:48 -05:00
|
|
|
if (textures.baseNormalMap ?? textures.normalmap) {
|
|
|
|
|
const isBase = textures.baseNormalMap !== undefined;
|
|
|
|
|
|
|
|
|
|
material.setNormalTexture(
|
|
|
|
|
document
|
|
|
|
|
.createTexture(node.materialName)
|
|
|
|
|
.setMimeType("image/webp")
|
|
|
|
|
.setImage(
|
|
|
|
|
await readNormal(
|
2025-11-26 13:18:44 -06:00
|
|
|
`Data/3d/${dirname(path)}/${
|
2025-10-11 21:22:48 -05:00
|
|
|
textures.baseNormalMap ?? textures.normalmap
|
|
|
|
|
}`,
|
2026-02-18 16:36:12 -06:00
|
|
|
isBase,
|
|
|
|
|
),
|
|
|
|
|
),
|
2024-02-20 11:21:01 -06:00
|
|
|
);
|
|
|
|
|
}
|
2025-10-11 21:22:48 -05:00
|
|
|
|
2026-03-11 16:50:18 -05:00
|
|
|
materials.set(id, material);
|
2025-10-11 21:22:48 -05:00
|
|
|
}
|
|
|
|
|
}
|
2023-12-25 12:37:37 -06:00
|
|
|
|
|
|
|
|
// replace children materials with parents
|
|
|
|
|
materials.forEach((material, id) => {
|
2025-09-05 20:45:04 -05:00
|
|
|
if (typeof material !== "bigint") return;
|
2023-12-25 12:37:37 -06:00
|
|
|
|
2024-02-20 11:21:01 -06:00
|
|
|
let resolvedMaterial: Material | bigint | undefined = material;
|
2023-12-25 12:37:37 -06:00
|
|
|
|
2025-09-05 20:45:04 -05:00
|
|
|
while (typeof resolvedMaterial === "bigint") {
|
2023-12-25 12:37:37 -06:00
|
|
|
const linkedParentMaterial = materials.get(resolvedMaterial);
|
|
|
|
|
|
|
|
|
|
resolvedMaterial = linkedParentMaterial;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
materials.set(id, resolvedMaterial);
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-20 17:20:57 +13:00
|
|
|
return materials;
|
2026-04-14 11:31:16 -05:00
|
|
|
}
|
2026-08-04 20:18:05 -05:00
|
|
|
|
2026-08-10 10:39:31 -05:00
|
|
|
async function readBaseColor(path: string) {
|
|
|
|
|
const raw = await readTexture(path);
|
2026-08-10 16:42:35 -05:00
|
|
|
const image = await sharp(raw.data, { raw }).webp().toBuffer();
|
2026-08-04 20:18:05 -05:00
|
|
|
|
2026-08-10 10:39:31 -05:00
|
|
|
return image;
|
|
|
|
|
}
|
2026-08-04 20:18:05 -05:00
|
|
|
|
2026-08-10 10:39:31 -05:00
|
|
|
async function readOcclusion(path: string) {
|
|
|
|
|
const raw = await readTexture(path);
|
2026-08-04 20:18:05 -05:00
|
|
|
|
2026-08-10 16:42:35 -05:00
|
|
|
const image = await sharp(raw.data, { raw })
|
2026-08-10 10:39:31 -05:00
|
|
|
.extractChannel(3)
|
2026-08-04 20:18:05 -05:00
|
|
|
.webp()
|
|
|
|
|
.toBuffer();
|
|
|
|
|
|
2026-08-05 14:17:33 -05:00
|
|
|
return image;
|
2026-08-04 20:18:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function readRoughnessMetallic(path: string) {
|
|
|
|
|
const raw = await readTexture(path);
|
2026-08-10 16:42:35 -05:00
|
|
|
const blitz = sharp(raw.data, { raw });
|
2026-08-04 20:18:05 -05:00
|
|
|
const metallicness = await blitz?.extractChannel(1).raw().toBuffer();
|
|
|
|
|
const roughness = await blitz?.extractChannel(3).raw().toBuffer();
|
|
|
|
|
|
|
|
|
|
const combined = Buffer.alloc(raw.width * raw.height * 3);
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < raw.width * raw.height; i++) {
|
|
|
|
|
combined[i * 3 + 1] = roughness[i];
|
|
|
|
|
combined[i * 3 + 2] = metallicness[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const image = await sharp(combined, {
|
|
|
|
|
raw: { width: raw.width, height: raw.height, channels: 3 },
|
|
|
|
|
})
|
|
|
|
|
.webp()
|
|
|
|
|
.toBuffer();
|
|
|
|
|
|
|
|
|
|
return image;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function readNormal(path: string, isBase: boolean) {
|
|
|
|
|
const raw = await readTexture(path);
|
|
|
|
|
|
|
|
|
|
if (!isBase) {
|
|
|
|
|
return await sharp(raw.data, { raw }).removeAlpha().jpeg().toBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bytes = 4 * raw.width * raw.height;
|
|
|
|
|
|
|
|
|
|
for (let index = 0; index < bytes; index += 4) {
|
|
|
|
|
/**
|
|
|
|
|
* Red is always 255 and blue is always 0. Only alpha and green contain any
|
|
|
|
|
* sort of information.
|
|
|
|
|
*/
|
|
|
|
|
let x = raw.data[index + 3] * (2 / 255) - 1;
|
|
|
|
|
let y = raw.data[index + 1] * (2 / 255) - 1;
|
|
|
|
|
let z = Math.sqrt(Math.max(0, 1 - x ** 2 - y ** 2));
|
|
|
|
|
|
|
|
|
|
raw.data[index] = Math.round((x + 1) * (255 / 2));
|
|
|
|
|
raw.data[index + 1] = Math.round((y + 1) * (255 / 2));
|
|
|
|
|
raw.data[index + 2] = Math.round((z + 1) * (255 / 2));
|
|
|
|
|
raw.data[index + 3] = 255;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await sharp(raw.data, { raw }).webp().toBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function readTexture(path: string) {
|
|
|
|
|
const ddsTexturePath = path.replace(".tex", ".dx11.dds");
|
|
|
|
|
const isDds = await vfs.resolve(ddsTexturePath);
|
|
|
|
|
const resolvedTexturePath = isDds
|
|
|
|
|
? ddsTexturePath
|
|
|
|
|
: ddsTexturePath.replace(".dds", ".pvr");
|
|
|
|
|
const file = await vfs.file(resolvedTexturePath);
|
|
|
|
|
|
|
|
|
|
const raw = isDds
|
|
|
|
|
? await new DdsReadStream(file.buffer as ArrayBuffer).dds()
|
|
|
|
|
: new PvrReadStream(file.buffer as ArrayBuffer).pvr();
|
|
|
|
|
|
|
|
|
|
return raw;
|
|
|
|
|
}
|