SCNNode childNodes的总高度 [英] Total height of SCNNode childNodes
问题描述
我目前正在使用以下内容获取 SCNNode
中所有子节点的总高度。是否有更高效/更好/更短/更快速的方式来做到这一点?
I'm currently using the following to get the total height of all of the child nodes in a SCNNode
. Is there a more efficient/better/shorter/more swift-like way to do this?
CGFloat(columnNode.childNodes.reduce(CGFloat()) {
let geometry = $1.geometry! as SCNBox
return $0 + geometry.height
})
推荐答案
是的,这也是一种能让你得到更正确答案的方法。求和所有子节点几何的高度...
Yes, and a way that'll get you a more correct answer, too. Summing the height of all the child nodes' geometries...
- 仅在几何为
SCNBox <时才有效/ code>
- 不考虑子节点的变换(如果它们被移动,旋转或缩放会怎样?)
- 不考虑父节点的变换(如果你想在场景空间中高度怎么办?)
你想要的是< a href =https://developer.apple.com/reference/scenekit/scnboundingvolume =nofollow> SCNBoundingVolume
协议,适用于所有节点和几何,并描述包含所有节点(及其子节点)内容的最小矩形或球形空间。
What you want is the SCNBoundingVolume
protocol, which applies to all nodes and geometries, and describes the smallest rectangular or spherical space containing all of a node's (and its subnodes') content.
在Swift 3中,这非常简单:
In Swift 3, this is super easy:
let (min, max) = columnNode.boundingBox
此后, min
和 max
是较低的坐标 - 最小的po的左前角和右上角包含 columnNode
内所有内容的ssible框,无论内容是如何排列的(以及涉及的几何类型)。这些坐标在与 columnNode.position
相同的系统中表示,因此如果您要查找的高度位于该空间的y轴方向,则只需减去这两个向量的y坐标:
After this, min
and max
are the coordinates of the lower-rear-left and upper-front-right corners of the smallest possible box containing everything inside columnNode
, no matter how that content is arranged (and what kind of geometry is involved). These coordinates are expressed in the same system as columnNode.position
, so if the "height" you're looking for is in the y-axis direction of that space, just subtract the y-coordinates of those two vectors:
let height = max.y - min.y
在Swift 2中,它的语法有点奇怪,但效果很好够了:
In Swift 2, the syntax for it is a little weird, but it works well enough:
var min = SCNVector3Zero
var max = SCNVector3Zero
columnNode.getBoundingBoxMin(&min, max: &max)
这篇关于SCNNode childNodes的总高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!