試しにSceneKit

iPhone SceneKitでpyramid

SceneKitが使えるようになったので、ためしにピラミッドを表示するiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController ()

@property (nonatomic, weak) SCNScene *scene;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    

    SCNScene *scene = [SCNScene scene];

    SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];

    sv.backgroundColor = [UIColor redColor];

    sv.scene = scene;

    [self.view addSubview:sv];

    

    SCNCamera *camera = [SCNCamera camera];

    camera.xFov = 40;

    camera.yFov = 40;

    SCNNode *cameraNode = [SCNNode node];

    cameraNode.camera = camera;

    cameraNode.position = SCNVector3Make(0, 0, 60);

    [scene.rootNode addChildNode:cameraNode];

    

    SCNPyramid *pyramid = [SCNPyramid pyramidWithWidth:10 height:10 length:10];

    SCNNode *pNode = [SCNNode nodeWithGeometry:pyramid];

    pNode.name = @”pyramid”;

    pNode.position = SCNVector3Make(0, 0, 0);

    [scene.rootNode addChildNode:pNode];

    

    SCNLight *light = [SCNLight light];

    light.type = SCNLightTypeDirectional;

    light.Color = [UIColor yellowColor];

    SCNNode *lightNode = [SCNNode node];

    lightNode.light = light;

    [scene.rootNode addChildNode:lightNode];

    

    self.scene = scene;

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    SCNNode *pyramid = [self.scene.rootNode childNodeWithName:@”pyramid” recursively:YES];

    

    [SCNTransaction begin];

    [SCNTransaction setAnimationDuration:8.0];

    pyramid.rotation = SCNVector4Make(1, 0, 1, M_PI * 4.0);

    [SCNTransaction commit];

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end