我正在尝试在ios上使用swift和scenekit开发一个增强现实应用程序。有没有办法将设备摄像头捕获的视频绘制为场景背景?
发布于 2014-11-15 12:25:28
这对我很有效,
我使用AVFoundation
来捕获设备摄像头的视频输入:
let captureSession = AVCaptureSession()
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
if let videoDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) {
var err: NSError? = nil
if let videoIn : AVCaptureDeviceInput = AVCaptureDeviceInput.deviceInputWithDevice(videoDevice, error: &err) as? AVCaptureDeviceInput {
if(err == nil){
if (captureSession.canAddInput(videoIn as AVCaptureInput)){
captureSession.addInput(videoIn as AVCaptureDeviceInput)
}
else {
println("Failed to add video input.")
}
}
else {
println("Failed to create video input.")
}
}
else {
println("Failed to create video capture device.")
}
}
captureSession.startRunning()
此时,根据苹果关于SCNScene
的background
属性的文档,我希望将AVCaptureVideoPreviewLayer
的实例添加到SCNScene
的background.contents
中,如下所示:
previewLayer.frame = sceneView.bounds
sceneView.scene.background.contents = previewLayer
这将我的场景的背景颜色从默认的白色更改为黑色,但没有提供视频输入。这可能是一个iOS错误?所以计划B,相反,我添加了'AVCaptureVideoPreviewLayer‘作为UIView
层的子层:
previewLayer.frame = self.view.bounds
self.view.layer.addSublayer(previewLayer)
然后,我将一个SCNView
设置为同一个UIView
的子视图,将SCNView
的背景颜色设置为clear:
let sceneView = SCNView()
sceneView.frame = self.view.bounds
sceneView.backgroundColor = UIColor.clearColor()
self.view.addSubview(sceneView)
设备摄像机的视频现在作为场景的背景可见。
我已经创建了一个小的demo。
发布于 2014-10-14 15:13:28
可以,您可以使用AVCaptureVideoPreviewLayer
作为材质属性的contents
(只需确保您给图层一个界限)。
场景视图具有背景的材质属性,您可以将视频预览指定给它(将图层指定给背景的内容)。
var background: SCNMaterialProperty! { get }
https://stackoverflow.com/questions/26345737
复制相似问题