ARKit : Lighting and Materials Part I
Download an AR camera starter project here.
The following tutorial aims to cover SCNLight’s, how to add them and what basic attributes are and what they signify.
Recommended Reading
The following post is based on the following documentation:
Outline
ARKit creates lighting models from two sources:
- Default lighting (Standard SceneKit Lighting)
- SCNLight Objects
Therefore, any custom lighting you may want to add to your scene must be composed of one or more ‘SCNLight’ objects.
To add SCNLights to the world, you must first create them and then add them to a SCNNode — which will represent it’s position in space. The code looks something like this
var aLight:SCNLight = SCNLight();
// Set your SCNLight attributes herevar aLightNode:SCNNode = SCNNode();
aLightNode.light = alight;
aLightNode.position = SCNVector3.init(X,Y,Z);
// Any other things you may want to donode.addchild(aLightNode);
This light then interacts with you SCNMaterial to create dynamic, real-world lighting.
SCNLight Basic Attributes: Light Types
SCNLight’s carry the possibility to be one of many types. These are best demonstrated by the visual provided by Apple below.
So, in plain english, what do these types mean?
Ambient
A light that illuminates all objects in the scene from all directions.
Directional
A light source with a uniform direction and constant intensity.
Omni
An omnidirectional light, also known as a point light.
Spot
A light source that illuminates a cone-shaped area.
Here’s sample code as to how to set the type.
var aLight:SCNLight = SCNLight();
aLight.type = .omni // my favourite
Cool but how can I change the strength and color?
SCNLight Basic Attributes: Color
One of two ways to set the “color” of the light is via the color attribute, this takes in any UIColor and can be coded as below
var aLight:SCNLight = SCNLight();
aLight.color = UIColor.init(red:R, green:G, blue:B, alpha:1);
SCNLight Basic Attributes: Temperature
The other way that you can set the color of the light is via the temperature attribute, which is more representative of photography lighting and is measured in Kelvin.
var aLight:SCNLight = SCNLight();
aLight.temperature = 7800; // That Scandinavian blue
SCNLight Basic Attributes: Intensity
Finally, the way to set how strong the light is is via the intensity attribute. This is measured in lumens.
var aLight:SCNLight = SCNLight();
aLight.intensity = 100; // 100 lumens
That’s all for now.
Oscar