尝试在 SCNNode 中设置 boundingBox 检测时遇到问题。我希望能够调用 boundingBox 函数来检测 SCNNode 内的对象并在按钮点击时隐藏/显示这些对象,以下是我当前的状态:
@IBAction func hideCubeButtonTapped(sender: UIButton) {
guard let hatNode = hatNode?.presentation.worldPosition else { return }
for cubeNode in cubeNodes {
// hide/show cubeNodes in the hat
if (hatNode.boundingBoxContains(point: cubeNode.presentation.worldPosition)) {
print("Hide Button Tapped")
if cubeNode.isHidden == true {
cubeNode.isHidden = false
} else {
cubeNode.isHidden = true
}
}
}
}
从这个结构和扩展调用 boundingBox 函数:
extension SCNNode {
func boundingBoxContains(point: SCNVector3, in node: SCNNode) -> Bool {
let localPoint = self.convertPosition(point, from: node)
return boundingBoxContains(point: localPoint)
}
func boundingBoxContains(point: SCNVector3) -> Bool {
return BoundingBox(self.boundingBox).contains(point)
}
}
struct BoundingBox { 让最小值:SCNVector3 让最大值:SCNVector3
init(_ boundTuple: (min: SCNVector3, max: SCNVector3)) {
min = boundTuple.min
max = boundTuple.max
}
func contains(_ point: SCNVector3) -> Bool {
let contains =
min.x <= point.x &&
min.y <= point.y &&
min.z <= point.z &&
max.x > point.x &&
max.y > point.y &&
max.z > point.z
return contains
}
}
调用 hatNode.boundingBoxContains 时显示此错误:“'SCNVector3' 类型的值没有成员 'boundingBoxContains'”
hatNode 没有被设置为 SCNVector3?我在这里想念什么?我是 swift 新手,所以请在这里纠正我!
这段代码改编自这个问题: How to detect if a specific SCNNode is located inside another SCNNode's boundingBox - SceneKit - iOS