mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-11-30 05:14:06 +01:00
apply batched mesh to all things
This commit is contained in:
21 files changed
+844
-892
No files matched your search
@@ -0,0 +1,107 @@
|
||||
import { useEffect, useMemo } from "react";
|
||||
import {
|
||||
Mesh,
|
||||
MeshStandardMaterial,
|
||||
type Material,
|
||||
type Matrix4,
|
||||
type Object3D,
|
||||
} from "three";
|
||||
import type { BatchItem } from "../components/Batch";
|
||||
import { prepareMaterial } from "../core/blitzkit/jsxTree";
|
||||
import { nameOf } from "../core/blitzkit/skinTree";
|
||||
import { flattenNodes } from "../core/three/flattenNodes";
|
||||
import type { Skin } from "./useSkin";
|
||||
|
||||
interface BatchGroup {
|
||||
material: MeshStandardMaterial;
|
||||
items: BatchItem[];
|
||||
}
|
||||
|
||||
interface SkinPart {
|
||||
part: Object3D;
|
||||
matrix: Matrix4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Model nodes grouped into one batch per material. Skins are layered in order,
|
||||
* so an attachment's states win over the camouflage's. Their parts aren't
|
||||
* batched so their bones can animate.
|
||||
*/
|
||||
export function useBatchGroups(nodes: Object3D[], skins: Skin[]) {
|
||||
const batchGroups = useMemo(() => {
|
||||
const skinMaterials = new Map<string, Material>();
|
||||
const states: Record<string, string | null> = {};
|
||||
|
||||
for (const skin of skins) {
|
||||
for (const [name, material] of skin.materials) {
|
||||
skinMaterials.set(name, material);
|
||||
}
|
||||
|
||||
Object.assign(states, skin.scene.userData.states);
|
||||
}
|
||||
|
||||
function stateChildren(object: Object3D) {
|
||||
if (object.userData.state === undefined) return object.children;
|
||||
|
||||
const name = nameOf(object);
|
||||
const state = name in states ? states[name] : object.userData.state;
|
||||
|
||||
return object.children.filter((child) => nameOf(child) === state);
|
||||
}
|
||||
|
||||
const groups = new Map<Material, BatchGroup>();
|
||||
const parts: SkinPart[] = [];
|
||||
|
||||
for (const { object, matrix } of flattenNodes(nodes, stateChildren)) {
|
||||
for (const skin of skins) {
|
||||
for (const part of skin.parts.get(object.name) ?? []) {
|
||||
parts.push({ part, matrix });
|
||||
}
|
||||
}
|
||||
|
||||
if (!(object instanceof Mesh)) continue;
|
||||
|
||||
const source =
|
||||
skinMaterials.get((object.material as Material).name) ??
|
||||
object.material;
|
||||
|
||||
if (!(source instanceof MeshStandardMaterial)) continue;
|
||||
|
||||
let group = groups.get(source);
|
||||
|
||||
if (!group) {
|
||||
const material = source.clone();
|
||||
|
||||
prepareMaterial(material);
|
||||
group = { material, items: [] };
|
||||
groups.set(source, group);
|
||||
}
|
||||
|
||||
group.items.push({ mesh: object, matrix });
|
||||
}
|
||||
|
||||
const materials = [...groups.values()].map(({ material }) => material);
|
||||
|
||||
for (const { part } of parts) {
|
||||
part.traverse((object) => {
|
||||
if (
|
||||
object instanceof Mesh &&
|
||||
object.material instanceof MeshStandardMaterial
|
||||
) {
|
||||
materials.push(object.material);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return { groups: [...groups.values()], parts, materials };
|
||||
}, [nodes, skins]);
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
for (const { material } of batchGroups.groups) material.dispose();
|
||||
},
|
||||
[batchGroups],
|
||||
);
|
||||
|
||||
return batchGroups;
|
||||
}
|
||||
Reference in new issue
Block a user