Monday, August 21, 2017

SWIFT / Procedural SceneKit Test for ARKit

I have been studying the ARKit in iOS 11 for almost 2 months now since it is being announced at WWDC 2017. It has been quite an interesting experience for me. I am learning a lot about the iOS app development and Swift, especially in area like SceneKit usage for ARKit.

Swift language itself is very fun and expressive to use, I found a lot of interesting things that Swift can do inside XCode to make interactive app and also inside Swift Playground on iPad.

SCENEKIT WITH SWIFT

So far, with ARKit, I manage to bring in some 3D assets and animation from Blender into XCode, to view as AR in 3D. That is pretty fun in itself, but I wanted to do more, perhaps some kind of procedural 3D scene and some basic interactions.

I am thinking of collecting and writing some Swift codes that probably can help to generate 3D SceneKit asset on the fly in this blog.

Apart from XCode, I am also using open source Blender 3D and nodes add-on like Sverchok and Animation Nodes to help me with algorithms and logics, which I can convert to Swift bits by bits to generate a cool procedural SceneKit setup.

I could imagine that before 2020, I think the iPhone and iPad perhaps will have some kind of Node  Based tools and system to play with 3D and maybe AR.

Obviously for basic layout 3D Scene, the SceneKit editor inside XCode would be sufficient, but sometimes coding the scene and procedurally laying out 3D objects would be handy after all.

I think AR world is very powerful when treated like game environment. Familiarity with SceneKit, and SpriteKit is really a big bonus if you want to transfer the knowledge as ARKit environment.

SWIFT: PROCEDURAL GRID OF SPHERES

This is pretty much basic and classic task, many examples of coding start this way. To generate bunch of 3D objects in a grid XYZ, we simply use some kind of for loop, iterating based on range of number we specify.



I am testing it out like this:

CODE:
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Set the view's delegate
        sceneView.delegate = self
        
        // Show statistics such as fps and timing information
        sceneView.showsStatistics = false
        
        // Create a new scene
        let scene = SCNScene(named: "art.scnassets/ship.scn")!
        
        makeSphere(scene: scene, rangeX: 10, rangeY: 20)

        // Set the scene to the view
        sceneView.scene = scene
    }



The function to generate a grid of Sphere would be something like below:

func makeSphere(scene: SCNScene, rangeX: Int, rangeY: Int) {
    for i in 0...rangeX {
        for j in 0...rangeY {

            let sphere = SCNSphere(radius: 0.03)
            let sphereNode = SCNNode(geometry: sphere)
            
            sphereNode.position = SCNVector3(Double(i) * 0.1, Double(j) * 0.1, 0)
            
            // Parent sphere node into the rootNode of SCENE
            scene.rootNode.addChildNode(sphereNode)
        }
    }

}

The code above is a quick and dirty code, where I really does everything in one go as the viewDidLoad() being called. Perhaps we can make the code for the app nicer, not calling everything at the beginning. That I will think later.

Also, dealing with many objects, would be nice to always keep track of the nodes generated and put it inside array list or dictionary of some sort. 

For now, I really want to get a hang of being able to just procedurally generate bunch of spheres grid.



INTERACTION IN APP

What interesting about ARKit app is that we can actually let USER to interact and experience the 3D and also give them the ability to spawn and generate the 3D objects, as much as we design and give them controls to do so when the app is running.

For the basic level SceneKit creations, using Swift, we should be able to easily:
- Generate some primitives 3D objects as provided by Apple XCode
- Assign Material, adjust the materials
- Place the 3D objects into the main SCN 
- Add Camera
- Add Light
- Add Particles
- Etc

This is a common task when working in 3D software, like Blender, Maya, Houdini. But a little bit more like game production, while giving more controls to the user when they actually experience the app. I found this quite interesting.

I supposed I shall continue collecting some knowledge around this area for AR experience.

No comments:

Post a Comment