我到处寻找这个但是我空白了。你如何复制Chris Lattner在WWDC上与Playgrounds和SceneKit展示的内容?我希望在Playgrounds中有一个SceneKit场景,动画。
我尝试从SceneKit项目模板中剪切并粘贴设置代码,认为它会神奇地开始渲染,但事实并非如此。
我尝试观看主题演讲,暂停和放大Lattner的屏幕,寻找源代码的提示,但他似乎是从他项目的其他地方导入他的所有代码,所以它没有给我任何线索。文档中似乎没有任何内容,或者我错过了它。
答案 0 :(得分:49)
由于Swift在版本之间没有源代码兼容性,因此本答案中的代码可能无法在将来或以前版本的Swift中使用。目前已更新为使用Swift 2.0在Xcode 7.0 Playgrounds中工作。
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, 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")
当我运行它时,它看起来像这样:
请注意,要在 iOS 游乐场中运行Scene Kit,您需要选中“在完整模拟器中运行”复选框。
您可以在实用程序窗格中找到Playground设置(⌥ ⌘ 0 隐藏或显示)
答案 1 :(得分:6)
为了让操场上以iOS为目标,并使用最新的Xcode 8.1,我得到了DavidRönnqvist原始代码的以下修改。
Tot Seq
1 2
2 2
3 4
4 3
5 7
6 8
7 9
8 4
9 2
10 1
11 2
12 1
您必须做的主要事情是:
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")
和答案 2 :(得分:5)
扩大Moshe的回应。
如果该键盘组合不适合您,请尝试转到菜单栏并选择查看>助理编辑>显示助理。
答案 3 :(得分:1)
如果操场上有抱怨,那么int不能兑换成CGFloat'然后你可以使用这行代码:
spin.toValue = NSValue(SCNVector4: SCNVector4(x: 1, y: 1, z: 0, w: CGFloat(2.0*M_PI)))
似乎没有在swift中定义隐式类型转换。
答案 4 :(得分:0)
在Xcode 10.2中,有一个PlaygroundSupport
框架。它共享游乐场数据,管理实时视图并控制游乐场的执行。
import PlaygroundSupport
您可以在操场上使用Playground Support来进行以下操作:
您还可以使用
PlaygroundSupport
显示和关闭实时视图,实时视图显示在操场上运行代码的结果。您可以利用许多现有类型上可用的内置实时视图表示形式,为自己的类型创建实时视图。 Xcode的游乐场和Swift游乐场都提供传统的实时取景。它们的运行方式与操场上的代码相同,因此您可以照常访问它们的属性和方法。但是,每次您运行游乐场时,它们都会重置。 Swift Livegrounds中始终在线的实时视图是在将LiveView.swift添加到页面时激活的,并以其自己的过程执行,因此您可以在连续运行之间保留信息和视觉效果。直到离开页面,始终在线的实时视图才会重置。
我稍微修改了代码:
import PlaygroundSupport
import UIKit
import SceneKit
import QuartzCore
var sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 1000, height: 200))
var scene = SCNScene()
sceneView.scene = scene
sceneView.backgroundColor = .black
PlaygroundPage.current.liveView = sceneView
var lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light?.type = .directional
lightNode.light?.intensity = 3000
lightNode.light?.shadowMode = .deferred
lightNode.rotation = SCNVector4(x: 0, y: 0, z: 0.5, w: 1.5 * Float.pi)
scene.rootNode.addChildNode(lightNode)
var cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(x: 2.5, y: 0, z: 5)
scene.rootNode.addChildNode(cameraNode)
var box = SCNBox(width: 3, height: 3, length: 3, chamferRadius: 0.4)
var boxNode = SCNNode(geometry: box)
scene.rootNode.addChildNode(boxNode)
box.firstMaterial?.diffuse.contents = UIColor.blue
box.firstMaterial?.specular.contents = UIColor.purple
boxNode.rotation = SCNVector4(x: 1.0, y: 1.0, z: 0.0, w: 0.0)
boxNode.scale = SCNVector3(x: 1.0, y: 1.0, z: 1.0)
var spin = CABasicAnimation(keyPath: "rotation.w")
var scale = CABasicAnimation(keyPath: "scale.x")
spin.toValue = 3 * -CGFloat.pi
spin.duration = 2
spin.repeatCount = .greatestFiniteMagnitude
scale.toValue = 1.5
scale.duration = 2
scale.repeatCount = .infinity
boxNode.addAnimation(spin, forKey: "spin around")
boxNode.addAnimation(scale, forKey: "scale x")