Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created September 1, 2015 13:21
Show Gist options
  • Save mzsima/6a850b01a109177d2961 to your computer and use it in GitHub Desktop.
Save mzsima/6a850b01a109177d2961 to your computer and use it in GitHub Desktop.
Shirts button different sides
//
// ViewController.swift
// ShirtsButtonDifferentSides
//
// Created by MizushimaYusuke on 9/1/15.
// Copyright (c) 2015 MizushimaYusuke. All rights reserved.
//
import UIKit
import SceneKit
class ViewController: UIViewController {
weak var sceneView : SCNView?
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
createShirts()
}
func setupScene() {
let sv = SCNView(frame: view.bounds)
sv.backgroundColor = UIColor.orangeColor()
sv.scene = SCNScene()
sv.autoenablesDefaultLighting = true
sv.allowsCameraControl = true
view.addSubview(sv)
sceneView = sv
}
func createShirts() {
let leftNum = Int(arc4random() % 32)
for i in 0..<32 {
let x = Float(i % 4) * 3.0
let y = Float(i / 4) * 3.0
let shirt = createShirtsRightButton(i == leftNum, color: UIColor.greenColor())
shirt.position = SCNVector3(x: x, y: y, z: 0)
}
}
func createShirtsRightButton(isRight:Bool, color:UIColor) -> SCNNode {
let shirt = SCNNode()
shirt.name = isRight ? "R" : "L"
let s : CGFloat = 1
let trapezoid = UIBezierPath()
trapezoid.moveToPoint(CGPoint(x: -s * 0.8, y: s))
trapezoid.addLineToPoint(CGPoint(x: s * 0.8, y: s))
trapezoid.addLineToPoint(CGPoint(x: s, y: -s))
trapezoid.addLineToPoint(CGPoint(x: -s, y: -s))
trapezoid.closePath()
let body = SCNShape(path: trapezoid, extrusionDepth: s)
body.firstMaterial?.diffuse.contents = color
let bodyNode = SCNNode(geometry: body)
shirt.addChildNode(bodyNode)
let arm = SCNBox(width: 1.2, height: 0.5, length: 0.5, chamferRadius: 0.1)
arm.firstMaterial?.diffuse.contents = color
for i in 0...1 {
let armNode = SCNNode(geometry: arm)
armNode.name = "arm"
armNode.pivot = SCNMatrix4MakeTranslation(i==0 ? 0.5 : -0.5, 0, 0)
armNode.position = SCNVector3Make(i==0 ? -0.8 : 0.8, 0.7, 0)
armNode.rotation = SCNVector4(x: 0, y: 0, z: 1, w: Float(M_PI) * (i==0 ? 0.3 : -0.3))
shirt.addChildNode(armNode)
}
let trapezoid2 = UIBezierPath()
if isRight {
trapezoid2.moveToPoint(CGPoint(x: -s * 0.2, y: s))
trapezoid2.addLineToPoint(CGPoint(x: s * 0.8, y: s))
trapezoid2.addLineToPoint(CGPoint(x: s, y: -s))
trapezoid2.addLineToPoint(CGPoint(x: -s * 0.2, y: -s))
} else {
trapezoid2.moveToPoint(CGPoint(x: -s * 0.8, y: s))
trapezoid2.addLineToPoint(CGPoint(x: s * 0.2, y: s))
trapezoid2.addLineToPoint(CGPoint(x: s * 0.2, y: -s))
trapezoid2.addLineToPoint(CGPoint(x: -s, y: -s))
}
trapezoid2.closePath()
let cloth = SCNShape(path: trapezoid2, extrusionDepth: 0.15)
cloth.firstMaterial?.diffuse.contents = color
cloth.firstMaterial?.specular.contents = UIColor.yellowColor()
cloth.firstMaterial?.shininess = 1
let clothNode = SCNNode(geometry: cloth)
clothNode.pivot = SCNMatrix4MakeTranslation(isRight ? Float(s) * 0.9 : -Float(s) * 0.9 , 0, 0)
clothNode.position = SCNVector3Make(isRight ? Float(s) * 0.9 : -Float(s) * 0.9, 0, 0.51)
shirt.addChildNode(clothNode)
for i in 0...4 {
let b = SCNBox(width: 0.2, height: 0.2, length: 0.1, chamferRadius: 0.1)
b.firstMaterial?.diffuse.contents = UIColor.darkGrayColor()
let button = SCNNode(geometry: b)
button.position = SCNVector3(x: 0, y: Float(i) * 0.25 - Float(s) * 0.8, z: 0.6)
shirt.addChildNode(button)
}
sceneView?.scene?.rootNode.addChildNode(shirt)
return shirt
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
let p = touch.locationInView(self.sceneView)
if let hit = self.sceneView?.hitTest(p, options: [SCNHitTestSortResultsKey:true])?.first as? SCNHitTestResult {
if hit.node.parentNode!.name == "R" {
// clear!
hit.node.parentNode!.childNodes
.filter {($0 as! SCNNode).name == "arm"}
.foreach { (i, e) -> Void in
e.runAction(SCNAction.rotateByAngle(CGFloat(M_PI) * (i==0 ? 0.6 : -0.6), aroundAxis: SCNVector3(x: 0, y: 0, z: 1), duration: 0.3))}
}
}
}
}
}
extension Array {
func foreach(doit:(Int, T) -> Void) { for (i, e) in enumerate(self) {doit(i, e)} }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment