Realitykit

作者 dpearson26998d90fd121a26無授權條款1.1K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫2 個月前更新

Build iOS augmented reality and 3D experiences with RealityKit and ARKit. Use when adding RealityView content, loading entities or USDZ models, anchoring objects to planes or world positions, distinguishing entity hit tests from ARKit real-world raycasts, handling AR camera availability, world tracking, scene updates, or RealityKit entity gestures and interactions.

AI 產生的概覽

指導使用 RealityKit 與 ARKit 打造 iOS 擴增實境與 3D 體驗,涵蓋 RealityView、實體、錨定、射線檢測與手勢。

功能
此技能提供參考指引,用於以 RealityKit 負責算繪、ARKit 負責世界追蹤來打造 iOS AR 與 3D 體驗。內容涵蓋 RealityView 設定、實體的載入與建立、錨定與放置、射線檢測的區別、手勢互動、場景理解、常見錯誤以及檢閱清單。它產出的是說明性指示與程式碼範例,而非檔案或指令碼。
適用情境
適用於加入 RealityView 內容、載入實體或 USDZ 模型、將物件錨定到平面或世界座標,以及處理 AR 相機可用性與世界追蹤的情況。也適合區分實體命中測試與 ARKit 真實世界射線檢測,以及實作 RealityKit 實體手勢。
執行需求
不隨附指令碼,僅為說明性指示。鎖定 Swift 6.3 與 iOS 26+,並假定熟悉 SwiftUI、RealityKit 與 ARKit。使用 AR 需要相機權限(NSCameraUsageDescription)與裝置支援檢查。

RealityKit

Build AR experiences on iOS using RealityKit for rendering and ARKit for world tracking. Covers RealityView, entity management, raycasting, scene understanding, and gesture-based interactions. Targets Swift 6.3 / iOS 26+.

Contents

Setup

Project Configuration

  1. Add NSCameraUsageDescription to Info.plist
  2. On iOS, RealityViewCameraContent displays an AR camera view by default (iOS 18+, macOS 15+); use .virtual camera mode for explicit non-AR fallback
  3. No entitlement is required for basic AR. If AR is core to the app, add the arkit required-device capability; otherwise gate AR UI with isSupported.

Device Requirements

Check the exact AR configuration's isSupported value before presenting AR UI.

swift
import ARKit
guard ARWorldTrackingConfiguration.isSupported else {    showUnsupportedDeviceMessage()    return}

Key Types

TypePlatformRole
RealityViewiOS 18+, visionOS 1+SwiftUI view that hosts RealityKit content
RealityViewCameraContentiOS 18+, macOS 15+Content displayed through an AR camera view on iOS, non-AR on macOS
EntityAllBase class for all scene objects
ModelEntityAllEntity with a visible 3D model
AnchorEntityAllTethers entities to a real-world anchor

RealityView Basics

RealityView is the SwiftUI entry point for RealityKit. RealityViewCameraContent is the iOS/macOS content type. On iOS, it uses an AR camera view by default and can use content.camera = .virtual for non-AR mode when requested or when AR/camera access is unavailable.

swift
import ARKitimport SwiftUIimport RealityKit
struct ARExperienceView: View {    var body: some View {        RealityView { (content: RealityViewCameraContent) in            if !ARWorldTrackingConfiguration.isSupported {                content.camera = .virtual            }
            let sphere = ModelEntity(                mesh: .generateSphere(radius: 0.05),                materials: [SimpleMaterial(                    color: .blue,                    isMetallic: true                )]            )            sphere.position = [0, 0, -0.5]  // 50cm in front of camera            content.add(sphere)        }    }}

Make and Update Pattern

Use the update closure to respond to SwiftUI state changes:

swift
struct PlacementView: View {    @State private var modelColor: UIColor = .red
    var body: some View {        RealityView { content in            let box = ModelEntity(                mesh: .generateBox(size: 0.1),                materials: [SimpleMaterial(                    color: .red,                    isMetallic: false                )]            )            box.name = "colorBox"            box.position = [0, 0, -0.5]            content.add(box)        } update: { content in            if let box = content.entities.first(                where: { $0.name == "colorBox" }            ) as? ModelEntity {                box.model?.materials = [SimpleMaterial(                    color: modelColor,                    isMetallic: false                )]            }        }
        Button("Change Color") {            modelColor = modelColor == .red ? .green : .red        }    }}

Loading and Creating Entities

Loading from USDZ Files

Load 3D models asynchronously to avoid blocking the main thread:

swift
RealityView { content in    if let robot = try? await ModelEntity(named: "robot") {        robot.position = [0, -0.2, -0.8]        robot.scale = [0.01, 0.01, 0.01]        content.add(robot)    }}

Adding Components

Entities use an ECS (Entity Component System) architecture. Add components to give entities behavior:

swift
let box = ModelEntity(    mesh: .generateBox(size: 0.1),    materials: [SimpleMaterial(color: .red, isMetallic: false)])
// Make it respond to physicsbox.components.set(PhysicsBodyComponent(    massProperties: .default,    material: .default,    mode: .dynamic))
// Add collision shape for interactionbox.components.set(CollisionComponent(    shapes: [.generateBox(size: [0.1, 0.1, 0.1])]))
// Enable input targeting for gesturesbox.components.set(InputTargetComponent())

Anchoring and Placement

AnchorEntity

Use AnchorEntity to anchor content to detected surfaces or world positions:

swift
RealityView { content in    // Anchor to a horizontal surface    let floorAnchor = AnchorEntity(.plane(        .horizontal,        classification: .floor,        minimumBounds: [0.2, 0.2]    ))
    let model = ModelEntity(        mesh: .generateBox(size: 0.1),        materials: [SimpleMaterial(color: .orange, isMetallic: false)]    )    floorAnchor.addChild(model)    content.add(floorAnchor)}

Anchor Targets

TargetDescription
.plane(.horizontal, ...)Horizontal surfaces (floors, tables)
.plane(.vertical, ...)Vertical surfaces (walls)
.plane(.any, ...)Any detected plane
.world(transform:)Fixed world-space position

Raycasting

Keep RealityKit scene queries separate from ARKit real-world raycasts:

  • RealityViewCameraContent.ray(through:in:to:) returns a camera ray in RealityKit coordinate spaces. It projects a screen point into the virtual scene; it is not proof of a detected physical surface.
  • RealityViewCameraContent.hitTest(point:in:query:mask:) hits virtual entities made hittable by CollisionComponent shapes. Use those shapes for entity picking and targeted gestures, not ARKit plane detection.
  • Use AnchorEntity(.plane(...)) for simple placement on detected planes.
  • Use ARKit ARRaycastQuery plus ARSession.raycast(_:) when the task needs a one-shot intersection with real-world surfaces, then anchor with AnchorEntity(raycastResult:).
swift
let results = session.raycast(query)if let result = results.first {    let anchor = AnchorEntity(raycastResult: result)    anchor.addChild(model)    content.add(anchor)}

Do not treat entity hit tests as substitutes for ARKit surface raycasts.

Gestures and Interaction

For gesture-based entity interaction, add CollisionComponent for the hittable shape and InputTargetComponent for input targeting.

Drag Gesture on Entities

swift
struct DraggableARView: View {    var body: some View {        RealityView { content in            let box = ModelEntity(                mesh: .generateBox(size: 0.1),                materials: [SimpleMaterial(color: .blue, isMetallic: true)]            )            box.position = [0, 0, -0.5]            box.components.set(CollisionComponent(                shapes: [.generateBox(size: [0.1, 0.1, 0.1])]            ))            box.components.set(InputTargetComponent())            box.name = "draggable"            content.add(box)        }        .gesture(            DragGesture()                .targetedToAnyEntity()                .onChanged { value in                    let entity = value.entity                    guard let parent = entity.parent else { return }                    entity.position = value.convert(                        value.location3D,                        from: .local,                        to: parent                    )                }        )    }}

Scene Understanding

Per-Frame Updates

Subscribe to SceneEvents.Update for continuous scene work rather than driving RealityKit with a SwiftUI timer. Keep the subscription alive and use event.deltaTime; see Entity Animations [blocked].

Platform Boundaries

On visionOS, ARKit provides a different API surface with ARKitSession, WorldTrackingProvider, and PlaneDetectionProvider. These visionOS-specific types are not available on iOS. On iOS, RealityKit handles world tracking automatically through RealityViewCameraContent.

For iOS architecture or migration notes, gate AR with ARWorldTrackingConfiguration.isSupported, host content with RealityViewCameraContent, and build scenes from Entity/ModelEntity placed with AnchorEntity.

Handoffs: CollisionComponent + InputTargetComponent handle RealityKit interaction; AccessibilityComponent handles entity accessibility metadata; detailed SwiftUI gestures and VoiceOver/Switch Control policy belong to siblings.

Treat existing SCNView/SCNNode work as either a separate SceneKit path or an explicit migration to RealityKit, not a mixed scene graph.

Common Mistakes

DON'T: Skip AR capability checks

Use the configuration-specific support check from Setup before presenting AR. Show non-AR content or an explicit unavailable state when it fails.

DON'T: Load heavy models synchronously

Loading large USDZ files on the main thread causes frame drops and hangs. The make closure of RealityView is async -- use it.

swift
// WRONG -- synchronous load blocks the main threadRealityView { content in    let model = try! Entity.load(named: "large-scene")    content.add(model)}
// CORRECT -- async loadRealityView { content in    if let model = try? await ModelEntity(named: "large-scene") {        content.add(model)    }}

DON'T: Forget collision and input target components for interactive entities

Interactive entities need both components shown in Gestures and Interaction; without them, taps and drags pass through.

DON'T: Create new entities in the update closure

The update closure runs on every SwiftUI state change. Creating entities there duplicates content on each render pass.

swift
// WRONG -- duplicates entities on every state changeRealityView { content in    // empty} update: { content in    let sphere = ModelEntity(mesh: .generateSphere(radius: 0.05))    content.add(sphere)  // Added again on every update}
// CORRECT -- create in make, modify in updateRealityView { content in    let sphere = ModelEntity(mesh: .generateSphere(radius: 0.05))    sphere.name = "mySphere"    content.add(sphere)} update: { content in    if let sphere = content.entities.first(        where: { $0.name == "mySphere" }    ) as? ModelEntity {        // Modify existing entity        sphere.position.y = newYPosition    }}

DON'T: Ignore camera permission

RealityKit on iOS needs camera access. If the user denies permission, the view shows a black screen with no explanation.

swift
// WRONG -- no permission handlingRealityView { content in    // Black screen if camera denied}
// CORRECT -- check and request permissionstruct ARContainerView: View {    @State private var cameraAuthorized = false
    var body: some View {        Group {            if cameraAuthorized {                RealityView { content in                    // AR content                }            } else {                ContentUnavailableView(                    "Camera Access Required",                    systemImage: "camera.fill",                    description: Text("Enable camera in Settings to use AR.")                )            }        }        .task {            let status = AVCaptureDevice.authorizationStatus(for: .video)            if status == .authorized {                cameraAuthorized = true            } else if status == .notDetermined {                cameraAuthorized = await AVCaptureDevice                    .requestAccess(for: .video)            }        }    }}

Review Checklist

  • NSCameraUsageDescription set in Info.plist
  • AR device capability checked before presenting AR views
  • Camera permission requested and denial handled with a fallback UI
  • arkit required-device capability added when AR is the app's core purpose
  • 3D models loaded asynchronously in the make closure
  • Entities created in make, modified in update (not created in update)
  • Interaction prerequisites and sibling handoffs follow the Gestures section
  • Entity hit tests, camera rays, and ARKit surface raycasts follow the Raycasting distinctions
  • SceneEvents.Update subscriptions used for per-frame logic (not SwiftUI timers)
  • Large scenes use ModelEntity(named:) async loading, not Entity.load(named:)
  • Anchor entities target appropriate surface types for the use case
  • Entity names set for lookup in the update closure

References

來源與署名

來源:dpearson2699/swift-ios-skills位於skills/realitykit提交8d90fd1

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架

更多來自 dpearson2699/swift-ios-skills 的技能

Widgetkit

dpearson2699

指導實作、審查與改進 iOS、iPadOS、watchOS 與 CarPlay 上的 WidgetKit 小工具與控制項。

Software Development1.1K2 個月前更新

Weatherkit

dpearson2699

指導 iOS 開發者使用 WeatherService 取得 WeatherKit 預報、警報與署名資訊。

Software Development1.1K2 個月前更新

Vision Framework

dpearson2699

Implement computer vision features including text recognition (OCR), face detection, barcode scanning, image segmentation, object tracking, and document scanning in iOS apps. Covers both the modern Swift-native Vision API (iOS 18+) and legacy VNRequest patterns, VisionKit DataScannerViewController for live camera scanning, and CoreMLRequest/VNCoreMLRequest for custom model inference. Use when adding OCR, barcode scanning, face detection, or custom Core ML model inference with Vision.

待分類1.1K2 個月前更新

Tipkit

dpearson2699

Implement and review Apple TipKit feature-discovery UI for iOS 17+ apps. Use when adding or auditing in-app tips, contextual help, coach marks, Tip, TipView, popoverTip, rules, events, actions, display frequency, testing overrides, reusable tip identifiers, or iOS 18+ TipGroup and CloudKit tip sync; avoid for generic SwiftUI navigation or layout outside tip presentation.

待分類1.1K2 個月前更新

Tabletopkit

dpearson2699

指導使用 TabletopKit 在 visionOS 上打造多人空間桌遊,涵蓋棋具、座位、動作與 RealityKit 算繪。

Software Development1.1K2 個月前更新

Swiftui Webkit

dpearson2699

指導在 iOS 26 及更新版本的 SwiftUI App 中使用 WebKit for SwiftUI 嵌入與控制網頁內容。

Software Development1.1K2 個月前更新