Files
blitzkit/src/core/blitz/extractModel/index.ts
T

284 lines
9.4 KiB
TypeScript
Raw Normal View History

2023-12-25 12:37:37 -06:00
import { Document, Material, Node, Scene } from '@gltf-transform/core';
2024-02-19 18:45:43 -06:00
import { times } from 'lodash';
2023-12-25 12:37:37 -06:00
import { dirname } from 'path';
2024-03-13 17:00:40 -05:00
import { Matrix4, Quaternion, Vector3, Vector4Tuple } from 'three';
2024-04-23 09:57:59 -05:00
import { readTexture } from '../../blitzkit/readTexture';
import { TextureMutation } from '../../blitzkit/readTexture/constants';
2024-03-12 23:36:56 -05:00
import { Hierarchy, Sc2ReadStream, Textures } from '../../streams/sc2';
import { ScgReadStream } from '../../streams/scg';
import { VertexAttribute } from '../../streams/scpg';
import { readDVPLFile } from '../readDVPLFile';
import {
vertexAttributeGLTFName,
vertexAttributeGltfVectorSizes,
} from './constants';
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 = {
2024-01-09 13:07:27 -06:00
start: ['chassis_chassis_', 'chassis_track_crash_', 'HP_'],
end: ['_POINT'],
2023-12-31 19:26:08 -06:00
};
2023-12-30 13:02:47 -06:00
2024-03-13 17:00:40 -05:00
export async function extractModel(data: string, path: string) {
2023-12-25 12:37:37 -06:00
const sc2Path = `${data}/3d/${path}.sc2.dvpl`;
const scgPath = `${data}/3d/${path}.scg.dvpl`;
2024-02-18 16:17:00 -06:00
const sc2 = new Sc2ReadStream((await readDVPLFile(sc2Path)).buffer).sc2();
const scg = new ScgReadStream((await readDVPLFile(scgPath)).buffer).scg();
2023-12-25 12:37:37 -06:00
const document = new Document();
const scene = document.createScene();
const buffer = document.createBuffer();
2024-02-20 11:21:01 -06:00
const materials = new Map<bigint, Material | bigint | undefined>();
2023-12-25 12:37:37 -06:00
// create materials
await Promise.all(
sc2['#dataNodes'].map(async (node) => {
2024-02-18 16:17:00 -06:00
const id = new DataView(node['#id']).getBigUint64(0, true);
2023-12-25 12:37:37 -06: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);
return;
}
const material = document.createMaterial(node.materialName);
let textures: Textures | undefined = undefined;
if (node.textures) {
textures = node.textures;
} else if (typeof node.configCount === 'number') {
textures = node.configArchive_0.textures;
}
if (textures) {
material.setBaseColorTexture(
document
.createTexture(node.materialName)
2024-03-16 15:34:48 -05:00
.setMimeType('image/jpeg')
2023-12-25 12:37:37 -06:00
.setImage(
await readTexture(
`${data}/3d/${dirname(path)}/${textures.albedo}`,
2024-03-13 17:00:40 -05:00
TextureMutation.Albedo,
2023-12-25 12:37:37 -06:00
),
),
);
if (textures.baseRMMap) {
material.setMetallicRoughnessTexture(
document
.createTexture(node.materialName)
2024-03-16 15:34:48 -05:00
.setMimeType('image/jpeg')
2023-12-25 12:37:37 -06:00
.setImage(
2023-12-26 20:02:01 -06:00
await readTexture(
`${data}/3d/${dirname(path)}/${textures.baseRMMap}`,
2024-03-13 17:00:40 -05:00
TextureMutation.RoughnessMetallicness,
2023-12-26 20:02:01 -06:00
),
2023-12-25 12:37:37 -06:00
),
);
}
2023-12-29 19:56:50 -06:00
if (textures.baseNormalMap ?? textures.normalmap) {
const isBase = textures.baseNormalMap !== undefined;
2023-12-25 12:37:37 -06:00
material.setNormalTexture(
document
.createTexture(node.materialName)
2024-03-16 15:34:48 -05:00
.setMimeType('image/jpeg')
2023-12-25 12:37:37 -06:00
.setImage(
2023-12-26 20:02:01 -06:00
await readTexture(
2023-12-29 19:56:50 -06:00
`${data}/3d/${dirname(path)}/${
textures.baseNormalMap ?? textures.normalmap
}`,
2024-03-13 17:00:40 -05:00
isBase ? TextureMutation.Normal : undefined,
2023-12-26 20:02:01 -06:00
),
2023-12-25 12:37:37 -06:00
),
);
}
2024-01-29 15:27:21 -06:00
if (textures.miscMap) {
material.setOcclusionTexture(
document
.createTexture(node.materialName)
2024-03-16 15:34:48 -05:00
.setMimeType('image/jpeg')
2024-01-29 15:27:21 -06:00
.setImage(
await readTexture(
`${data}/3d/${dirname(path)}/${textures.miscMap}`,
2024-03-13 17:00:40 -05:00
TextureMutation.Miscellaneous,
2024-01-29 15:27:21 -06:00
),
),
);
}
2023-12-25 12:37:37 -06:00
2024-02-20 11:21:01 -06:00
materials.set(
new DataView(node['#id']).getBigUint64(0, true),
material,
);
}
2023-12-25 12:37:37 -06:00
}),
);
// replace children materials with parents
materials.forEach((material, id) => {
if (typeof material !== 'bigint') return;
2024-02-20 11:21:01 -06:00
let resolvedMaterial: Material | bigint | undefined = material;
2023-12-25 12:37:37 -06:00
while (typeof resolvedMaterial === 'bigint') {
const linkedParentMaterial = materials.get(resolvedMaterial);
resolvedMaterial = linkedParentMaterial;
}
materials.set(id, resolvedMaterial);
});
function parseHierarchies(hierarchies: Hierarchy[], parent: Scene | Node) {
hierarchies.forEach((hierarchy) => {
2023-12-31 19:26:08 -06:00
if (
omitMeshNames.start.some((omit) => hierarchy.name.startsWith(omit)) ||
omitMeshNames.end.some((omit) => hierarchy.name.endsWith(omit))
)
return;
2023-12-30 13:02:47 -06:00
2023-12-25 12:37:37 -06:00
const node = document.createNode(hierarchy.name);
const components = times(
hierarchy.components.count,
(index) => hierarchy.components[index.toString().padStart(4, '0')],
);
components.forEach((component) => {
switch (component['comp.typename']) {
2024-01-20 23:45:45 -06:00
case 'LodComponent':
// found and used later by transform component
// TODO: lod component always shows up before transform component; cache it before parsing transform
break;
2023-12-25 12:37:37 -06:00
case 'TransformComponent': {
const translation = new Vector3();
const rotation = new Quaternion();
const scale = new Vector3();
new Matrix4()
.multiplyMatrices(
new Matrix4().compose(
new Vector3().fromArray(component['tc.worldTranslation']),
new Quaternion().fromArray(component['tc.worldRotation']),
new Vector3().fromArray(component['tc.worldScale']),
),
new Matrix4().compose(
new Vector3().fromArray(component['tc.localTranslation']),
new Quaternion().fromArray(component['tc.localRotation']),
new Vector3().fromArray(component['tc.localScale']),
),
)
.decompose(translation, rotation, scale);
node.setTranslation(translation.toArray());
node.setRotation(rotation.toArray() as Vector4Tuple);
node.setScale(scale.toArray());
break;
}
case 'RenderComponent': {
2024-02-20 11:21:01 -06:00
const renderObject = component['rc.renderObj'];
2023-12-25 12:37:37 -06:00
2024-02-20 11:21:01 -06:00
times(renderObject['ro.batchCount'], (batchIndex): void => {
const lodIndex = renderObject[`rb${batchIndex}.lodIndex`];
2023-12-25 12:37:37 -06:00
2024-02-20 11:21:01 -06:00
if (lodIndex !== 0) return;
2023-12-25 12:37:37 -06:00
2024-02-20 11:21:01 -06: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']);
2023-12-25 12:37:37 -06:00
2024-02-20 11:21:01 -06:00
if (!(material instanceof Material)) {
// probably shadow material
return;
}
if (polygonGroup === undefined) {
throw new Error(
`Missing polygon group ${batch['rb.datasource']}`,
);
}
2023-12-25 12:37:37 -06:00
2024-02-20 11:21:01 -06:00
const lodNode = document.createNode(batchKey);
const indicesAccessor = document
.createAccessor()
.setType('SCALAR')
.setArray(new Uint16Array(polygonGroup.indices))
2023-12-30 12:43:46 -06:00
.setBuffer(buffer);
2024-02-20 11:21:01 -06:00
const primitive = document
.createPrimitive()
.setIndices(indicesAccessor)
.setMaterial(material);
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);
});
});
if (attributes.has(VertexAttribute.TANGENT)) {
attributes.set(
VertexAttribute.TANGENT,
attributes
.get(VertexAttribute.TANGENT)!
.map((tangent) => [...tangent, 1]),
);
}
attributes.forEach((value, attribute) => {
const name = vertexAttributeGLTFName[attribute];
if (!name || primitive.getAttribute(name)) 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);
});
const mesh = document
.createMesh(batch['##name'])
.addPrimitive(primitive);
lodNode.setMesh(mesh);
node.addChild(lodNode);
2023-12-25 12:37:37 -06:00
});
break;
}
2024-01-20 23:20:00 -06:00
default: {
if (ERROR_ON_UNKNOWN_COMPONENT) {
2024-01-20 23:45:45 -06:00
throw new Error(
`Unhandled component type: ${component['comp.typename']}`,
);
2024-01-20 23:20:00 -06:00
}
}
2023-12-25 12:37:37 -06:00
}
});
if (hierarchy['#hierarchy']) {
parseHierarchies(hierarchy['#hierarchy'], node);
}
parent.addChild(node);
});
}
2024-01-01 14:48:46 -06:00
parseHierarchies(sc2['#hierarchy'], scene);
2023-12-25 12:37:37 -06:00
return document;
}