从 didAdd 锚点获取 ARAnchor 的旋转

Posted

技术标签:

【中文标题】从 didAdd 锚点获取 ARAnchor 的旋转【英文标题】:Getting rotation of ARAnchor from didAdd anchors 【发布时间】:2020-02-11 00:30:18 【问题描述】:

所以我使用session(_ session: ARSession, didAdd anchors: [ARAnchor] 来获取所有检测到的平面(我不能使用renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor),因为我使用的是RealityKit 而不是ARKit)。

所以我的问题是,如何通过这种方法获得检测到的锚点的旋转?我想创建一个平面实体,它与检测到的锚点具有完全相同的位置、大小、方向和旋转。到目前为止,我使用大小的范围,位置的中心,但我不知道如何获得旋转。因为现在它检测到正确(正确的位置和大小)但与真实平面的旋转错误。

【问题讨论】:

@ARGeo 你知道答案吗? 【参考方案1】:

您可以从 ARPlaneAnchor 的 transform 属性中获取位置和方向,然后使用它在 RealityKit 中渲染边界平面。

extension float4x4 

  /// Returns the translation components of the matrix
  func toTranslation() -> SIMD3<Float> 
    return [self[3,0], self[3,1], self[3,2]]
  

  /// Returns a quaternion representing the 
  /// rotation component of the matrix
  func toQuaternion() -> simd_quatf 
    return simd_quatf(self)
  


func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) 

  guard let planeAnchor = anchors[0] as? ARPlaneAnchor else 
    return
  

  // NOTE: When the ARPlaneAnchor is first created its transform
  // contains the position and the plane.center == [0,0,0]. On updates
  // the plane.center will change as the extents of the plane change.
  let position = planeAnchor.transform.toTranslation()
  let orientation = planeAnchor.transform.toQuaternion()

  // The center is a position before the orientation is taken in to
  // account, so we need to rotate it  to get the true position before
  // we add it to the anchors position
  let rotatedCenter = orientation.act(planeAnchor.center)

  // You have a ModelEntity that you created earlier
  // e.g. modelEntity

  // Assuming you added the entity to an anchor that is just 
  // fixed at 0,0,0 in the world, or you created a custom entity
  // with HasAnchor set to 0,0,0
  modelEntity.transform.translation = position + rotatedCenter
  modelEntity.transform.rotation = orientation

  // Doesn't seem to be a way to update meshes in RealityKit so
  // just create a new plane mesh for the updated dimensions
  modelEntity.model?.mesh = MeshResource.generatePlane(
    width: planeAnchor.extent.x, 
    depth: planeAnchor.extent.z
  )

【讨论】:

以上是关于从 didAdd 锚点获取 ARAnchor 的旋转的主要内容,如果未能解决你的问题,请参考以下文章

renderer(_:didAdd:for:) 方法中的全局和局部参数名称

HtmlUnit - 无法从 div 获取锚点

使用 ARAnchor 插入节点和直接插入节点有啥区别?

如何使用 jQuery 从 URL 中获取锚点?

ARCore - 如何将特定对象位置添加到ARAnchor?

如何在“func renderer(SCNSceneRenderer, nodeFor: ARAnchor) -> SCNNode?”中旋转 SCNNode?