gpt4 book ai didi

swift - 如何在 SCNNode 对象 boundingBox 中定位对象以显示/隐藏功能

转载 作者:搜寻专家 更新时间:2023-11-01 07:06:37 24 4
gpt4 key购买 nike

尝试在 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)
}

结构边界框{ 让最小值: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

最佳答案

worldPosition 和它的 sibling ,以及他们的 simd 表亲在 iOS11 中新引入了 SCNNode,即使它没有(或没有)记录,您可以从术语“世界”本身以及源代码中的注释中获得一些线索,如下所示。

/*!
@abstract Determines the receiver's position in world space (relative to the scene's root node).
*/
@available(iOS 11.0, *)
open var simdWorldPosition: simd_float3

对于您的问题,有两种解决方案,但您必须选择一种您喜欢的方式。从您的代码中,您正在使用 2 个不同 坐标空间进行混合。

  1. 如果您选择使用“世界”空间,您应该将帽子和立方体转换为“世界”空间。显然,您的立方体是,但不是。

  2. 如果您不想理会世界,而是相信相对论,您可以简单地将立方体转换为帽子的坐标/空间,使用您喜欢的 convertPosition 或 simdConvertPosition, 在 boundingBoxContains

  3. 之前

关于swift - 如何在 SCNNode 对象 boundingBox 中定位对象以显示/隐藏功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47666319/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com