A simple iOS 8 Scene Kit example using C# and Xamarin
using System; | |
using MonoTouch.UIKit; | |
using MonoTouch.SceneKit; | |
using MonoTouch.Foundation; | |
namespace HelloSceneKit | |
{ | |
public class HelloSceneKitController : UIViewController | |
{ | |
SCNView sceneView; | |
SCNScene scene; | |
SCNCamera camera; | |
SCNNode cameraNode; | |
SCNLight ambientLight; | |
SCNNode ambientLightNode; | |
SCNSphere sphere; | |
SCNNode sphereNode; | |
SCNMaterial material; | |
UIButton animateButton; | |
public override void ViewDidLoad () | |
{ | |
base.ViewDidLoad (); | |
animateButton = UIButton.FromType (UIButtonType.System); | |
animateButton.SetTitle ("Animate", UIControlState.Normal); | |
animateButton.Frame = new System.Drawing.RectangleF (0, 0, 100, 50); | |
animateButton.TouchUpInside += (sender, e) => { | |
SCNTransaction.Begin (); | |
SCNTransaction.AnimationDuration = 2.0; | |
sphereNode.Rotation = new SCNVector4 (0, 1, 0, (float)Math.PI * 4); | |
material.Shininess = 0.1f; | |
SCNTransaction.Commit (); | |
}; | |
scene = SCNScene.Create (); | |
sceneView = new SCNView (View.Frame); | |
sceneView.AutoresizingMask = UIViewAutoresizing.All; | |
sceneView.Scene = scene; | |
// camera | |
camera = new SCNCamera { | |
XFov = 40, | |
YFov = 40 | |
}; | |
cameraNode = new SCNNode { | |
Camera = camera, | |
Position = new SCNVector3 (0, 0, 40) | |
}; | |
scene.RootNode.AddChildNode (cameraNode); | |
// sphere | |
sphere = SCNSphere.Create (10.0f); | |
sphereNode = SCNNode.FromGeometry (sphere); | |
sphereNode.Position = new SCNVector3 (0, 0, 0); | |
scene.RootNode.AddChildNode (sphereNode); | |
// ambient light | |
ambientLight = SCNLight.Create (); | |
ambientLightNode = SCNNode.Create (); | |
ambientLight.LightType = SCNLightType.Ambient; | |
ambientLight.Color = UIColor.Purple; | |
ambientLightNode.Light = ambientLight; | |
scene.RootNode.AddChildNode (ambientLightNode); | |
// diffuse lights | |
scene.RootNode.AddChildNode (CreateDiffuseLightNode (UIColor.Blue, new SCNVector3 (-40, 40, 60), SCNLightType.Omni)); | |
scene.RootNode.AddChildNode (CreateDiffuseLightNode (UIColor.Yellow, new SCNVector3 (20, 20, -70), SCNLightType.Omni)); | |
scene.RootNode.AddChildNode (CreateDiffuseLightNode (UIColor.Red, new SCNVector3 (20, -20, 40), SCNLightType.Omni)); | |
scene.RootNode.AddChildNode (CreateDiffuseLightNode (UIColor.Green, new SCNVector3 (-20, -40, 70), SCNLightType.Omni)); | |
// material | |
material = SCNMaterial.Create (); | |
material.Diffuse.Contents = UIImage.FromFile ("monkey.png"); | |
material.Specular.Contents = UIColor.White; | |
sphere.Materials = new SCNMaterial[] { material }; | |
sceneView.AllowsCameraControl = true; | |
View.AddSubview (sceneView); | |
View.AddSubview (animateButton); | |
} | |
SCNNode CreateDiffuseLightNode (UIColor color, SCNVector3 position, NSString lightType) | |
{ | |
var light = SCNLight.Create (); | |
var lightNode = SCNNode.Create (); | |
light.LightType = lightType; | |
light.Color = color; | |
lightNode.Light = light; | |
lightNode.Position = position; | |
return lightNode; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment