mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-11-21 12:53:56 +01:00
Merge branch 'main' into data-driven
This commit is contained in:
44 files changed
+1337
-358
No files matched your search
@@ -8,18 +8,35 @@ import {
|
||||
toUniqueId,
|
||||
VertexAttribute,
|
||||
type ConfigArchive,
|
||||
type Hierarchy,
|
||||
type TankParameters,
|
||||
type Textures,
|
||||
type VehicleDefinitionList,
|
||||
} from "@blitzkit/core";
|
||||
import type { AbstractVFS } from "@blitzkit/core/src/blitzkit/vfs/abstract";
|
||||
import { Document, Material, Node, NodeIO, Scene } from "@gltf-transform/core";
|
||||
import {
|
||||
Document,
|
||||
Material,
|
||||
Node,
|
||||
NodeIO,
|
||||
Scene,
|
||||
type Skin,
|
||||
} from "@gltf-transform/core";
|
||||
import { ALL_EXTENSIONS } from "@gltf-transform/extensions";
|
||||
import { dedup, prune } from "@gltf-transform/functions";
|
||||
import type { APIContext } from "astro";
|
||||
import { isEqual, times } from "lodash-es";
|
||||
import { dirname } from "path";
|
||||
import sharp from "sharp";
|
||||
import type { Motion } from "../../../core/blitzkit/skinAnimations/motion";
|
||||
import {
|
||||
bindSocket,
|
||||
createMotionClip,
|
||||
createSkeleton,
|
||||
loadMotion,
|
||||
skinPrimitive,
|
||||
} from "../../../core/blitzkit/skinAnimations/skeleton";
|
||||
import { partGarage } from "../../../core/blitzkit/switcherStates";
|
||||
import { vfs } from "../../../core/blitzkit/vfs";
|
||||
import {
|
||||
vertexAttributeGLTFName,
|
||||
@@ -79,6 +96,7 @@ export async function extractModel(
|
||||
vfs: AbstractVFS,
|
||||
path: string,
|
||||
isPart = false,
|
||||
preset?: string,
|
||||
) {
|
||||
const sc2Path = `Data/3d/${path}.sc2`;
|
||||
const scgPath = `Data/3d/${path}.scg`;
|
||||
@@ -92,6 +110,35 @@ export async function extractModel(
|
||||
const scene = document.createScene();
|
||||
const buffer = document.createBuffer();
|
||||
const materials = await extractMaterials(document, sc2, path);
|
||||
const skeletons = new Map<Hierarchy, ReturnType<typeof createSkeleton>>();
|
||||
const jointBindings: [
|
||||
owner: Node,
|
||||
entity: string,
|
||||
joint: Node,
|
||||
skin: Skin,
|
||||
][] = [];
|
||||
const motions = new Map<Hierarchy, Motion>();
|
||||
const garage = isPart ? partGarage(sc2, preset) : undefined;
|
||||
|
||||
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) => {
|
||||
motions.set(hierarchy, motion);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
await Promise.all(loading);
|
||||
}
|
||||
|
||||
parseHierarchies<Scene | Node>(
|
||||
sc2["#hierarchy"],
|
||||
@@ -105,6 +152,40 @@ export async function extractModel(
|
||||
|
||||
const node = document.createNode(hierarchy.name);
|
||||
|
||||
if (isPart) {
|
||||
const skeleton = components.find(
|
||||
(component) => component["comp.typename"] === "SkeletonComponent",
|
||||
);
|
||||
const motionComponent = components.find(
|
||||
(component) => component["comp.typename"] === "MotionComponent",
|
||||
);
|
||||
const motion = motions.get(hierarchy);
|
||||
|
||||
if (skeleton) {
|
||||
const created = createSkeleton(
|
||||
document,
|
||||
buffer,
|
||||
node,
|
||||
skeleton,
|
||||
motion,
|
||||
);
|
||||
|
||||
skeletons.set(hierarchy, created);
|
||||
|
||||
// only what loops in the garage is exported
|
||||
if (motion && motionComponent && garage?.motions.has(hierarchy)) {
|
||||
createMotionClip(
|
||||
document,
|
||||
buffer,
|
||||
`motion${document.getRoot().listAnimations().length}`,
|
||||
created.joints,
|
||||
motion,
|
||||
motionComponent,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
components.forEach((component) => {
|
||||
switch (component["comp.typename"]) {
|
||||
case "LodComponent":
|
||||
@@ -125,10 +206,36 @@ export async function extractModel(
|
||||
|
||||
case "StateSwitcherComponent":
|
||||
node.setExtras({
|
||||
state: component[`ssc.state${component["ssc.activeState"]}`],
|
||||
// a part's scenarios can pick its state as it loads
|
||||
state: garage
|
||||
? (garage.states.get(hierarchy) ?? null)
|
||||
: component[`ssc.state${component["ssc.activeState"]}`],
|
||||
});
|
||||
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);
|
||||
|
||||
if (!joint) throw new Error(`Missing joint ${binding.jointUid}`);
|
||||
|
||||
jointBindings.push([
|
||||
node,
|
||||
binding.entityUid,
|
||||
joint,
|
||||
skeleton.skin,
|
||||
]);
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
case "RenderComponent": {
|
||||
const renderObject = component["rc.renderObj"];
|
||||
|
||||
@@ -214,10 +321,24 @@ export async function extractModel(
|
||||
primitive.setAttribute(name, attributeAccessor);
|
||||
});
|
||||
|
||||
const skeleton = skeletons.get(hierarchy);
|
||||
const mesh = document
|
||||
.createMesh(batch["##name"])
|
||||
.addPrimitive(primitive);
|
||||
lodNode.setMesh(mesh);
|
||||
|
||||
if (
|
||||
skeleton &&
|
||||
skinPrimitive(
|
||||
document,
|
||||
buffer,
|
||||
primitive,
|
||||
attributes,
|
||||
renderObject[`skinnedObject.batch${batchIndex}.targetsData`],
|
||||
)
|
||||
) {
|
||||
lodNode.setSkin(skeleton.skin);
|
||||
}
|
||||
node.addChild(lodNode);
|
||||
});
|
||||
|
||||
@@ -240,6 +361,20 @@ export async function extractModel(
|
||||
},
|
||||
);
|
||||
|
||||
for (const [owner, entity, joint, skin] of jointBindings) {
|
||||
// the game only binds the skeleton entity's own children
|
||||
const socket = owner
|
||||
.listChildren()
|
||||
.find(
|
||||
(child) =>
|
||||
child.getName() === entity && !skin.listJoints().includes(child),
|
||||
);
|
||||
|
||||
if (!socket) throw new Error(`Missing socket ${entity}`);
|
||||
|
||||
bindSocket(socket, joint, skin);
|
||||
}
|
||||
|
||||
scene.addChild(document.createNode("test"));
|
||||
|
||||
await document.transform(prune({ keepAttributes: true }), dedup());
|
||||
|
||||
Reference in new issue
Block a user