If you are a newbie, this will help you to create your simple 3D application in swift. Now a days, as per statistics, swift has been popular than Objective C as it simple and easy to understand.

Before starting with the creation of new own 3D application, let’s get familiar with Swift. Those who are already known can directly be jumped to the section titled “Steps to create 3D application”.

What is Swift?
Swift is an auxiliary programming language developed by Apple Inc for iOS and OS X. Swift is raptness of many languages such as C, Objective C, Javascript, etc. Swift is built on the open-source LLVM compiler framework and it has been included in Xcode(IDE) since version 6.

For more details, please have a look at  https://developer.apple.com/swift/

Features / Advantages of Swift
It is more readable because of its clean syntax, which makes it easier to read, write and understand.

  1. The number of code lines on Swift is a lot fewer than for Objective-C.
  2. No need to purchase the license as it is Open Source.
  3. Less memory-consuming.
  4. It is faster.
  5. Multiple return values and Tuples.
  6. Structs which support extensions, methods, and protocols.
  7. Powerful error handling

Steps to create 3D Cube

  • Using Xcode create a “Single View Application” project and from “Object library” import a SCNView type object to view controller.

                     Fig 1. Adding SCNView

  • Import the SceneKit module to viewcontroller.swift and link previously imported SCNView to ViewController.swift.

                            Fig 2. Linking SCNView

  • Initiate instance of the scene.
  • Create cube from SCNBox of size 0.1*0.1*0.1. Set color to created box geometry.
  • From box geometry, create SCNNode and add it to a scene for rendering.
  • To allow user interaction, set allowsCameraControl to true.

Finally, build the solution, it will show red cube and you perform pan, rotate, zoom operations.

                                                                                           Fig 3. Red Color 3D Cube

Please refer the code for more details,

import UIKit
import SceneKit

class ViewController: UIViewController {
@IBOutlet weak var mySceneView: SCNView!
override func viewDidLoad() {
mySceneView.allowsCameraControl = true
//Create instance of scene
let scene = SCNScene()
mySceneView.scene = scene
//Add cube to scnview

override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

func AddCubeToScene()
//Create a box of size 0.1 * 0.1 * 0.1
let myBox = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
myBox.firstMaterial?.diffuse.contents = UIColor.red
myBox.firstMaterial?.isDoubleSided = true
//Create SCnnode of from geometry and specified its position
let cubeNode = SCNNode(geometry: myBox)
//Add created scnnode to scene

As SceneKit provides the rich set of API, there are many more CAD things for e.g. CAD File Viewer which could be done using Scene Kit.

If you enjoyed this post or need more information on 3D application with Swift, please contact us and do share with friends. Thank you!
For more blog, please visit us at https://www.prototechsolutions.com/blog/

Enjoy! We will be back with another blog in this series titled “Building a Simple AR kit Demo with iOS Scene Kit”. Keep following us on that.

Author: Saurabh Dhole
Contact us:
ProtoTech Solutions Pvt Ltd.