在Swift Playground中使用SceneKit

use*_*848 27 scenekit swift

我到处寻找这个但是我空白了.你如何复制Chris Lattner在WWDC上与Playgrounds和SceneKit展示的内容?我希望在Playgrounds中有一个SceneKit场景,动画.

我尝试从SceneKit项目模板中剪切并粘贴设置代码,认为它会神奇地开始渲染,但事实并非如此.

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

Dav*_*ist 51

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


XCPlayground框架是你需要什么,它记录在这里.

这是一个非常简单的场景,可以帮助您开始使用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, 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.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")
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述


请注意,要在iOS操场中运行Scene Kit ,您需要选中"在完全模拟器中运行"复选框.

在此输入图像描述

您可以在Utilities窗格中找到Playground Setting(0隐藏或显示)

  • 谢谢David,让它运转起来.还发现主要技巧是点击"显示辅助编辑器"图标(西装+领结的图标). (2认同)

Dhi*_*pta 7

为了让操场上以iOS为目标,并使用最新的Xcode 8.1,我得到了DavidRönnqvist原始代码的以下修改.

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")
Run Code Online (Sandbox Code Playgroud)

你必须做的主要事情是:

  • 指派到操场上liveView,
  • 还打开Xcode的助手编辑器(工具栏上的两个相交的圆圈图标)


小智 5

扩大Moshe的反应.

如果该键盘组合不适合您,请尝试转到菜单栏并选择"视图">"助理编辑器">"显示助手".