我一直在学习 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
使用本地
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)
关于ios - ARKit中何时使用worldTransform()以及何时使用transform(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54141166/