2023-12-25 12:37:37 -06:00
|
|
|
import { Document, Material, Node, Scene } from '@gltf-transform/core';
|
|
|
|
|
import { range, times } from 'lodash';
|
|
|
|
|
import { dirname } from 'path';
|
2023-12-28 20:58:18 -06:00
|
|
|
import {
|
|
|
|
|
Matrix4,
|
|
|
|
|
Quaternion,
|
|
|
|
|
Vector3,
|
|
|
|
|
Vector3Tuple,
|
|
|
|
|
Vector4Tuple,
|
|
|
|
|
} from 'three';
|
|
|
|
|
import { TextureMutation, readTexture } from '../blitzkrieg/readTexture';
|
2023-12-25 12:37:37 -06:00
|
|
|
import { Hierarchy, Sc2Stream, Textures } from '../streams/sc2';
|
|
|
|
|
import { ScgStream, vertexAttributeVectorSizes } from '../streams/scg';
|
|
|
|
|
import { VertexAttribute } from '../streams/scpg';
|
|
|
|
|
import { readDVPLFile } from './readDVPLFile';
|
|
|
|
|
|
|
|
|
|
const MAX_FLOAT32 = 2 ** 127 * (2 - 2 ** -23);
|
|
|
|
|
|
|
|
|
|
const vertexAttributeGLTFName: Partial<Record<VertexAttribute, string>> = {
|
|
|
|
|
[VertexAttribute.VERTEX]: 'POSITION',
|
|
|
|
|
[VertexAttribute.NORMAL]: 'NORMAL',
|
|
|
|
|
[VertexAttribute.COLOR]: 'COLOR_0',
|
|
|
|
|
[VertexAttribute.TEXCOORD0]: 'TEXCOORD_0',
|
|
|
|
|
[VertexAttribute.TEXCOORD1]: 'TEXCOORD_0',
|
|
|
|
|
[VertexAttribute.TEXCOORD2]: 'TEXCOORD_0',
|
|
|
|
|
[VertexAttribute.TEXCOORD3]: 'TEXCOORD_0',
|
|
|
|
|
[VertexAttribute.TANGENT]: 'TANGENT',
|
|
|
|
|
[VertexAttribute.JOINTINDEX]: 'JOINT_0',
|
|
|
|
|
[VertexAttribute.JOINTWEIGHT]: 'WEIGHT_0',
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-30 12:43:46 -06:00
|
|
|
const vertexAttributeGltfVectorSizes = {
|
|
|
|
|
...vertexAttributeVectorSizes,
|
|
|
|
|
|
|
|
|
|
[VertexAttribute.TANGENT]: 4,
|
|
|
|
|
} as const;
|
|
|
|
|
|
2023-12-31 19:26:08 -06:00
|
|
|
const omitMeshNames = {
|
|
|
|
|
start: ['chassis_chassis_', 'chassis_track_crash_', 'HP_'],
|
|
|
|
|
end: ['_POINT'],
|
|
|
|
|
};
|
2023-12-30 13:02:47 -06:00
|
|
|
|
2023-12-28 20:58:18 -06:00
|
|
|
export async function extractModel(
|
|
|
|
|
data: string,
|
|
|
|
|
path: string,
|
2024-01-07 20:07:45 -06:00
|
|
|
baseColor?: Vector3Tuple,
|
2023-12-28 20:58:18 -06:00
|
|
|
) {
|
2023-12-25 12:37:37 -06:00
|
|
|
const sc2Path = `${data}/3d/${path}.sc2.dvpl`;
|
|
|
|
|
const scgPath = `${data}/3d/${path}.scg.dvpl`;
|
|
|
|
|
const sc2 = new Sc2Stream(await readDVPLFile(sc2Path)).sc2();
|
|
|
|
|
const scg = new ScgStream(await readDVPLFile(scgPath)).scg();
|
|
|
|
|
const document = new Document();
|
|
|
|
|
const scene = document.createScene();
|
|
|
|
|
const buffer = document.createBuffer();
|
|
|
|
|
const materials = new Map<bigint, Material | bigint>();
|
|
|
|
|
|
|
|
|
|
// create materials
|
|
|
|
|
await Promise.all(
|
|
|
|
|
sc2['#dataNodes'].map(async (node) => {
|
|
|
|
|
const id = node['#id'].readBigUInt64LE();
|
|
|
|
|
|
|
|
|
|
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)
|
2023-12-26 20:02:01 -06:00
|
|
|
.setMimeType('image/png')
|
2023-12-25 12:37:37 -06:00
|
|
|
.setImage(
|
|
|
|
|
await readTexture(
|
|
|
|
|
`${data}/3d/${dirname(path)}/${textures.albedo}`,
|
2024-01-07 20:07:45 -06:00
|
|
|
baseColor
|
|
|
|
|
? { mutation: TextureMutation.BaseColor, baseColor }
|
|
|
|
|
: undefined,
|
2023-12-25 12:37:37 -06:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (textures.baseRMMap) {
|
|
|
|
|
material.setMetallicRoughnessTexture(
|
|
|
|
|
document
|
|
|
|
|
.createTexture(node.materialName)
|
2023-12-26 20:02:01 -06:00
|
|
|
.setMimeType('image/png')
|
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}`,
|
2023-12-28 20:58:18 -06:00
|
|
|
{ mutation: 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)
|
2023-12-26 20:02:01 -06:00
|
|
|
.setMimeType('image/png')
|
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
|
|
|
|
|
}`,
|
|
|
|
|
isBase ? { mutation: TextureMutation.Normal } : undefined,
|
2023-12-26 20:02:01 -06:00
|
|
|
),
|
2023-12-25 12:37:37 -06:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
materials.set(node['#id'].readBigUInt64LE(), material);
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// replace children materials with parents
|
|
|
|
|
materials.forEach((material, id) => {
|
|
|
|
|
if (typeof material !== 'bigint') return;
|
|
|
|
|
|
|
|
|
|
let resolvedMaterial: Material | bigint = material;
|
|
|
|
|
|
|
|
|
|
while (typeof resolvedMaterial === 'bigint') {
|
|
|
|
|
const linkedParentMaterial = materials.get(resolvedMaterial);
|
|
|
|
|
|
|
|
|
|
if (linkedParentMaterial === undefined) {
|
|
|
|
|
throw new Error('Could not resolve material');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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']) {
|
2023-12-29 13:01:45 -06:00
|
|
|
case 'ScenarioComponent':
|
2023-12-25 12:37:37 -06:00
|
|
|
case 'LodComponent':
|
|
|
|
|
case 'DecorItemComponent':
|
|
|
|
|
case 'NewSlotComponent':
|
2023-12-29 13:01:45 -06:00
|
|
|
case 'StateSwitcherComponent':
|
2023-12-29 19:56:50 -06:00
|
|
|
case 'CustomPropertiesComponent':
|
2024-01-02 13:31:34 -06:00
|
|
|
case 'ActionComponent':
|
|
|
|
|
case 'ParticleEffectComponent':
|
2023-12-25 12:37:37 -06:00
|
|
|
case 'SlotComponent':
|
2024-01-02 14:50:46 -06:00
|
|
|
case 'MotionComponent':
|
2024-01-02 13:31:34 -06:00
|
|
|
case 'TankElementComponent':
|
2024-01-02 14:50:46 -06:00
|
|
|
case 'SkeletonComponent':
|
2023-12-25 12:37:37 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
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-01-03 19:18:31 -06:00
|
|
|
let minLODDistanceBatchId: undefined | number;
|
2023-12-25 12:37:37 -06:00
|
|
|
|
|
|
|
|
if (component['rc.renderObj']['rb0.lodIndex'] === -1) {
|
|
|
|
|
minLODDistanceBatchId = 0;
|
|
|
|
|
} else {
|
|
|
|
|
const lodList = components.find(
|
|
|
|
|
(component) => component['comp.typename'] === 'LodComponent',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
lodList === undefined ||
|
|
|
|
|
lodList['comp.typename'] !== 'LodComponent' // type annotation hack
|
|
|
|
|
) {
|
|
|
|
|
throw new SyntaxError('Missing LodComponent');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let minLODDistance = Infinity;
|
2023-12-30 11:44:42 -06:00
|
|
|
let isFirstMaxFloat = true;
|
2024-01-03 19:18:31 -06:00
|
|
|
let lodIndexStreak = 0;
|
|
|
|
|
let lodIndexStreakIndex: undefined | number = undefined;
|
2023-12-25 12:37:37 -06:00
|
|
|
|
|
|
|
|
range(component['rc.renderObj']['ro.batchCount']).forEach(
|
|
|
|
|
(id) => {
|
|
|
|
|
const lodIndex =
|
|
|
|
|
component['rc.renderObj'][`rb${id}.lodIndex`];
|
|
|
|
|
const lodDistance =
|
|
|
|
|
lodList['lc.loddist'][`distance${lodIndex}`];
|
|
|
|
|
|
2024-01-03 19:18:31 -06:00
|
|
|
if (lodIndexStreakIndex === lodIndex) {
|
|
|
|
|
lodIndexStreak++;
|
|
|
|
|
} else {
|
|
|
|
|
lodIndexStreak = 0;
|
|
|
|
|
lodIndexStreakIndex = lodIndex;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-25 12:37:37 -06:00
|
|
|
if (
|
2024-01-03 19:18:31 -06:00
|
|
|
lodDistance < minLODDistance ||
|
|
|
|
|
(lodDistance === minLODDistance && lodIndexStreak > 0) ||
|
2023-12-30 11:45:44 -06:00
|
|
|
(lodDistance === MAX_FLOAT32 && isFirstMaxFloat)
|
2023-12-25 12:37:37 -06:00
|
|
|
) {
|
2023-12-30 11:44:42 -06:00
|
|
|
if (lodDistance === MAX_FLOAT32) isFirstMaxFloat = false;
|
2023-12-25 12:37:37 -06:00
|
|
|
minLODDistance = lodDistance;
|
|
|
|
|
minLODDistanceBatchId = id;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const batch =
|
|
|
|
|
component['rc.renderObj']['ro.batches'][
|
|
|
|
|
minLODDistanceBatchId!.toString().padStart(4, '0')
|
|
|
|
|
];
|
|
|
|
|
const polygonGroup = scg.get(batch['rb.datasource']);
|
|
|
|
|
|
|
|
|
|
if (!polygonGroup) {
|
|
|
|
|
console.warn(
|
|
|
|
|
`Missing polygon group ${batch['rb.datasource']}; skipping...`,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const material = materials.get(batch['rb.nmatname']);
|
|
|
|
|
|
|
|
|
|
if (!(material instanceof Material)) {
|
|
|
|
|
throw new Error(`Material ${batch['rb.nmatname']} is unresolved`);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-30 12:43:46 -06:00
|
|
|
const indicesAccessor = document
|
|
|
|
|
.createAccessor()
|
|
|
|
|
.setType('SCALAR')
|
|
|
|
|
.setArray(new Uint16Array(polygonGroup.indices))
|
|
|
|
|
.setBuffer(buffer);
|
2023-12-25 12:37:37 -06:00
|
|
|
const primitive = document
|
|
|
|
|
.createPrimitive()
|
2023-12-30 12:43:46 -06:00
|
|
|
.setIndices(indicesAccessor)
|
2023-12-25 12:37:37 -06:00
|
|
|
.setMaterial(material);
|
2023-12-30 12:43:46 -06:00
|
|
|
const attributes: Partial<Record<VertexAttribute, number[][]>> = {};
|
|
|
|
|
const indexedAttributes: VertexAttribute[] = [];
|
2023-12-25 12:37:37 -06:00
|
|
|
|
2023-12-30 12:43:46 -06:00
|
|
|
polygonGroup.vertices.forEach((vertex) => {
|
|
|
|
|
vertex.forEach(({ attribute, value }) => {
|
|
|
|
|
if (attributes[attribute] === undefined) {
|
|
|
|
|
attributes[attribute] = [];
|
|
|
|
|
indexedAttributes.push(attribute);
|
|
|
|
|
}
|
2023-12-25 12:37:37 -06:00
|
|
|
|
2023-12-30 12:43:46 -06:00
|
|
|
attributes[attribute]!.push(value);
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-12-25 12:37:37 -06:00
|
|
|
|
2023-12-30 12:43:46 -06:00
|
|
|
if (indexedAttributes.includes(VertexAttribute.TANGENT)) {
|
|
|
|
|
attributes[VertexAttribute.TANGENT] = attributes[
|
|
|
|
|
VertexAttribute.TANGENT
|
|
|
|
|
]!.map((tangent) => [...tangent, 1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
indexedAttributes.forEach((attribute) => {
|
|
|
|
|
const name = vertexAttributeGLTFName[attribute];
|
2024-01-04 12:11:36 -06:00
|
|
|
if (!name || primitive.getAttribute(name)) return;
|
2023-12-30 12:43:46 -06:00
|
|
|
const vertexSize = vertexAttributeGltfVectorSizes[attribute];
|
|
|
|
|
|
|
|
|
|
const attributeAccessor = document
|
|
|
|
|
.createAccessor(name)
|
|
|
|
|
.setType(vertexSize === 1 ? 'SCALAR' : `VEC${vertexSize}`)
|
|
|
|
|
.setArray(new Float32Array(attributes[attribute]!.flat()))
|
|
|
|
|
.setBuffer(buffer);
|
|
|
|
|
primitive.setAttribute(name, attributeAccessor);
|
2023-12-25 12:37:37 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const mesh = document
|
|
|
|
|
.createMesh(batch['##name'])
|
|
|
|
|
.addPrimitive(primitive);
|
|
|
|
|
|
|
|
|
|
node.setMesh(mesh);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new TypeError(
|
|
|
|
|
`Unhandled component type: ${component['comp.typename']}`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|