← Back to list

Deep dive into visionOS 30Days -Day 9

Original source code - satoshi0212

Coaspe · 2023-09-05 12:11 · 0 claps · 1.3 min read
#visionos #provision #swiftui
Open on Medium ↗
Wiki topics: 3D · Motion & 3D Design 📱 · Mobile Development

Deep dive into visionOS 30Days -Day 9

Original source code - satoshi0212

In this project, we create a portal with Earth ModelEntity. It has only two Swift files: Day9App.swift, ContentView.swift.

Day9App

import SwiftUI

@main
struct Day9App: App {

    var body: some Scene {
        WindowGroup {
            PortalView()
        }
    }
}

It has WindowGroup Scene with PortalView() that ContentView.swift contains.

ContentView

import SwiftUI
import RealityKit
import RealityKitContent

struct PortalView : View {
    var body: some View {
        RealityView { content in
            let world = makeWorld()
            let portal = makePortal(world: world)
            content.add(world)
            content.add(portal)
        }
    }

    func makeWorld() -> Entity {
        let world = Entity()
        world.components[WorldComponent.self] = .init()

        let environment = try! EnvironmentResource.load(named: "Sunlight")
        world.components[ImageBasedLightComponent.self] = .init(source: .single(environment), intensityExponent: 12)
        world.components[ImageBasedLightReceiverComponent.self] = .init(imageBasedLight: world)

        let earth = try! ModelEntity.load(named: "Earth", in: realityKitContentBundle)
        earth.position = SIMD3<Float>(x: -0.1, y: 0, z: -0.4)
        world.addChild(earth)

        return world
    }

    func makePortal(world: Entity) -> Entity {
        let portal = Entity()

        portal.components[ModelComponent.self] = .init(mesh: .generatePlane(width: 0.5,
                                                                            height: 0.5,
                                                                            cornerRadius: 0.5),
                                                       materials: [PortalMaterial()])
        portal.components[PortalComponent.self] = .init(target: world)

        return portal
    }
}

ContentView.swift has PortalView that contains RealityView as a body . In make closure of RealityView , the portal and world are added to View .

While making a world entity, we set several components with Entity.components which is a Entity.ComponentSet . And EnvironmentResource gives more realistic light to Entity .

A Portal Entity is made with predefined PortalMaterial() material.

WordComponent

When set on an entity, a WorldComponent separates the entity and its subtree to be rendered as part of a different world, that is only visible through a portal.

struct WorldComponent

EnvironmentResource

Defines a environment resource for use in the ARView.Environment.

@MainActor @preconcurrency
class EnvironmentResource

ImageBasedLightComponent

struct ImageBasedLightComponent

ImageBasedLightReceiverComponent

struct ImageBasedLightReceiverComponent

PortalComponent

struct PortalComponent

메타데이터
post_id
7c18a5bb3754
slug
deep-dive-into-visionos-30days-day-9-7c18a5bb3754
url
https://medium.com/@aspalt85/deep-dive-into-visionos-30days-day-9-7c18a5bb3754
canonical_url
https://medium.com/@aspalt85/deep-dive-into-visionos-30days-day-9-7c18a5bb3754
author_url
https://medium.com/@aspalt85
status
ok
fetched_at
2026-06-17 08:20:12