import SwiftUI
import RealityKit
import ARKit
import AVFoundation
struct ContentView : View {
var body: some View {
ARViewContainer().edgesIgnoringSafeArea(.all)
}
}
struct ARViewContainer: UIViewRepresentable {
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
arView.session.delegate = context.coordinator
let worldConfig = ARWorldTrackingConfiguration()
worldConfig.planeDetection = .horizontal
// worldConfig.providesAudioData = true // open here -----> Error:
arView.session.run(worldConfig)
addTestEntity(arView: arView)
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {}
func makeCoordinator() -> Coordinator {
Coordinator()
}
class Coordinator: NSObject, ARSessionDelegate, ARSessionObserver {
func session(_ session: ARSession, didOutputAudioSampleBuffer audioSampleBuffer: CMSampleBuffer) {
}
}
}
func addTestEntity(arView: ARView) {
let mesh = MeshResource.generatePlane(width: 0.5, depth: 0.35)
guard let url = Bundle.main.url(forResource: "videoplayback", withExtension: "mp4") else { return }
let player = AVPlayer(url: url)
let videoMaterial = VideoMaterial(avPlayer: player)
let model = ModelEntity(mesh: mesh, materials: [videoMaterial])
model.transform.translation.y = 0.05
let anchor = AnchorEntity(.plane(.horizontal, classification: .any, minimumBounds: SIMD2<Float>(0.2, 0.2)))
anchor.children.append(model)
player.play()
arView.scene.anchors.append(anchor)
}
Error:
failed to update STS state: Error Domain=com.apple.STS-N Code=1396929899 "Error: failed to signal change" UserInfo={NSLocalizedDescription=Error: failed to signal change}
failed to update STS state: Error Domain=com.apple.STS-N Code=1396929899 "Error: failed to signal change" UserInfo={NSLocalizedDescription=Error: failed to signal change}
......
ARSession <0x125d88040>: did fail with error: Error Domain=com.apple.arkit.error Code=102 "Required sensor failed." UserInfo={NSLocalizedFailureReason=A sensor failed to deliver the required input., NSUnderlyingError=0x302922dc0 {Error Domain=AVFoundationErrorDomain Code=-11819 "Cannot Complete Action" UserInfo={NSLocalizedDescription=Cannot Complete Action, NSLocalizedRecoverySuggestion=Try again later.}}, NSLocalizedRecoverySuggestion=Make sure that the application has the required privacy settings., NSLocalizedDescription=Required sensor failed.}
iOS 17.5.1
Xcode 15.4
Post
Replies
Boosts
Views
Activity
After adding gestures to the EntityModel, when it is necessary to remove the EntityModel, if the method uiView.gestureRecognizers?.removeAll() is not executed, the instance in memory cannot be cleared. However, executing this method affects gestures for other EntityModels in the ARView. Does anyone have a better method to achieve this?
Example Code:
struct ContentView : View {
@State private var isRemoveEntityModel = false
var body: some View {
ZStack(alignment: .bottom) {
ARViewContainer(isRemoveEntityModel: $isRemoveEntityModel).edgesIgnoringSafeArea(.all)
Button {
isRemoveEntityModel = true
} label: {
Image(systemName: "trash")
.font(.system(size: 35))
.foregroundStyle(.orange)
}
}
}
}
ARViewContainer:
struct ARViewContainer: UIViewRepresentable {
@Binding var isRemoveEntityModel: Bool
let arView = ARView(frame: .zero)
func makeUIView(context: Context) -> ARView {
let model = CustomEntityModel()
model.transform.translation.y = 0.05
model.generateCollisionShapes(recursive: true)
__**arView.installGestures(.all, for: model)**__ // here--> After executing this line of code, it allows the deletion of a custom EntityModel in ARView.scene, but the deinit {} method of the custom EntityModel is not executed.
arView.installGestures(.all, for: model)
let anchor = AnchorEntity(.plane(.horizontal, classification: .any, minimumBounds: SIMD2<Float>(0.2, 0.2)))
anchor.children.append(model)
arView.scene.anchors.append(anchor)
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {
if isRemoveEntityModel {
let customEntityModel = uiView.scene.findEntity(named: "Box_EntityModel")
// --->After executing this line of code, ARView.scene can correctly delete the CustomEntityModel, and the deinit {} method of CustomEntityModel can also be executed properly. However, other CustomEntityModels in ARView.scene lose their Gestures as well.
__** uiView.gestureRecognizers?.removeAll()**__
customEntityModel?.removeFromParent()
}
}
}
CustomEntityModel:
class CustomEntityModel: Entity, HasModel, HasAnchoring, HasCollision {
required init() {
super.init()
let mesh = MeshResource.generateBox(size: 0.1)
let material = SimpleMaterial(color: .gray, isMetallic: true)
self.model = ModelComponent(mesh: mesh, materials: [material])
self.name = "Box_EntityModel"
}
deinit {
**print("CustomEntityModel_remove")**
}
}
Example Code:
struct ContentView : View {
@State private var isRemoveEntityModel = false
var body: some View {
ZStack(alignment: .bottom) {
ARViewContainer(isRemoveEntityModel: $isRemoveEntityModel).edgesIgnoringSafeArea(.all)
Button {
isRemoveEntityModel = true
} label: {
Image(systemName: "trash")
.font(.system(size: 35))
.foregroundStyle(.orange)
}
}
}
}
struct ARViewContainer: UIViewRepresentable {
@Binding var isRemoveEntityModel: Bool
let arView = ARView(frame: .zero)
func makeUIView(context: Context) -> ARView {
let model = CustomEntityModel()
model.transform.translation.y = 0.05
model.generateCollisionShapes(recursive: true)
arView.installGestures(.all, for: model) // --> After executing this line of code, it allows the deletion of a custom EntityModel in ARView.scene, but the deinit {} method of the custom EntityModel is not executed.
let anchor = AnchorEntity(.plane(.horizontal, classification: .any, minimumBounds: SIMD2(0.2, 0.2)))
anchor.children.append(model)
arView.scene.anchors.append(anchor)
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {
if isRemoveEntityModel {
let customEntityModel = uiView.scene.findEntity(named: "Box_EntityModel")
uiView.gestureRecognizers?.removeAll() // --->After executing this line of code, ARView.scene can correctly delete the CustomEntityModel, and the deinit {} method of CustomEntityModel can also be executed properly. However, other CustomEntityModels in ARView.scene lose their Gestures as well.
customEntityModel?.removeFromParent()
}
}
}
class CustomEntityModel: Entity, HasModel, HasAnchoring, HasCollision {
required init() {
super.init()
let mesh = MeshResource.generateBox(size: 0.1)
let material = SimpleMaterial(color: .gray, isMetallic: true)
self.model = ModelComponent(mesh: mesh, materials: [material])
self.name = "Box_EntityModel"
}
deinit {
print("CustomEntityModel_remove")
}
}
struct ARViewContainer: UIViewRepresentable {
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
arView.debugOptions = .showStatistics // Error:
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {}
}
-[MTLDebugRenderCommandEncoder validateCommonDrawErrors:]:5775: failed assertion `Draw Errors Validation
Vertex Function(vsSdfFont): the offset into the buffer viewConstants that is bound at buffer index 4 must be a multiple of 256 but was set to 61840.
'
This video example can not find all the code, some details have doubts, I would like to ask you to help me, thank you very much!
email: wu.shaopeng@aol.
Please forgive me, this mailbox also has (com)
iPhone 13 Pro Max 15.4.1
Xcode 13.3
After this update, when the RealityKit moves the phone after anchoring the entity, the entity drifts very seriously. Have you encountered the same situation?