Import a 3D Model and automatically assign and export animations
TypeScript
Fix ACCESSOR_JOINTS_USED_ZERO_WEIGHT error by removing 0 weight skinning
Scott Petrovic049072c3732c
diff --git a/src/lib/io/fbx/GeometryParser.test.ts b/src/lib/io/fbx/GeometryParser.test.tsnew file mode 100644index 0000000..83aba6b--- /dev/null+++ b/src/lib/io/fbx/GeometryParser.test.ts@@ -0,0 +1,77 @@+import { describe, it, expect } from 'vitest'+import { GeometryParser } from './GeometryParser'++/** A single triangle. PolygonVertexIndex marks the last vertex of a face as (index ^ -1). */+function make_geo_node (): any {+ return {+ Vertices: { a: [0, 0, 0, 1, 0, 0, 0, 1, 0] },+ PolygonVertexIndex: { a: [0, 1, -3] }+ }+}++/** Minimal stand-in for the skeleton deformer: each raw bone weights vertices by index. */+function make_skeleton (raw_bones: Array<{ indices: number[], weights: number[] }>): any {+ return { rawBones: raw_bones }+}++describe('GeometryParser skin weights', () => {+ it('drops zero-weight cluster entries so their bone ids never pair with a zero weight', () => {+ // bone 2 lists all three vertices but only actually influences vertex 1.+ // Exporters like Maya write the full cluster membership this way, and glTF+ // validators reject a non-zero joint index that carries a zero weight.+ const skeleton = make_skeleton([+ { indices: [0, 1, 2], weights: [1, 0.5, 1] },+ { indices: [], weights: [] },+ { indices: [0, 1, 2], weights: [0, 0.5, 0] }+ ])++ const parser = new GeometryParser()+ const geo_info = parser.parseGeoNode(make_geo_node(), skeleton)+ const buffers = parser.genBuffers(geo_info)++ expect(buffers.weightsIndices.length).toBe(12) // 3 vertices x 4 influences+ for (let i = 0; i < buffers.weightsIndices.length; i++) {+ if (buffers.vertexWeights[i] === 0) {+ expect(buffers.weightsIndices[i]).toBe(0)+ }+ }+ })++ it('keeps real influences and still normalizes each vertex to a full weight', () => {+ const skeleton = make_skeleton([+ { indices: [0, 1, 2], weights: [1, 0.5, 1] },+ { indices: [], weights: [] },+ { indices: [0, 1, 2], weights: [0, 0.5, 0] }+ ])++ const parser = new GeometryParser()+ const buffers = parser.genBuffers(parser.parseGeoNode(make_geo_node(), skeleton))++ // vertex 1 (second corner of the face) keeps both of its real influences+ const vertex_one_indices = buffers.weightsIndices.slice(4, 8)+ const vertex_one_weights = buffers.vertexWeights.slice(4, 8)+ expect(vertex_one_indices).toContain(0)+ expect(vertex_one_indices).toContain(2)++ for (let vertex = 0; vertex < 3; vertex++) {+ const total = buffers.vertexWeights+ .slice(vertex * 4, vertex * 4 + 4)+ .reduce((sum: number, weight: number) => sum + weight, 0)+ expect(total).toBeCloseTo(1)+ }+ })++ it('leaves a vertex whose influences are all zero-weight fully unskinned', () => {+ const skeleton = make_skeleton([+ { indices: [0, 1, 2], weights: [1, 1, 0] },+ { indices: [2], weights: [0] }+ ])++ const parser = new GeometryParser()+ const buffers = parser.genBuffers(parser.parseGeoNode(make_geo_node(), skeleton))++ // vertex 2 had only zero-weight entries, so it pads out to joint 0 / weight 0+ expect(buffers.weightsIndices.slice(8, 12)).toEqual([0, 0, 0, 0])+ expect(buffers.vertexWeights.slice(8, 12)).toEqual([0, 0, 0, 0])+ })+})diff --git a/src/lib/io/fbx/GeometryParser.ts b/src/lib/io/fbx/GeometryParser.tsindex c6ce2cb..cae411b 100644--- a/src/lib/io/fbx/GeometryParser.ts+++ b/src/lib/io/fbx/GeometryParser.ts@@ -285,6 +285,13 @@ class GeometryParser {// loop over the bone's vertex indices and weightsrawBone.indices.forEach(function (index: number, j: number) {+ // Skin clusters can list vertices with a weight of exactly 0 (some+ // exporters write the full cluster membership). Carrying those through+ // would pair a real bone id with a zero weight, which glTF validators+ // reject on export (ACCESSOR_JOINTS_USED_ZERO_WEIGHT). Dropping them+ // here lets the padding below fill the slot with joint 0 instead.+ if (rawBone.weights[j] === 0) return;+if (geoInfo.weightTable[index] === undefined) geoInfo.weightTable[index] = [];geoInfo.weightTable[index].push({