Post

Replies

Boosts

Views

Activity

Reply to WorldTrackingProvider Not running. Arkitsession terminated
Thank you for the reply! All I am trying to do is to be able to create a simple example of using the world Anchor to lock a cube in place and be able to find it exactly where I left it off in the world after leaving/closing the application. My previous example I had troubles running the Arkit session so I tried to get a response by calling it inside a drag gesture just to see what the logs were telling me. What I am trying to do here: 1) Find the cube from a loaded scene from Reality Composer pro 2) Drag in space using a drag gesture and snap it onto surfaces using PlaneDetection. 3) The moment I finish dragging the cube I want its position in the world to be saved and be able reload its position again in the world whenever the user enters the application again. To do this I am saving the anchor id and its transform matrix in two separate text files. The functions get triggered automatically every time at .onEnded of the drag gesture. In the .onAppear block is where I am attempting to reload the anchor id and transform and re-apply its position to the cube. I can get the transform and ID save and reload ok, the log shows the functions are working but how can I make the cube appear where I left it exactly like last time? do I need to query the device position somehow and let the cube be aware that the device location has changed and there for the cube position needs to be offset? sorry is my first time working with an Vision Pro project so I'm not familiar what the anchors can do out of the box. var body: some View { RealityView { content in //Load "Scene" from Reality Composer Pro if let Scene = try? await Entity.load(named: "WorldTrackingScene", in: realityKitContentBundle){ //Add scene to the view content.add(Scene) //Look for the cube entity if let cubeEntity = Scene.findEntity(named: "Cube"){ myCube = cubeEntity // Create collission for the cube myCube.generateCollisionShapes(recursive: true) // Allow inputs to interact myCube.components.set(InputTargetComponent(allowedInputTypes: .indirect)) // set some ground shadows myCube.components.set(GroundingShadowComponent(castsShadow: true)) } } // Add your RealityKit scene setup here Task { // Start the ARKit session try await session.run([planeData,worldData]) print("ARKit session started") //Add anchor back if let restoredAnchor = restoredAnchor{ try? await worldData.addAnchor(restoredAnchor) } } // Task for handling plane anchor updates Task { for await planeUpdate in planeData.anchorUpdates { switch planeUpdate.event { case .added, .updated: //print("Plane anchor location: \(extractTranslation(from: planeUpdate.anchor.originFromAnchorTransform))") currentPlaneAnchor = planeUpdate.anchor case .removed: print("Plane anchor removed") } } } //Check if anchor exists Task{ for await myAnchor in worldData.anchorUpdates{ if myAnchor.anchor.id == testUUID{ print("Anchor ID found : \(myAnchor.anchor.id)") loadedTransform = extractTranslation(from: myAnchor.anchor.originFromAnchorTransform) } let currentTime = CACurrentMediaTime() let deviceLocation = worldData.queryDeviceAnchor(atTimestamp: currentTime) //print("device location: \(deviceLocation)") } myCube.position = loadedTransform } } .onChange(of: myScenePhase){newvalue in switch myScenePhase { case .active: print("App active") case .inactive, .background: print("App closing") } } .onAppear{ // Load UUID if let loadedUUID = loadUUIDFromFile(filename: "savedUUID.txt"){ testUUID = loadedUUID print("Anchor ID ON APPEAR : \(testUUID)") //Load Trans if let savedTrans = loadTransformFromFile(filename: "savedTransform.txt"){ print("Anchor TRANSFORM ON APPEAR : \(savedTrans)") //let retrievedAnchor = WorldAnchor(originFromAnchorTransform: savedTrans) //restoredAnchor = retrievedAnchor } } } .gesture(DragGesture().targetedToAnyEntity() //When entity changes in position .onChanged{ value in value.entity.position = value.convert(value.location3D, from: .local, to: value.entity.parent!) myCube = value.entity //Place cube snapped to the surface if let planeAnchor = currentPlaneAnchor{ // Assign the anchors position Y to the cube's position Y myCube.position.y = planeAnchor.originFromAnchorTransform.columns.3.y } } .onEnded{value in // Task for handling world anchor updates Task{ let worldAnchor = WorldAnchor(originFromAnchorTransform: currentPlaneAnchor!.originFromAnchorTransform) // Add the world anchor to the worldData provider try? await worldData.addAnchor(worldAnchor) //Store the UUID in a file testUUID = worldAnchor.id print("World anchor added: \(worldAnchor.id)----\(testUUID)") print("word anchor is tracked: \(worldAnchor.isTracked)") saveUUIDToFile(testUUID, filename: "savedUUID.txt") //Store the transform saveTransformToFile(worldAnchor.originFromAnchorTransform, filename: "savedTransform.txt") } } ) } }
Oct ’24