Separate RealityView inside a RealityView attachment

Hi, I am having some troubles creating a "nested" RealityView content using MapKit attachment.

I am building a visionOS app that has horizontal MapKit map as an attachment to RealityView. I want to display 3D pins on that map, therefore I am using native map annotation and inside of these annotations, I create a new RealityView just for the 3D pin. This worked completely fine, unitil I wanted to have those RealityViews interact with each other.

By interaction of those RealityViews I mean that I wanted to group entities from the first "main" RealityViews content with the 3D pins using ModelSortGroupComponent.

Why I want this? I want to make the map circular, that is not a problem. Problem is that when I move the map with 3D pins, these pins have their own RealityView space and are only bounded by volumetric window dimensions. What happes is that these pins float next to the map (shown on attached image). So I came up with this solution: create a custom "toroid" like 3D entity model that occludes the pins that go outside the map region. In order to occlude only the pins, I need to use ModelSortGroupComponent to group the "toroid" entity with 3D pins entities (as described in another forum thread).

To summarize: need the content of the superior RealityView to interact with map attachment annotations RealityView content in order to group them. There might be of course another, better way to achieve my whole goal, so I would naturally appreciate any help or guidance.

Image below showing 3D pins on circular map. Since pins RealityView does no know anything about other RealityViews, it just overlows and hangs in space until is cropped by volumetric window boundary.

Simplified code:

var body: some View {
    let modelSortGroup = ModelSortGroup(depthPass: .prePass)

    RealityView { content, attachments in
        var mainEntity = Entity()
        // My other entities here...

        if let mapAttachment = attachments.entity(for: "mapAttachment") {
            // Edit map properties, position, horizontal layout etc.
            mainEntity.addChild(mapAttachment)
        }

        // Create and add to content mask "toroid" entity mapMaskEntity. Use OcclusionMaterial() material.
        mapMaskEntity.components.set(ModelSortGroupComponent(group: modelSortGroup, order: 0))

        // For all pins, somehow also set the group
        // 3DPinEntity.components.set(ModelSortGroupComponent(group: modelSortGroup, order: 1))

        content.add(mainEntity)
    } attachments: {
        Attachment(id: "mapAttachment") {
            Map {
                ForEach(mapViewModel.clusters, id: \.id) { cluster in
                    Annotation("", coordinate: cluster.coordinate) {
                        MapPin3DView(cluster: cluster)
                    }
                }
            }
            .clipShape(Circle())
        }
    }
}

// MapPin3DView is an map annotation view that includes a model of 3D pin and some details like image etc., uses RealityView.
struct MapPin3DView: View {
    var body: some View {
        RealityView { content in
            // 3D pin entities...
        }
    }
}

Hey @elukasino,

I don't believe that OcclusionMaterial will work for you in this scenario. To help visualize how this works you might first use a UnlitMaterial. I was able to create an occlusion material that worked on RealityViews inside of Attachments. However, I don't think that creating an entity who's shape works to hide the pins when they are outside of the cylinder is possible, as you would need this shape to morph as the user moves their head as to not occlude the front of the cylinder. You could occlude pins that are in the back half of the volume by using an occlusion shape that is a rectangle with a cylinder cut out of it.

I believe that you could accomplish what you are looking for by using Attachments just for the SwiftUI views. You could have entities all within the same RealityView, and just use the Attachments to add SwiftUI views to your pin Entity. Given they are in the same RealityView you could then use the position to determine if the entity is within the cylinder. Another option would be to use collision events to determine when the pins are inside or outside of the cylinder.

Let me know if this helps,
Michael

Separate RealityView inside a RealityView attachment
 
 
Q