在 touchesBegan 方法中放置多个 SCN 对象

Place multiple SCN objects in touchesBegan method

我的 Swift 下面的代码使用 func touchesBegan 在 ARKit 视图中放置一个 SCN 对象。问题是——它只放置了一次对象。我想以用户可以 select 任何区域放置 SCN 对象的方式创建代码,并且它也可以根据需要放置任意多次。

这里是GitHublink

class ViewController: UIViewController, ARSCNViewDelegate { 

    @IBOutlet var sceneView: ARSCNView! 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        // Handle the shooting
        guard let frame = sceneView.session.currentFrame else { return }
        let camMatrix = SCNMatrix4(frame.camera.transform)

        let direction = SCNVector3Make(camMatrix.m31 * 5.0, 
                                       camMatrix.m32 * 10.0, 
                                       camMatrix.m33 * 5.0)

        let position = SCNVector3Make(camMatrix.m41, camMatrix.m42, camMatrix.m43)
        let scene = SCNScene(named: "art.scnassets/dontCare.scn")!
        sceneView.scene = scene
    }    
}  

一个'object'是一个SCNNode。这些显示在 3D 场景中,SCNScene。每次点击屏幕时,都会将场景应用到 sceneView,而不是向场景添加节点。

您还需要找到用户在场景中点击的位置,即触摸的3D 位置。这需要命中测试。

试试这个

class ViewController: UIViewController, ARSCNViewDelegate {

@IBOutlet var sceneView: ARSCNView!

override func viewDidLoad() {
    super.viewDidLoad()
    sceneView.delegate = self
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    let configuration = ARWorldTrackingConfiguration()
    sceneView.session.run(configuration)
}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard
        let touchPosition = touches.first?.location(in: sceneView),
        let hitTest = sceneView.hitTest(touchPosition, types: .featurePoint).first
        else {return}

    let node = SCNScene(named: "art.scnassets/dontCare.scn")!.rootNode.childNodes.first!
    node.simdTransform = hitTest.worldTransform
    sceneView.scene.rootNode.addChildNode(node)
}

}

提示:如果您使用 RealityKit,请阅读此 post。

解决方案 1

使用 touchesBegan(:with:)

添加 3D 模型

Use the following code to get a desired effect (place as many objects into a scene as you want):

首先为方便起见创建一个扩展:

import ARKit

extension SCNVector3 {
    
    static func + (lhs: SCNVector3, rhs: SCNVector3) -> SCNVector3 {
        return SCNVector3(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z)
    }
}

然后在你的 ViewController 中使用它来添加 pointOfView.positiondesiredVector:

class ViewController: UIViewController {

    @IBOutlet var sceneView: ARSCNView!
    
    override func touchesBegan(_ touches: Set<UITouch>,
                              with event: UIEvent?) {
        
        sceneView.isMultipleTouchEnabled = true
        
        guard let pointOfView = sceneView.pointOfView   // Camera of SCNScene
        else { return }
        
        let cameraMatrix = pointOfView.transform
        
        let desiredVector = SCNVector3(cameraMatrix.m31 * -0.5,
                                       cameraMatrix.m32 * -0.5,
                                       cameraMatrix.m33 * -0.5)
        
        // What the extension SCNVector3 is for //
        let position = pointOfView.position + desiredVector 
        
        let sphereNode = SCNNode()
        sphereNode.geometry = SCNSphere(radius: 0.05)
        sphereNode.geometry?.firstMaterial?.diffuse.contents = UIColor.green
        sphereNode.position = position
        sceneView.scene.rootNode.addChildNode(sphereNode)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        let scene = SCNScene(named: "art.scnassets/myScene.scn")!
        sceneView.scene = scene
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let config = ARWorldTrackingConfiguration()
        sceneView.session.run(config)
    }
}

And if you want to retrieve a 3D model from .scn file use the following code:

(Instead of the sphereNode):

var model = SCNNode()
let myScene = SCNScene(named: "art.scnassets/ship.scn")

// Model's name in a Scene graph hierarchy.
// Pay particular attention – it's not a name of .scn file.
let nodeName = "ship"
    
model = (myScene?.rootNode.childNode(withName: nodeName, recursively: true))!
model.position = position
sceneView.scene.rootNode.addChildNode(model)


解决方案 2

使用平面检测 + 命中测试添加 3D 模型

Use the following code if you want to add models using plane detection and Hit-testing :

首先为方便起见创建一个扩展:

extension float4x4 {
    var simdThree: SIMD3<Float> {
        let translation = self.columns.3            
        return SIMD3<Float>(translation.x, translation.y, translation.z)
    }
}

然后在ViewController中使用它:

class ViewController: UIViewController {

    @IBOutlet weak var sceneView: ARSCNView!

    override func viewDidLoad() {
        super.viewDidLoad()
        addGesture()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        sceneView.delegate = self             // for ARSCNViewDelegate

        let config = ARWorldTrackingConfiguration()
        config.planeDetection = [.horizontal]
        sceneView.session.run(config)
    }

    func addGesture() {
        let tapGesture = UITapGestureRecognizer(target: self, 
                                                action: #selector(addModel))
        sceneView.addGestureRecognizer(tapGesture)
    }

    // Don't forget to drag-and-drop TapGestureRecognizer object from library
    @objc func addModel(recognizer: UIGestureRecognizer) {
        
        let tap: CGPoint = recognizer.location(in: sceneView)

        let results: [ARHitTestResult] = sceneView.hitTest(tap, 
                                           types: .existingPlaneUsingExtent)
        
        guard let hitTestResult = results.first 
        else { return }

        let translation = hitTestResult.worldTransform.simdThree
        let x = translation.x
        let y = translation.y
        let z = translation.z
        
        guard let scene = SCNScene(named: "art.scnassets/myScene.scn"),
              let robotNode = scene.rootNode.childNode(withName: "robot", 
                                                    recursively: true)
        else { return }

        robotNode.position = SCNVector3(x, y, z)
        robotNode.scale = SCNVector3(0.02, 0.02, 0.02)
        sceneView.scene.rootNode.addChildNode(robotNode)
    }
}

And, you have to implement a logic inside two renderer() methods for ARPlaneAnchors:

extension ViewController: ARSCNViewDelegate {

    func renderer(_ renderer: SCNSceneRenderer,
                 didAdd node: SCNNode,
                  for anchor: ARAnchor) { // your logic here....  }

    func renderer(_ renderer: SCNSceneRenderer,
              didUpdate node: SCNNode,
                  for anchor: ARAnchor) { // your logic here....  }

}