diff --git a/public/test/5137.glb b/public/test/5137.glb index f2622dfed..53b78306d 100644 Binary files a/public/test/5137.glb and b/public/test/5137.glb differ diff --git a/scripts/buildAssets/tankArmors.ts b/scripts/buildAssets/tankArmors.ts index d8a1ab748..e80b9a36c 100644 --- a/scripts/buildAssets/tankArmors.ts +++ b/scripts/buildAssets/tankArmors.ts @@ -36,7 +36,7 @@ export async function buildTankArmors() { 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)); } diff --git a/src/core/blitz/extractArmor.ts b/src/core/blitz/extractArmor.ts index 785701ae6..7be3c4c23 100644 --- a/src/core/blitz/extractArmor.ts +++ b/src/core/blitz/extractArmor.ts @@ -1,5 +1,4 @@ import { Document, Node, Scene } from '@gltf-transform/core'; -import { writeFile } from 'fs/promises'; import { range, times } from 'lodash'; import { Matrix4, @@ -51,12 +50,8 @@ export async function extractArmor( const scene = document.createScene(); const buffer = document.createBuffer(); - writeFile('test.sc2.json', JSON.stringify(sc2, null, 2)); - function parseHierarchies(hierarchies: Hierarchy[], parent: Scene | Node) { hierarchies.forEach((hierarchy) => { - console.log(`hierarchy: ${hierarchy.name}`); - const node = document.createNode(hierarchy.name); const components = times( hierarchy.components.count, diff --git a/src/core/streams/scg.ts b/src/core/streams/scg.ts index 5162a1570..cbc693dcc 100644 --- a/src/core/streams/scg.ts +++ b/src/core/streams/scg.ts @@ -1,4 +1,3 @@ -import { writeFile } from 'fs/promises'; import { ScpgStream, VertexAttribute } from './scpg'; interface PolygonGroupRaw { @@ -67,7 +66,23 @@ export class ScgStream extends ScpgStream { 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) => { const indicesStream = new ScpgStream(polygonGroupRaw.indices); @@ -83,29 +98,21 @@ export class ScgStream extends ScpgStream { const verticesStream = new ScpgStream(polygonGroupRaw.vertices); const vertices: BlitzkriegVertex[] = []; - const stride = - ScgStream.vertexStride(polygonGroupRaw.vertexFormat) * - polygonGroupRaw.vertexCount; + const { format, stride } = ScgStream.parseVertexFormat( + polygonGroupRaw.vertexFormat, + ); + const strideBasedSize = stride * polygonGroupRaw.vertexCount; - if (verticesStream.stream.length !== stride) { + if (verticesStream.stream.length !== strideBasedSize) { console.warn( - `Vertex stride mismatch; expected ${stride}, got ${verticesStream.stream.length}; skipping...`, + `Vertex stride mismatch; expected ${strideBasedSize}, got ${verticesStream.stream.length}; skipping...`, ); 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++) { - vertices[index] = vertexFormat.map((attribute) => { + vertices[index] = format.map((attribute) => { return { attribute, value: verticesStream.vectorN( @@ -128,18 +135,22 @@ export class ScgStream extends ScpgStream { return polygonGroups; } - static vertexStride(vertexFormat: number) { - const flags = vertexFormat.toString(2); + static parseVertexFormat(formatInt: number) { + const format: VertexAttribute[] = []; + let stride = 0; - return Object.values(VertexAttribute) + Object.values(VertexAttribute) .filter((value) => typeof value === 'number') - .reduce( - (accumulator, attribute) => - flags[attribute as VertexAttribute] === '1' - ? accumulator + - vertexAttributeVectorSizes[attribute as VertexAttribute] * 4 - : accumulator, - 0, - ); + .forEach((attributeUntyped) => { + const attribute = attributeUntyped as VertexAttribute; + const mask = 1 << attribute; + + if (formatInt & mask) { + format.push(attribute); + stride += vertexAttributeVectorSizes[attribute] * 4; + } + }); + + return { format, stride }; } }