【问题标题】:How to make a model that follows camera如何制作跟随相机的模型
【发布时间】:2018-07-29 04:31:43
【问题描述】:

我想添加一个跟随 iOS 设备摄像头的 3D 模型。到目前为止,我的代码还不够,因为没有轮换。更重要的是,我觉得在ARKit 内部有更简单的解决方案,比如SceneKit,所以我想请你帮忙:

import UIKit
import SceneKit
import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {

    @IBOutlet var sceneView: ARSCNView!
    var timer = Timer()
    var pistolNode = SCNNode()

    override func viewDidLoad()
    {
        super.viewDidLoad()

        // Set the view's delegate
        sceneView.delegate = self

        // Create a new scene
        let pistol = SCNScene(named: "art.scnassets/pistol.dae")!
        pistolNode = pistol.rootNode.childNode(withName: "pistol", recursively: true)!
        pistolNode.position = SCNVector3Make(0, -0.3, -0.5)
        let scene = SCNScene()
        scene.rootNode.addChildNode(pistolNode)

        // Set the scene to the view
        sceneView.scene = scene
        timer = Timer.scheduledTimer(timeInterval: 1, target: self,   selector: (#selector(ViewController.restartSession)), userInfo: nil, repeats: true)
    }

    override func viewWillAppear(_ animated: Bool)
    {
        super.viewWillAppear(animated)

        // Create a session configuration
        let configuration = ARWorldTrackingConfiguration()

        // Run the view's session
        sceneView.session.run(configuration)
    }

    override func viewWillDisappear(_ animated: Bool)
    {
        super.viewWillDisappear(animated)

        // Pause the view's session
        sceneView.session.pause()
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.
    }

    @objc func restartSession()
    {
        pistolNode.position = SCNVector3Make((sceneView.pointOfView?.position.x)!, (sceneView.pointOfView?.position.y)! - 0.3, (sceneView.pointOfView?.position.z)! - 0.5)
    }

    @IBOutlet weak var info: UILabel!
}

提前致谢!

【问题讨论】:

    标签: ios swift 3d scenekit arkit


    【解决方案1】:

    如果将节点添加为包含相机的节点的子节点,则该节点将与相机一起移动。您可以使用sceneView.pointOfView? 获取包含相机的节点,并使用addChildNode(child:) 方法将pistolNode 添加为其子节点之一。

    // Create a new scene
    let pistol = SCNScene(named: "art.scnassets/pistol.dae")!
    pistolNode = pistol.rootNode.childNode(withName: "pistol", recursively: true)!
    pistolNode.position = SCNVector3Make(0, -0.3, -0.5)
    // let scene = SCNScene()
    sceneView.pointOfView?.addChildNode(pistolNode)
    
    // Set the scene to the view
    sceneView.scene = pistol
    

    【讨论】:

      猜你喜欢
      • 2012-10-25
      • 1970-01-01
      • 2017-08-13
      • 2017-08-28
      • 2016-02-24
      • 1970-01-01
      • 2022-10-04
      • 2015-12-21
      • 1970-01-01
      相关资源
      最近更新 更多