send data or share variables between Immersive view and "attached" 2D view

I have an immersive space with a RealityKit view which is running an ARKitSession to access main camera frames.

This frame is processed with custom computer vision algorithms (and deep learning models).

There is a 3D Entity in the RealityKit view which I'm trying to place in the world, but I want to debug my (2D) algorithms in an "attached" view (display images in windows).

How to I send/share data or variables between the views (and and spaces)?

Answered by Vision Pro Engineer in 812484022

Hi @adizhol,

Are you trying to use attachments in a RealityView and share variables across the entity and attachment? If so, here's an example using an Observable class:


@Observable
class Model {
    var count: Int = 0
}

struct ImmersiveView: View {
    @State private var model: Model = Model()
    var body: some View {
        RealityView { content, attachments in
            
// Create the entity
            let cube = ModelEntity(mesh: .generateBox(size: 0.1), materials: [SimpleMaterial(color: .green, isMetallic: true)])
            cube.components.set(HoverEffectComponent())
            cube.components.set(InputTargetComponent())
            cube.generateCollisionShapes(recursive: true)
            
            content.add(cube)
            cube.setPosition([0, 0.75, -1], relativeTo: nil)
            
// add the attachment
            if let attachment = attachments.entity(for: "tapAttachment") {
                cube.addChild(attachment)
                attachment.setPosition([0, 0.2, 0], relativeTo: cube)
            }
         
        } attachments: {
            Attachment(id: "tapAttachment") {
                HStack {
                    Text("Taps:")
// Use the observable model's count as the text in the attachment. this will update each time you tap the entity
                    Text(model.count.description)
                }
                .padding()
                .glassBackgroundEffect()
            }
        }
        .gesture(SpatialTapGesture().targetedToAnyEntity()
            .onEnded({ value in
// update the tap count
                model.count += 1
            }))
    }
}

If this is not what you were looking to do, can you please explain further?

Accepted Answer

Hi @adizhol,

Are you trying to use attachments in a RealityView and share variables across the entity and attachment? If so, here's an example using an Observable class:


@Observable
class Model {
    var count: Int = 0
}

struct ImmersiveView: View {
    @State private var model: Model = Model()
    var body: some View {
        RealityView { content, attachments in
            
// Create the entity
            let cube = ModelEntity(mesh: .generateBox(size: 0.1), materials: [SimpleMaterial(color: .green, isMetallic: true)])
            cube.components.set(HoverEffectComponent())
            cube.components.set(InputTargetComponent())
            cube.generateCollisionShapes(recursive: true)
            
            content.add(cube)
            cube.setPosition([0, 0.75, -1], relativeTo: nil)
            
// add the attachment
            if let attachment = attachments.entity(for: "tapAttachment") {
                cube.addChild(attachment)
                attachment.setPosition([0, 0.2, 0], relativeTo: cube)
            }
         
        } attachments: {
            Attachment(id: "tapAttachment") {
                HStack {
                    Text("Taps:")
// Use the observable model's count as the text in the attachment. this will update each time you tap the entity
                    Text(model.count.description)
                }
                .padding()
                .glassBackgroundEffect()
            }
        }
        .gesture(SpatialTapGesture().targetedToAnyEntity()
            .onEnded({ value in
// update the tap count
                model.count += 1
            }))
    }
}

If this is not what you were looking to do, can you please explain further?

send data or share variables between Immersive view and "attached" 2D view
 
 
Q