Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created August 25, 2015 13:19
Show Gist options
  • Save mzsima/e89ba92c05561650135b to your computer and use it in GitHub Desktop.
Save mzsima/e89ba92c05561650135b to your computer and use it in GitHub Desktop.
cube to sphere
//
// ViewController.m
// CubeSphere
//
// Created by MizushimaYusuke on 8/25/15.
// Copyright (c) 2015 MizushimaYusuke. All rights reserved.
//
#import "ViewController.h"
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createCube];
[self createCamera];
}
- (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [UIColor yellowColor];
sv.scene = [SCNScene scene];
sv.allowsCameraControl = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
- (void)createCube {
SCNBox *b = [SCNBox boxWithWidth:1 height:1 length:1 chamferRadius:0];
b.firstMaterial.diffuse.contents = [UIColor blackColor];
int size = 10;
for (int i=0; i<size; i++) {
for (int j=0; j<size; j++) {
for (int k=0; k<size; k++) {
SCNNode *n = [SCNNode nodeWithGeometry:b];
n.position = SCNVector3Make(i - size * 0.45, j - size * 0.5, k - size * 0.5);
[self.sceneView.scene.rootNode addChildNode:n];
}}}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.sceneView.scene.rootNode.childNodes enumerateObjectsUsingBlock:^(SCNNode *n, NSUInteger idx, BOOL *stop) {
float d = 1.0 - sqrt(pow(n.position.x, 2) + pow(n.position.y, 2) + pow(n.position.z, 2)) / 5;
d = MAX(d, 0.01);
[n runAction:[SCNAction scaleTo:d duration:5.0]];
}];
}
- (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(0, 0, 20);
camera.camera.zNear = 0;
[self.sceneView.scene.rootNode addChildNode:camera];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment