ソースを参照

Merge pull request #169 from breiting/i168-bbox

Initialize bounding box for node and geometry with min/max bounds
Daniel Salvadori 5 年 前
コミット
78541849ab
2 ファイル変更12 行追加5 行削除
  1. 7 2
      core/node.go
  2. 5 3
      geometry/geometry.go

+ 7 - 2
core/node.go

@@ -5,9 +5,11 @@
 package core
 
 import (
+	"math"
+	"strings"
+
 	"github.com/g3n/engine/gls"
 	"github.com/g3n/engine/math32"
-	"strings"
 )
 
 // INode is the interface for all node types.
@@ -115,7 +117,10 @@ func (n *Node) GetNode() *Node {
 // Computes union of own bounding box with those of all descendents.
 func (n *Node) BoundingBox() math32.Box3 {
 
-	bbox := math32.Box3{n.position, n.position}
+	bbox := math32.Box3{
+		Min: math32.Vector3{X: math.MaxFloat32, Y: math.MaxFloat32, Z: math.MaxFloat32},
+		Max: math32.Vector3{X: -math.MaxFloat32, Y: -math.MaxFloat32, Z: -math.MaxFloat32},
+	}
 	for _, inode := range n.Children() {
 		childBbox := inode.BoundingBox()
 		bbox.Union(&childBbox)

+ 5 - 3
geometry/geometry.go

@@ -6,10 +6,12 @@
 package geometry
 
 import (
+	"math"
+	"strconv"
+
 	"github.com/g3n/engine/gls"
 	"github.com/g3n/engine/math32"
 	"github.com/g3n/engine/util/logger"
-	"strconv"
 )
 
 // Package logger
@@ -317,8 +319,8 @@ func (g *Geometry) BoundingBox() math32.Box3 {
 	}
 
 	// Reset bounding box
-	g.boundingBox.Min.Set(0, 0, 0)
-	g.boundingBox.Max.Set(0, 0, 0)
+	g.boundingBox.Min.Set(math.MaxFloat32, math.MaxFloat32, math.MaxFloat32)
+	g.boundingBox.Max.Set(-math.MaxFloat32, -math.MaxFloat32, -math.MaxFloat32)
 
 	// Expand bounding box by each vertex
 	g.ReadVertices(func(vertex math32.Vector3) bool {