iPhone人の波(黄色)

黄色い何かで人の波をつくるiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController () <SCNSceneRendererDelegate>

@property (nonatomic, weak) SCNView *sceneView;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    [self setupScene];

    [self createSlider];

}

– (void)setupScene {

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

    sv.backgroundColor = [UIColor colorWithHue:0.125 saturation:0.1 brightness:1 alpha:1];

    sv.scene = [SCNScene scene];

    sv.autoenablesDefaultLighting = YES;

    sv.delegate = self;

    [self.view addSubview:sv];

    

    self.sceneView = sv;

}

– (void)createSlider {

    SCNNode *slider = [SCNNode node];

    slider.rotation = SCNVector4Make(1, 0, 0, 0.2);

    [self.sceneView.scene.rootNode addChildNode:slider];

    

    SCNBox *slope = [SCNBox boxWithWidth:10 height:0.1 length:30 chamferRadius:0];

    slope.firstMaterial.diffuse.contents = [UIColor colorWithHue:0.4 saturation:0.1 brightness:1 alpha:0.4];

    SCNNode *slopeNode = [SCNNode nodeWithGeometry:slope];

    [slider addChildNode:slopeNode];

    

    

    for (int i=0; i<2; i++) {

        SCNBox *side = [SCNBox boxWithWidth:0.1 height:1 length:30 chamferRadius:0];

        side.firstMaterial.diffuse.contents = [UIColor colorWithHue:0.4 saturation:0.1 brightness:1 alpha:0.4];

        SCNNode *sideNode =[SCNNode nodeWithGeometry:side];

        sideNode.position = SCNVector3Make(i==0 ? –5 : 5, 0.4, 0);

        [slider addChildNode:sideNode];

    }

    

    slider.physicsBody = [SCNPhysicsBody staticBody];

}

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

{

    SCNNode *yellow = [SCNNode node];

    yellow.name = @”yellow”;

    yellow.position = SCNVector3Make(0, 5, –2);

    [self.sceneView.scene.rootNode addChildNode:yellow];

    

    SCNCapsule *c = [SCNCapsule capsuleWithCapRadius:1 height:2.6];

    c.firstMaterial.diffuse.contents = [UIColor yellowColor];

    SCNNode *cNode = [SCNNode nodeWithGeometry:c];

    [yellow addChildNode:cNode];

    

    for (int i=0; i<2; i++) {

        SCNCylinder *eye = [SCNCylinder cylinderWithRadius:0.14 height:0.1];

        eye.firstMaterial.diffuse.contents = [UIColor blackColor];

        SCNNode *eyeNode = [SCNNode nodeWithGeometry:eye];

        eyeNode.pivot = SCNMatrix4MakeRotation(M_PI * 0.5, 1, 0, 0);

        eyeNode.position = SCNVector3Make(i==0 ? 0.3 : –0.3, 0.4, 1.01);

        [yellow addChildNode:eyeNode];

        

        SCNCylinder *arm = [SCNCylinder cylinderWithRadius:0.07 height:1];

        arm.firstMaterial.diffuse.contents = [UIColor blackColor];

        SCNNode *armNode = [SCNNode nodeWithGeometry:arm];

        armNode.pivot = SCNMatrix4MakeRotation(i==0 ? 0.5 : –0.5, 0, 0, 1);

        armNode.position = SCNVector3Make(i==0 ? 1.2 : –1.2, 0.2, 0);

        [yellow addChildNode:armNode];

    }

    

    yellow.physicsBody = [SCNPhysicsBody dynamicBody];

    yellow.physicsBody.friction = 0;

    yellow.physicsBody.angularVelocityFactor = SCNVector3Make(0, 1, 0);

}

– (void)renderer:(id<SCNSceneRenderer>)aRenderer updateAtTime:(NSTimeInterval)time {

    [self.sceneView.scene.rootNode.childNodes enumerateObjectsUsingBlock:^(SCNNode *n, NSUInteger idx, BOOL *stop) {

        if ([n.name isEqual:@”yellow”] && n.presentationNode.position.y < –2) {

            [n removeFromParentNode];

        }

    }];

}

@end