Guest User

Untitled

a guest
Nov 21st, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // BambooCopter
  4. //
  5. // Created by MizushimaYusuke on 2017/11/20.
  6. // Copyright © 2017 MizushimaYusuke. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import SceneKit
  11. import ARKit
  12.  
  13. class ViewController: UIViewController, ARSCNViewDelegate {
  14.  
  15. @IBOutlet var sceneView: ARSCNView!
  16.  
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19.  
  20. // Set the view's delegate
  21. sceneView.delegate = self
  22.  
  23. // Show statistics such as fps and timing information
  24. sceneView.showsStatistics = true
  25.  
  26. // Create a new scene
  27. let scene = SCNScene()
  28.  
  29. // Set the scene to the view
  30. sceneView.scene = scene
  31. }
  32.  
  33. override func viewWillAppear(_ animated: Bool) {
  34. super.viewWillAppear(animated)
  35.  
  36. // Create a session configuration
  37. let configuration = ARWorldTrackingConfiguration()
  38.  
  39. // Run the view's session
  40. sceneView.session.run(configuration)
  41. }
  42.  
  43. override func viewWillDisappear(_ animated: Bool) {
  44. super.viewWillDisappear(animated)
  45.  
  46. // Pause the view's session
  47. sceneView.session.pause()
  48. }
  49. func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
  50.  
  51. renderer.autoenablesDefaultLighting = true
  52.  
  53. let fan = SCNBox(width: 0.04, height: 0.001, length: 0.005, chamferRadius: 0.0001)
  54. fan.firstMaterial?.diffuse.contents = UIColor.green
  55. let fanNode = SCNNode(geometry: fan)
  56. fanNode.position = SCNVector3(0, 0.02, 0)
  57.  
  58. let bar = SCNCylinder(radius: 0.0005, height: 0.04)
  59. bar.firstMaterial?.diffuse.contents = UIColor.brown
  60. let barNode = SCNNode(geometry: bar)
  61.  
  62. let copter = SCNNode()
  63. copter.addChildNode(fanNode)
  64. copter.addChildNode(barNode)
  65.  
  66. node.addChildNode(copter)
  67.  
  68. copter.runAction(.repeatForever(.rotateBy(x: 0, y: .pi, z: 0, duration: 0.25)))
  69. copter.runAction(.sequence([
  70. .moveBy(x: 0, y: 0.05, z: 0, duration: 1.0),
  71. .moveBy(x: 0, y: -0.05, z: 0, duration: 1.0),
  72. ]))
  73. }
  74.  
  75. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  76. if let p = touches.first?.location(in: sceneView) {
  77. if let hit = sceneView.hitTest(p, types: .featurePoint).first {
  78. sceneView.session.add(anchor: ARAnchor(transform: hit.worldTransform))
  79. }
  80. }
  81.  
  82. }
  83. }
Add Comment
Please, Sign In to add comment