ARKit从屏幕平移创建节点

时间:2018-06-06 00:56:39

标签: ios swift drawing arkit

我目前正在开发一款应用程序,其目标是让用户能够在屏幕上轻扫手指并在场景中看到一系列3D对象,类似于3D绘图。我使用手势识别器进行了场景设置,但我不知道如何根据屏幕上的平移在3D空间中创建节点。这就是我现在拥有的手势识别器。

`@IBAction func panRecognized(_ sender: UIPanGestureRecognizer) {
    print("Panning")
    print(sender.location(in: self.view).x)
    print(sender.location(in: self.view).y)
}`

我想知道的是如何使用x和y坐标在3D空间中创建节点,同时还使用设备的方向和位置?目标是将物体放置在离设备约半米的位置。

1 个答案:

答案 0 :(得分:1)

您可以通过以下几种方式实现这一目标,但请注意,这只是一个起点,并未以任何方式进行优化:

您要做的第一件事是在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做这样的事情(如果你想让你的画作说距离相机1米那么在第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
    https://stackoverflow.com/questions/42029347/position-a-scenekit-object-in-front-of-scncameras-current-orientation/42030679
    */

    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)
}