RealityKit ARImageAnchor with VideoMaterial problems
When I move the camera closer, sometimes the image from the ARResources overlaps the video with itself. What could be the problem?
Links:
https://www.dropbox.com/s/b8yaczq4xjk9v1p/IMG_9429.PNG?dl=0
https://www.dropbox.com/s/59dj4ldf6l3yj4u/RPReplay_Final1637392988.mov?dl=0
VideoEntity class
final class VideoEntity {
var videoPlayer = AVPlayer()
func videoModelEntity(width: Float?, height: Float?) -> ModelEntity {
let plane = MeshResource.generatePlane(width: width ?? Float(), height: height ?? Float())
let videoItem = createVideoItem(with: "Cooperation")
let videoMaterial = createVideoMaterial(with: videoItem)
return ModelEntity(mesh: plane, materials: [videoMaterial])
}
func placeVideoScreen(videoEntity: ModelEntity, imageAnchor: ARImageAnchor, arView: ARView) {
let anchorEntity = AnchorEntity(anchor: imageAnchor)
let rotationAngle = simd_quatf(angle: GLKMathDegreesToRadians(-90), axis: SIMD3<Float>(x: 1, y: 0, z: 0))
videoEntity.setOrientation(rotationAngle, relativeTo: anchorEntity)
videoEntity.setPosition(SIMD3<Float>(x: 0, y: 0.015, z: 0), relativeTo: anchorEntity)
anchorEntity.addChild(videoEntity)
arView.scene.addAnchor(anchorEntity)
}
private func createVideoItem(with filename: String) -> AVPlayerItem {
guard let url = Bundle.main.url(forResource: filename, withExtension: "mov") else {
fatalError("Fatal Error: - No file source.")
}
return AVPlayerItem(url: url)
}
private func createVideoMaterial(with videoItem: AVPlayerItem) -> VideoMaterial {
let videoMaterial = VideoMaterial(avPlayer: videoPlayer)
videoPlayer.replaceCurrentItem(with: videoItem)
videoPlayer.actionAtItemEnd = .none
videoPlayer.play()
NotificationCenter.default.addObserver(self, selector: #selector(loopVideo), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: videoPlayer.currentItem)
return videoMaterial
}
@objc
private func loopVideo(notification: Notification) {
guard let playerItem = notification.object as? AVPlayerItem else { return }
playerItem.seek(to: CMTime.zero, completionHandler: nil)
videoPlayer.play()
}
}
ViewModel class
func startImageTracking(arView: ARView) {
guard let arReferenceImage = ARReferenceImage.referenceImages(inGroupNamed: "ARResources", bundle: Bundle.main) else { return }
let configuration = ARImageTrackingConfiguration().do {
$0.trackingImages = arReferenceImage
$0.maximumNumberOfTrackedImages = 1
}
let personSegmentation: ARWorldTrackingConfiguration.FrameSemantics = .personSegmentationWithDepth
if ARWorldTrackingConfiguration.supportsFrameSemantics(personSegmentation) {
configuration.frameSemantics.insert(personSegmentation)
}
arView.session.run(configuration, options: [.resetTracking])
}
ARSessionDelegate protocol
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
for anchor in anchors {
if let imageAnchor = anchor as? ARImageAnchor {
let videoEntity = viewModel.videoEntity.videoModelEntity(width: Float(imageAnchor.referenceImage.physicalSize.width), height: Float(imageAnchor.referenceImage.physicalSize.height))
viewModel.videoEntity.placeVideoScreen(videoEntity: videoEntity, imageAnchor: imageAnchor, arView: arView)
}
}
}