ARKit - 相机位置

时间:2017-10-03 17:06:40

标签: ios iphone swift scenekit arkit

我试图在全局坐标中的3D空间中获取相机的位置。

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        sceneView.delegate = self
        sceneView.showsStatistics = true
        sceneView.session.delegate = self
    }

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // Create a session configuration with plane detection
    let configuration = ARWorldTrackingConfiguration()
    configuration.worldAlignment = .gravityAndHeading
    // Run the view's session
    sceneView.session.run(configuration)
}

func session(_ session: ARSession, didUpdate frame: ARFrame) {
        print(frame.camera.transform)
    }

我运行上面的代码,下面是我得到的示例输出

  

可选(simd_float4x4([[ - 0.739868,-0.052769,-0.670678,0.0)],   [-0.569322,0.580241,0.582402,0.0)],[0.358422,0.812733,-0.459345,   0.0)],[0.0,0.0,0.0,1.0)]]))可选(simd_float4x4([[ - 0.790284,-0.0315036,-0.611929,0.0)],[ - 0.510883,0.585261,0.629656,0.0)],[0.338302 ,0.810233,-0.478617,0.0)],[0.0,0.0,0.0,1.0)]]))   可选(simd_float4x4([[ - 0.828626,-0.0174089,-0.559531,0.0)],   [-0.458209,0.595292,0.6660054,0.0)],[0.321594,0.803321,-0.501252,   0.0)],[0.0,0.0,0.0,1.0)]]))可选(simd_float4x4([[ - 0.862658,-0.00656158,-0.505744,0.0)],[ - 0.404869,0.608273,0.682703,0.0)],[0.303151 ,0.793701,-0.527388,0.0)],[0.0,0.0,0.0,1.0)]]))   可选(simd_float4x4([[ - 0.898201,0.0059859,-0.43954,0.0)],   [-0.337589,0.631021,0.69405,0.0)],[0.28154,0.775742,-0.564764,   0.0)],[0.0,0.0,0.0,1.0]]]))可选(simd_float4x4([[ - 0.920434,0.0197568,-0.390395,0.0)],[ - 0.284616,0.650707,0.703969,0.0)],[0.267941, 0.759072,-0.593311,0.0)],[0.0,0.0,0.0,1.0)]]))   可选(simd_float4x4([[ - 0.940432,0.041811,-0.337396,0.0)],   [-0.224465,0.666996,0.708559,0.0)],[0.255342,0.7242089,-0.619762,   0.0)],[0.0,0.0,0.0,1.0]]]))可选(simd_float4x4([[ - 0.960581,0.0803864,-0.266119,0.0)],[ - 0.1141218,0.683463,0.716193,0.0)],[0.239455, 0.725546,-0.64517,0.0)],[0.0,0.0,0.0,1.0)]]))   可选(simd_float4x4([[ - 0.969532,0.108638,-0.219551,0.0)],   [-0.0873999,0.683869,0.724349,0.0)],[0.228836,0.721471,   -0.653539,0.0)],[0.0,0.0,0.0,1.0)]]))

如您所见,即使我正在移动手机,第4列始终为[0.0,0.0,0.0,1.0]。 有人可以帮助我理解这个4x4矩阵(我认为这是相机旋转与全局坐标中的位置连接,即相机外部参数)。

2 个答案:

答案 0 :(得分:3)

可能需要一段时间(根据我的经验,在iPhone 7上为10-15秒),ARKit可以获得足够的相机/运动数据来提供位置跟踪;在那之前,它只能给你定位,并且位置将是 - 你正在观察 - (0,0,0)。要处理此问题,您可以实施session:cameraDidChangeTrackingState:方法并检查相机的trackingState - 当它从notAvailablelimited切换到normal时,您就可以开始使用位置数据。

答案 1 :(得分:0)

我认为[0,0,0,1]是第4行,它始终保持不变。它只是一个填充行,使矩阵乘法很好地工作。