3

I'm trying to render a frame, with realistic depth of field effect. I've already tried the depth of field properties in the camera node, but it doesn't produce usable results.

Is there a switch to max-out rendering quality of the depth of field effect? Performance is not a factor, I just need to render a frame, and user can wait for it.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Mabedan
  • 857
  • 8
  • 30
  • Please post a screenshot, and the camera parameters you're using. Tell us what is not usable about the result, and what you would like to see instead. – Hal Mueller Dec 15 '16 at 00:59

2 Answers2

6

Realistic Depth of Field effect in SceneKit

In SceneKit you can easily accomplish cool-looking shallow/deep depth of field (DoF). And it's not extremely intense for processing. .focusDistance and .fStop parameters are crucial for applying DoF:

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

enter image description here


Use the following code for testing (it's macOS version):

import SceneKit
import Cocoa

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
    }
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
2

SceneKit isn't able to do (out of the box) heavy, high quality post processing or still image rendering computation of this type. Theoretically you could probably build a setup that uses its rendering approaches to do both. But it's not a high quality renderer. If the user can wait, and you really want to focus on quality of imagery, Unreal Engine has the capacity to do this sort of thing, built in, and far higher quality post processing, effects, lights, materials, particles and rendering.

Confused
  • 6,048
  • 6
  • 34
  • 75
  • Maybe possible with a custom [SCNTechnique](https://developer.apple.com/reference/scenekit/scntechnique) ? – Karl Sigiscar Dec 16 '16 at 10:45
  • If you're looking to go mad, reinventing wheels and then putting them on a horse and buggy cart, I suppose so. – Confused Dec 16 '16 at 11:18
  • @KarlSigiscar Unreal Engine is so far ahead of this, due to the interest in architectural rendering with it, and is free... that I'd have to suggest there needs to be incredible reasons to try to not use it, for this sort of thing. – Confused Dec 16 '16 at 11:19
  • Agreed. You are preaching a converted. I am already learning UNREAL Engine, Blueprints, UNREAL C++ and C++ 11. I just need to finish my current project in SceneKit. – Karl Sigiscar Dec 16 '16 at 11:22
  • Let me know when you get started, I'd be keen to learn C++ with someone else, in the Unreal environment. I'm a designer/modeller/renderer. Shitful at programming. A line a day. – Confused Dec 16 '16 at 11:32
  • If you, or the OP need to do this in Unreal... the screenshots, do this: In Matinee, click on the Movie icon in the top right corner and choose "Custom Render Passes", it's pretty ridiculous what's in there. @KarlSigiscar – Confused Dec 16 '16 at 11:33
  • You don't need to be a developer to use UNREAL. Two designers created a VR game using UNREAL. They gave a speech at the GDC this year (you can find this session on the UNREAL channel on YouTube). They shipped their game using only Blueprints. They said that the bottleneck was never blueprints in terms of performance. – Karl Sigiscar Dec 16 '16 at 11:42
  • Sorry, you misunderstand my intent. I WANT to learn C++ inside the Unreal Environment. Nowhere else, and not for any other purpose, ever... but this is a desire. – Confused Dec 16 '16 at 12:13
  • @KarlSigiscar outside of Unreal, I have zero interest in C++, and I'm reliably informed that C++ inside Unreal is kind of like C++ Lite, so very keen to have a gander and get into it. – Confused Dec 16 '16 at 12:14
  • OK. Cool. UNREAL C++ is based on C++ 11 but it has its own APIs. For example, they have their own [array type](https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/TArrays/). – Karl Sigiscar Dec 16 '16 at 14:43
  • Though I am a developer, I will follow the advice of using standard Blueprints first. Develop in C++ custom Blueprints when needed. If the performance is not there, check potential problems with your assets or convert Blueprints to C++ (automatic). If the performance is still not there, re-develop the particular section of the game in C++. – Karl Sigiscar Dec 16 '16 at 14:46
  • Oh. No worries. I kind of studied BluePrints already, from a theoretical perspective, and Unity's equivalent, a magic thing made by a Russian magician of a programmer, called Antares. Visual scripting is something I'm probably a bit overly familiar with, and considering too deeply. I want to know real, modern programming. Need an entry point to C++, and think putting time into it in Unreal will be valuable for my next stages of thinking about visual programming. – Confused Dec 16 '16 at 15:31
  • is there an easy way for us to privately swap email addresses? @KarlSigiscar, if you're interested, obviously... – Confused Dec 16 '16 at 15:31
  • I don't think there is a way. Just add me to your contacts on Skype: karl.sigiscar1 – Karl Sigiscar Dec 16 '16 at 16:46