Try it with this code:
//…
@State var anyImmersiveSpaceOpen: Bool = false
//…
//when Button Click
Task {
if anyImmersiveSpaceOpen {
await dismissImmersiveSpace()
anyImmersiveSpaceOpen = false
}else {
if anyImmersiveSpaceOpen {
await dismissImmersiveSpace()
}
await openImmersiveSpace(id: id)
anyImmersiveSpaceOpen = true
}
}
Post
Replies
Boosts
Views
Activity
You can adjust the default size of a volumetric window with following code:
WindowGroup(id: "WindowName") {
SomeView()
}
.windowStyle(.volumetric)
.defaultSize(width: 1, height: 1, depth: 1, in: .meters)
Hello,
It is very easy to represent a 360° Video in the Vision Pro environment.
You have to create videoPlayer
create a very big sphere I recommend a sphere with the size 1E3 (1* 10^3)
assign a VideoMaterial to this entity
adjust the properties of the entity for your needs and add some videoPlayer controls to your video if it is you want.
Here is a simple approach and a little code snippet:
//Create Entity for the video
let videoEntity = Entity()
//Search for video in paths
guard let url = Bundle.main.url(forResource: "example", withExtension: "mp4") else {fatalError("Video was not found!")}
//create a simple AVPlayer
let asset = AVURLAsset(url: url)
let playerItem = AVPlayerItem(asset: asset)
let player = AVPlayer()
//create a videoMaterial
let material = VideoMaterial(avPlayer: player)
//Made a Sphere with the videoEntity and asign the videoMaterial to it
videoEntity.components.set(ModelComponent(mesh: .generateSphere(radius: 1E3), materials: [material]))
//adjust the properties of the videoEntity(Sphere) if needed
videoEntity.scale = .init(x: 1, y: 1, z: -1)
videoEntity.transform.translation += SIMD3<Float>(0.0, 10.0, 0.0)
let angle = Angle.degrees(90)
let rotation = simd_quatf(angle: Float(angle.radians), axis: .init(x: 0, y: 0, z: 0))
videoEntity.transform.rotation = rotation
//add VideoEntity to realityView
content.add(videoEntity)
//start the VideoPlayer
player.replaceCurrentItem(with: playerItem)
player.play()
}
I would recommend actually to use a video which was actually designed for 360° view :)
I hope I could help you :)
Create a curved surface in a 3D modeling program and convert it to USDZ afterwards
Make a USDA Scene with the curved surface
Make an Entity with the scene and assign the VideoPlayerComponent to it.
RealityView() { content in
//load your scene with the curved surface
if let entity = try? await Entity(named: "EntityName", in: realityKitContentBundle) {
//check if Video exists and is accessible
guard let url = Bundle.main.url(forResource: "videoname", withExtension: "mp4") else {fatalError("Video was not found!")}
let asset = AVURLAsset(url: url)
let playerItem = AVPlayerItem(asset: asset)
let player = AVPlayer()
//add VideoPlayerComponent to entity
entity.components[VideoPlayerComponent.self] = .init(avPlayer: player)
//Adjust the scale of the entity
entity.scale *= 0.5
// you can also adjust the position of your entity here in code if needed
// entity.position = SIMD3(x: , y: , z: )
content.add(entity)
player.replaceCurrentItem(with: playerItem)
player.play()
}else {
print("Entity with the given name was not found!")
}
}
}
Hope I could help you :)
attachments are optionals so you have to unwrap them:
if let panelEntity = attachments.entity(for: "panel") {
content.add(panelEntity)
}
It is a really strange bug. I could fix it with this process (workaround probably not the most efficent).
Create a new project -> Import your code -> Open the realityKitBundle of your new project in reality composer pro -> There you create a new scene where you drag and drop the scene of your old project. The realityKitContentBundle should load now.
Keep in Mind that it also will import the assets for each scene in a different folder.
When you have multiple usda files with the same use of assets I would recommend to make a new folder with all of the assets. Then you have to update the references of the scenes and it should work.
I hope I could help you
New update: I recreated the whole App now it works.
Error
You have to define the window style before adding the default size
WindowGroup() {
ContentView()
}.windowStyle(.plain).defaultSize(width: <#T##CGFloat#>, height: <#T##CGFloat#>, depth: <#T##CGFloat#>)
Hello, now it works with following code:
// ImmersiveView.swift
// NewDimensionn
//
// Created by Patrick Schnitzer on 18.07.23.
//
import SwiftUI
import RealityKit
import RealityKitContent
struct ImmersiveView: View {
var body: some View {
RealityView { content in
async let sphere = ModelEntity(named: "EarthScene", in: realityKitContentBundle)
if let sphere = try? await sphere {
// A name for identifying this entity.
sphere.name = "sphere"
// Generates collision shapes for the sphere based on its geometry.
sphere.generateCollisionShapes(recursive: false)
// Give the sphere an InputTargetComponent.
sphere.components.set(InputTargetComponent())
// Set the initial position.
sphere.position = .init(0, 1, -0.5)
content.add(sphere)
}
}
.gesture(dragGesture)
}
var dragGesture: some Gesture {
DragGesture()
.targetedToAnyEntity()
.onChanged { value in
print(value.entity.name)
value.entity.position = value.convert(value.location3D, from: .local, to: value.entity.parent!)
}
}
}
#Preview {
ImmersiveView()
.previewLayout(.sizeThatFits)
}
Thank you very much ;)
Hello thank you for your comment!
I have already added the components to the scenes.
Input Target
Collision