Learn how to display objects on screen.
- [Instructor] With an understanding of how to position nodes using the coordinate system in SceneKit, it's time to learn how to display the objects on the screen. Before we get too technical, let's think for a moment about how movie scenes are created. If I was a movie director and wanted to get a shot of a character like this, with the right lens I might place my camera someplace like this. The properties of the camera, including the focal length of the lens, the size of the sensor, the zoom setting, the height and distance all determine how the shot actually appears and the same is true in SceneKit.
In order to display a scene, you designate a node whose camera property contains a camera object as the point of view. We use the SCN node object that contains the camera to define the orientation of the camera, or in other words, the point of view. A camera's direction of view is always along the negative Z axis of the node's local coordinate system. Of course, you can change the direction of view and point the camera at other parts of your scene using different node properties such as position, rotation, or transform.
Keep in mind that by default, a camera defines a perspective projection whose field of view or FOV, and the near and far visibility limits you control using various properties that you set as variables. You can also add depth of field and blur effects using variables like focal distance and focal size. These settings determine the visible depth of your camera and are also known as the viewing frustum. Keep in mind that a camera all by itself won't be of much use or do anything until it is part of the node hierarchy.
All right, let's go write some code and see how we do this in Xcode. Making sure that you're in the GameViewController.swift file of your project, we'll create an empty SCN node and assign it to a variable. To do this, we'll go to the top of our code and right under the variable for SCNScene we're going to type in var cameraNode: and we'll call the SCNNode class, SCNNode, and then add an exclamation mark.
Next, I'm going to go to the bottom of my code, right underneath setupScene, create a new method to set up the camera. To do that I'll type in func setupCamera(), and I'll type space, and then opening curly brace, press return, and Xcode will give me the closing curly brace. I'll then type in cameraNode = SCNNode() and I'll type in cameraNode.camera to assign a camera class SCNCamera().
Then we'll define the position of the camera by typing cameraNode.position = and then we'll use the SCNVector3 class, so SCNVector, use the vector three. Open parentheses, and now we're going to type in the coordinates, type in x: 0, y: 0, and then z: and I'm going to give it a 15.
The reason I picked 15 is I just wanted to give a little bit of depth to the camera. Finally, I'm going to type in scnScene.rootNode.addChildNode and we're going to attach that to the camera node. That last line of code there adds the camera node to the scene as a child of the scene's root node. Now, in order to get this to work, we need to call this method in viewDidLoad, so just under setupScene, here in viewDidLoad, I'm going to press return and type setupCamera.
Normally I would build a code and run it in the Xcode simulator, but I haven't added any objects to this scene to be displayed, so we won't see anything just yet.
- Setting up SceneKit
- The SceneKit coordinate system
- Using built-in view features
- Physics in SceneKit
- Spawning multiple objects and timers
- Optimizing performance
- Creating your first particle system
- Configuring and implementing a particle system
- Enabling touch