How to add a video material to a ModelEntity in RealityKit

I use the code to add a picture texture in RealityKit and it works fine.

I try to use this code to add a video file to be a texture, but it crashes!!!

guard let url = Bundle.main.url(forResource: "data", withExtension: "mp4") else { return } 
 material.baseColor = try! .texture(.load(contentsOf: url))

How can I add a video file???

Replies

from my investigations thus far it is not possible. I tried even adding a gif, but only the first frame is rendered onto the model.


I really hope this is something which will be added soon, and in a nicer way than there is in SceneKit too.

The code snippet at 4:52 of What's new in RealityKit says to do this...

Code Block
// Use AVFoundation to load a video
let asset = AVURLAsset(url: Bundle.main.url(forResource: "glow", withExtension: "mp4")!)
let playerItem = AVPlayerItem(asset: asset)
// Create a Material and assign it to your model entity...
let player = AVPlayer()
bugEntity.materials = [VideoMaterial(player: player)]
// Tell the player to load and play
player.replaceCurrentItem(with: playerItem)
player.play()


Can you let me know if this works?


Yup - you create an AVPlayer using AVFoundation and use it to create a new VideoMaterial.