Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // BoxWalk
  4. //
  5. // Created by MizushimaYusuke on 2/6/16.
  6. // Copyright © 2016 MizushimaYusuke. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import SceneKit
  11.  
  12. class ViewController: UIViewController {
  13.  
  14. weak var sceneView : SCNView?
  15.  
  16. class Walk {
  17. var count = 0
  18. var positions = [SCNVector3(0, 0, 0), SCNVector3(x: -0.5, y: 0, z: 0.5), SCNVector3(x: -1.0, y: 0, z: 0), SCNVector3(x: -0.5, y: 0, z:-0.5)]
  19. var pivots = [SCNMatrix4MakeTranslation(0, -0.5, 0.5), SCNMatrix4MakeTranslation(-0.5, -0.5, 0), SCNMatrix4MakeTranslation(0, -0.5, -0.5), SCNMatrix4MakeTranslation(0.5, -0.5, 0)]
  20. var direction = [SCNVector3(x: 1, y: 0, z: 0), SCNVector3(x: 0, y: 0, z: 1), SCNVector3(x: -1, y: 0, z: 0), SCNVector3(x: 0, y: 0, z: -1)]
  21. func rotate(box : SCNNode) {
  22. box.runAction(SCNAction.rotateByAngle(CGFloat(M_PI) * 0.5, aroundAxis: direction[count], duration: 0.5), completionHandler: {
  23. self.count = ++self.count % 4
  24. box.transform = SCNMatrix4Identity
  25. box.pivot = self.pivots[self.count]
  26. box.position = self.positions[self.count]
  27. })
  28. }
  29. }
  30. var walk = Walk()
  31.  
  32. override func viewDidLoad() {
  33. super.viewDidLoad()
  34. setupScene()
  35. createBase()
  36. createBox()
  37. createButton()
  38. }
  39.  
  40. func setupScene() {
  41. let sv = SCNView(frame: view.bounds)
  42. sv.backgroundColor = UIColor.lightGrayColor()
  43. sv.scene = SCNScene()
  44. sv.autoenablesDefaultLighting = true
  45. sv.allowsCameraControl = true
  46. view.addSubview(sv)
  47. sceneView = sv
  48. }
  49.  
  50. func createBase() {
  51. let base = SCNBox(width: 10, height: 1, length: 10, chamferRadius: 0)
  52. base.firstMaterial?.diffuse.contents = UIColor.brownColor()
  53. let node = SCNNode(geometry: base)
  54. node.position = SCNVector3(0, -0.5, 0)
  55. sceneView?.scene?.rootNode.addChildNode(node)
  56. }
  57.  
  58. func createBox() {
  59. let box = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0)
  60. box.firstMaterial?.diffuse.contents = UIColor.yellowColor()
  61. let node = SCNNode(geometry: box)
  62. node.pivot = SCNMatrix4MakeTranslation(0, -0.5, 0.5)
  63. node.name = "box"
  64. sceneView?.scene?.rootNode.addChildNode(node)
  65. }
  66.  
  67. func createButton() {
  68. let btn = UIButton(type: .System)
  69. btn.setTitle("Turn", forState: .Normal)
  70. btn.titleLabel?.font = UIFont.systemFontOfSize(40)
  71. btn.sizeToFit()
  72. btn.center = CGPoint(x: CGRectGetMaxX(view.bounds) - 150, y: CGRectGetMidY(view.bounds) - 150)
  73. view.addSubview(btn)
  74. btn.addTarget(self, action: "turn", forControlEvents: .TouchUpInside)
  75. }
  76.  
  77. func turn() {
  78. if let box = sceneView?.scene?.rootNode.childNodeWithName("box", recursively: false) {
  79. walk.rotate(box)
  80. }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement