Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created August 19, 2015 13:49
Show Gist options
  • Save mzsima/f244575560d06ab85b06 to your computer and use it in GitHub Desktop.
Save mzsima/f244575560d06ab85b06 to your computer and use it in GitHub Desktop.
yosegi block
//
// ViewController.m
// yosegiBlock
//
// Created by MizushimaYusuke on 8/19/15.
// Copyright (c) 2015 MizushimaYusuke. All rights reserved.
//
#import "ViewController.h"
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@property (nonatomic) int count;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createBlocks];
[self createCamera];
}
- (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [self color:0];
sv.scene = [SCNScene scene];
sv.allowsCameraControl = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
- (void)createBlocks {
SCNNode* (^yosegi)(float) = ^SCNNode* (float s) {
SCNNode *yosegiBlock = [SCNNode node];
for (int i=0; i<16; i++) {
float x = (i % 4) * s - 1.5 * s;
float y = (i / 4) * s - 1.5 * s;
SCNBox *b = [SCNBox boxWithWidth:s*1.2 height:s*1.2 length:s chamferRadius:s * 0.14];
b.firstMaterial.diffuse.contents = [self color:((i / 4 + i % 4) % 2) ? 2 : 4];
SCNNode *bNode = [SCNNode nodeWithGeometry:b];
bNode.position = SCNVector3Make(x, y, 0);
[yosegiBlock addChildNode:bNode];
}
return yosegiBlock;
};
for (int i=0; i<3; i++) {
SCNNode *yosegiClone = yosegi(0.2);
yosegiClone.name = [NSString stringWithFormat:@"%d", i];
[self.sceneView.scene.rootNode addChildNode:yosegiClone];
}
}
- (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(0, 0, 5);
[self.sceneView.scene.rootNode addChildNode:camera];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
SCNNode *n0 = [self.sceneView.scene.rootNode childNodeWithName:@"0" recursively:false];
SCNNode *n1 = [self.sceneView.scene.rootNode childNodeWithName:@"1" recursively:false];
SCNNode *n2 = [self.sceneView.scene.rootNode childNodeWithName:@"2" recursively:false];
if (self.count == 0) {
[n0 runAction:[SCNAction moveTo:SCNVector3Make(0, 1.2, 0) duration:0.5]];
[n2 runAction:[SCNAction moveTo:SCNVector3Make(0, -1.2, 0) duration:0.5]];
}
if (self.count == 1) {
for (SCNNode *n in @[n0, n1, n2]) {
[n runAction:[SCNAction rotateByX:0 y:0 z:M_PI * 0.25 duration:0.5]];
}
}
if (self.count == 2) {
for (SCNNode *n in @[n0, n1, n2]) {
[n runAction:[SCNAction sequence:@[[SCNAction waitForDuration:[n.name intValue] * 0.4], [SCNAction repeatActionForever:[SCNAction rotateByAngle:M_PI aroundAxis:SCNVector3Make(0, 1, 0) duration:1.2]]]]];
}
}
self.count++;
}
#define ColorHex(rgb) [UIColor colorWithRed:((rgb & 0xFF0000) >> 16)/255.0 green:((rgb & 0xFF00) >> 8)/255.0 blue:(rgb & 0xFF)/255.0 alpha:1.0]
- (UIColor *)color:(int)i {
if (i > 4) return nil;
int code[] = {0x5E9977, 0xE9DBA6, 0xC9BD8A, 0xF25C05, 0x362B24};
return ColorHex(code[i]);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment