Skip to content

Commit

Permalink
More example scenes; better scene switching UI.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinw committed Oct 8, 2024
1 parent 7d47adc commit b7d4a3d
Show file tree
Hide file tree
Showing 51 changed files with 1,780 additions and 99 deletions.
12 changes: 8 additions & 4 deletions GodotVisionExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
objects = {

/* Begin PBXBuildFile section */
DE2A91D32CB55A7D00F3548C /* GodotVisionSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE2A91D22CB55A7900F3548C /* GodotVisionSettingsView.swift */; };
DE2E63972B718B10003F7878 /* GodotVision in Frameworks */ = {isa = PBXBuildFile; productRef = DE2E63962B718B10003F7878 /* GodotVision */; };
DE54EAB62B3C75D500594A05 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE54EAB52B3C75D500594A05 /* ContentView.swift */; };
DE54EAB62B3C75D500594A05 /* ExampleScenesBrowser.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE54EAB52B3C75D500594A05 /* ExampleScenesBrowser.swift */; };
DE54EABA2B3C75D600594A05 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DE54EAB92B3C75D600594A05 /* Assets.xcassets */; };
DE54EABD2B3C75D600594A05 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DE54EABC2B3C75D600594A05 /* Preview Assets.xcassets */; };
DEEA88FC2CB40AF200A8CD7D /* GodotVisionExampleApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEEA88FB2CB40AF200A8CD7D /* GodotVisionExampleApp.swift */; };
DEF363ED2B57F443005E175D /* Godot_Project in Resources */ = {isa = PBXBuildFile; fileRef = DEF363EC2B57F443005E175D /* Godot_Project */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
DE2A91D22CB55A7900F3548C /* GodotVisionSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GodotVisionSettingsView.swift; sourceTree = "<group>"; };
DE54EAAC2B3C75D500594A05 /* GodotVisionExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = GodotVisionExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
DE54EAB52B3C75D500594A05 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
DE54EAB52B3C75D500594A05 /* ExampleScenesBrowser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleScenesBrowser.swift; sourceTree = "<group>"; };
DE54EAB92B3C75D600594A05 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
DE54EABC2B3C75D600594A05 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
DE54EABE2B3C75D600594A05 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -57,8 +59,9 @@
DE54EAAE2B3C75D500594A05 /* GodotVisionExample */ = {
isa = PBXGroup;
children = (
DE2A91D22CB55A7900F3548C /* GodotVisionSettingsView.swift */,
DEEA88FB2CB40AF200A8CD7D /* GodotVisionExampleApp.swift */,
DE54EAB52B3C75D500594A05 /* ContentView.swift */,
DE54EAB52B3C75D500594A05 /* ExampleScenesBrowser.swift */,
DE54EAB92B3C75D600594A05 /* Assets.xcassets */,
DE54EABE2B3C75D600594A05 /* Info.plist */,
DE54EABB2B3C75D600594A05 /* Preview Content */,
Expand Down Expand Up @@ -179,7 +182,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
DE54EAB62B3C75D500594A05 /* ContentView.swift in Sources */,
DE54EAB62B3C75D500594A05 /* ExampleScenesBrowser.swift in Sources */,
DE2A91D32CB55A7D00F3548C /* GodotVisionSettingsView.swift in Sources */,
DEEA88FC2CB40AF200A8CD7D /* GodotVisionExampleApp.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
76 changes: 0 additions & 76 deletions GodotVisionExample/ContentView.swift

This file was deleted.

86 changes: 86 additions & 0 deletions GodotVisionExample/ExampleScenesBrowser.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Created by Kevin Watters on 12/27/23.

import SwiftUI
import RealityKit
import GodotVision

/// The path to the folder containing the "project.godot" you wish Godot to load.
fileprivate let pathToGodotProject = "Godot_Project"

fileprivate let exampleScenes: [(label: String, systemImageName: String, resourcePath: String)] = [
("Hello", "hand.wave", "res://examples/hello/example_godot_vision_scene.tscn"),
("Physics", "shippingbox.and.arrow.backward", "res://examples/physics_toy/physics_toy.tscn"),
("Skeletons", "figure.cooldown", "res://examples/rigged_models/example_rigged_models.tscn"),
("Joints", "lamp.desk.fill", "res://scenes/joints/joints.tscn"),
//("Soft Bodies", resourcePath: "res://scenes/soft_bodies/soft_bodies.tscn"),
("CSG", "circle.circle", "res://examples/csg/csg.tscn"),
]

fileprivate let UI_PANEL_ATTACHMENT_NAME = "ui_panel"


struct ExampleScenesBrowser: View {
@StateObject private var godotVision = GodotVisionCoordinator()
@State private var selectedScene: String = exampleScenes[0].resourcePath
@State private var showSettings: Bool = false

var body: some View {
GeometryReader3D { (geometry: GeometryProxy3D) in
RealityView { content, attachments in
// Initialize Godot
let rkEntityGodotRoot = godotVision.setupRealityKitScene(content, volumeSize: VOLUME_SIZE, projectFileDir: pathToGodotProject)
print("Godot scene root: \(rkEntityGodotRoot)")

if let uiPanel = attachments.entity(for: UI_PANEL_ATTACHMENT_NAME) {
content.add(uiPanel)
uiPanel.position = uiPanelPosition(volumeSize: VOLUME_SIZE)
}
} update: { content, attachments in
// update called when SwiftUI @State in this ContentView changes. See docs for RealityView.
// user can change the volume size from the default by selecting a different zoom level.
// we watch for changes via the GeometryReader and scale the godot root accordingly
let frame = content.convert(geometry.frame(in: .local), from: .local, to: .scene)
let volumeSize = simd_double3(frame.max - frame.min)
godotVision.changeScaleIfVolumeSizeChanged(volumeSize)
if let uiPanel = attachments.entity(for: UI_PANEL_ATTACHMENT_NAME) {
// swiftui attachment also needs to be repositioned
uiPanel.position = uiPanelPosition(volumeSize: volumeSize)
}
} attachments: {
Attachment(id: "ui_panel") {
HStack {
Button { godotVision.reloadScene() } label: {
Label("Reload", systemImage: "arrow.clockwise.circle")
}
Button { withAnimation { showSettings.toggle() } } label: {
Label("Settings", systemImage: "gear")
}
if showSettings {
GodotVisionSettingsView(godotVision: godotVision)
}
}.padding().glassBackgroundEffect()
}
}
}
.modifier(GodotVisionRealityViewModifier(coordinator: godotVision))
.onChange(of: selectedScene) { _, sceneResourcePath in
godotVision.changeSceneToFile(atResourcePath: sceneResourcePath)
}
.ornament(visibility: .automatic, attachmentAnchor: .scene(.leading), contentAlignment: .center) {
// Show a tab bar on the left allowing the user to select example scenes.
TabView(selection: $selectedScene) {
ForEach(exampleScenes, id: \.resourcePath) { (label, systemImage, resourcePath) in
Tab(label, systemImage: systemImage, value: resourcePath) { /* empty tab content; we load the scene in .onChange(of: selectedScene) */ }
}
}
}
}

private func uiPanelPosition(volumeSize: simd_double3) -> simd_float3 {
.init(0, Float(volumeSize.y / -2 + 0.1), Float(volumeSize.z / 2 - 0.01))
}
}

#Preview(windowStyle: .volumetric) {
ExampleScenesBrowser()
}
13 changes: 7 additions & 6 deletions GodotVisionExample/GodotVisionExampleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import SwiftUI

// visionOS will silently clamp to volume size if set above or below limits
// max for all dimensions is 1.98
// min for all dimensions is 0.24
let VOLUME_SIZE = simd_double3(1.8, 1.0, 1.5)

@main
struct GodotVisionExampleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
ExampleScenesBrowser()
}
.windowStyle(.volumetric)
.defaultSize(width: VOLUME_SIZE.x, height: VOLUME_SIZE.y, depth: VOLUME_SIZE.z, in: .meters)
}
}

// Note: visionOS will silently clamp to volume size if set above or below limits
// max for all dimensions is 1.98
// min for all dimensions is 0.24
let VOLUME_SIZE = simd_double3(1.8, 1.0, 1.5)

17 changes: 17 additions & 0 deletions GodotVisionExample/GodotVisionSettingsView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Created by Kevin Watters on 10/8/24.

import SwiftUI
import GodotVision

struct GodotVisionSettingsView: View {
@ObservedObject var godotVision: GodotVisionCoordinator

var body: some View {
Text("Physics FPS")
Picker("Physics FPS", selection: $godotVision.physicsTicksPerSecond) {
Text("30").tag(30)
Text("60").tag(60)
Text("90").tag(90)
}.frame(width: 150).pickerStyle(.palette)
}
}
14 changes: 14 additions & 0 deletions Godot_Project/examples/common/environments/default.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[gd_resource type="Environment" load_steps=3 format=3 uid="uid://cgl5t5rlv6o6v"]

[sub_resource type="PhysicalSkyMaterial" id="PhysicalSkyMaterial_yivqe"]
rayleigh_color = Color(0.301961, 0.403922, 0.6, 1)
mie_color = Color(0.690196, 0.729412, 0.811765, 1)
ground_color = Color(0.690196, 0.729412, 0.811765, 1)

[sub_resource type="Sky" id="Sky_tuy8d"]
sky_material = SubResource("PhysicalSkyMaterial_yivqe")

[resource]
background_mode = 2
sky = SubResource("Sky_tuy8d")
tonemap_mode = 2
8 changes: 8 additions & 0 deletions Godot_Project/examples/common/materials/brown.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://3dvxsth5fn3n"]

[resource]
cull_mode = 2
shading_mode = 2
specular_mode = 2
albedo_color = Color(0.294118, 0.270588, 0.227451, 1)
metallic_specular = 0.0
8 changes: 8 additions & 0 deletions Godot_Project/examples/common/materials/green.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://ctm5wb05otmn4"]

[resource]
cull_mode = 2
shading_mode = 2
specular_mode = 2
albedo_color = Color(0.435294, 0.643137, 0, 1)
metallic_specular = 0.0
8 changes: 8 additions & 0 deletions Godot_Project/examples/common/materials/grey.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://bd8s48i3httg2"]

[resource]
cull_mode = 2
shading_mode = 2
specular_mode = 2
albedo_color = Color(0.2, 0.2, 0.2, 1)
metallic_specular = 0.0
Loading

0 comments on commit b7d4a3d

Please sign in to comment.