我使用代码在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))
如何添加视频文件?
答案 0 :(得分:2)
您只能在RealityKit 2.0
中使用video textures(需要具有iOS 14目标的Xcode 12)。 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()
答案 1 :(得分:0)
我可能是错的,但目前RealityKit不支持视频。视频不是普通的纹理,而是一组动画纹理。
答案 2 :(得分:0)
我找到了解决方法,例如下面的代码
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支持视频材料。