在Swift Playground中使用SceneKit

我已经看到无处不在,但我来到空白。如何复制Chris Lattner在WWDC的Playgrounds和SceneKit上展示的内容?我想在Playgrounds中有一个SceneKit场景,动画。

我试图从SceneKit项目模板中剪切和粘贴设置代码,认为它会神奇地开始渲染,但是它并没有。

我尝试观看主题演讲,暂停和放大Lattner的屏幕,寻找源代码提示,但他似乎正在从他的项目的其他地方导入他的所有代码,所以它给了我没有线索。文档中似乎没有什么,或者我错过了。

由于Swift在版本之间没有源兼容性,因此此回答中的代码在以后的版本或以前版本的Swift中可能无效。目前已经更新到使用Swift 2.0的Xcode 7.0游乐场。

XCPlayground框架是您需要的,而且是it is documented here

这是一个非常简单的场景,让您从Swift开始使用Scene Kit:

import Cocoa        // (or UIKit for iOS)
import SceneKit
import QuartzCore   // for the basic animation
import XCPlayground // for the live preview

// 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
XcpshowView("The Scene View",view: sceneView)

// default lighting
sceneView.autoenablesDefaultLighting = true

// a camera
var cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(x: 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.redColor()   // (or UIColor on iOS)
torus.firstMaterial?.specular.contents = NSColor.whiteColor() // (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*M_PI
spin.duration = 3
spin.repeatCount = HUGE // for infinity
torusNode.addAnimation(spin,forKey: "spin around")

当我运行它,它看起来像这样:

请注意,要在iOS操作系统中运行“场景套件”,您需要检查“运行在全模拟器”复选框。

您可以在实用程序窗格中找到游乐场设置(⌥⌘0隐藏或显示)

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...