Convert matrix_float4x4 to x y z space

微笑、不失礼 提交于 2019-11-30 00:35:09

I'm using matrix_float4x4 extension for that:

extension matrix_float4x4 {
    func position() -> SCNVector3 {
        return SCNVector3(columns.3.x, columns.3.y, columns.3.z)
    }
}

Using:

let position = transform.position()

Or you can convert position directly in code:

let position = SCNVector3(
    transform.columns.3.x,
    transform.columns.3.y,
    transform.columns.3.z
)

You should learn about 4x4 matrices as they are really the basis for all 3D visualization and mathematics.

  • To get the position from a 4x4 matrix use the entries m41, m42, m43.
  • To get the rotation use the 3x3 matrix in m11 ... m33.

Two ways you can get the position of the camera:

  1. By using the currentFrame property of session you can get the transform of the camera and then use that to get the position:

    matrix_float4x4 cameraTransform = self.sceneView.session.currentFrame.camera.transform;
    SCNVector3 cameraPosition = SCNVector3Make(cameraTransform.columns[3].x, 
    cameraTransform.columns[3].y, cameraTransform.columns[3].z);
    
  2. Or get the position of the camera using the pointOfView property of the sceneView:

    SCNVector3 cameraPosition = self.sceneView.pointOfView.worldPosition;
    
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!