forked from heckj/CameraControlARView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RealityView.swift
58 lines (46 loc) · 1.59 KB
/
RealityView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// RealityView.swift
// CompleteAnatomy
//
// Created by Volodymyr Boichentsov on 11/10/2022.
// Copyright © 2022 3D4Medical, LLC. All rights reserved.
//
import SwiftUI
import Combine
import RealityKit
fileprivate let arContainer = ARViewContainer.init(cameraARView: CameraControlARView.init(frame: .zero))
public struct RealityKitView: View {
public struct Context {
public var base: RealityKit.Scene?
public func add(_ entity:Entity) {
let originAnchor = AnchorEntity(world: .zero)
originAnchor.addChild(entity)
arContainer.cameraARView.scene.anchors.append(originAnchor)
}
}
let context = Context.init(base: arContainer.cameraARView.scene)
var update: (() -> Void)?
var updateCancellable: Cancellable?
public init(_ content: @escaping (_ context:Context) -> Void, update: (() -> Void)? = nil) {
content(context)
self.update = update
if let update = self.update {
self.updateCancellable = arContainer.cameraARView.scene.subscribe(to: SceneEvents.Update.self) { event in
update()
}
}
}
public var body: some View {
arContainer
}
}
struct RealityView_Previews: PreviewProvider {
static var previews: some View {
RealityKitView( { context in
let entity = ModelEntity.init(mesh: .generateBox(size: SIMD3<Float>.init(repeating: 1)))
context.add(entity)
}, update: {
print("update")
}).frame(width: 300, height: 300)
}
}