mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-24 12:09:51 +02:00
feat: glb convertion
This commit is contained in:
1 parent
9db43db3cf
commit
6d2bb7b131
3 files changed
+190
-28
No files matched your search
+171
-17
@@ -1,12 +1,14 @@
|
|||||||
|
import { Document, Node, NodeIO, Scene } from '@gltf-transform/core';
|
||||||
import { config } from 'dotenv';
|
import { config } from 'dotenv';
|
||||||
import { existsSync, writeFileSync } from 'fs';
|
import { existsSync, writeFileSync } from 'fs';
|
||||||
import { mkdir, readdir, rm } from 'fs/promises';
|
import { mkdir, readdir, rm } from 'fs/promises';
|
||||||
import { times } from 'lodash';
|
import { times } from 'lodash';
|
||||||
import { argv } from 'process';
|
import { argv } from 'process';
|
||||||
import sharp from 'sharp';
|
import sharp from 'sharp';
|
||||||
|
import { Matrix4, Quaternion, Vector3, Vector4Tuple } from 'three';
|
||||||
import { TankType } from '../src/components/Tanks';
|
import { TankType } from '../src/components/Tanks';
|
||||||
import { NATION_IDS } from '../src/constants/nations';
|
import { NATION_IDS } from '../src/constants/nations';
|
||||||
import { SCPGStream } from '../src/core/blitz/SCPGStream';
|
import { SCPGStream, VertexType } from '../src/core/blitz/SCPGStream';
|
||||||
import { readBase64DVPL } from '../src/core/blitz/readBase64DVPL';
|
import { readBase64DVPL } from '../src/core/blitz/readBase64DVPL';
|
||||||
import { readDVPLFile } from '../src/core/blitz/readDVPLFile';
|
import { readDVPLFile } from '../src/core/blitz/readDVPLFile';
|
||||||
import { readStringDVPL } from '../src/core/blitz/readStringDVPL';
|
import { readStringDVPL } from '../src/core/blitz/readStringDVPL';
|
||||||
@@ -25,6 +27,7 @@ import {
|
|||||||
Tier,
|
Tier,
|
||||||
TurretDefinition,
|
TurretDefinition,
|
||||||
} from '../src/core/blitzkrieg/tankDefinitions';
|
} from '../src/core/blitzkrieg/tankDefinitions';
|
||||||
|
import { Hierarchy } from '../src/types/sc2';
|
||||||
|
|
||||||
(BigInt.prototype as any).toJSON = function () {
|
(BigInt.prototype as any).toJSON = function () {
|
||||||
return this.toString();
|
return this.toString();
|
||||||
@@ -512,29 +515,89 @@ if (allTargets || targets?.includes('tankModels')) {
|
|||||||
const parameters = await readYAMLDVPL<TankParameters>(
|
const parameters = await readYAMLDVPL<TankParameters>(
|
||||||
`${DATA}/${DOI.tankParameters}/${nation}/${tankIndex}.yaml.dvpl`,
|
`${DATA}/${DOI.tankParameters}/${nation}/${tankIndex}.yaml.dvpl`,
|
||||||
);
|
);
|
||||||
const sc2Path = parameters.resourcesPath.blitzModelPath;
|
const scg = (
|
||||||
const scgPath = parameters.resourcesPath.blitzModelPath.replace(
|
await SCPGStream.fromDVPLFile(
|
||||||
|
`${DATA}/${
|
||||||
|
DOI['3d']
|
||||||
|
}/${parameters.resourcesPath.blitzModelPath.replace(
|
||||||
/\.sc2$/,
|
/\.sc2$/,
|
||||||
'.scg',
|
'.scg',
|
||||||
);
|
)}.dvpl`,
|
||||||
const sc2Stream = await SCPGStream.fromDVPLFile(
|
)
|
||||||
`${DATA}/${DOI['3d']}/${sc2Path}.dvpl`,
|
).consumeSCG();
|
||||||
);
|
const sc2 = (
|
||||||
const scgStream = await SCPGStream.fromDVPLFile(
|
await SCPGStream.fromDVPLFile(
|
||||||
`${DATA}/${DOI['3d']}/${scgPath}.dvpl`,
|
`${DATA}/${DOI['3d']}/${parameters.resourcesPath.blitzModelPath}.dvpl`,
|
||||||
);
|
)
|
||||||
|
).consumeSC2();
|
||||||
|
const document = new Document();
|
||||||
|
const scene = document.createScene();
|
||||||
|
const buffer = document.createBuffer();
|
||||||
|
|
||||||
const scg = scgStream.consumeSCG();
|
function parseHierarchies(
|
||||||
const sc2 = sc2Stream.consumeSC2();
|
hierarchies: Hierarchy[],
|
||||||
|
parent: Scene | Node,
|
||||||
|
) {
|
||||||
|
hierarchies.forEach((hierarchy) => {
|
||||||
|
const node = document.createNode(hierarchy.name);
|
||||||
|
|
||||||
sc2['#hierarchy'][0]['#hierarchy']!.forEach((hierarchy) => {
|
|
||||||
times(
|
times(
|
||||||
hierarchy.components.count,
|
hierarchy.components.count,
|
||||||
(index) => hierarchy.components[index.toString().padStart(4, '0')],
|
(index) =>
|
||||||
|
hierarchy.components[index.toString().padStart(4, '0')],
|
||||||
).forEach((component) => {
|
).forEach((component) => {
|
||||||
if (component['comp.typename'] !== 'RenderComponent') return;
|
switch (component['comp.typename']) {
|
||||||
|
case 'TransformComponent': {
|
||||||
|
const localRotation = new Quaternion().fromArray(
|
||||||
|
component['tc.localRotation'],
|
||||||
|
);
|
||||||
|
const localScale = new Vector3().fromArray(
|
||||||
|
component['tc.localScale'],
|
||||||
|
);
|
||||||
|
const localTranslation = new Vector3().fromArray(
|
||||||
|
component['tc.localTranslation'],
|
||||||
|
);
|
||||||
|
const worldRotation = new Quaternion().fromArray(
|
||||||
|
component['tc.worldRotation'],
|
||||||
|
);
|
||||||
|
const worldScale = new Vector3().fromArray(
|
||||||
|
component['tc.worldScale'],
|
||||||
|
);
|
||||||
|
const worldTranslation = new Vector3().fromArray(
|
||||||
|
component['tc.worldTranslation'],
|
||||||
|
);
|
||||||
|
const localMatrix = new Matrix4().compose(
|
||||||
|
localTranslation,
|
||||||
|
localRotation,
|
||||||
|
localScale,
|
||||||
|
);
|
||||||
|
const worldMatrix = new Matrix4().compose(
|
||||||
|
worldTranslation,
|
||||||
|
worldRotation,
|
||||||
|
worldScale,
|
||||||
|
);
|
||||||
|
const combinedMatrix = new Matrix4().multiplyMatrices(
|
||||||
|
worldMatrix,
|
||||||
|
localMatrix,
|
||||||
|
);
|
||||||
|
const combinedRotation = new Quaternion();
|
||||||
|
const combinedScale = new Vector3();
|
||||||
|
const combinedTranslation = new Vector3();
|
||||||
|
|
||||||
hierarchy.name,
|
combinedMatrix.decompose(
|
||||||
|
combinedTranslation,
|
||||||
|
combinedRotation,
|
||||||
|
combinedScale,
|
||||||
|
);
|
||||||
|
|
||||||
|
node.setRotation(combinedRotation.toArray() as Vector4Tuple);
|
||||||
|
node.setScale(combinedScale.toArray());
|
||||||
|
node.setTranslation(combinedTranslation.toArray());
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'RenderComponent': {
|
||||||
times(
|
times(
|
||||||
component['rc.renderObj']['ro.batchCount'],
|
component['rc.renderObj']['ro.batchCount'],
|
||||||
(index) =>
|
(index) =>
|
||||||
@@ -545,12 +608,103 @@ if (allTargets || targets?.includes('tankModels')) {
|
|||||||
const polygonGroup = scg.find(
|
const polygonGroup = scg.find(
|
||||||
(group) => group.id === batch['rb.datasource'],
|
(group) => group.id === batch['rb.datasource'],
|
||||||
);
|
);
|
||||||
|
const unpackedVertices: number[] = [];
|
||||||
|
|
||||||
console.log(batch['rb.datasource'], polygonGroup !== undefined);
|
if (!polygonGroup)
|
||||||
|
throw new Error(
|
||||||
|
`Missing polygon group ${batch['rb.datasource']}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
polygonGroup.vertices.forEach((vertex) => {
|
||||||
|
vertex.map((vertexItem) => {
|
||||||
|
switch (vertexItem.type) {
|
||||||
|
case VertexType.VERTEX: {
|
||||||
|
unpackedVertices.push(...vertexItem.value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// default:
|
||||||
|
// throw new TypeError(
|
||||||
|
// `Unhandled vertex type: ${
|
||||||
|
// VertexType[vertexItem.type]
|
||||||
|
// } (${vertexItem.type})`,
|
||||||
|
// );
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const vertexAccessor = document
|
||||||
|
.createAccessor()
|
||||||
|
.setType('VEC3')
|
||||||
|
.setArray(new Float32Array(unpackedVertices))
|
||||||
|
.setBuffer(buffer);
|
||||||
|
const indexAccessor = document
|
||||||
|
.createAccessor()
|
||||||
|
.setType('SCALAR')
|
||||||
|
.setArray(new Uint16Array(polygonGroup.indices))
|
||||||
|
.setBuffer(buffer);
|
||||||
|
|
||||||
|
const primitive = document
|
||||||
|
.createPrimitive()
|
||||||
|
.setIndices(indexAccessor)
|
||||||
|
.setAttribute('POSITION', vertexAccessor);
|
||||||
|
|
||||||
|
const mesh = document
|
||||||
|
.createMesh(batch['##name'])
|
||||||
|
.addPrimitive(primitive);
|
||||||
|
|
||||||
|
const polygons = document
|
||||||
|
.createNode(batch['##name'])
|
||||||
|
.setMesh(mesh);
|
||||||
|
|
||||||
|
node.addChild(polygons);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// default:
|
||||||
|
// throw new TypeError(
|
||||||
|
// `Unhandled component type: ${component['comp.typename']}`,
|
||||||
|
// );
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (hierarchy['#hierarchy']) {
|
||||||
|
parseHierarchies(hierarchy['#hierarchy'], node);
|
||||||
|
}
|
||||||
|
|
||||||
|
parent.addChild(node);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
parseHierarchies(sc2['#hierarchy'], scene);
|
||||||
|
writeFileSync('test.glb', await new NodeIO().writeBinary(document));
|
||||||
|
|
||||||
|
// sc2['#hierarchy'][0]['#hierarchy']!.forEach((hierarchy) => {
|
||||||
|
// times(
|
||||||
|
// hierarchy.components.count,
|
||||||
|
// (index) => hierarchy.components[index.toString().padStart(4, '0')],
|
||||||
|
// ).forEach((component) => {
|
||||||
|
// if (component['comp.typename'] !== 'RenderComponent') return;
|
||||||
|
|
||||||
|
// hierarchy.name,
|
||||||
|
// times(
|
||||||
|
// component['rc.renderObj']['ro.batchCount'],
|
||||||
|
// (index) =>
|
||||||
|
// component['rc.renderObj']['ro.batches'][
|
||||||
|
// index.toString().padStart(4, '0')
|
||||||
|
// ],
|
||||||
|
// ).map((batch) => {
|
||||||
|
// const polygonGroup = scg.find(
|
||||||
|
// (group) => group.id === batch['rb.datasource'],
|
||||||
|
// );
|
||||||
|
|
||||||
|
// console.log(batch['rb.datasource'], polygonGroup !== undefined);
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
|
||||||
// writeFile('test.sc2.json', JSON.stringify(sc2, null, 2));
|
// writeFile('test.sc2.json', JSON.stringify(sc2, null, 2));
|
||||||
// writeFile('test.scg.json', JSON.stringify(scg, null, 2));
|
// writeFile('test.scg.json', JSON.stringify(scg, null, 2));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ enum KAType {
|
|||||||
UNCONFIRMED_TRANSFORM = 29,
|
UNCONFIRMED_TRANSFORM = 29,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum VertexType {
|
export enum VertexType {
|
||||||
VERTEX,
|
VERTEX,
|
||||||
NORMAL,
|
NORMAL,
|
||||||
COLOR,
|
COLOR,
|
||||||
@@ -90,14 +90,18 @@ const VECTOR_SIZES = [
|
|||||||
[
|
[
|
||||||
VertexType.VERTEX,
|
VertexType.VERTEX,
|
||||||
VertexType.NORMAL,
|
VertexType.NORMAL,
|
||||||
VertexType.TANGENT,
|
|
||||||
VertexType.BINORMAL,
|
VertexType.BINORMAL,
|
||||||
VertexType.CUBETEXCOORD0,
|
VertexType.CUBETEXCOORD0,
|
||||||
VertexType.CUBETEXCOORD1,
|
VertexType.CUBETEXCOORD1,
|
||||||
VertexType.CUBETEXCOORD2,
|
VertexType.CUBETEXCOORD2,
|
||||||
VertexType.CUBETEXCOORD3,
|
VertexType.CUBETEXCOORD3,
|
||||||
],
|
],
|
||||||
[VertexType.PIVOT4, VertexType.JOINTINDEX, VertexType.JOINTWEIGHT],
|
[
|
||||||
|
VertexType.PIVOT4,
|
||||||
|
VertexType.JOINTINDEX,
|
||||||
|
VertexType.JOINTWEIGHT,
|
||||||
|
VertexType.TANGENT,
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
export class SCPGStream {
|
export class SCPGStream {
|
||||||
@@ -252,6 +256,8 @@ export class SCPGStream {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(verticesStream.readRemaining().length);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: polygonGroupRaw['#id'].readBigUInt64LE(),
|
id: polygonGroupRaw['#id'].readBigUInt64LE(),
|
||||||
vertices,
|
vertices,
|
||||||
|
|||||||
+10
-8
@@ -1,3 +1,5 @@
|
|||||||
|
import { Vector3Tuple, Vector4Tuple } from 'three';
|
||||||
|
|
||||||
export interface SC2 {
|
export interface SC2 {
|
||||||
'#dataNodes': DataNode[];
|
'#dataNodes': DataNode[];
|
||||||
'#hierarchy': Hierarchy[];
|
'#hierarchy': Hierarchy[];
|
||||||
@@ -67,12 +69,12 @@ interface Textures {
|
|||||||
normalmap: string;
|
normalmap: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Hierarchy {
|
export interface Hierarchy {
|
||||||
'##name': 'Entity';
|
'##name': 'Entity';
|
||||||
'#hierarchy'?: Hierarchy[];
|
'#hierarchy'?: Hierarchy[];
|
||||||
components: Components;
|
components: Components;
|
||||||
flags: number;
|
flags: number;
|
||||||
id: number;
|
id: bigint;
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,12 +106,12 @@ interface RbAabbox {
|
|||||||
type Component =
|
type Component =
|
||||||
| {
|
| {
|
||||||
'comp.typename': 'TransformComponent';
|
'comp.typename': 'TransformComponent';
|
||||||
'tc.localRotation': number[];
|
'tc.localRotation': Vector4Tuple;
|
||||||
'tc.localScale': number[];
|
'tc.localScale': Vector3Tuple;
|
||||||
'tc.localTranslation': number[];
|
'tc.localTranslation': Vector3Tuple;
|
||||||
'tc.worldRotation': number[];
|
'tc.worldRotation': Vector4Tuple;
|
||||||
'tc.worldScale': number[];
|
'tc.worldScale': Vector3Tuple;
|
||||||
'tc.worldTranslation': number[];
|
'tc.worldTranslation': Vector3Tuple;
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
'comp.typename': 'RenderComponent';
|
'comp.typename': 'RenderComponent';
|
||||||
|
|||||||
Reference in new issue
Block a user