Воспроизведение прозрачного фонового видео в SceneKit - PullRequest
0 голосов
/ 01 марта 2020

Я следую этому уроку https://www.raywenderlich.com/6957-building-a-museum-app-with-arkit-2, чтобы создать приложение дополненной реальности, которое распознает изображение, и поверх него я хочу воспроизводить видео, прозрачное для SceneView. С BG все работает нормально, но в случае отсутствия BG он не работает.

Я следовал некоторым руководствам в Интернете https://medium.com/@quentinfasquel / ios -transparent-video-in-spritekit-then- scenekit-2fc66b8706a6

Там говорится сначала SpriteKit, а затем SceneKit, который также не удалось реализовать. Может кто-нибудь, пожалуйста, помогите мне воспроизвести прозрачное видео в SceneKit с / без SpriteKit.

 ``` class MyVideoScene: SKScene {
var player: AVPlayer!
var playerLooper: AVPlayerLooper!
override func sceneDidLoad() {
    super.sceneDidLoad()
    //    guard let url = Bundle.main.url(forResource: "playdoh-bat", withExtension: "mp4") else {
    guard let url = Bundle.main.url(forResource: "VideoInserts05-Col+Alp", withExtension: "mp4") else {
        print("Can't find example video")
        return
    }
    // Creating our player
    let playerItem = AVPlayerItem(url: url)
    player = AVQueuePlayer(playerItem: playerItem)
    playerLooper = AVPlayerLooper(player: player as! AVQueuePlayer, templateItem: playerItem)

    // Getting the size of our video
    let videoTrack = playerItem.asset.tracks(withMediaType: .video).first!
    let videoSize = videoTrack.naturalSize

    // Adding a `SKVideoNode` to display video in our scene
    let videoNode = SKVideoNode(avPlayer: player)
    //    videoNode.xRotation = CGFloat(-.pi * 0.5)


    videoNode.position = CGPoint(x: frame.midX, y: frame.midY)
    videoNode.size = videoSize.applying(CGAffineTransform(scaleX: 1.0, y: 0.5))

    // Let's make it transparent, using an SKEffectNode,
    // since a shader cannot be applied to a SKVideoNode directly
    let effectNode = SKEffectNode()
    // Loving Swift's multiline syntax here:
    effectNode.shader = SKShader(source: """
void main() {
vec2 texCoords = v_tex_coord;
vec2 colorCoords = vec2(texCoords.x, (1.0 + texCoords.y) * 0.5);
vec2 alphaCoords = vec2(texCoords.x, texCoords.y * 0.5);
vec4 color = texture2D(u_texture, colorCoords);
float alpha = texture2D(u_texture, alphaCoords).r;
gl_FragColor = vec4(color.rgb, alpha);
}
""")
    addChild(effectNode)
    effectNode.addChild(videoNode)

    player.play()
}

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
}

} `` `

Вот код, который я реализовал для воспроизведения прозрачного видео в верхней части обнаружения изображений в ARSceneView Метод делегата didAdd Node

                        let bounds = CGRect(x: 0, y: 0, width: 640, height: 480)
                        let sceneView = SCNView(frame: bounds, options: [:])
                        sceneView.backgroundColor = .black
                        sceneView.allowsCameraControl = true
                        // Create scene
                        let scene = SCNScene()
                        let plane = SCNPlane()
                        let videoNode = SCNNode(geometry: plane)
                        scene.rootNode.addChildNode(videoNode)

                        let scene2d = MyVideoScene(size: bounds.size)
                        scene2d.backgroundColor = .clear
                        plane.firstMaterial?.diffuse.contents = scene2d
                        sceneView.scene = scene

Я хотел показать это видео enter image description here как этот вывод: https://drive.google.com/file/d/1xgclIX-LzQi4Eih1NvBvsMeYvo6VKZ_Q/view?usp=sharing. Но не могу этого добиться, нужна помощь, когда я делаю что-то не так или как добавить вывод SpriteKit в SceneKit.

...