iOS 8, Scene Kit @ 60FPS, programmed in F#, using Xamarin.iOS

I have the best job in the world:

namespace SceneKitFSharp

open System
open MonoTouch.UIKit
open MonoTouch.Foundation
open MonoTouch.SceneKit
open MonoTouch.CoreAnimation

type MySceneKitController () =
    inherit UIViewController()

    override this.ViewDidLoad () =
        let scene = new SCNScene ()

        //Positions everyone!
        let boxNode = new SCNNode ()
        boxNode.Geometry <- new SCNBox(
            Width = 1.0F, 
            Height = 1.0F, 
            Length = 1.0F, 
            ChamferRadius = 0.02f
        )
        scene.RootNode.AddChildNode (boxNode)

        let material = new SCNMaterial ()
        material.Diffuse.Contents <- UIImage.FromFile ("textureX.png")
        material.Specular.Contents <- UIColor.Gray
        material.LocksAmbientWithDiffuse <- true
        boxNode.Geometry.FirstMaterial <- material

        //Lights!
        let lightNode = new SCNNode()
        lightNode.Light <- new SCNLight ()
        lightNode.Light.LightType <- SCNLightType.Omni
        lightNode.Position <- new SCNVector3 (0.0F, 10.0F, 10.0F)
        scene.RootNode.AddChildNode (lightNode)

        let ambientLightNode = new SCNNode ()
        ambientLightNode.Light <- new SCNLight ()
        ambientLightNode.Light.LightType <- SCNLightType.Ambient
        ambientLightNode.Light.Color <- UIColor.DarkGray
        scene.RootNode.AddChildNode (ambientLightNode)

        //Camera!
        let cameraNode = new SCNNode ()
        cameraNode.Camera <- new SCNCamera ()
        scene.RootNode.AddChildNode (cameraNode)
        cameraNode.Position <- new SCNVector3 (0.0F, 0.0F, 3.0F)


        // Action!
        let animation = new CABasicAnimation(
            KeyPath = "rotation"
        )
        let t = new SCNVector4 (1.0F, 1.0F, 0.0F, float32 (Math.PI * 2.0))
        animation.To <- NSValue.FromVector (t)

        animation.Duration <- float 5.0F
        animation.RepeatCount <- float32 Double.MaxValue //repeat forever
        boxNode.AddAnimation(animation,new NSString("rotation"))

        let scnView = new SCNView(UIScreen.MainScreen.Bounds)
        scnView.Scene <- scene
        scnView.AllowsCameraControl <- true
        scnView.ShowsStatistics <- true
        scnView.BackgroundColor <- UIColor.Black

        this.View <- scnView
       

[<Register ("AppDelegate")>]
type AppDelegate () =
    inherit UIApplicationDelegate ()

    // This method is invoked when the application is ready to run.
    override this.FinishedLaunching (app, options) =
        let window = new UIWindow (UIScreen.MainScreen.Bounds)
        window.RootViewController <- new MySceneKitController()
        window.MakeKeyAndVisible ()
        true

module Main =
    [<EntryPoint>]
    let main args =
        UIApplication.Main (args, null, "AppDelegate")
        0

IMG_0272

photo

17 thoughts on “iOS 8, Scene Kit @ 60FPS, programmed in F#, using Xamarin.iOS

  1. Pingback: F# Weekly #24-#25, 2014 | Sergey Tihon's Blog

  2. I had a problem when running the code in the simulator. I think it’s GC related. When FinishedLaunching returns, the “window” value goes out of scope, the UIWindow get garbage collected and disappears. Adding an override of the Window property in the AppDelegate solved the problem:

    type AppDelegate () =
        inherit UIApplicationDelegate ()
     
        override val Window:UIWindow = null with get, set
    
        // This method is invoked when the application is ready to run.
        override this.FinishedLaunching (app, options) =
            this.Window &lt;- new UIWindow (UIScreen.MainScreen.Bounds)
            this.Window.RootViewController &lt;- new MySceneKitController()
            this.Window.MakeKeyAndVisible ()
            true
    

    PS: thanks for this great blog! Please, keep posting cool F# stuff!

Comments are closed.