Advertisement
Guest User

Octahedron

a guest
Apr 27th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.59 KB | None | 0 0
  1. import SceneKit
  2.  
  3. class GameViewController: NSViewController {
  4.    
  5.     override func viewDidLoad() {
  6.         super.viewDidLoad()
  7.        
  8.         // новая сцена
  9.         let scene = SCNScene()
  10.        
  11.         // создаём и добавляем камеру
  12.         let 🎥 = SCNNode()
  13.         🎥.camera = SCNCamera()
  14.         scene.rootNode.addChildNode(🎥)
  15.        
  16.         // располагаем камеру
  17.         🎥.position = SCNVector3(x: 0, y: 0, z: 15)
  18.        
  19.         // устанавливаем свет на сцене
  20.         let lightNode0 = SCNNode()
  21.         lightNode0.light = SCNLight()
  22.         lightNode0.light!.type = .omni
  23.         lightNode0.position = SCNVector3(x: 0, y: 10, z: 10)
  24.         scene.rootNode.addChildNode(lightNode0)
  25.        
  26.         let lightNode1 = SCNNode()
  27.         lightNode1.light = SCNLight()
  28.         lightNode1.light!.type = .omni
  29.         lightNode1.position = SCNVector3(5, -10, 0)
  30.         scene.rootNode.addChildNode(lightNode1)
  31.    
  32.         let scnView = self.view as! SCNView
  33.         scnView.scene = scene
  34.        
  35.         // позволяем пользователю управлять камерой
  36.         scnView.allowsCameraControl = true
  37.        
  38.         // Настраиваем цвета фона
  39.         scnView.backgroundColor = NSColor.black
  40.        
  41.         createOctahedron()
  42.     }
  43.    
  44.     func createOctahedron() {
  45.         let vertices: [SCNVector3] = [
  46.             SCNVector3(0, 1, 0),
  47.             SCNVector3(-0.5, 0, 0.5),
  48.             SCNVector3(0.5, 0, 0.5),
  49.             SCNVector3(0.5, 0, -0.5),
  50.             SCNVector3(-0.5, 0, -0.5),
  51.             SCNVector3(0, -1, 0)
  52.         ]
  53.        
  54.         let source = SCNGeometrySource(vertices: vertices)
  55.        
  56.         let indices: [UInt16] = [
  57.             0, 1, 2,
  58.             2, 3, 0,
  59.             3, 4, 0,
  60.             4, 1, 0,
  61.             1, 5, 2,
  62.             2, 5, 3,
  63.             3, 5, 4,
  64.             4, 5, 1
  65.         ]
  66.        
  67.         let element = SCNGeometryElement(indices: indices, primitiveType: .triangles)
  68.         let geometry = SCNGeometry(sources: [source], elements: [element])
  69.         let node = SCNNode(geometry: geometry)
  70.  
  71.         node.geometry?.firstMaterial?.diffuse.contents = NSColor.green // назначаем цвет октаэдру
  72.        
  73.         let scnView = self.view as! SCNView
  74.         scnView.scene?.rootNode.addChildNode(node)
  75.        
  76.         let rotateAction = SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: .pi, z: 0, duration: 5))
  77.         node.runAction(rotateAction)
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement