I am trying to create a 360deg immersive video environment. I am new to Swift/SwiftUI dev, so please bare with me if I am missing something. In the preview of the code below, as well as the simulator, "skyBoxEntity" does not appear in full. If radius is > 0.5, it doesn't appear at all. Otherwise it only partially appears.
import SwiftUI
import AVKit
import AVFoundation
import RealityKit
import RealityKitContent
struct ImmersiveReaderView: View {
@Binding var id: Book.ID?
@Binding var isImmersive: Bool
var resourceId = "sampleVideo"
private func createSkybox () -> Entity? {
// partially visible when radius <= 0.5, but is obstructed??
let skyBoxMesh = MeshResource.generateSphere(radius: 100)
guard let url = Bundle.main.url(forResource: resourceId, withExtension: "mp4") else {
fatalError("Video not found")
}
let player = AVPlayer(url: url)
let videoMaterial = VideoMaterial(avPlayer: player)
let skyBoxEntity = ModelEntity(
mesh: skyBoxMesh,
materials: [videoMaterial]
)
skyBoxEntity.scale *= .init(x: 1, y: 1, z: -1)
skyBoxEntity.transform.translation += SIMD3<Float>(0.0, 0.0, 0)
return skyBoxEntity
}
var body: some View {
ZStack {
let _ = print("Updated ImmersiveReaderView")
if isImmersive == true {
let _ = print("Immersive On")
RealityView { content, attachments in
guard let skybox = createSkybox() else { return }
// Add the video to the main content.
content.add(skybox)
// Add the ReaderView as an attachment
if let reader = attachments.entity(for: "reader") {
reader.setPosition([0, 0, 0.25], relativeTo: skybox)
content.add(reader)
}
} attachments: {
Attachment(id: "reader") {
ReaderView(id: $id, isImmersive: $isImmersive)
}
}
} else {
ReaderView(id: $id, isImmersive: $isImmersive)
}
}
}
}
struct ImmersiveReaderViewPreviewContainer : View {
@State private var bookId: Book.ID? = "Example ID"
@State private var isImmersive: Bool = true
var body: some View {
ImmersiveReaderView(id: $bookId, isImmersive: $isImmersive)
}
}
struct ImmersiveReaderPreview_Previews : PreviewProvider {
static var previews: some View {
ImmersiveReaderViewPreviewContainer()
}
}