Guest User

Untitled

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