首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从didAdd锚点获取ARAnchor的旋转

从didAdd锚点获取ARAnchor的旋转
EN

Stack Overflow用户
提问于 2019-10-14 19:27:05
回答 1查看 622关注 0票数 0

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

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

EN

回答 1

Stack Overflow用户

发布于 2019-10-18 11:01:12

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

代码语言:javascript
复制
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
  )
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58375806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档