feat: optimizations

This commit is contained in:
TrèsAbhi committed 2023-12-11 11:21:40 -06:00
1 parent 3f40e94235
commit 662e0bb9ce
1 file changed
+30 -51
+30 -51
View File
@@ -55,11 +55,6 @@ enum VertexType {
CUBETEXCOORD3, CUBETEXCOORD3,
} }
interface SCPGBuffer {
type: 'Buffer';
data: number[];
}
interface PolygonGroupRaw { interface PolygonGroupRaw {
'##name': 'PolygonGroup'; '##name': 'PolygonGroup';
'#id': Buffer; '#id': Buffer;
@@ -76,10 +71,6 @@ interface PolygonGroupRaw {
vertices: Buffer; vertices: Buffer;
} }
interface PolygonGroup extends Omit<PolygonGroupRaw, 'indices'> {
indices: number[];
}
const VECTOR_SIZES = [ const VECTOR_SIZES = [
[VertexType.COLOR, VertexType.FLEXIBILITY, VertexType.HARD_JOINTINDEX], [VertexType.COLOR, VertexType.FLEXIBILITY, VertexType.HARD_JOINTINDEX],
[ [
@@ -92,7 +83,7 @@ const VECTOR_SIZES = [
[ [
VertexType.VERTEX, VertexType.VERTEX,
VertexType.NORMAL, VertexType.NORMAL,
// VertexType.TANGENT, // moved to 4 VertexType.TANGENT,
VertexType.BINORMAL, VertexType.BINORMAL,
VertexType.CUBETEXCOORD0, VertexType.CUBETEXCOORD0,
VertexType.CUBETEXCOORD1, VertexType.CUBETEXCOORD1,
@@ -103,7 +94,7 @@ const VECTOR_SIZES = [
VertexType.PIVOT4, VertexType.PIVOT4,
VertexType.JOINTINDEX, VertexType.JOINTINDEX,
VertexType.JOINTWEIGHT, VertexType.JOINTWEIGHT,
VertexType.TANGENT, // moved from 3 // VertexType.TANGENT,
], ],
]; ];
@@ -122,7 +113,7 @@ export class SCPGStream {
} }
consume(size: number) { consume(size: number) {
const subarray = this.read(size); const subarray = this.read(size);
this.index += size; this.skip(size);
return subarray; return subarray;
} }
@@ -161,10 +152,7 @@ export class SCPGStream {
return this.consume(2).readUInt16LE(); return this.consume(2).readUInt16LE();
} }
consumeUInt32() { consumeUInt32() {
const buffer = this.consume(4); return this.consume(4).readUInt32LE();
const value = buffer.readUInt32LE();
return value;
} }
consumeUInt64() { consumeUInt64() {
return this.consume(8).readBigUInt64LE(); return this.consume(8).readBigUInt64LE();
@@ -177,33 +165,30 @@ export class SCPGStream {
return this.consume(1).readUInt8() === 1; return this.consume(1).readUInt8() === 1;
} }
consumeVectorN(size: number) {
return times(size, () => this.consumeFloat());
}
consumeVector2() { consumeVector2() {
return [this.consumeFloat(), this.consumeFloat()]; return this.consumeVectorN(2);
} }
consumeVector3() { consumeVector3() {
return [...this.consumeVector2(), this.consumeFloat()]; return this.consumeVectorN(3);
} }
consumeVector4() { consumeVector4() {
return [...this.consumeVector3(), this.consumeFloat()]; return this.consumeVectorN(4);
} }
consumeMatrixN(size: number) {
return times(size, () => this.consumeVectorN(size));
}
consumeMatrix2() { consumeMatrix2() {
return [this.consumeVector2(), this.consumeVector2()]; return this.consumeMatrixN(2);
} }
consumeMatrix3() { consumeMatrix3() {
return [ return this.consumeMatrixN(3);
this.consumeVector3(),
this.consumeVector3(),
this.consumeVector3(),
];
} }
consumeMatrix4() { consumeMatrix4() {
return [ return this.consumeMatrixN(4);
this.consumeVector4(),
this.consumeVector4(),
this.consumeVector4(),
this.consumeVector4(),
];
} }
consumeSCGHeader() { consumeSCGHeader() {
@@ -229,30 +214,29 @@ export class SCPGStream {
for (let index = 0; index < polygonGroupRaw.indexCount; index++) { for (let index = 0; index < polygonGroupRaw.indexCount; index++) {
indices.push( indices.push(
polygonGroupRaw.indexFormat polygonGroupRaw.indexFormat === 0
? indicesStream.consumeUInt32() ? indicesStream.consumeUInt16()
: indicesStream.consumeUInt16(), : indicesStream.consumeUInt32(),
); );
} }
const verticesStream = new SCPGStream(polygonGroupRaw.vertices); const verticesStream = new SCPGStream(polygonGroupRaw.vertices);
const vertexFormat = polygonGroupRaw.vertexFormat const vertexFormat = [
.toString(2) ...polygonGroupRaw.vertexFormat.toString(2).padEnd(64, '0'),
.padStart(8 * 8, '0') ]
.split('') .map((bit, index) => (bit === '1' ? index : null))
.map(parseInt)
.map((bit, index) => (bit ? index : null))
.filter((index) => index !== null) as VertexType[]; .filter((index) => index !== null) as VertexType[];
const vertices: { type: VertexType; value: number[] }[] = []; const vertices: Partial<Record<VertexType, number[]>>[] = [];
for (let index = 0; index < polygonGroupRaw.vertexCount; index++) { for (let index = 0; index < polygonGroupRaw.vertexCount; index++) {
vertexFormat.forEach((type) => { vertexFormat.forEach((type) => {
const resolved = VECTOR_SIZES.some((types, size) => { const resolved = VECTOR_SIZES.some((types, size) => {
if (types.includes(type)) { if (types.includes(type)) {
vertices.push({ if (!(index in vertices)) vertices[index] = {};
type,
value: times(size + 1).map(() => verticesStream.consumeFloat()), vertices[index][type] = times(size + 1).map(() =>
}); verticesStream.consumeFloat(),
);
return true; return true;
} }
@@ -267,12 +251,7 @@ export class SCPGStream {
return { vertices, indices }; return { vertices, indices };
}); });
return polygonGroups.map((group) => ({ return polygonGroups;
vertices: group.vertices
.filter(({ type }) => type === VertexType.VERTEX)
.map(({ value }) => value),
indices: group.indices,
}));
} }
consumeSC2Header() { consumeSC2Header() {