RealityKit –如何将视频素材添加到ModelEntity? [英] RealityKit – How to add a Video Material to a ModelEntity?

查看:160
本文介绍了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()

这篇关于RealityKit –如何将视频素材添加到ModelEntity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆