我需要将ARSCNView的2d坐标空间中的一个点转换为3d空间中的一个坐标。基本上是从视点到触摸位置的光线(最远可达设定的距离)。
我想使用arView.unprojectPoint(vec2d),但返回的点似乎总是位于视图的中心
vec2d是从2d坐标创建的SCNVector3,如下所示
SCNVector3(x, y, 0) // 0 specifies camera near plane我做错了什么?如何获得想要的结果?
发布于 2018-05-29 01:30:28
我认为你至少有两种可能的解决方案:
第一
使用hitTest(_:types:)实例方法:
此方法在捕获的摄像机图像中搜索与SceneKit视图中的点相对应的真实世界对象或AR锚点。
let sceneView = ARSCNView()
func calculateVector(point: CGPoint) -> SCNVector3? {
let hitTestResults = sceneView.hitTest(point,
types: [.existingPlane])
if let result = hitTestResults.first {
return SCNVector3.init(SIMD3(result.worldTransform.columns.3.x,
result.worldTransform.columns.3.y,
result.worldTransform.columns.3.z))
}
return nil
}
calculateVector(point: yourPoint)第二
使用unprojectPoint(_:ontoPlane:)实例方法:
此方法返回点从2D视图到由ARKit检测到的3D世界空间中的平面的投影。
@nonobjc func unprojectPoint(_ point: CGPoint,
ontoPlane planeTransform: simd_float4x4) -> simd_float3?或者:
let point = CGPoint()
var planeTransform = simd_float4x4()
sceneView.unprojectPoint(point,
ontoPlane: planeTransform)发布于 2020-05-28 16:36:20
在相机前面的'x‘cm偏移处添加一个空节点,并使其成为相机的子节点。
//Add a node in front of camera just after creating scene
hitNode = SCNNode()
hitNode!.position = SCNVector3Make(0, 0, -0.25) //25 cm offset
sceneView.pointOfView?.addChildNode(hitNode!)
func unprojectedPosition(touch: CGPoint) -> SCNVector3 {
guard let hitNode = self.hitNode else {
return SCNVector3Zero
}
let projectedOrigin = sceneView.projectPoint(hitNode.worldPosition)
let offset = sceneView.unprojectPoint(SCNVector3Make(Float(touch.x), Float(touch.y), projectedOrigin.z))
return offset
}https://stackoverflow.com/questions/49573794
复制相似问题