Total height of SCNNode children

I am currently using the following to get the total height of all child nodes in SCNNode

. Is there a more efficient / better / shorter / faster way to do this?

CGFloat(columnNode.childNodes.reduce(CGFloat()) {
    let geometry = $1.geometry! as SCNBox
    return $0 + geometry.height
})

      

+3


source to share


1 answer


Yes, and a way that will give you a more correct answer. Summing the heights of all child node geometries ...

  • only works if the geometry is SCNBox

  • ignores transformations of child nodes (what if they are moved, rotated or scaled?)
  • ignores the parent transform of the node (what if you want the height to be in scene space?)

What you want is a protocol SCNBoundingVolume

that applies to all nodes and geometries and describes the smallest rectangular or spherical space containing all the contents of a node (and its trays).

In Swift 3 it is very simple:

let (min, max) = columnNode.boundingBox

      



After that, min

and max

are the coordinates of the bottom and bottom left and top right corners of the smallest possible box containing everything inside columnNode

, regardless of how that content is organized (and what geometry is involved). These coordinates are expressed in the same system as and columnNode.position

, so if the "height" you are looking for is in the y-direction of that space, just subtract the y-coordinates of these two vectors:

let height = max.y - min.y

      


In Swift 2, the syntax for it is a bit weird, but it works well enough:

var min = SCNVector3Zero
var max = SCNVector3Zero
columnNode.getBoundingBoxMin(&min, max: &max)

      

+11


source







All Articles