在 Swift Playground 中使用 SceneKit [英] Using SceneKit in Swift Playground

查看:60
本文介绍了在 Swift Playground 中使用 SceneKit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处寻找这个,但我是空白的.你如何复制 Chris Lattner 在 WWDC 上用 Playgrounds 和 SceneKit 展示的内容?我想在 Playgrounds 中有一个 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.

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

I tried cutting and pasting the setup code from the SceneKit project template, thinking it would magically start rendering, but it does not.

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

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.

推荐答案

由于 Swift 没有版本之间的源代码兼容性,因此此答案中的代码可能不适用于未来或以前版本的 Swift.目前已更新为可在 Xcode 7.0 Playgrounds 和 Swift 2.0 中使用.

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.

XCPlayground 框架正是您所需要的,此处记录.

The XCPlayground framework is what you need, and it is documented here.

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

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:

请注意,要在 iOS 游乐场中运行 Scene Kit,您需要选中Run in Full Simulator".复选框.

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

您可以在实用程序窗格中找到 Playground Setting(0 以隐藏或显示)

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

这篇关于在 Swift Playground 中使用 SceneKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆