Created
August 13, 2015 13:04
-
-
Save mzsima/02f2eb29f6725f63be35 to your computer and use it in GitHub Desktop.
Japanese pattern yagasuri
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// yagasuri | |
// | |
// Created by MizushimaYusuke on 8/13/15. | |
// Copyright (c) 2015 MizushimaYusuke. All rights reserved. | |
// | |
#import "ViewController.h" | |
@import SceneKit; | |
@interface ViewController () | |
@property (nonatomic, weak) SCNView *sceneView; | |
@property (nonatomic, strong) NSMutableArray *patterns; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
[self setupScene]; | |
[self createJapanesePattern]; | |
[self createCamera]; | |
} | |
- (void)setupScene { | |
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds]; | |
sv.scene = [SCNScene scene]; | |
sv.backgroundColor = [self color:1]; | |
sv.allowsCameraControl = YES; | |
sv.autoenablesDefaultLighting = YES; | |
[self.view addSubview:sv]; | |
self.sceneView = sv; | |
} | |
- (void)createJapanesePattern { | |
self.patterns = [NSMutableArray array]; | |
for (int i=0; i<45; i++) { | |
float x = (i / 5) - 4; | |
float y = 3.6 * (i % 5); | |
UIColor *color = [self color: (i%2) ? 3 : 4]; | |
SCNNode *yagasuri = [self createYagasuri:color]; | |
yagasuri.position = SCNVector3Make(x, y, -50); | |
[self.patterns addObject:yagasuri]; | |
} | |
} | |
- (SCNNode *)createYagasuri:(UIColor *)color { | |
float h = 2; | |
float w = 0.5; | |
UIBezierPath *path = [UIBezierPath bezierPath]; | |
[path moveToPoint:CGPointMake(-w, h)]; | |
[path addLineToPoint:CGPointMake(-w, -h * 0.8)]; | |
[path addLineToPoint:CGPointMake(0, -h)]; | |
[path addLineToPoint:CGPointMake(0, h * 0.8)]; | |
[path closePath]; | |
SCNNode *yagasuri = [SCNNode node]; | |
[self.sceneView.scene.rootNode addChildNode:yagasuri]; | |
for (int i=0; i<2; i++) { | |
SCNShape *half = [SCNShape shapeWithPath:path extrusionDepth:0.01]; | |
half.firstMaterial.diffuse.contents = color; | |
SCNNode *halfNode = [SCNNode nodeWithGeometry:half]; | |
if (i==0) halfNode.rotation = SCNVector4Make(0, 1, 0, M_PI); | |
[yagasuri addChildNode:halfNode]; | |
} | |
return yagasuri; | |
} | |
- (void)createCamera { | |
SCNNode *camera = [SCNNode node]; | |
camera.camera = [SCNCamera camera]; | |
camera.camera.usesOrthographicProjection = YES; | |
camera.camera.orthographicScale = 10; | |
camera.camera.zNear = -50; | |
camera.camera.zFar = 100; | |
[self.sceneView.scene.rootNode addChildNode:camera]; | |
} | |
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { | |
[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(turn) userInfo:nil repeats:YES]; | |
} | |
- (void)turn { | |
SCNNode *yagasuri = [self.patterns lastObject]; | |
[self.patterns removeObject:yagasuri]; | |
[self.patterns insertObject:yagasuri atIndex:0]; | |
[yagasuri.childNodes enumerateObjectsUsingBlock:^(SCNNode *node, NSUInteger idx, BOOL *stop) { | |
[node runAction:[SCNAction sequence:@[[SCNAction waitForDuration:idx * 0.5], [SCNAction rotateByAngle:-M_PI aroundAxis:SCNVector3Make(0, 1, 0) duration:1.0]]]]; | |
}]; | |
} | |
#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[] = {0xFF5B55, 0x18090E, 0xE5AE3C, 0xFFFFFF, 0x618443}; | |
return ColorHex(code[i]); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment