【发布时间】:2023-04-04 00:20:01
【问题描述】:
我将一个对象放在墙上,然后尝试识别点击它,但点击测试返回 0 个对象。当我改变对象的 Z 位置并将其放置在更靠近凸轮的位置时,可以很好地识别它,但这不是解决方案,因为平面总是在变化,它可以随时覆盖对象。如何使 hitTest 正常工作并识别平面后面的节点?或者,也许,我使用了错误的方法?
fileprivate func addNode(atPoint point: CGPoint) {
let hits = sceneView.hitTest(point, types: .existingPlaneUsingExtent)
if hits.count > 0, let firstHit = hits.first, let originNode = originNode {
let node = originNode.clone()
sceneView.scene.rootNode.addChildNode(node)
node.position = SCNVector3Make(firstHit.worldTransform.columns.3.x, firstHit.worldTransform.columns.3.y, firstHit.worldTransform.columns.3.z)
let resize = simd_float4x4(SCNMatrix4MakeScale(0.2, 0.2, 0.2))
let rotation = simd_float4x4(SCNMatrix4MakeRotation(.pi / 2, -1, 0, 0))
let transform = simd_mul(firstHit.worldTransform, resize)
let finalTransform = simd_mul(transform, rotation)
node.simdTransform = finalTransform
addedNodes.insert(node)
}
}
func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {
print("Unable to identify touches on any plane. Ignoring interaction...")
return
}
let touchPoint = touch.location(in: sceneView)
let hits = sceneView.hitTest(touchPoint, options: [SCNHitTestOption.boundingBoxOnly: true])
let filtered = hits.filter({ addedNodes.contains($0.node) })
print("\(hits.count) vs \(filtered.count), \(hits.first?.node.name ?? "no name")")
if let node = filtered.first?.node {
node.removeFromParentNode()
addedNodes.remove(node)
return
}
addPictureToPlane(atPoint: touchPoint)
}
addedNodes - 设置有添加的对象。当我添加平移变换并改变 Z 坐标至少在 0.05(靠近相机)检测工作良好。至少在平面改变和前进节点之前。
【问题讨论】:
-
请分享您的代码 - 不知道您在做什么就无法回答。
-
添加了一段代码