Skip to content

Commit b4f4dad

Browse files
committed
refined bounding box gen in gltf
1 parent a6c6d64 commit b4f4dad

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

pkg/file_handlers/gltf.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ func LoadGLTF(file io.Reader, desiredSize *Types.Vector) (*Types.Mesh, error) {
3838
}
3939
}
4040

41-
// TODO: refine this as pure rotation breaks bounding box -> possibly rotate all 8 outer points of bounding box and redetermine
4241
// calculate outer dimensions
4342
min := Types.Vector{X: math.Inf(1), Y: math.Inf(1), Z: math.Inf(1)}
4443
max := Types.Vector{X: math.Inf(-1), Y: math.Inf(-1), Z: math.Inf(-1)}
@@ -47,29 +46,38 @@ func LoadGLTF(file io.Reader, desiredSize *Types.Vector) (*Types.Mesh, error) {
4746
for _, p := range m.Primitives {
4847
// contains Min and Max attr (for dimension calc)
4948
posAccessor := doc.Accessors[p.Attributes[gltf.POSITION]]
50-
// do transformation before getting the outer dimensions
51-
tempMin := transformationMatrices[node_index].MulPosition(Types.Vector{
49+
tempMin := Types.Vector{
5250
X: posAccessor.Min[0],
5351
Y: posAccessor.Min[1],
5452
Z: posAccessor.Min[2],
55-
})
56-
tempMax := transformationMatrices[node_index].MulPosition(Types.Vector{
53+
}
54+
tempMax := Types.Vector{
5755
X: posAccessor.Max[0],
5856
Y: posAccessor.Max[1],
5957
Z: posAccessor.Max[2],
60-
})
61-
min = min.Min(tempMin)
62-
max = max.Max(tempMax)
58+
}
59+
// determine all outer points as min/max might switch due to transformation
60+
outer_points := [8]Types.Vector{
61+
{X: tempMin.X, Y: tempMax.Y, Z: tempMax.Z}, // up front left
62+
{X: tempMin.X, Y: tempMax.Y, Z: tempMin.Z}, // up back left
63+
{X: tempMin.X, Y: tempMin.Y, Z: tempMax.Z}, // down front left
64+
{X: tempMin.X, Y: tempMin.Y, Z: tempMin.Z}, // down back left
65+
{X: tempMax.X, Y: tempMax.Y, Z: tempMax.Z}, // up front right
66+
{X: tempMax.X, Y: tempMax.Y, Z: tempMin.Z}, // up back right
67+
{X: tempMax.X, Y: tempMin.Y, Z: tempMax.Z}, // down front right
68+
{X: tempMax.X, Y: tempMin.Y, Z: tempMin.Z}, // down back right
69+
}
70+
for _, vec := range outer_points {
71+
vec = transformationMatrices[node_index].MulPosition(vec)
72+
min = min.Min(vec)
73+
max = max.Max(vec)
74+
}
6375
}
6476
}
6577

6678
scaling := Types.Vector{X: 1, Y: 1, Z: 1}
6779
if desiredSize != nil {
6880
scaling = desiredSize.Div(max.Sub(min))
69-
// following is a temporary fix, TODO: remove once bounding box is refined
70-
scaling.X = math.Abs(scaling.X)
71-
scaling.Y = math.Abs(scaling.Y)
72-
scaling.Z = math.Abs(scaling.Z)
7381
}
7482

7583
for _, node := range gltfNodes {

0 commit comments

Comments
 (0)