import code
[app.GameProjectTest] / GameProjectTest / GameViewController.swift
1 //
2 //  GameViewController.swift
3 //  GameProjectTest
4 //
5 //  Created by Alan Knowles on 29/3/2022.
6 //
7
8 import UIKit
9 import QuartzCore
10 import SceneKit
11
12
13
14
15  
16
17 class GameViewController: UIViewController {
18
19     override func viewDidLoad() {
20         super.viewDidLoad()
21         
22         // create a new scene
23         let scene = SCNScene(named: "art.scnassets/ship.scn")!
24         
25         // create and add a camera to the scene
26         let cameraNode = SCNNode()
27         cameraNode.camera = SCNCamera()
28         scene.rootNode.addChildNode(cameraNode)
29         
30         // place the camera
31         cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
32         
33         // create and add a light to the scene
34         let lightNode = SCNNode()
35         lightNode.light = SCNLight()
36         lightNode.light!.type = .omni
37         lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
38         scene.rootNode.addChildNode(lightNode)
39         
40         // create and add an ambient light to the scene
41         let ambientLightNode = SCNNode()
42         ambientLightNode.light = SCNLight()
43         ambientLightNode.light!.type = .ambient
44         ambientLightNode.light!.color = UIColor.darkGray
45         scene.rootNode.addChildNode(ambientLightNode)
46         
47         // retrieve the ship node
48         let ship = scene.rootNode.childNode(withName: "ship", recursively: true)!
49         
50         // animate the 3d object
51         ship.runAction(SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: 2, z: 0, duration: 1)))
52         
53         // retrieve the SCNView
54         let scnView = self.view as! SCNView
55         
56         // set the scene to the view
57         scnView.scene = scene
58         
59         // allows the user to manipulate the camera
60         scnView.allowsCameraControl = true
61         
62         // show statistics such as fps and timing information
63         scnView.showsStatistics = true
64         
65         // configure the view
66         scnView.backgroundColor = UIColor.black
67         
68         // add a tap gesture recognizer
69         let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
70         scnView.addGestureRecognizer(tapGesture)
71         scnView.isHidden = true
72          
73     }
74     
75     @objc
76     func handleTap(_ gestureRecognize: UIGestureRecognizer) {
77         // retrieve the SCNView
78         let scnView = self.view as! SCNView
79         
80         // check what nodes are tapped
81         let p = gestureRecognize.location(in: scnView)
82         let hitResults = scnView.hitTest(p, options: [:])
83         // check that we clicked on at least one object
84         if hitResults.count > 0 {
85             // retrieved the first clicked object
86             let result = hitResults[0]
87             
88             // get its material
89             let material = result.node.geometry!.firstMaterial!
90             
91             // highlight it
92             SCNTransaction.begin()
93             SCNTransaction.animationDuration = 0.5
94             
95             // on completion - unhighlight
96             SCNTransaction.completionBlock = {
97                 SCNTransaction.begin()
98                 SCNTransaction.animationDuration = 0.5
99                 
100                 material.emission.contents = UIColor.black
101                 
102                 SCNTransaction.commit()
103             }
104             
105             material.emission.contents = UIColor.red
106             
107             SCNTransaction.commit()
108         }
109     }
110     
111     override var shouldAutorotate: Bool {
112         return true
113     }
114     
115     override var prefersStatusBarHidden: Bool {
116         return true
117     }
118     
119     override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
120         if UIDevice.current.userInterfaceIdiom == .phone {
121             return .allButUpsideDown
122         } else {
123             return .all
124         }
125     }
126
127 }