问题描述
我正在尝试对ARSCNView中的摄像机实时蒸汽图像应用模糊效果.我已经检查了WWDC视频.他们只提到了使用Metal进行自定义渲染的方法,但是我没有在网络上找到任何完整的示例.知道怎么做吗?
I'm trying to apply a blur effect to camera live steam image in ARSCNView. I have checked the WWDC videos. They only mentioned the custom rendering with Metal, but I didn't found any complete example on web. Any idea how to do that?
已更新1我试图将过滤器应用于背景.显示方向错误.我该如何解决?
Updated 1I have tried to apply a filter to the background. It show incorrect orientation. How can I fix this?
let bg=self.session.currentFrame?.capturedImage
if(bg != nil){
let context = CIContext()
let filter:CIFilter=CIFilter(name:"CIColorInvert")!
let image:CIImage=CIImage(cvPixelBuffer: bg!)
filter.setValue(image, forKey: kCIInputImageKey)
let result=filter.outputImage!
self.sceneView.scene.background.contents = context.createCGImage(result, from: result.extent)
}
推荐答案
我发现了一个很好的解决方案,该方法是只要设备方向发生变化,便只需对background
属性应用相应的几何变换即可.
I've found a pretty good solution, which is to simply apply a corresponding geometric transform to the background
property whenever the device orientation changes:
func session(_ session: ARSession, didUpdate frame: ARFrame) {
let image = CIImage(cvPixelBuffer: frame.capturedImage)
filter.setValue(image, forKey: kCIInputImageKey)
let context = CIContext()
if let result = filter.outputImage,
let cgImage = context.createCGImage(result, from: result.extent) {
sceneView.scene.background.contents = cgImage
if let transform = currentScreenTransform() {
sceneView.scene.background.contentsTransform = transform
}
}
}
private func currentScreenTransform() -> SCNMatrix4? {
switch UIDevice.current.orientation {
case .landscapeLeft:
return SCNMatrix4Identity
case .landscapeRight:
return SCNMatrix4MakeRotation(.pi, 0, 0, 1)
case .portrait:
return SCNMatrix4MakeRotation(.pi / 2, 0, 0, 1)
case .portraitUpsideDown:
return SCNMatrix4MakeRotation(-.pi / 2, 0, 0, 1)
default:
return nil
}
}
确保首先在viewDidLoad
方法中调用UIDevice.current.beginGeneratingDeviceOrientationNotifications()
.
Make sure you call UIDevice.current.beginGeneratingDeviceOrientationNotifications()
in your viewDidLoad
method first.
这篇关于我可以将CIFilter应用于ARkit相机供稿吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!