何时在 ARKit 中使用 worldTransform() 以及何时使用 transform()
When to use worldTransform() and when to use transform() in ARKit
我一直在学习 ARKit 有两个基础知识可以根据对象的相对位置来转换对象。想知道什么时候使用 transform()
方法和 worldTransform()
方法,通过示例清楚地区分会有用。
let transform = result.worldTransform
let isOnPlane = result.anchor is ARPlaneAnchor
object.setTransform(transform, relativeTo: cameraTransform,
smoothMovement: !isOnPlane,
alignment: planeAlignment,
allowAnimation: allowAnimation)
1
对于 ARAnchor
和 ARCamera
使用 local
transform 实例 属性.
transform
is a matrix encoding the position, orientation, and scale of the anchor relative to the world coordinate space of the AR session the anchor is placed in.
例如,您可以轻松获得由 4x4 矩阵表示的 ARAnchor 或 ARCamera 的变换。
var transform: simd_float4x4 { get }
您应该以这种方式使用此变换属性(对于本地定位和定向的对象):
var translation = matrix_identity_float4x4
translation.columns.3.z = -0.25
let transform = currentFrame.camera.transform * translation
// OR
let transform = currentFrame.camera.transform
let anchor = ARAnchor(transform: transform)
sceneView.session.add(anchor: anchor)
2
对于 hitTestResults
和 ARAnchors
使用 global
worldTransform 实例 属性.
worldTransform
is the position and orientation of the hit test result relative to the world coordinate system.
var worldTransform: simd_float4x4 { get }
你可以这样使用它:
if !hitTestResult.isEmpty {
guard let hitResult = hitTestResult.first
else { return }
addPlane(hitTestResult: hitResult)
print(hitResult.worldTransform.columns.3)
}
// OR
let anchor = ARAnchor(name: "PANTHER", transform: hitTestResult.worldTransform)
sceneView.session.add(anchor: anchor)
我一直在学习 ARKit 有两个基础知识可以根据对象的相对位置来转换对象。想知道什么时候使用 transform()
方法和 worldTransform()
方法,通过示例清楚地区分会有用。
let transform = result.worldTransform
let isOnPlane = result.anchor is ARPlaneAnchor
object.setTransform(transform, relativeTo: cameraTransform,
smoothMovement: !isOnPlane,
alignment: planeAlignment,
allowAnimation: allowAnimation)
1
对于 ARAnchor
和 ARCamera
使用 local
transform 实例 属性.
transform
is a matrix encoding the position, orientation, and scale of the anchor relative to the world coordinate space of the AR session the anchor is placed in.
例如,您可以轻松获得由 4x4 矩阵表示的 ARAnchor 或 ARCamera 的变换。
var transform: simd_float4x4 { get }
您应该以这种方式使用此变换属性(对于本地定位和定向的对象):
var translation = matrix_identity_float4x4
translation.columns.3.z = -0.25
let transform = currentFrame.camera.transform * translation
// OR
let transform = currentFrame.camera.transform
let anchor = ARAnchor(transform: transform)
sceneView.session.add(anchor: anchor)
2
对于 hitTestResults
和 ARAnchors
使用 global
worldTransform 实例 属性.
worldTransform
is the position and orientation of the hit test result relative to the world coordinate system.
var worldTransform: simd_float4x4 { get }
你可以这样使用它:
if !hitTestResult.isEmpty {
guard let hitResult = hitTestResult.first
else { return }
addPlane(hitTestResult: hitResult)
print(hitResult.worldTransform.columns.3)
}
// OR
let anchor = ARAnchor(name: "PANTHER", transform: hitTestResult.worldTransform)
sceneView.session.add(anchor: anchor)