Guest User

Untitled

a guest
Apr 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. import UIKit
  2. import ARKit
  3.  
  4. class SimpleViewController: UIViewController {
  5.  
  6. @IBOutlet var sceneView: ARSCNView!
  7.  
  8. override func viewDidLoad() {
  9. super.viewDidLoad()
  10. sceneView.scene = SCNScene(named: "duck.scn", inDirectory: "models.scnassets/furniture")!
  11. }
  12.  
  13. override func viewWillAppear(_ animated: Bool) {
  14. super.viewWillAppear(animated)
  15. sceneView.session.run(ARWorldTrackingConfiguration())
  16. }
  17.  
  18. override func viewWillDisappear(_ animated: Bool) {
  19. super.viewWillDisappear(animated)
  20. sceneView.session.pause()
  21. }
  22. }
  23.  
  24. /// Scales An SCNNode
  25. ///
  26. /// - Parameter gesture: UIPinchGestureRecognizer
  27. @objc func scaleObject(gesture: UIPinchGestureRecognizer) {
  28.  
  29. guard let nodeToScale = currentNode else { return }
  30. if gesture.state == .changed {
  31.  
  32. let pinchScaleX: CGFloat = gesture.scale * CGFloat((nodeToScale.scale.x))
  33. let pinchScaleY: CGFloat = gesture.scale * CGFloat((nodeToScale.scale.y))
  34. let pinchScaleZ: CGFloat = gesture.scale * CGFloat((nodeToScale.scale.z))
  35. nodeToScale.scale = SCNVector3Make(Float(pinchScaleX), Float(pinchScaleY), Float(pinchScaleZ))
  36. gesture.scale = 1
  37.  
  38. }
  39. if gesture.state == .ended { }
  40.  
  41. }
  42.  
  43. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
  44.  
  45. //1. Get The Current Touch Point
  46. guard let currentTouchPoint = touches.first?.location(in: self.augmentedRealityView),
  47. //2. Get The Next Feature Point Etc
  48. let hitTest = augmentedRealityView.hitTest(currentTouchPoint, types: .existingPlane).first else { return }
  49.  
  50. //3. Convert To World Coordinates
  51. let worldTransform = hitTest.worldTransform
  52.  
  53. //4. Set The New Position
  54. let newPosition = SCNVector3(worldTransform.columns.3.x, worldTransform.columns.3.y, worldTransform.columns.3.z)
  55.  
  56. //5. Apply To The Node
  57. currentNode.simdPosition = float3(newPosition.x, newPosition.y, newPosition.z)
  58.  
  59. }
  60.  
  61. //Store The Rotation Of The CurrentNode
  62. var currentAngleY: Float = 0.0
  63.  
  64. /// Rotates An SCNNode Around It's YAxis
  65. ///
  66. /// - Parameter gesture: UIRotationGestureRecognizer
  67. @objc func rotateNode(_ gesture: UIRotationGestureRecognizer){
  68.  
  69. //1. Get The Current Rotation From The Gesture
  70. let rotation = Float(gesture.rotation)
  71.  
  72. //2. If The Gesture State Has Changed Set The Nodes EulerAngles.y
  73. if gesture.state == .changed{
  74.  
  75. currentNode.eulerAngles.y = currentAngleY + rotation
  76. }
  77.  
  78. //3. If The Gesture Has Ended Store The Last Angle Of The Cube
  79. if(gesture.state == .ended) {
  80. currentAngleY = currentNode.eulerAngles.y
  81.  
  82. }
  83. }
Add Comment
Please, Sign In to add comment