首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CIFilter不能应用于SCNMaterial

CIFilter不能应用于SCNMaterial
EN

Stack Overflow用户
提问于 2020-08-25 04:57:09
回答 1查看 172关注 0票数 1

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

代码语言:javascript
复制
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的材料,则不起作用:

代码语言:javascript
复制
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)
}

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

你怎么了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-25 05:46:22

您使用该构造函数创建的UIImage在这一时刻实际上并不呈现。图像的接收方需要知道图像在使用前需要呈现,而SceneKit似乎没有处理。

详情请看我的答案here

下面是如何在Swift中呈现CIImage

代码语言:javascript
复制
// 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: $0) })

您可以将CGImage传递给材料或将其包装到UIImage中,两者都应该工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63572284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档