Skip to content

Commit 2baffcb

Browse files
committed
improved gltf parser (now also properly respecting scale/translation/rotation if matrix is unset
1 parent 0004d5e commit 2baffcb

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

pkg/file_handlers/gltf.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"math"
88

9+
"github.com/Patch2PDF/GDTF-Mesh-Reader/v2/pkg/MeshTypes"
910
Types "github.com/Patch2PDF/GDTF-Mesh-Reader/v2/pkg/MeshTypes"
1011
"github.com/qmuntal/gltf"
1112
)
@@ -23,11 +24,15 @@ func LoadGLTF(file io.Reader, desiredSize *Types.Vector) (*Types.Mesh, error) {
2324
continue
2425
}
2526
matrix := node.MatrixOrDefault()
26-
transformationMatrices[*node.Mesh] = Types.Matrix{
27-
X00: matrix[0], X01: matrix[4], X02: matrix[8], X03: matrix[12],
28-
X10: matrix[1], X11: matrix[5], X12: matrix[9], X13: matrix[13],
29-
X20: matrix[2], X21: matrix[6], X22: matrix[10], X23: matrix[14],
30-
X30: matrix[3], X31: matrix[7], X32: matrix[11], X33: matrix[15],
27+
if matrix != gltf.DefaultMatrix {
28+
transformationMatrices[*node.Mesh] = Types.Matrix{
29+
X00: matrix[0], X01: matrix[4], X02: matrix[8], X03: matrix[12],
30+
X10: matrix[1], X11: matrix[5], X12: matrix[9], X13: matrix[13],
31+
X20: matrix[2], X21: matrix[6], X22: matrix[10], X23: matrix[14],
32+
X30: matrix[3], X31: matrix[7], X32: matrix[11], X33: matrix[15],
33+
}
34+
} else {
35+
transformationMatrices[*node.Mesh] = gltfParseScaleRotationTranslation(node.RotationOrDefault(), node.ScaleOrDefault(), node.TranslationOrDefault())
3136
}
3237
}
3338

@@ -164,3 +169,12 @@ func gltfIndices(doc *gltf.Document, acc *gltf.Accessor) ([]int, error) {
164169

165170
return out, nil
166171
}
172+
173+
func gltfParseScaleRotationTranslation(rotation [4]float64, scale [3]float64, translation [3]float64) MeshTypes.Matrix {
174+
return MeshTypes.Matrix{
175+
X00: (1 - 2*(rotation[1]*rotation[1]+rotation[2]*rotation[2])) * scale[0], X01: 2 * (rotation[0]*rotation[1] - rotation[3]*rotation[2]) * scale[1], X02: 2 * (rotation[0]*rotation[2] + rotation[3]*rotation[1]) * scale[2], X03: translation[0],
176+
X10: 2 * (rotation[0]*rotation[1] + rotation[3]*rotation[2]) * scale[0], X11: (1 - 2*(rotation[0]*rotation[0]+rotation[2]*rotation[2])) * scale[1], X12: 2 * (rotation[1]*rotation[2] - rotation[3]*rotation[0]) * scale[2], X13: translation[1],
177+
X20: 2 * (rotation[0]*rotation[2] - rotation[3]*rotation[1]) * scale[0], X21: 2 * (rotation[1]*rotation[2] + rotation[3]*rotation[0]) * scale[1], X22: (1 - 2*(rotation[0]*rotation[0]+rotation[1]*rotation[1])) * scale[2], X23: translation[2],
178+
X30: 0, X31: 0, X32: 0, X33: 1,
179+
}
180+
}

0 commit comments

Comments
 (0)