CIFilter 不能应用于 SCNMaterial

CIFilter can't be applied to SCNMaterial

任何 CIFilter 应用于 UIImageView 时都可以正常工作。

import UIKit
import CoreImage

@IBOutlet var imageView: UIImageView!
let ciBlurFilter = CIFilter(name: "CIGaussianBlur")!


func gaussianBlur() -> UIImage? {        
    let uiImage = UIImage(named: "texture.png")!
    let ciImage = CIImage(image: uiImage)
    ciBlurFilter.setValue(ciImage, forKey: "inputImage")
    let resultedImage = ciBlurFilter.value(forKey: "outputImage") as! CIImage
    let blurredImage = UIImage(ciImage: resultedImage)
    return blurredImage
}

override func viewDidLoad() {
    super.viewDidLoad()        
    imageView.image = self.gaussianBlur()
}

但是应用于SceneKit的就不行了material:

import SceneKit

@IBOutlet var sceneView: SCNView!
let ciBlurFilter = CIFilter(name: "CIGaussianBlur")!


func gaussianBlur() -> UIImage? {        
    let uiImage = UIImage(named: "texture.png")!
    let ciImage = CIImage(image: uiImage)
    ciBlurFilter.setValue(ciImage, forKey: "inputImage")
    let resultedImage = ciBlurFilter.value(forKey: "outputImage") as! CIImage
    let blurredImage = UIImage(ciImage: resultedImage)
    return blurredImage
}

override func viewDidLoad() {
    super.viewDidLoad()
    sceneView.scene = SCNScene()       
    let sphereNode = SCNNode(geometry: SCNSphere(radius: 0.1))
    sphereNode.geometry?.firstMaterial?.diffuse.contents = self.gaussianBlur()
    sceneView.scene?.rootNode.addChildNode(sphereNode)
}

为什么带有 CIFilter 的 SCNMaterial 是不可见的(尽管它支持 UIImages)?

怎么了?

您使用该构造函数创建的 UIImage 此时实际上并未呈现。图片的接收者需要知道图片在使用前需要渲染,SceneKit貌似没有处理。

详情请看我的回答

这是在 Swift 中呈现 CIImage 的方式:

// ideally you create this once and re-use it;
// you should not create a new context for every draw call
let ciContext = CIContext()

let cgImage = ciContext.createCGImage(ciImage, from: ciImage.extent)
let uiImage = cgImage.flatMap({ UIImage.init(cgImage: [=10=]) })

您可以将 CGImage 传递给 material 或将其包装到 UIImage 中,两者都应该有效。