Advertisement
Guest User

Octahedron

a guest
May 4th, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.59 KB | None | 0 0
  1. import SceneKit
  2. import QuartzCore
  3.  
  4. class GameViewController: NSViewController {
  5.    
  6.     override func viewDidLoad() {
  7.         super.viewDidLoad()
  8.        
  9.         // cновая сцена
  10.         let scene = SCNScene()
  11.        
  12.         // создаём и добавляем камеру
  13.         let cameraNode = SCNNode()
  14.         cameraNode.camera = SCNCamera()
  15.         scene.rootNode.addChildNode(cameraNode)
  16.        
  17.         // располагаем камеру
  18.         cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
  19.        
  20.         // устанавливаем свет на сцене
  21.         let lightNode0 = SCNNode()
  22.         lightNode0.light = SCNLight()
  23.         lightNode0.light!.type = .omni
  24.         lightNode0.position = SCNVector3(x: 0, y: 10, z: 10)
  25.         scene.rootNode.addChildNode(lightNode0)
  26.        
  27.         let lightNode1 = SCNNode()
  28.         lightNode1.light = SCNLight()
  29.         lightNode1.light!.type = .omni
  30.         lightNode1.position = SCNVector3(5, -10, 0)
  31.         scene.rootNode.addChildNode(lightNode1)
  32.        
  33.  
  34.        
  35.        
  36.         let scnView = self.view as! SCNView
  37.        
  38.      
  39.         scnView.scene = scene
  40.        
  41.         // позволяем пользователю управлять камерой
  42.         scnView.allowsCameraControl = true
  43.        
  44.         // Настраиваем цвета фона
  45.         scnView.backgroundColor = NSColor.lightGray
  46.        
  47.         createOctahedron()
  48.     }
  49.    
  50.     func createOctahedron() {
  51.        
  52.         let vertices: [SCNVector3] = [
  53.             SCNVector3(0, 1, 0),
  54.             SCNVector3(-0.5, 0, 0.5),
  55.             SCNVector3(0.5, 0, 0.5),
  56.             SCNVector3(0.5, 0, -0.5),
  57.             SCNVector3(-0.5, 0, -0.5),
  58.             SCNVector3(0, -1, 0),
  59.            
  60.             SCNVector3(0, 1, 0),
  61.             SCNVector3(-0.5, 0, 0.5),
  62.             SCNVector3(0.5, 0, 0.5),
  63.             SCNVector3(0.5, 0, -0.5),
  64.             SCNVector3(-0.5, 0, -0.5),
  65.             SCNVector3(0, -1, 0),
  66.            
  67.             SCNVector3(0, 1, 0),
  68.             SCNVector3(-0.5, 0, 0.5),
  69.             SCNVector3(0.5, 0, 0.5),
  70.             SCNVector3(0.5, 0, -0.5),
  71.             SCNVector3(-0.5, 0, -0.5),
  72.             SCNVector3(0, -1, 0),
  73.            
  74.             SCNVector3(0, 1, 0),
  75.             SCNVector3(-0.5, 0, 0.5),
  76.             SCNVector3(0.5, 0, 0.5),
  77.             SCNVector3(0.5, 0, -0.5),
  78.             SCNVector3(-0.5, 0, -0.5),
  79.             SCNVector3(0, -1, 0)
  80.         ]
  81.        
  82.         let indices: [UInt16] = [
  83.             0, 1, 2,
  84.             2, 3, 0,
  85.             3, 4, 0,
  86.             4, 1, 0,
  87.             1, 5, 2,
  88.             2, 5, 3,
  89.             3, 5, 4,
  90.             4, 5, 1
  91.         ]
  92.        
  93.          let width:CGFloat = 1/8
  94.          let coordinates: [CGPoint] = [
  95.          CGPoint(x: 0,     y: 0),// BAC
  96.          CGPoint(x: width, y: 0),
  97.          CGPoint(x: width, y: 1),
  98.          CGPoint(x: 0,     y: 1),// BAE
  99.          CGPoint(x: width * 7, y: 0),
  100.          CGPoint(x: 1,         y: 0),
  101.          CGPoint(x: 1,         y: 1),// EDA
  102.          CGPoint(x: width * 7, y: 1),
  103.          CGPoint(x: width * 6, y: 0),
  104.          CGPoint(x: width * 7, y: 0),// DAC
  105.          CGPoint(x: width,     y: 0),
  106.          CGPoint(x: width * 4, y: 0),
  107.          CGPoint(x: width * 7, y: 1),// DFC
  108.          CGPoint(x: width * 6, y: 1),
  109.          CGPoint(x: width * 4, y: 1),
  110.          CGPoint(x: width,     y: 1),// BFC
  111.          CGPoint(x: width * 4, y: 0),
  112.          CGPoint(x: width * 5, y: 0),
  113.          CGPoint(x: width * 5, y: 1),// BFE
  114.          CGPoint(x: width * 6, y: 1),
  115.          CGPoint(x: width * 5, y: 0),
  116.          CGPoint(x: width * 6, y: 0),// EFD
  117.          CGPoint(x: width * 4, y: 1),
  118.          CGPoint(x: width * 5, y: 1),
  119.          ]
  120.        
  121.         let source = [
  122.             SCNGeometrySource(vertices: vertices),
  123.             SCNGeometrySource(textureCoordinates: coordinates)
  124.         ]
  125.         let element = SCNGeometryElement(indices: indices, primitiveType: .triangles)
  126.        
  127.         let octahedron = SCNGeometry(sources: source, elements: [element])
  128.  
  129.         let imageSim = NSImage(named: NSImage.Name("sims"))
  130.         octahedron.firstMaterial?.diffuse.contents = imageSim
  131.         octahedron.firstMaterial?.specular.contents = NSColor.green
  132.        
  133.         let node = SCNNode(geometry: octahedron)
  134.         let scnView:SCNView = self.view as! SCNView
  135.         scnView.scene!.rootNode.addChildNode(node)
  136.        
  137.         let rotateAction = SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: .pi, z: 0, duration: 5))
  138.         node.runAction(rotateAction)
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement