0
votes

So I am using session(_ session: ARSession, didAdd anchors: [ARAnchor] to get all detected planes (I cannot use renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) Because I am using RealityKit instead of ARKit).

So my question is, How can I get the rotation of the detected anchor from this method? I want to create a plane Entity that has exactly the same position, size, orientation & rotation of the detected anchor. So far Im using the extent for the size, center for position but I can't figure out how to get the rotation. Because right now Its detected correctly (Right position and size)but with wrong rotation that the real plane.

1
@ARGeo Do you know an answer for this ?Mostafa Mohamed Raafat

1 Answers

1
votes

You can get the position and orientation of the ARPlaneAnchor from its transform property, then use that to render the bounding plane in 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
  )
}