在Swift Playground中使用SceneKit

前端之家收集整理的这篇文章主要介绍了在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:

  1. import Cocoa // (or UIKit for iOS)
  2. import SceneKit
  3. import QuartzCore // for the basic animation
  4. import XCPlayground // for the live preview
  5.  
  6. // create a scene view with an empty scene
  7. var sceneView = SCNView(frame: CGRect(x: 0,y: 0,width: 300,height: 300))
  8. var scene = SCNScene()
  9. sceneView.scene = scene
  10.  
  11. // start a live preview of that view
  12. XCPShowView("The Scene View",view: sceneView)
  13.  
  14. // default lighting
  15. sceneView.autoenablesDefaultLighting = true
  16.  
  17. // a camera
  18. var cameraNode = SCNNode()
  19. cameraNode.camera = SCNCamera()
  20. cameraNode.position = SCNVector3(x: 0,z: 3)
  21. scene.rootNode.addChildNode(cameraNode)
  22.  
  23. // a geometry object
  24. var torus = SCNTorus(ringRadius: 1,pipeRadius: 0.35)
  25. var torusNode = SCNNode(geometry: torus)
  26. scene.rootNode.addChildNode(torusNode)
  27.  
  28. // configure the geometry object
  29. torus.firstMaterial?.diffuse.contents = NSColor.redColor() // (or UIColor on iOS)
  30. torus.firstMaterial?.specular.contents = NSColor.whiteColor() // (or UIColor on iOS)
  31.  
  32. // set a rotation axis (no angle) to be able to
  33. // use a nicer keypath below and avoid needing
  34. // to wrap it in an NSValue
  35. torusNode.rotation = SCNVector4(x: 1.0,y: 1.0,z: 0.0,w: 0.0)
  36.  
  37. // animate the rotation of the torus
  38. var spin = CABasicAnimation(keyPath: "rotation.w") // only animate the angle
  39. spin.toValue = 2.0*M_PI
  40. spin.duration = 3
  41. spin.repeatCount = HUGE // for infinity
  42. torusNode.addAnimation(spin,forKey: "spin around")

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

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

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

猜你在找的Swift相关文章