ARKit 从屏幕平移创建节点
ARKit create node from screen pan
我目前正在开发一个应用程序,其目标是让用户能够在屏幕上滑动手指并在场景中看到一行 3D 对象,类似于 3D 绘图。我使用手势识别器设置了场景,但我不知道如何根据屏幕上的平移在 3D space 中创建节点。这就是我现在所有的手势识别器。
`@IBAction func panRecognized(_ sender: UIPanGestureRecognizer) {
print("Panning")
print(sender.location(in: self.view).x)
print(sender.location(in: self.view).y)
}`
我想知道的是如何使用这些 x 和 y 坐标在 3D space 中创建节点,同时还使用设备的方向和位置?目标是放置在距设备约半米远的物体上。
您可以通过以下几种方法实现此目的,但请注意,这只是一个起点,并未进行任何优化:
你要做的第一件事是在 viewDidLoad 中创建一个 UIPanGestureRecognizer
例如:
let panToDrawGesture = UIPanGestureRecognizer(target: self, action: #selector(createNodesFromPan(_:)))
self.view.addGestureRecognizer(panToDrawGesture)
为了让它更有趣一点,还添加一个 [UIColor] 数组,例如:
let colours: [UIColor] = [.red, .green, .cyan, .orange, .purple]
然后要使用您的 gestureRecognizer 进行绘制,您可以这样做:
///Creates An SCNNode At The Touch Location Of The Gesture Recognizer
@objc func createNodesFromPan(_ gesture: UIPanGestureRecognizer){
//1. Get The Current Touch Location
let currentTouchPoint = gesture.location(in: self.augmentedRealityView)
//2. Perform An ARHitTest For Detected Feature Points
guard let featurePointHitTest = self.augmentedRealityView.hitTest(currentTouchPoint, types: .featurePoint).first else { return }
//3. Get The World Coordinates
let worldCoordinates = featurePointHitTest.worldTransform
//4. Create An SCNNode With An SCNSphere Geeomtery
let sphereNode = SCNNode()
let sphereNodeGeometry = SCNSphere(radius: 0.005)
//5. Generate A Random Colour For The Node's Geometry
let randomColour = colours[Int(arc4random_uniform(UInt32(colours.count)))]
sphereNodeGeometry.firstMaterial?.diffuse.contents = randomColour
sphereNode.geometry = sphereNodeGeometry
//6. Position & Add It To The Scene Hierachy
sphereNode.position = SCNVector3(worldCoordinates.columns.3.x, worldCoordinates.columns.3.y, worldCoordinates.columns.3.z)
self.augmentedRealityView.scene.rootNode.addChildNode(sphereNode)
}
或者,您可以使用触摸来执行绘图,而不是使用 gestureRecognizer:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
//1. Get The Current Touch Location
guard let currentTouchPoint = touches.first?.location(in: self.augmentedRealityView),
//2. Perform An ARHitTest For Detected Feature Points
let featurePointHitTest = self.augmentedRealityView.hitTest(currentTouchPoint, types: .featurePoint).first else { return }
//3. Get The World Coordinates
let worldCoordinates = featurePointHitTest.worldTransform
//4. Create An SCNNode With An SCNSphere Geeomtery
let sphereNode = SCNNode()
let sphereNodeGeometry = SCNSphere(radius: 0.005)
//5. Generate A Random Colour For The Node's Geometry
let randomColour = colours[Int(arc4random_uniform(UInt32(colours.count)))]
sphereNodeGeometry.firstMaterial?.diffuse.contents = randomColour
sphereNode.geometry = sphereNodeGeometry
//6. Position & Add It To The Scene Hierachy
sphereNode.position = SCNVector3(worldCoordinates.columns.3.x, worldCoordinates.columns.3.y, worldCoordinates.columns.3.z)
self.augmentedRealityView.scene.rootNode.addChildNode(sphereNode)
}
在我的示例中,augmentedRealityView 指的是 ARSCNView,例如:
@IBOutlet weak var augmentedRealityView: ARSCNView!
希望对您有所帮助...
更新:
如果你想在设定的距离处绘图,并且只使用 ARCamera 的位置,你可以使用 ARSessionDelegate
来做这样的事情(如果你想让你的绘图距离 ARCamera 1m相机然后更改第 3 部分中 sphereNode.position 的 Z 值:
func session(_ session: ARSession, didUpdate frame: ARFrame) {
//1. Create An SCNNode With An SCNSphere Geeomtery
let sphereNode = SCNNode()
let sphereNodeGeometry = SCNSphere(radius: 0.01)
//2. Generate A Random Colour For The Node's Geometry
let randomColour = colours[Int(arc4random_uniform(UInt32(colours.count)))]
sphereNodeGeometry.firstMaterial?.diffuse.contents = randomColour
sphereNode.geometry = sphereNodeGeometry
//3. Position & Add It To The Scene Hierachy
sphereNode.position = SCNVector3(0, 0, -0.5)
updatePositionAndOrientationOf(sphereNode, withPosition: sphereNode.position, relativeTo: self.augmentedRealityView.pointOfView!)
self.augmentedRealityView.scene.rootNode.addChildNode(sphereNode)
}
/// Updates The Position Of An SCNNode In Relation To The Camera Node
///
/// - Parameters:
/// - node: SCNNode
/// - position: SCNVector3
/// - referenceNode: SCNNode
func updatePositionAndOrientationOf(_ node: SCNNode, withPosition position: SCNVector3, relativeTo referenceNode: SCNNode) {
/* Full Credit To Pablo
*/
let referenceNodeTransform = matrix_float4x4(referenceNode.transform)
// Create A Translation Matrix With The Desired Position
var translationMatrix = matrix_identity_float4x4
translationMatrix.columns.3.x = position.x
translationMatrix.columns.3.y = position.y
translationMatrix.columns.3.z = position.z
// Multiply The Configured Translation With The ReferenceNode's Transform
let updatedTransform = matrix_multiply(referenceNodeTransform, translationMatrix)
node.transform = SCNMatrix4(updatedTransform)
}
我目前正在开发一个应用程序,其目标是让用户能够在屏幕上滑动手指并在场景中看到一行 3D 对象,类似于 3D 绘图。我使用手势识别器设置了场景,但我不知道如何根据屏幕上的平移在 3D space 中创建节点。这就是我现在所有的手势识别器。
`@IBAction func panRecognized(_ sender: UIPanGestureRecognizer) {
print("Panning")
print(sender.location(in: self.view).x)
print(sender.location(in: self.view).y)
}`
我想知道的是如何使用这些 x 和 y 坐标在 3D space 中创建节点,同时还使用设备的方向和位置?目标是放置在距设备约半米远的物体上。
您可以通过以下几种方法实现此目的,但请注意,这只是一个起点,并未进行任何优化:
你要做的第一件事是在 viewDidLoad 中创建一个 UIPanGestureRecognizer
例如:
let panToDrawGesture = UIPanGestureRecognizer(target: self, action: #selector(createNodesFromPan(_:)))
self.view.addGestureRecognizer(panToDrawGesture)
为了让它更有趣一点,还添加一个 [UIColor] 数组,例如:
let colours: [UIColor] = [.red, .green, .cyan, .orange, .purple]
然后要使用您的 gestureRecognizer 进行绘制,您可以这样做:
///Creates An SCNNode At The Touch Location Of The Gesture Recognizer
@objc func createNodesFromPan(_ gesture: UIPanGestureRecognizer){
//1. Get The Current Touch Location
let currentTouchPoint = gesture.location(in: self.augmentedRealityView)
//2. Perform An ARHitTest For Detected Feature Points
guard let featurePointHitTest = self.augmentedRealityView.hitTest(currentTouchPoint, types: .featurePoint).first else { return }
//3. Get The World Coordinates
let worldCoordinates = featurePointHitTest.worldTransform
//4. Create An SCNNode With An SCNSphere Geeomtery
let sphereNode = SCNNode()
let sphereNodeGeometry = SCNSphere(radius: 0.005)
//5. Generate A Random Colour For The Node's Geometry
let randomColour = colours[Int(arc4random_uniform(UInt32(colours.count)))]
sphereNodeGeometry.firstMaterial?.diffuse.contents = randomColour
sphereNode.geometry = sphereNodeGeometry
//6. Position & Add It To The Scene Hierachy
sphereNode.position = SCNVector3(worldCoordinates.columns.3.x, worldCoordinates.columns.3.y, worldCoordinates.columns.3.z)
self.augmentedRealityView.scene.rootNode.addChildNode(sphereNode)
}
或者,您可以使用触摸来执行绘图,而不是使用 gestureRecognizer:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
//1. Get The Current Touch Location
guard let currentTouchPoint = touches.first?.location(in: self.augmentedRealityView),
//2. Perform An ARHitTest For Detected Feature Points
let featurePointHitTest = self.augmentedRealityView.hitTest(currentTouchPoint, types: .featurePoint).first else { return }
//3. Get The World Coordinates
let worldCoordinates = featurePointHitTest.worldTransform
//4. Create An SCNNode With An SCNSphere Geeomtery
let sphereNode = SCNNode()
let sphereNodeGeometry = SCNSphere(radius: 0.005)
//5. Generate A Random Colour For The Node's Geometry
let randomColour = colours[Int(arc4random_uniform(UInt32(colours.count)))]
sphereNodeGeometry.firstMaterial?.diffuse.contents = randomColour
sphereNode.geometry = sphereNodeGeometry
//6. Position & Add It To The Scene Hierachy
sphereNode.position = SCNVector3(worldCoordinates.columns.3.x, worldCoordinates.columns.3.y, worldCoordinates.columns.3.z)
self.augmentedRealityView.scene.rootNode.addChildNode(sphereNode)
}
在我的示例中,augmentedRealityView 指的是 ARSCNView,例如:
@IBOutlet weak var augmentedRealityView: ARSCNView!
希望对您有所帮助...
更新:
如果你想在设定的距离处绘图,并且只使用 ARCamera 的位置,你可以使用 ARSessionDelegate
来做这样的事情(如果你想让你的绘图距离 ARCamera 1m相机然后更改第 3 部分中 sphereNode.position 的 Z 值:
func session(_ session: ARSession, didUpdate frame: ARFrame) {
//1. Create An SCNNode With An SCNSphere Geeomtery
let sphereNode = SCNNode()
let sphereNodeGeometry = SCNSphere(radius: 0.01)
//2. Generate A Random Colour For The Node's Geometry
let randomColour = colours[Int(arc4random_uniform(UInt32(colours.count)))]
sphereNodeGeometry.firstMaterial?.diffuse.contents = randomColour
sphereNode.geometry = sphereNodeGeometry
//3. Position & Add It To The Scene Hierachy
sphereNode.position = SCNVector3(0, 0, -0.5)
updatePositionAndOrientationOf(sphereNode, withPosition: sphereNode.position, relativeTo: self.augmentedRealityView.pointOfView!)
self.augmentedRealityView.scene.rootNode.addChildNode(sphereNode)
}
/// Updates The Position Of An SCNNode In Relation To The Camera Node
///
/// - Parameters:
/// - node: SCNNode
/// - position: SCNVector3
/// - referenceNode: SCNNode
func updatePositionAndOrientationOf(_ node: SCNNode, withPosition position: SCNVector3, relativeTo referenceNode: SCNNode) {
/* Full Credit To Pablo
*/
let referenceNodeTransform = matrix_float4x4(referenceNode.transform)
// Create A Translation Matrix With The Desired Position
var translationMatrix = matrix_identity_float4x4
translationMatrix.columns.3.x = position.x
translationMatrix.columns.3.y = position.y
translationMatrix.columns.3.z = position.z
// Multiply The Configured Translation With The ReferenceNode's Transform
let updatedTransform = matrix_multiply(referenceNodeTransform, translationMatrix)
node.transform = SCNMatrix4(updatedTransform)
}