swift RealityKit -如何将视频素材添加到模型实体中?

nkkqxpd9  于 2023-01-12  发布在  Swift
关注(0)|答案(3)|浏览(149)

我用代码在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))

如何添加视频文件?

bis0qfac

bis0qfac1#

您可以在RealityKit 2.0及更高版本中使用video textures。RealityKit 1.0不支持视频素材。
下面的代码展示了如何应用它:

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: "video", 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()
gjmwrych

gjmwrych2#

我找到了变通方案,例如下面的代码

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支持视频素材。

bz4sfanl

bz4sfanl3#

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

相关问题