Skip to content

Instantly share code, notes, and snippets.

@christianselig
Created March 7, 2024 04:09
Show Gist options
  • Save christianselig/cee2d47217be336218a16c011dcd8250 to your computer and use it in GitHub Desktop.
Save christianselig/cee2d47217be336218a16c011dcd8250 to your computer and use it in GitHub Desktop.
import SwiftUI
import RealityKit
import AVFoundation
import MetalKit
struct ImmersiveView: View {
@State var textureResource: TextureResource?
var body: some View {
RealityView { content in
// 🎬 Create video sphere ✅
let player = AVPlayer(url: Bundle.main.url(forResource: "gorillaz-short", withExtension: "mov")!)
player.isMuted = true
player.play()
let videoMaterial = VideoMaterial(avPlayer: player)
let sphere1 = ModelEntity(mesh: .generateSphere(radius: 0.4), materials: [videoMaterial])
sphere1.position = [0.5, 1.3, -1.5]
content.add(sphere1)
// 🔗 Create an image sphere based on DrawableQueue ❌
var material = UnlitMaterial()
let textureResource = try! TextureResource.generate(from: UIImage(named: "gorillaz.jpg")!.cgImage!, options: .init(semantic: .color))
material.color = .init(texture: .init(textureResource))
self.textureResource = textureResource
let sphere2 = ModelEntity(mesh: .generateSphere(radius: 0.4), materials: [material])
sphere2.position = [-0.5, 1.3, -1.5]
content.add(sphere2)
}
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) {
let drawableQueue = try! TextureResource.DrawableQueue(.init(pixelFormat: .rgba8Unorm, width: 3840, height: 2048, usage: [.renderTarget, .shaderRead], mipmapsMode: .none))
self.textureResource!.replace(withDrawables: drawableQueue)
let device = MTLCreateSystemDefaultDevice()!
let commandQueue = device.makeCommandQueue()!
let textureLoader = MTKTextureLoader(device: device)
let url = Bundle.main.url(forResource: "gorillaz", withExtension: "jpg")!
let texture: MTLTexture = try! textureLoader.newTexture(URL: url, options: nil)
let drawable = try! drawableQueue.nextDrawable()
let commandBuffer = commandQueue.makeCommandBuffer()!
let blitCommandEncoder = commandBuffer.makeBlitCommandEncoder()!
blitCommandEncoder.copy(from: texture, to: drawable.texture)
blitCommandEncoder.endEncoding()
commandBuffer.commit()
commandBuffer.waitUntilCompleted()
drawable.present()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment