fix: backwards vertex format

This commit is contained in:
TrèsAbhi committed 2024-01-08 19:13:54 -06:00
1 parent 7235291e69
commit 1f378137f1
4 files changed
+40 -34

No files matched your search

Binary file not shown.
+1 -1
View File
@@ -36,7 +36,7 @@ export async function buildTankArmors() {
const armor = await extractArmor(DATA, `${nation}-${tankKey}`); const armor = await extractArmor(DATA, `${nation}-${tankKey}`);
// await nodeIO.write('temp/armor/index.gltf', armor); await nodeIO.write('temp/armor/index.gltf', armor);
// writeFile('test.sc2.json', JSON.stringify(sc2, null, 2)); // writeFile('test.sc2.json', JSON.stringify(sc2, null, 2));
} }
-5
View File
@@ -1,5 +1,4 @@
import { Document, Node, Scene } from '@gltf-transform/core'; import { Document, Node, Scene } from '@gltf-transform/core';
import { writeFile } from 'fs/promises';
import { range, times } from 'lodash'; import { range, times } from 'lodash';
import { import {
Matrix4, Matrix4,
@@ -51,12 +50,8 @@ export async function extractArmor(
const scene = document.createScene(); const scene = document.createScene();
const buffer = document.createBuffer(); const buffer = document.createBuffer();
writeFile('test.sc2.json', JSON.stringify(sc2, null, 2));
function parseHierarchies(hierarchies: Hierarchy[], parent: Scene | Node) { function parseHierarchies(hierarchies: Hierarchy[], parent: Scene | Node) {
hierarchies.forEach((hierarchy) => { hierarchies.forEach((hierarchy) => {
console.log(`hierarchy: ${hierarchy.name}`);
const node = document.createNode(hierarchy.name); const node = document.createNode(hierarchy.name);
const components = times( const components = times(
hierarchy.components.count, hierarchy.components.count,
+39 -28
View File
@@ -1,4 +1,3 @@
import { writeFile } from 'fs/promises';
import { ScpgStream, VertexAttribute } from './scpg'; import { ScpgStream, VertexAttribute } from './scpg';
interface PolygonGroupRaw { interface PolygonGroupRaw {
@@ -67,7 +66,23 @@ export class ScgStream extends ScpgStream {
polygonGroupsRaw.push(polygonGroupRaw); polygonGroupsRaw.push(polygonGroupRaw);
} }
writeFile('test.scg.json', JSON.stringify({ polygonGroupsRaw }, null, 2)); // writeFile(
// 'test.scg.txt',
// polygonGroupsRaw
// .map((polygonGroupRaw) => {
// const floats: number[] = [];
// const stream = new BufferStream(polygonGroupRaw.vertices);
// while (stream.readRemainingLength()) {
// floats.push(stream.float());
// }
// return `format: ${polygonGroupRaw.vertexFormat.toString(
// 2,
// )}\n\n${floats.join('\n')}`;
// })
// .join('\n\n#####\n\n'),
// );
polygonGroupsRaw.forEach((polygonGroupRaw) => { polygonGroupsRaw.forEach((polygonGroupRaw) => {
const indicesStream = new ScpgStream(polygonGroupRaw.indices); const indicesStream = new ScpgStream(polygonGroupRaw.indices);
@@ -83,29 +98,21 @@ export class ScgStream extends ScpgStream {
const verticesStream = new ScpgStream(polygonGroupRaw.vertices); const verticesStream = new ScpgStream(polygonGroupRaw.vertices);
const vertices: BlitzkriegVertex[] = []; const vertices: BlitzkriegVertex[] = [];
const stride = const { format, stride } = ScgStream.parseVertexFormat(
ScgStream.vertexStride(polygonGroupRaw.vertexFormat) * polygonGroupRaw.vertexFormat,
polygonGroupRaw.vertexCount; );
const strideBasedSize = stride * polygonGroupRaw.vertexCount;
if (verticesStream.stream.length !== stride) { if (verticesStream.stream.length !== strideBasedSize) {
console.warn( console.warn(
`Vertex stride mismatch; expected ${stride}, got ${verticesStream.stream.length}; skipping...`, `Vertex stride mismatch; expected ${strideBasedSize}, got ${verticesStream.stream.length}; skipping...`,
); );
return; return;
} }
const vertexFormat = (
polygonGroupRaw.vertexFormat
.toString(2)
.split('')
.map((bitString, index) =>
bitString === '1' ? index : null,
) as VertexAttribute[]
).filter((type) => type !== null);
for (let index = 0; index < polygonGroupRaw.vertexCount; index++) { for (let index = 0; index < polygonGroupRaw.vertexCount; index++) {
vertices[index] = vertexFormat.map((attribute) => { vertices[index] = format.map((attribute) => {
return { return {
attribute, attribute,
value: verticesStream.vectorN( value: verticesStream.vectorN(
@@ -128,18 +135,22 @@ export class ScgStream extends ScpgStream {
return polygonGroups; return polygonGroups;
} }
static vertexStride(vertexFormat: number) { static parseVertexFormat(formatInt: number) {
const flags = vertexFormat.toString(2); const format: VertexAttribute[] = [];
let stride = 0;
return Object.values(VertexAttribute) Object.values(VertexAttribute)
.filter((value) => typeof value === 'number') .filter((value) => typeof value === 'number')
.reduce( .forEach((attributeUntyped) => {
(accumulator, attribute) => const attribute = attributeUntyped as VertexAttribute;
flags[attribute as VertexAttribute] === '1' const mask = 1 << attribute;
? accumulator +
vertexAttributeVectorSizes[attribute as VertexAttribute] * 4 if (formatInt & mask) {
: accumulator, format.push(attribute);
0, stride += vertexAttributeVectorSizes[attribute] * 4;
); }
});
return { format, stride };
} }
} }