Another exciting announcement at WWDC 2015 was a new property on SCNNode: filters which is an optional array of CIFilter instances.
By simply populating this array with a filter or filters, the node and its child nodes are filtered accordingly. This gives game developers, for example, a great way of stylising their 3D scenes with a halftone or pixellated look with next to no effort.
I've taken my old Scene Kit material editor project and added a segmented control that allows the user to add Core Image filters. The options are CIDroste (above) which imitates an Escher drawing, CICMYKHalftone which gives a four colour halftone:
...and CIPixellate which makes the image look blocky - great for those hipster 8 bit games:
The implementation is pretty simple. I have an extended segmented control that returns a value of type optional CIFilter:
var value: CIFilter?
{
let returnFilter: CIFilter?
switchsegmentedControl.selectedSegmentIndex
{
case0:
returnFilter = CIFilter(name: "CIDroste")!
case1:
returnFilter = CIFilter(name: "CICMYKHalftone", withInputParameters: ["inputWidth": 20])!
case2:
returnFilter = CIFilter(name: "CIPixellate", withInputParameters: [kCIInputScaleKey: 30])!
default:
returnFilter = nil
}
return returnFilter
}
...and my Scene Kit widget viewer uses that to populate its sphere node's filter array:
var filter: CIFilter?
{
didSet
{
iflet filter = filter
{
sphereNode.filters = [filter]
}
else
{
sphereNode.filters = nil
}
}
}
The source code for this demo lives here. Enjoy!
Addendum
Of course, as any any fule kno, the filters property was added to SCNNode in iOS 8. What's changed in iOS 9 is that filters is now properly typed to CIFilter. To remedy this schoolboy error, I've reengineered the layout code to use UIStackView. What's more, the layout is adaptive, so that in portrait it looks like:
....and in landscape, it looks like:
Cheers!