Created
December 25, 2024 11:59
-
-
Save banjun/049890908d9a1fba9621953861309783 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import RealityKit | |
@main | |
struct KibouApp: App { | |
@Environment(\.scenePhase) private var scenePhase | |
@State private var transform: Transform = .identity | |
@GestureState private var dragState: SIMD3<Float>? | |
var body: some SwiftUI.Scene { | |
ImmersiveSpace(id: "i") { | |
RealityView { content in | |
let kibou = try! await Entity(named: "Assets_KIBOU_gltf") | |
content.add(kibou) | |
let sphere = ModelEntity(mesh: .generateSphere(radius: 10)) | |
sphere.components.set(InputTargetComponent(allowedInputTypes: .indirect)) | |
sphere.components.set(CollisionComponent(shapes: [.generateSphere(radius: 10)])) | |
content.add(sphere) | |
let light = PointLight() | |
light.position.y = 1.5 | |
content.add(light) | |
} update: { content in | |
let root = content.entities.first! | |
root.transform = transform //.init(matrix: transform.matrix.inverse) | |
} | |
.gesture(DragGesture(coordinateSpace: .immersiveSpace).targetedToAnyEntity().updating($dragState) { value, state, transaction in | |
state = state ?? transform.translation | |
var translation = value.translation3D / 1000 | |
translation.y = 0 | |
transform = .init(translation: state! + .init(translation)) | |
}) | |
} | |
.immersionStyle(selection: .constant(.progressive(0.3..., initialAmount: 1)), in: .progressive) | |
.onChange(of: scenePhase) { _, new in | |
if new == .background { exit(0) } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment