-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
visionOS sample code: add view model, get Mesh construction off the main thread #116
Draft
halmueller
wants to merge
7
commits into
nicklockwood:main
Choose a base branch
from
halmueller:vpObservable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d3afbeb
Change Example's Embed filter from just iOS/Mac Catalyst to "allow al…
halmueller 1399eec
Add SwiftUI visionOS example. (#115)
halmueller 8a798e5
break out into an async approach
halmueller cf4b18e
tinkering
halmueller 09a0d97
Clean up the observation architecture and content construction flags.
halmueller e1d72c2
Cleaner logging, remove global function.
halmueller ae3e6a8
lint
halmueller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
...xcassets/AppIcon.solidimagestack/Back.solidimagestacklayer/Content.imageset/Contents.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "vision", | ||
"scale" : "2x" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...eVisionOS/Assets.xcassets/AppIcon.solidimagestack/Back.solidimagestacklayer/Contents.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
ExampleVisionOS/Assets.xcassets/AppIcon.solidimagestack/Contents.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
}, | ||
"layers" : [ | ||
{ | ||
"filename" : "Front.solidimagestacklayer" | ||
}, | ||
{ | ||
"filename" : "Middle.solidimagestacklayer" | ||
}, | ||
{ | ||
"filename" : "Back.solidimagestacklayer" | ||
} | ||
] | ||
} |
12 changes: 12 additions & 0 deletions
12
...cassets/AppIcon.solidimagestack/Front.solidimagestacklayer/Content.imageset/Contents.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "vision", | ||
"scale" : "2x" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...VisionOS/Assets.xcassets/AppIcon.solidimagestack/Front.solidimagestacklayer/Contents.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...assets/AppIcon.solidimagestack/Middle.solidimagestacklayer/Content.imageset/Contents.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "vision", | ||
"scale" : "2x" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...isionOS/Assets.xcassets/AppIcon.solidimagestack/Middle.solidimagestacklayer/Contents.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// ContentView.swift | ||
// ExampleVisionOS | ||
// | ||
// Created by Hal Mueller on 3/5/24. | ||
// Copyright © 2024 Nick Lockwood. All rights reserved. | ||
// | ||
|
||
import RealityKit | ||
import SwiftUI | ||
|
||
struct ContentView: View { | ||
@State private var enlarge = false | ||
|
||
var body: some View { | ||
VolumetricView() | ||
} | ||
} | ||
|
||
#Preview(windowStyle: .volumetric) { | ||
ContentView() | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// ExampleVisionOSApp.swift | ||
// ExampleVisionOS | ||
// | ||
// Created by Hal Mueller on 3/5/24. | ||
// Copyright © 2024 Nick Lockwood. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
@main | ||
struct ExampleVisionOSApp: App { | ||
var body: some Scene { | ||
WindowGroup { | ||
ContentView() | ||
} | ||
.windowStyle(.volumetric) | ||
.defaultSize(width: 1.5, height: 1.5, depth: 1.5, in: .meters) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>UIApplicationSceneManifest</key> | ||
<dict> | ||
<key>UIApplicationPreferredDefaultSceneSessionRole</key> | ||
<string>UIWindowSceneSessionRoleVolumetricApplication</string> | ||
<key>UIApplicationSupportsMultipleScenes</key> | ||
<true/> | ||
<key>UISceneConfigurations</key> | ||
<dict/> | ||
</dict> | ||
</dict> | ||
</plist> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// MeshViewModel.swift | ||
// Euclid | ||
// | ||
// Created by Hal Mueller on 3/11/24. | ||
// Copyright © 2024 Nick Lockwood. All rights reserved. | ||
// | ||
|
||
import CoreGraphics | ||
import Euclid | ||
import RealityKit | ||
|
||
/// Holds an Entity built from a Euclid Mesh, plus state for the VolumetricView. | ||
@Observable | ||
class MeshViewModel: ObservableObject { | ||
/// The Euclid mesh's Entity lands here. | ||
public var entity: Entity? = nil | ||
/// Euclid has finished preparing the content. | ||
public var contentReady = false | ||
/// The RealityView instance has added the content. | ||
public var contentAdded = false | ||
|
||
/// A demonstration Mesh generated by Euclid | ||
private let euclidMesh: Mesh = { | ||
let start = CFAbsoluteTimeGetCurrent() | ||
|
||
// create some geometry using Euclid | ||
let cube = Mesh.cube(size: 0.8, material: Color.red) | ||
let sphere = Mesh.sphere(slices: 120, material: CGImage.checkerboard()) | ||
let mesh = cube.subtracting(sphere).makeWatertight() | ||
|
||
print("Time:", CFAbsoluteTimeGetCurrent() - start) | ||
print("Polygons:", mesh.polygons.count) | ||
print("Triangles:", mesh.triangulate().polygons.count) | ||
print("Watertight:", mesh.isWatertight) | ||
|
||
// Sleep here to simulate a long-running mesh construction. | ||
sleep(5) | ||
|
||
return mesh | ||
}() | ||
|
||
func prepareContent() { | ||
Task.init { | ||
do { | ||
let demoBoxEntity = try await ModelEntity(euclidMesh.scaled(by: 0.5)) | ||
|
||
// for more realism, add a shadow | ||
await demoBoxEntity.components.set(GroundingShadowComponent(castsShadow: true)) | ||
|
||
// needed for tap detection/response | ||
await demoBoxEntity.generateCollisionShapes(recursive: true) | ||
|
||
// for gesture targeting | ||
await demoBoxEntity.components.set(InputTargetComponent()) | ||
|
||
print(#function, "complete") | ||
entity = demoBoxEntity | ||
contentReady = true | ||
} catch { | ||
print(#function, "failed to update content") | ||
} | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
ExampleVisionOS/Preview Content/Preview Assets.xcassets/Contents.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// VolumetricView.swift | ||
// ExampleVisionOS | ||
// | ||
// Created by Hal Mueller on 3/6/24. | ||
// Copyright © 2024 Nick Lockwood. All rights reserved. | ||
// | ||
|
||
import RealityKit | ||
import SwiftUI | ||
|
||
struct VolumetricView: View { | ||
@State private var spinX = 0.0 | ||
@State private var spinY = 0.0 | ||
|
||
private var viewModel = MeshViewModel() | ||
|
||
var body: some View { | ||
RealityView { content in | ||
// In this demo we'll skip adding content at init time. But if you have other content | ||
// that you want visible while the EuclidMesh is being built, add it in this closure. | ||
// A simulated expensive, long-running Mesh generation happens on its own Task. | ||
debugPrint(content) | ||
} update: { content in | ||
if viewModel.contentReady, | ||
!viewModel.contentAdded, | ||
let box = viewModel.entity | ||
{ | ||
content.add(box) | ||
viewModel.contentAdded = true | ||
} | ||
guard let entity = content.entities.first else { | ||
return | ||
} | ||
|
||
let pitch = Transform(pitch: Float(spinX * -1)).matrix | ||
let yaw = Transform(yaw: Float(spinY)).matrix | ||
entity.transform.matrix = pitch * yaw | ||
} | ||
.gesture( | ||
DragGesture(minimumDistance: 0) | ||
.targetedToAnyEntity() | ||
.onChanged { value in | ||
let startLocation = value.convert(value.startLocation3D, from: .local, to: .scene) | ||
let currentLocation = value.convert(value.location3D, from: .local, to: .scene) | ||
let delta = currentLocation - startLocation | ||
spinX = Double(delta.y) * 5 | ||
spinY = Double(delta.x) * 5 | ||
} | ||
) | ||
.task { | ||
viewModel.prepareContent() | ||
print("content preparation launched...") | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
VolumetricView() | ||
} |
This file contains 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not a huge deal, but what about moving
euclidMesh
into this class? (I always look askance at global functions.)