3

我使用代码在 RealityKit 中添加图片纹理,效果很好。

var material = SimpleMaterial()

material.baseColor = try! .texture(.load(named: "image.jpg"))

我尝试使用此代码将视频文件添加为纹理,但它崩溃了!!!

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

如何添加视频文件?

4

3 回答 3

3

您只能在(需要带有 iOS 14 目标的 Xcode 12)中使用视频纹理。RealityKit 2.0RealityKit 的早期版本不支持视频材料。

这是一个代码,向您展示如何应用它:

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()
于 2020-01-27T21:56:59.660 回答
2

我找到了解决方法,例如下面的代码

self.arView = arView
let scene = SCNScene()
scnView = SCNView(frame: arView.frame)
scnView?.scene = scene
scnView?.backgroundColor = UIColor.clear
scnView?.scene?.background.contents = UIColor.clear

然后添加 SCN 摄像头并从 ARFrame 设置摄像头变换,例如:

let rotation = SCNMatrix4MakeRotation(.pi / 2.0, 0, 0, 1)
        let cameraTransform = simd_mul(frame.camera.transform, simd_float4x4(rotation))

let projectionMatrix = SCNMatrix4(frame.camera.projectionMatrix(for: .portrait,
                                                                         viewportSize: self.viewBounds!.size,
                                                                         zNear: 0.001,
                                                                         zFar: 10000) )
self.arCameraNode?.simdTransform = cameraTransform
self.arCameraNode?.camera?.projectionTransform = projectionMatrix

最后,将你的SCN视频节点添加到rootNode
中,不过有一点偏移,我想你可以等待Realitykit支持视频素材。

于 2019-11-25T13:51:58.927 回答
0

我可能错了,但目前 RealityKit 不支持视频。视频不是普通的纹理,它是一组动画纹理。

于 2019-10-01T20:35:33.023 回答