RealityKit – 如何将视频材质添加到 ModelEntity?
问题描述:
我使用代码在 RealityKit 中添加图片纹理,效果很好.
I use the code to add a picture texture in RealityKit and it works fine.
var material = SimpleMaterial()
material.baseColor = try! .texture(.load(named: "image.jpg"))
我尝试用这段代码将视频文件添加为纹理,但是崩溃了!!!
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))
如何添加视频文件?
答
您可以使用 视频纹理 仅在 RealityKit 2.0
中(需要带有 iOS 14 目标的 Xcode 12).以前版本的 RealityKit 不支持视频材料.
You can use video textures only in RealityKit 2.0
(Xcode 12 with iOS 14 target is needed). A previous version of RealityKit doesn't support video materials.
这是一个向您展示如何应用它的代码:
Here's a code showing you how to apply it:
import AVKit
import RealityKit
@IBOutlet var arView: ARView!
// AVPLAYER
guard let pathToVideo = Bundle.main.path(forResource: "video", ofType: "mp4")
else {
return
}
let videoURL = URL(fileURLWithPath: pathToVideo)
let avPlayer = AVPlayer(url: videoURL)
// ENTITY
let mesh = MeshResource.generatePlane(width: 1.92, depth: 1.08) // 16:9 video
let material = VideoMaterial(avPlayer: avPlayer)
let planeEntity = ModelEntity(mesh: mesh, materials: [material])
// ANCHOR
let anchor = AnchorEntity(.plane(.vertical,
classification: .wall,
minimumBounds: [0.3, 0.3])
anchor.addChild(planeEntity)
arView.scene.anchors.append(anchor)
// PLAYBACK
avPlayer.play()
此外,您可以通过这种方式添加VideoMaterial
:
Also, you can add VideoMaterial
this way:
// AVPLAYER and PlayerItem
let url = Bundle.main.url(forResource: "aaa",
withExtension: "mp4")
let asset = AVAsset(url: url!)
let playerItem = AVPlayerItem(asset: asset)
let avPlayer = AVPlayer()
// ENTITY
let mesh = MeshResource.generateSphere(radius: 1)
let material = VideoMaterial(avPlayer: avPlayer)
let entity = ModelEntity(mesh: mesh, materials: [material])
// ANCHOR
let anchor = AnchorEntity(world: [0,0,-10])
anchor.addChild(entity)
arView.scene.anchors.append(anchor)
// PLAYBACK
avPlayer.replaceCurrentItem(with: playerItem)
avPlayer.play()