From 7e0fb82905860e802031ff2623b1a2112817c142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Sat, 30 Dec 2023 12:43:46 -0600 Subject: [PATCH] fix: vec4 tangents --- scripts/buildAssets.ts | 4 +- src/core/blitz/extractModel.ts | 78 +++++++++++++++++----------------- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/scripts/buildAssets.ts b/scripts/buildAssets.ts index 15c663754..c4cae1de1 100644 --- a/scripts/buildAssets.ts +++ b/scripts/buildAssets.ts @@ -518,9 +518,9 @@ if (allTargets || targets?.includes('tankModels')) { // if (id !== 4417) continue; // amx m4 mle // if (id !== 7297) continue; // 60tp // if (id !== 1) continue; // t-34 - // if (id !== 6753) continue; // type 71 + if (id !== 6753) continue; // type 71 // if (id !== 5137) continue; // tiger ii - if (id !== 6225) continue; // fv215b + // if (id !== 6225) continue; // fv215b console.log(`Building model ${id} @ ${nation}/${tankIndex}`); diff --git a/src/core/blitz/extractModel.ts b/src/core/blitz/extractModel.ts index 77ddb3d1f..d3e961a26 100644 --- a/src/core/blitz/extractModel.ts +++ b/src/core/blitz/extractModel.ts @@ -29,6 +29,12 @@ const vertexAttributeGLTFName: Partial> = { [VertexAttribute.JOINTWEIGHT]: 'WEIGHT_0', }; +const vertexAttributeGltfVectorSizes = { + ...vertexAttributeVectorSizes, + + [VertexAttribute.TANGENT]: 4, +} as const; + export async function extractModel( data: string, path: string, @@ -234,56 +240,52 @@ export async function extractModel( break; } - const indexedVertexAttributes: VertexAttribute[] = []; - const unpackedVertices: Partial> = - {}; - - polygonGroup.vertices.forEach((vertex) => { - vertex.map((vertexItem) => { - if (!(vertexItem.attribute in unpackedVertices)) { - unpackedVertices[vertexItem.attribute] = []; - indexedVertexAttributes.push(vertexItem.attribute); - } - - unpackedVertices[vertexItem.attribute]!.push( - ...vertexItem.value, - ); - }); - }); - - const indexAccessor = document - .createAccessor() - .setType('SCALAR') - .setArray(new Uint16Array(polygonGroup.indices)) - .setBuffer(buffer); const material = materials.get(batch['rb.nmatname']); if (!(material instanceof Material)) { throw new Error(`Material ${batch['rb.nmatname']} is unresolved`); } + const indicesAccessor = document + .createAccessor() + .setType('SCALAR') + .setArray(new Uint16Array(polygonGroup.indices)) + .setBuffer(buffer); const primitive = document .createPrimitive() - .setIndices(indexAccessor) + .setIndices(indicesAccessor) .setMaterial(material); + const attributes: Partial> = {}; + const indexedAttributes: VertexAttribute[] = []; - indexedVertexAttributes.forEach((attribute) => { - if (!(attribute in vertexAttributeGLTFName)) return; + polygonGroup.vertices.forEach((vertex) => { + vertex.forEach(({ attribute, value }) => { + if (attributes[attribute] === undefined) { + attributes[attribute] = []; + indexedAttributes.push(attribute); + } - const vertexSize = vertexAttributeVectorSizes[attribute]; + attributes[attribute]!.push(value); + }); + }); - if ( - !primitive.getAttribute(vertexAttributeGLTFName[attribute]!) - ) { - primitive.setAttribute( - vertexAttributeGLTFName[attribute]!, - document - .createAccessor() - .setType(vertexSize === 1 ? 'SCALAR' : `VEC${vertexSize}`) - .setArray(new Float32Array(unpackedVertices[attribute]!)) - .setBuffer(buffer), - ); - } + if (indexedAttributes.includes(VertexAttribute.TANGENT)) { + attributes[VertexAttribute.TANGENT] = attributes[ + VertexAttribute.TANGENT + ]!.map((tangent) => [...tangent, 1]); + } + + indexedAttributes.forEach((attribute) => { + const name = vertexAttributeGLTFName[attribute]; + if (!name) return; // no equivalent GLTF attribute + 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); }); const mesh = document