-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SwiftUI visionOS example. (#115)
- Loading branch information
1 parent
d3afbeb
commit 1399eec
Showing
16 changed files
with
422 additions
and
9 deletions.
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,26 @@ | ||
// | ||
// EuclidMesh.swift | ||
// ExampleVisionOS | ||
// | ||
// Created by Hal Mueller on 3/5/24. | ||
// Copyright © 2024 Nick Lockwood. All rights reserved. | ||
// | ||
|
||
import CoreGraphics | ||
import Euclid | ||
|
||
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) | ||
|
||
return mesh | ||
}() |
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> |
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,53 @@ | ||
// | ||
// 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 | ||
|
||
var body: some View { | ||
RealityView { content in | ||
if let demoBoxEntity = try? ModelEntity(euclidMesh.scaled(by: 0.5)) { | ||
// for more realism, add a shadow | ||
demoBoxEntity.components.set(GroundingShadowComponent(castsShadow: true)) | ||
|
||
// needed for tap detection/response | ||
demoBoxEntity.generateCollisionShapes(recursive: true) | ||
|
||
// for gesture targeting | ||
demoBoxEntity.components.set(InputTargetComponent()) | ||
|
||
content.add(demoBoxEntity) | ||
} | ||
} update: { content in | ||
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 | ||
} | ||
) | ||
} | ||
} | ||
|
||
#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