I've looked everywhere for this but I'm coming up blank. How do you replicate what Chris Lattner was demonstrating with Playgrounds and SceneKit at WWDC? I want to have a SceneKit scene, animating, in Playgrounds.
I tried cutting and pasting the setup code from the SceneKit project template, thinking it would magically start rendering, but it does not.
I tried watching the keynote and pausing and zooming on on Lattner's screen looking for hints at the source code, but he appeared to be importing all his code from elsewhere in his project, so it gave me no clues. There does not seem to be anything in the documentation, or I'm missing it.
SceneKit is a high-level 3D graphics framework that helps you create 3D animated scenes and effects in your apps.
- Swift Playgrounds has been a great tool for learning to code in Swift, and now you can take things a step further and build apps too!
As an aspiring app developer, you'll learn how Swift works with data and Swift playgrounds. You'll also adopt a protocol-oriented programming mindset as you work on developing apps on iPhone, Mac, or iPad. It takes about three to four learning hours to complete this course.
Swift is enough in order to write some simple 3d code. Apple has a framework called SceneKit that gives you easy tools to build some complex 3d scenes. Even Xcode has some tools to give you the ability to view the 3d scenes. This is the way to go if you want to start easy & learn cool things in the process.
Since Swift doesn't have source compatibility between versions, the code in this answer might not work in either future or previous versions of Swift. Currently is has been updated to work in Xcode 7.0 Playgrounds with Swift 2.0.
The XCPlayground
framework is what you need, and it is documented here.
Here is a very simple scene to get you started with Scene Kit in Swift:
import SceneKit import QuartzCore // for the basic animation import XCPlayground // for the live preview import PlaygroundSupport // create a scene view with an empty scene var sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 300, height: 300)) var scene = SCNScene() sceneView.scene = scene // start a live preview of that view PlaygroundPage.current.liveView = sceneView // default lighting sceneView.autoenablesDefaultLighting = true // a camera var cameraNode = SCNNode() cameraNode.camera = SCNCamera() cameraNode.position = SCNVector3(x: 0, y: 0, z: 3) scene.rootNode.addChildNode(cameraNode) // a geometry object var torus = SCNTorus(ringRadius: 1, pipeRadius: 0.35) var torusNode = SCNNode(geometry: torus) scene.rootNode.addChildNode(torusNode) // configure the geometry object torus.firstMaterial?.diffuse.contents = NSColor.red // (or UIColor on iOS) torus.firstMaterial?.specular.contents = NSColor.white // (or UIColor on iOS) // set a rotation axis (no angle) to be able to // use a nicer keypath below and avoid needing // to wrap it in an NSValue torusNode.rotation = SCNVector4(x: 1.0, y: 1.0, z: 0.0, w: 0.0) // animate the rotation of the torus var spin = CABasicAnimation(keyPath: "rotation.w") // only animate the angle spin.toValue = 2.0*Double.pi spin.duration = 3 spin.repeatCount = HUGE // for infinity torusNode.addAnimation(spin, forKey: "spin around")
When I run it, it looks like this:
Note that to run Scene Kit in an iOS playground, you need to check the "Run in Full Simulator" checkbox.
You find the Playground Setting in the Utilities Pane (⌥⌘0 to hide or show)
To get the playground running with iOS as target, and using the latest Xcode 8.1, I got it working with the following modifications to David Rönnqvist's original code.
import UIKit import SceneKit import QuartzCore // for the basic animation import PlaygroundSupport // create a scene view with an empty scene var sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 300, height: 300)) var scene = SCNScene() sceneView.scene = scene PlaygroundPage.current.liveView = sceneView // default lighting sceneView.autoenablesDefaultLighting = true // a camera var cameraNode = SCNNode() cameraNode.camera = SCNCamera() cameraNode.position = SCNVector3(x: 0, y: 0, z: 3) scene.rootNode.addChildNode(cameraNode) // a geometry object var torus = SCNTorus(ringRadius: 1, pipeRadius: 0.35) var torusNode = SCNNode(geometry: torus) scene.rootNode.addChildNode(torusNode) // configure the geometry object torus.firstMaterial?.diffuse.contents = UIColor.red torus.firstMaterial?.specular.contents = UIColor.white // set a rotation axis (no angle) to be able to // use a nicer keypath below and avoid needing // to wrap it in an NSValue torusNode.rotation = SCNVector4(x: 1.0, y: 1.0, z: 0.0, w: 0.0) // animate the rotation of the torus var spin = CABasicAnimation(keyPath: "rotation.w") // only animate the angle spin.toValue = 2.0*M_PI spin.duration = 3 spin.repeatCount = HUGE // for infinity torusNode.addAnimation(spin, forKey: "spin around")
The main things you have to do different are:
liveView
and,If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With