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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let myCamera = SCNCamera() | |
myCamera.xFov = 40 | |
myCamera.yFov = 40 | |
let myCameraNode = SCNNode() | |
myCameraNode.camera = myCamera | |
myCameraNode.position = SCNVector3(x: -25, y: 20, z: 30) | |
myCameraNode.orientation = SCNQuaternion(x: -0.26, y: -0.32, z: 0, w: 0.91) | |
myScene.rootNode.addChildNode(myCameraNode) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Enable Default Light | |
// | |
myView?.autoenablesDefaultLighting = true | |
// | |
// Create a box and put it at the center of the scene | |
// | |
let myBox = SCNBox(width: 15, height: 10, length: 12, chamferRadius: 0) | |
let myBoxNode = SCNNode(geometry: myBox) | |
myBoxNode.position = SCNVector3(x: 0, y: 0, z: 0) | |
myScene.rootNode.addChildNode(myBoxNode) |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
var myView : SCNView? | |
... | |
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) { | |
// | |
// In this delegate, you can get the positions and orientations of camera | |
// when setting allowsCameraControl to true. | |
// | |
let quaternion = myView?.pointOfView.orientation | |
let position = myView?.pointOfView.position | |
println("Orientation: (\(quaternion?.x),\(quaternion?.y),\(quaternion?.z),\(quaternion?.w)) Position: (\(position?.x),\(position?.y),\(position?.z)") | |
} | |
} |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let myOmniLight = SCNLight() | |
let myOmniLightNode = SCNNode() | |
myOmniLight.type = SCNLightTypeOmni | |
myOmniLight.color = UIColor.yellowColor() | |
myOmniLightNode.light = myOmniLight | |
myOmniLightNode.position = SCNVector3(x: -30, y: 30, z: 60) | |
myScene.rootNode.addChildNode(myOmniLightNode) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let myAmbientLight = SCNLight() | |
myAmbientLight.type = SCNLightTypeAmbient | |
myAmbientLight.color = UIColor.yellowColor() | |
let myAmbientLightNode = SCNNode() | |
myAmbientLightNode.light = myAmbientLight | |
myScene.rootNode.addChildNode(myAmbientLightNode) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let myDirectLight = SCNLight() | |
myDirectLight.type = SCNLightTypeDirectional | |
myDirectLight.color = UIColor.yellowColor() | |
let myDirectLightNode = SCNNode() | |
myDirectLightNode.light = myDirectLight | |
myDirectLightNode.orientation = SCNQuaternion(x: 0, y: 0, z: 1, w: 0) | |
myScene.rootNode.addChildNode(myDirectLightNode) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let mySpotLight = SCNLight() | |
mySpotLight.type = SCNLightTypeSpot | |
mySpotLight.color = UIColor.yellowColor() | |
let mySpotLightNode = SCNNode() | |
mySpotLightNode.light = mySpotLight | |
mySpotLightNode.position = SCNVector3(x: 0, y: 0, z: 20) | |
mySpotLightNode.orientation = SCNQuaternion(x: 0, y: 0, z: 1, w: 0.5) | |
myScene.rootNode.addChildNode(mySpotLightNode) |
|
| ||||
|
|
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.
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
ReplyDeletepiss off
Delete