如何在SceneKit中实现逼真的景深效果?

时间:2016-12-14 19:27:53

标签: swift 3d camera scenekit

我试图渲染一个帧,具有逼真的景深效果。我已经在camera节点中尝试过景深属性,但它不会产生可用的结果。

是否有切换到景深效果的最大渲染质量?性能不是一个因素,我只需要渲染一个框架,用户可以等待它。

2 个答案:

答案 0 :(得分:2)

SceneKit无法进行(开箱即用)重型,高质量的后期处理或此类型的静态图像渲染计算。从理论上讲,您可以构建一个使用其渲染方法来执行这两种操作的设置。但它不是高质量的渲染器。如果用户可以等待,并且你真的想要专注于图像质量,那么虚幻引擎就有能力做这种事情,内置,并且后处理,效果,灯光,材料,粒子和渲染的质量要高得多。 / p>

答案 1 :(得分:0)

在SceneKit中,您可以轻松实现看起来很酷的浅景深。而且处理的强度不是很高。 .focusDistance.fStop参数至关重要:

cameraNode.camera?.wantsDepthOfField = true
cameraNode.camera?.focusDistance = 5
cameraNode.camera?.fStop = 0.01
cameraNode.camera?.focalLength = 24

enter image description here

这是我的测试代码(Mac版):

import SceneKit
import QuartzCore

class GameViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let scene = SCNScene()

        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        cameraNode.camera?.wantsDepthOfField = true
        cameraNode.camera?.focusDistance = 5
        cameraNode.camera?.fStop = 0.01
        cameraNode.camera?.focalLength = 24
        scene.rootNode.addChildNode(cameraNode)
        cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)

        let lightNode = SCNNode()
        lightNode.light = SCNLight()
        lightNode.light!.type = .omni
        lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
        scene.rootNode.addChildNode(lightNode)

        let ambientLightNode = SCNNode()
        ambientLightNode.light = SCNLight()
        ambientLightNode.light!.type = .ambient
        ambientLightNode.light!.color = NSColor.darkGray
        scene.rootNode.addChildNode(ambientLightNode)

        let cylinderNode01 = SCNNode()
        cylinderNode01.geometry = SCNCylinder(radius: 2, height: 10)
        cylinderNode01.position = SCNVector3(0, 0, 0)
        cylinderNode01.geometry?.materials.first?.diffuse.contents = NSImage(named: NSImage.Name("checker01.png"))
        scene.rootNode.addChildNode(cylinderNode01)

        let cylinderNode02 = SCNNode()
        cylinderNode02.geometry = SCNCylinder(radius: 2, height: 10)
        cylinderNode02.position = SCNVector3(5, 0, 5)
        cylinderNode02.geometry?.materials.first?.diffuse.contents = NSImage(named: NSImage.Name("checker02.jpg"))
        scene.rootNode.addChildNode(cylinderNode02)

        let cylinderNode03 = SCNNode()
        cylinderNode03.geometry = SCNCylinder(radius: 2, height: 10)
        cylinderNode03.position = SCNVector3(10, 0, 10)
        cylinderNode03.geometry?.materials.first?.diffuse.contents = NSImage(named: NSImage.Name("checker01.png"))
        scene.rootNode.addChildNode(cylinderNode03)

        let cylinderNode04 = SCNNode()
        cylinderNode04.geometry = SCNCylinder(radius: 2, height: 10)
        cylinderNode04.position = SCNVector3(-5, 0, -5)
        cylinderNode04.geometry?.materials.first?.diffuse.contents = NSImage(named: NSImage.Name("checker02.jpg"))
        scene.rootNode.addChildNode(cylinderNode04)

        let cylinderNode05 = SCNNode()
        cylinderNode05.geometry = SCNCylinder(radius: 2, height: 10)
        cylinderNode05.position = SCNVector3(-10, 0, -10)
        cylinderNode05.geometry?.materials.first?.diffuse.contents = NSImage(named: NSImage.Name("checker01.png"))
        scene.rootNode.addChildNode(cylinderNode05)

        let scnView = self.view as! SCNView
        scnView.scene = scene
        scnView.allowsCameraControl = true
        scnView.backgroundColor = NSColor.black
    }
}