Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created September 8, 2015 14:57
Show Gist options
  • Save mzsima/0c9597f1f647aae87ee4 to your computer and use it in GitHub Desktop.
Save mzsima/0c9597f1f647aae87ee4 to your computer and use it in GitHub Desktop.
find diff color
//
// ViewController.m
// FindDiffColor
//
// Created by MizushimaYusuke on 9/8/15.
// Copyright (c) 2015 MizushimaYusuke. All rights reserved.
//
#import "ViewController.h"
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@property (nonatomic, weak) UISlider *levelSlider;
@property (nonatomic, strong) NSMutableArray *boxes;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createSlider];
[self createColorCubes];
[self createCamera];
}
- (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.scene = [SCNScene scene];
sv.allowsCameraControl = YES;
sv.backgroundColor = [UIColor blackColor];
[self.view addSubview:sv];
self.sceneView = sv;
}
- (void)createColorCubes {
float hue = (arc4random() % 10) * 0.1;
float d = (self.levelSlider.maximumValue - self.levelSlider.value) * 0.3;
UIColor *colorA = [UIColor colorWithHue:hue saturation:0.4 brightness:1 alpha:1];
UIColor *colorB = [UIColor colorWithHue:hue saturation:0.39 + d brightness:1 alpha:1];
self.boxes = [NSMutableArray array];
int rand = arc4random() % 25;
for (int i=0; i<25; i++) {
float x = (i % 5) - 2;
float y = (i / 5) - 2;
SCNBox *box = [SCNBox boxWithWidth:0.8 height:0.8 length:0.8 chamferRadius:0.1];
SCNNode *boxNode = [SCNNode nodeWithGeometry:box];
if (i == rand) {
box.firstMaterial.diffuse.contents = colorB;
boxNode.name = @"diff";
} else {
box.firstMaterial.diffuse.contents = colorA;
}
boxNode.position = SCNVector3Make(x, y, 0);
[self.sceneView.scene.rootNode addChildNode:boxNode];
[self.boxes addObject:boxNode];
}
}
- (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(0, 0, 8);
[self.sceneView.scene.rootNode addChildNode:camera];
}
- (void)createSlider {
UISlider *s = [[UISlider alloc] init];
s.minimumValue = 1;
s.maximumValue = 3;
s.center = CGPointMake(CGRectGetMaxX(self.view.bounds) - 80, CGRectGetMaxY(self.view.bounds) - 100);
[self.view addSubview:s];
self.levelSlider = s;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint p = [[touches anyObject] locationInView:self.sceneView];
SCNHitTestResult *hit = [self.sceneView hitTest:p options:@{SCNHitTestSortResultsKey : @YES}].firstObject;
if ([hit.node.name isEqual:@"diff"]) {
[self.boxes enumerateObjectsUsingBlock:^(SCNNode *box, NSUInteger idx, BOOL *stop) {
[box removeFromParentNode];
}];
[self createColorCubes];
}
}
@end
@mzsima
Copy link
Author

mzsima commented Sep 8, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment