iPhone now loading ball

ボールをぴょんぴょこさせてnow loading.. っぽくするiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController () <SCNSceneRendererDelegate>

@property (nonatomic, weak) SCNView *sceneView;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor brownColor];

    

    [self setupScene];

    [self createCamera];

    [self createLight];

    [self createGround];

    [self createBall];

}

– (void)setupScene {

    float w = CGRectGetMaxX(self.view.bounds);

    SCNView *sv = [[SCNView alloc] initWithFrame:CGRectMake(0, 0, w, w)];

    sv.backgroundColor = [UIColor brownColor];

    sv.delegate = self;

    sv.scene = [SCNScene scene];

    sv.scene.physicsWorld.speed = 3.0;

    [self.view addSubview:sv];

    self.sceneView = sv;

}

– (void)createGround {

    SCNBox *box = [SCNBox boxWithWidth:40 height:1 length:40 chamferRadius:0];

    box.firstMaterial.diffuse.contents = [UIColor brownColor];

    SCNNode *boxNode = [SCNNode nodeWithGeometry:box];

    boxNode.physicsBody = [SCNPhysicsBody staticBody];

    boxNode.physicsBody.restitution = 1.0;

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

}

– (void)createBall {

    SCNSphere *ball = [SCNSphere sphereWithRadius:1];

    ball.firstMaterial.diffuse.contents = [UIColor whiteColor];

    SCNNode *ballNode = [SCNNode nodeWithGeometry:ball];

    ballNode.name = @”ball”;

    ballNode.position = SCNVector3Make(0, 15, 0);

    ballNode.physicsBody = [SCNPhysicsBody dynamicBody];

    ballNode.physicsBody.restitution = 1.0;

    ballNode.physicsBody.damping = 0;

    ballNode.physicsBody.friction = 0;

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

    

    SCNNode *ballDisplayNode = [SCNNode nodeWithGeometry:ball];

    ballDisplayNode.name = @”balldisplay”;

    ballDisplayNode.transform = SCNMatrix4MakeScale(2, 2, 2);

    [ballNode addChildNode:ballDisplayNode];

    

}

– (void)createCamera {

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

    camera.position = SCNVector3Make(0, 30, 20);

    camera.rotation = SCNVector4Make(1, 0, 0, –M_PI/2.0 * 0.5);

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

}

– (void)createLight {

    SCNLight *light = [SCNLight light];

    light.type = SCNLightTypeSpot;

    light.castsShadow = YES;

    light.spotOuterAngle = 180.0;

    SCNNode *lightNode = [SCNNode node];

    lightNode.light = light;

    lightNode.position = SCNVector3Make(0, 50, 10);

    lightNode.rotation = SCNVector4Make(1, 0, 0, –M_PI/2.0 * 0.9);

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

    

    SCNLight *light2 = [SCNLight light];

    light2.type = SCNLightTypeAmbient;

    SCNNode *lightNode2 = [SCNNode node];

    lightNode2.light = light2;

    lightNode2.position = SCNVector3Make(0, 50, 10);

    lightNode2.rotation = SCNVector4Make(1, 0, 0, –M_PI/2.0 * 0.9);

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

}

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

{

    SCNNode *ball = [self.sceneView.scene.rootNode childNodeWithName:@”ball” recursively:NO];

    float y = ball.presentationNode.position.y;

    if ((y < 6.0 && ball.physicsBody.velocity.y > 0) || (y < 2.5 && ball.physicsBody.velocity.y < 0)) {

        SCNNode *balldisp = [ball childNodeWithName:@”balldisplay” recursively:NO];

        float s = 1.00.25 * (y – 2.0);

        balldisp.transform = SCNMatrix4MakeScale(2.0 + s,  2.0 – s, 2.0 + s);

    }

}

@end