ios - ARKit中何时使用worldTransform()以及何时使用transform()

标签 ios swift matrix augmented-reality 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

对于 ARAnchorARCamera 使用本地 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

对于 hitTestResultsARAnchors 使用 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/

相关文章:

ios - "NSArray is not convertible to NSData"错误

ios - swift 3.1 : Cannot convert value of type '(_) -> ()' error/Problems with closures

swift - 如何使用调用回调的函数制作 swift 3.0 类?

r - 当矩阵的值非常小时,为什么矩阵乘积会变慢?

java - 将字符串转换为矩阵位置

ios - 为 iOS 应用程序加载基于 SVG 的图像资源

ios - UITableView: `tableView:cellForRowAtIndexPath:` 没有为所有行调用

swift - 无法让 NSCollectionView NSCollectionViewItem 工作

xcode - 子类化仅公开便利 init 的类

google-maps - Google 使用限制 - 说明