如何将视频素材添加到模型实体?

问题描述 投票:0回答:4

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

如何添加视频文件?

swift swiftui scenekit arkit realitykit
4个回答
8
投票

RealityKit 视频素材

您可以在 RealityKit 2.0 及更高版本中使用

Video Material
。 RealityKit v1.0 不支持视频素材。这是一个向您展示如何应用视频素材的代码:

方法A

import SwiftUI
import RealityKit
import AVFoundation

struct ARViewContainer : UIViewRepresentable {
    
    let arView = ARView(frame: .zero)
    let anchor = AnchorEntity()
    
    func makeUIView(context: Context) -> ARView {
        anchor.position.z += 1.0
        self.loadVideoMaterial()
        return arView
    }
    func loadVideoMaterial() {
        guard let pathToVideo = Bundle.main.path(forResource: "video",
                                                      ofType: "mp4")
        else { return }

        let videoURL = URL(fileURLWithPath: pathToVideo)
        let avPlayer = AVPlayer(url: videoURL)
        // 16:9 video
        let mesh = MeshResource.generatePlane(width: 1.92, height: 1.08)
        let material = VideoMaterial(avPlayer: avPlayer)
        let planeModel = ModelEntity(mesh: mesh, materials: [material])

        anchor.addChild(planeModel)
        arView.scene.anchors.append(anchor)
        
        avPlayer.volume = 0.05
        avPlayer.play()
    }
    func updateUIView(_ view: ARView, context: Context) { }
}

struct ContentView : View {
    var body: some View {
        ARViewContainer().ignoresSafeArea()
    }
}

此外,您还可以这样添加 RealityKit 的

VideoMaterial

方法B

// 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()

RealityKit 的 VideoPlayerComponent (visionOS)

在visionOS中,

VideoPlayerComponent
是创建视频场景的另一种方式(包括具有透明度的HEVC视频)。

import SwiftUI
import AVKit
import RealityKit

struct ContentView: View {
    
    @State var player: AVPlayer?
    let screen = Entity()
    
    var body: some View {
        RealityView { content in
            let url = Bundle.main.url(forResource: "puppets", withExtension: "mov")!
            player = AVPlayer(url: url)
            screen.components[VideoPlayerComponent.self] = .init(avPlayer: player!)
            
            let anchor = AnchorEntity()
            anchor.addChild(screen)
            content.add(anchor)
            player?.play()
        }
    }
}
#Preview {
    ContentView()
}


SceneKit视频素材

import SwiftUI
import SceneKit
import AVFoundation

struct VRViewContainer : UIViewRepresentable {
    
    let sceneView = SCNView(frame: .zero)
    
    func makeUIView(context: Context) -> SCNView {
        sceneView.scene = SCNScene()
        sceneView.backgroundColor = .black
        sceneView.pointOfView?.position.z += 0.5
        sceneView.isPlaying = true
        self.loadVideoMaterial()
        return sceneView
    }
    func loadVideoMaterial() {
        guard let pathToVideo = Bundle.main.path(forResource: "video",
                                                      ofType: "mp4")
        else { return }

        let videoURL = URL(fileURLWithPath: pathToVideo)
        let avPlayer = AVPlayer(url: videoURL)
        // 16:9 video
        let material = SCNMaterial()
        material.diffuse.contents = avPlayer
        let mesh = SCNPlane(width: 1.92, height: 1.08)
        mesh.materials[0] = material
        let planeModel = SCNNode(geometry: mesh)

        sceneView.scene?.rootNode.addChildNode(planeModel)
        
        avPlayer.volume = 0.05
        avPlayer.play()
    }
    func updateUIView(_ view: SCNView, context: Context) { }
}

struct ContentView : View {
    var body: some View {
        VRViewContainer().ignoresSafeArea()
    }
}

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


0
投票

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


0
投票

将其添加到 3D 模型而不是生成的平面的任何可能方法

© www.soinside.com 2019 - 2024. All rights reserved.