且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

arView.session.currentFrame.camera.transform 和 arview.cameraTransform 有什么区别?

更新时间:2023-11-18 11:42:52

您的第一个示例是变换矩阵,它定义了相机在世界坐标中的旋转/平移.

打开 var 变换:simd_float4x4 { get }

您的第二个示例是一种特殊类型的变换矩阵.

public var cameraTransform: Transform { get }//或者@MainActor var cameraTransform: Transform { get }

苹果的定义:

请注意,Transform 是一种特殊类型,它不支持可以由通用 4x4 矩阵表示的所有转换.从 4x4 矩阵设置 Transform 是一种有损操作.它可能会导致某些转换(例如剪切)被丢弃!

但是,在某些情况下,cameraTransform 是比 transform 更方便的属性.

I stumbled upon this while applying rotation from the camera onto an entity in RealityKit. I thought I have to do some matrix math to gain the euler angles from arView.session.currentFrame.camera.transform matrix, but retrieving the rotation from arview.cameraTransform.rotation did the trick (found here).

So: What is the difference of both matrices and when should be used which?

Your first sample is the transform matrix that defines the camera’s rotation/translation in world coordinates.

open var transform: simd_float4x4 { get }

Your second sample is a specialized type of the transform matrix.

public var cameraTransform: Transform { get }

// or

@MainActor var cameraTransform: Transform { get }

Apple's definition:

Note that Transform is a specialized type that does not support all transformations that can be represented by a general 4x4 matrix. Setting the Transform from a 4x4 matrix is a lossy operation. It may result in certain transformations (such as shearing) being dropped!

However, in some situations cameraTransform is a more convenient property than transform.