Monday, September 1, 2014

SceneKit Tutorial Part 2 - Camera and Lights

In Part 1 of this Tutorial, I've created a simple box with yellow light shining on it. In this part, I will introduce SCNCamera which is another important class in SceneKit. And I will also dig into SCNLight and show you the 4 different types of lights in SceneKit. The project to demo the samples in this part of tutorial could be found at here.

As you can read from SceneKit Framework Reference ,  the default camera is orthogonal to the scene plain. Like a real camera, we also want to put it to a point in the 3D space and set its orientation. The position and orientation of a camera is controlled by the SCNNode which contains the camera. Also we also care if the camera has a broad field to view a wider space in the scene. The camera's field is defined by two properties of SCNCamera: xFov and yFov (Fov is short for Field of view). If you give xFov and yFov a bigger value, you will get a broader view to the scene.

Here is the code to create a camera and put it into a scene:

If you've created a box and put it in the center of the scene and enabled the default light like this:

You will see the box from another point of view instead in the front of box:


Notice that the orientation property of a SCNNode is defined by a quaternion.  If you feel complex to calculate the number or confused on how to get the 4 numbers for orientation, a property from SCNView can help you when you set allow camera control to true:

myView?.allowsCameraControl = true

Then you can drag and drop the scene to control the camera, or even zoom in and zoom out the view for the camera. If you want to get the camera's properties like position and orientation when you controlling the camera, you can customize the touchedEnded delegate in view controller:

When you feel the view looks what you expected, you can release the touch and see the position and orientation of the camera in console, and you can use the positions and orientations shows in console in your code. So it is easier than calculating the values by yourself.

Next, I will add different types of lights to the box to demonstrate the light effects in SceneKit.

There are 4 types of light. The first type of light is omni(omnidirectional light) and type is SCNLightTypeOmni, which presents a point light, a light of this type illuminates all directions from a single point. I've used this type of light in Part 1.  Because lights are tied with nodes, this type of light just care about position of the node, the orientation is ignored. Here is the snippet of code to create an omni light:

The second type of light is SCNLightTypeAmbient, this type of light illuminates from all directions with equal effect. So the orientation and position of its node are ignored. Here is the snippet to create an ambient light:

The third type of light is SCNLightTypeDirectional, this type of light illuminates from one direction. So the position of its node is ignored. The orientation of the node defines the light comes from which direction. Here is the snippet to create a directional light:

The fourth type of light is SCNLightTypeSpot, this type of light illuminates a cone-shaped area. The orientation and the position of the light are defined by its node's properties:

Now here are the snapshots of the results of the 4 types of lights using above snippets:
Omni Light
Ambient Light(with default light)
Spot Light
Directional Light

Now I've done my demo in this part, you may now have intuitive feelings on camera and light in SceneKit. But this is just a small set of features offered by SceneKit. You may need more time to investigate other features by yourself.

In next part of this tutorial, I will talk about the materials in SceneKit.

2 comments:

  1. Omni is really among the best brands when it comes to lighting solution. And we are very product to be among its manufacturers in the philippines. LSG Omni Products

    ReplyDelete