Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SceneKit
- class GameViewController: NSViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- // новая сцена
- let scene = SCNScene()
- // создаём и добавляем камеру
- let 🎥 = SCNNode()
- 🎥.camera = SCNCamera()
- scene.rootNode.addChildNode(🎥)
- // располагаем камеру
- 🎥.position = SCNVector3(x: 0, y: 0, z: 15)
- // устанавливаем свет на сцене
- let lightNode0 = SCNNode()
- lightNode0.light = SCNLight()
- lightNode0.light!.type = .omni
- lightNode0.position = SCNVector3(x: 0, y: 10, z: 10)
- scene.rootNode.addChildNode(lightNode0)
- let lightNode1 = SCNNode()
- lightNode1.light = SCNLight()
- lightNode1.light!.type = .omni
- lightNode1.position = SCNVector3(5, -10, 0)
- scene.rootNode.addChildNode(lightNode1)
- let scnView = self.view as! SCNView
- scnView.scene = scene
- // позволяем пользователю управлять камерой
- scnView.allowsCameraControl = true
- // Настраиваем цвета фона
- scnView.backgroundColor = NSColor.black
- createOctahedron()
- }
- func createOctahedron() {
- let vertices: [SCNVector3] = [
- SCNVector3(0, 1, 0),
- SCNVector3(-0.5, 0, 0.5),
- SCNVector3(0.5, 0, 0.5),
- SCNVector3(0.5, 0, -0.5),
- SCNVector3(-0.5, 0, -0.5),
- SCNVector3(0, -1, 0)
- ]
- let source = SCNGeometrySource(vertices: vertices)
- let indices: [UInt16] = [
- 0, 1, 2,
- 2, 3, 0,
- 3, 4, 0,
- 4, 1, 0,
- 1, 5, 2,
- 2, 5, 3,
- 3, 5, 4,
- 4, 5, 1
- ]
- let element = SCNGeometryElement(indices: indices, primitiveType: .triangles)
- let geometry = SCNGeometry(sources: [source], elements: [element])
- let node = SCNNode(geometry: geometry)
- node.geometry?.firstMaterial?.diffuse.contents = NSColor.green // назначаем цвет октаэдру
- let scnView = self.view as! SCNView
- scnView.scene?.rootNode.addChildNode(node)
- let rotateAction = SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: .pi, z: 0, duration: 5))
- node.runAction(rotateAction)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement