【发布时间】:2018-10-02 06:29:04
【问题描述】:
我正在尝试在 ARKit 中为视频设置色键,我所做的与@Felix 在这里所做的非常相似:GPUImageView inside SKScene as SKNode material - Playing transparent video on ARKit
但是,当视频应该显示时(在这种情况下,当检测到 AR 参考图像时)我收到 [SceneKit] Error: Cannot get pixel buffer (CVPixelBufferRef) 错误并且视频不再播放。在我实现chromaKeyMaterial 之前它确实发挥了作用。这是我的代码,从检测到 AR 参考图像后开始:
DispatchQueue.main.async {
let filePath = Bundle.main.path(forResource: "wigz", ofType: "mp4")
let videoURL = NSURL(fileURLWithPath: filePath!)
let player = AVPlayer(url: videoURL as URL)
let spriteKitScene = SKScene(size: CGSize(width: 640, height: 480))
let videoSpriteKitNode = SKVideoNode(avPlayer: player)
let videoNode = SCNNode()
videoNode.geometry = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width,
height: imageAnchor.referenceImage.physicalSize.height)
videoNode.eulerAngles = SCNVector3(-Float.pi/2, 0, 0)
// Use spritekit with videonode inside
spriteKitScene.scaleMode = .aspectFit
videoSpriteKitNode.position = CGPoint(x: spriteKitScene.size.width / 2,
y: spriteKitScene.size.height / 2)
videoSpriteKitNode.size = spriteKitScene.size
videoSpriteKitNode.yScale = -1.0
videoSpriteKitNode.play()
// Loop video
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: .main) { _ in
player.seek(to: kCMTimeZero)
player.play()
}
spriteKitScene.addChild(videoSpriteKitNode)
videoNode.geometry?.firstMaterial?.diffuse.contents = spriteKitScene
videoNode.geometry?.firstMaterial?.isDoubleSided = true
let chromaKeyMaterial = ChromaKeyMaterial()
chromaKeyMaterial.diffuse.contents = player
videoNode.geometry!.materials = [chromaKeyMaterial]
node.addChildNode(videoNode)
self.imageDetectView.scene.rootNode.addChildNode(node)
}
在 ChromaKeyMaterial.swift 文件中,我将这些行更改为:
float maskY = 0.0 * c_colorToReplace.r + 1.0 * c_colorToReplace.g + 0.0 * c_colorToReplace.b;
float maskCr = 0.7132 * (c_colorToReplace.r - maskY);
float maskCb = 0.5647 * (c_colorToReplace.b - maskY);
float Y = 0.0 * textureColor.r + 1.0 * textureColor.g + 0.0 * textureColor.b;
float Cr = 0.7132 * (textureColor.r - Y);
float Cb = 0.5647 * (textureColor.b - Y);
试图用色键抠出纯绿色,但我不确定这是否是正确的方法。
任何帮助将不胜感激!
【问题讨论】:
-
请注意,在最新版本的 SceneKit 中使用
SKScene和SKVideoNode是不必要的。您可以直接将AVPlayer设置为SCNMaterialProperty实例的内容。 -
@mnuages 很有趣!我会检查的!谢谢。
-
@mnuages 哇。简单得多,它似乎运行得更好,尽管这可能是我的记忆力让我失望或安慰剂。感谢您的提示!
标签: ios swift sprite-kit scenekit arkit