ARKit - 沿特定轴(不在平面上)拖动节点 [英] ARKit – Drag a node along a specific axis (not on a plane)
本文介绍了ARKit - 沿特定轴(不在平面上)拖动节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在 Y
轴上我的手指在屏幕上的准确位置拖动一个节点.但我找不到任何方法来做到这一点,这是我当前的代码.你有什么想法吗?
I am trying to drag a node exactly where my finger is on the screen on the Y
-axis. But I don't find any way to do it, here is my current code. Do you have any idea?
var movedObject:SCNNode?
@objc func handlePan(_ recognizer: UIPanGestureRecognizer) {
if recognizer.state == .began {
let tapPoint: CGPoint = recognizer.location(in: sceneView)
let result = sceneView.hitTest(tapPoint, options: nil)
if result.count == 0 {
return
}
let hitResult: SCNHitTestResult? = result.first
movedObject = hitResult?.node //.parent?.parent
} else if recognizer.state == .changed {
if (movedObject != nil) {
let tapPoint: CGPoint = recognizer.location(in: sceneView)
let hitResults = sceneView.hitTest(tapPoint, types: .featurePoint)
let result: ARHitTestResult? = hitResults.last
if result?.worldTransform != nil{
let matrix: SCNMatrix4 = SCNMatrix4.init((result?.worldTransform)!)
let vector: SCNVector3 = SCNVector3Make(matrix.m41, matrix.m42, matrix.m43)
movedObject?.position = vector
}
}
} else if recognizer.state == .ended {
movedObject = nil
}
}
推荐答案
找到解决方案!我在评论中添加了一些解释.
Found the solution ! I added some explanations in comments.
if (movedObject != nil) {
//Normalized-depth coordinate matching the plane I want
let projectedOrigin = sceneView.projectPoint((movedObject?.position)!)
//Location of the finger in the view on a 2D plane
let location2D = recognizer.location(in: sceneView)
//Location of the finger in a 3D vector
let location3D = SCNVector3Make(Float(location2D.x), Float(location2D.y), projectedOrigin.z)
//Unprojects a point from the 2D pixel coordinate system of the renderer to the 3D world coordinate system of the scene
let realLocation3D = sceneView.unprojectPoint(location3D)
if movedObject?.position != nil {
//Only updating Y axis position
movedObject?.position = SCNVector3Make((movedObject?.position.x)!, realLocation3D.y, (movedObject?.position.z)!)
}
}
有助于理解的帖子:
这篇关于ARKit - 沿特定轴(不在平面上)拖动节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文