Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SceneKit in Swift Playground

Tags:

swift

scenekit

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.

like image 431
user1845848 Avatar asked Jun 09 '14 18:06

user1845848


People also ask

What is SceneKit used for?

SceneKit is a high-level 3D graphics framework that helps you create 3D animated scenes and effects in your apps.

Can I make an app in Swift playgrounds?

- 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!

How long does it take to complete swift playgrounds?

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.

Can you make a 3d game in Swift?

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.


2 Answers

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:

enter image description here


Note that to run Scene Kit in an iOS playground, you need to check the "Run in Full Simulator" checkbox.

enter image description here

You find the Playground Setting in the Utilities Pane (0 to hide or show)

like image 156
David Rönnqvist Avatar answered Sep 22 '22 07:09

David Rönnqvist


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:

  • to assign to the playground's liveView and,
  • also open up Xcode's Assistant Editor (The two intersecting circles icon on the toolbar)
like image 43
Dhiraj Gupta Avatar answered Sep 20 '22 07:09

Dhiraj Gupta