繁体   English   中英

如何在我的SceneKit / ARKit场景中添加节点,保持其位置但重置其方向/旋转

[英]How do I add a node to my SceneKit/ARKit scene, retaining its position but resetting its orientation/rotation

我想在场景中添加一个节点,保持相对于摄像机的位置,但保持场景的方向/旋转。 到目前为止我要去哪里...

当我点击屏幕时,我在场景中添加了一个锚点,其变形等于相机的变形。 然后在didUpdateNode (锚节点已设置其位置信息),我添加了一个包含模型的子节点,该模型的position.z = -0.5 模型节点直接添加到摄像机前面的场景中,并反映了摄像机的旋转特性。 当手机向下倾斜并倾斜时,可能看起来像这样:

在此处输入图片说明

受锚点旋转数据影响的模型节点以与摄影机相同的方式旋转。 这个特定的模型在它的侧面出现-我并不担心,它只是一个占位符。 还要忽略顶部用于调试的红色按钮。

到现在为止还挺好。

我要代替此发生的是将旋转信息从本质上“重置”,因此它出现在与我的相机相同的高度上,并且面向默认的面朝前位置。 如果我以不同的方式旋转相机并在房间中四处移动,则会在不同的位置添加其他模型,但仍要在相同的高度和相同的方向上。

可能看起来像这样。 想象一下,我刚刚轻按屏幕来添加对象,它以标准高度显示,并且旋转与场景相关,而不是与相机相关。

在此处输入图片说明

我试着玩弄节点的eulerAnglesrotationorientation性,但不能得到这个满意地工作。 下面是我迄今..我创建了一个空白的场景的代码,我使用ship.scn从标准SceneKit项目作为模型:

class ViewController: UIViewController, ARSCNViewDelegate {
  @IBOutlet var sceneView: ARSCNView!

  var anchors = [ARAnchor]()    

  override func viewDidLoad() {
    super.viewDidLoad()

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

    // Show statistics such as fps and timing information
    sceneView.showsStatistics = true

    // Create a new scene
    let scene = SCNScene(named: "scene.scn")!

    // Set the scene to the view
    sceneView.scene = scene
  }

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

    // Create a session configuration
    let configuration = ARWorldTrackingSessionConfiguration()
    configuration.planeDetection = .horizontal

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

  func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
    if anchors.contains(anchor) {
        // Create a new scene
      let scene = SCNScene(named: "art.scnassets/ship.scn")!
      let shipNode = scene.rootNode.childNode(withName: "ship", recursively: true)!
      shipNode.position.z = -5
      node.addChildNode(shipNode)
    }

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

        if let currentFrame = sceneView.session.currentFrame {
        let translation = matrix_identity_float4x4
        let transform = simd_mul(currentFrame.camera.transform, translation)
        let anchor = ARAnchor(transform: transform)

        anchors.append(anchor)
        sceneView.session.add(anchor: anchor)
        }
    }
  }
}

我是否缺少某些东西?还是只希望每个模型都具有相同的方向,而与相机的方向无关?

在哪种情况下,您可以仅将每个模型的方向设置为与会话开始运行时添加的节点相同?

即,当会话开始时,您将一个节点添加到场景中(可见或隐藏),保存其方向,然后稍后再使用?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM