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

查看:31
本文介绍了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天全站免登陆