您只能在(需要带有 iOS 14 目标的 Xcode 12)中使用视频纹理。RealityKit 2.0
RealityKit 的早期版本不支持视频材料。
这是一个代码,向您展示如何应用它:
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
以下方式添加:
// 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()