IOS 学习AR(4)( 3D引擎 SceneKit SCNAction类)太阳系运动

之前说的了SCNAction类,没有上代码实践,就自己写了个太阳系运动。下面就是效果图:




代码块:

1. 太阳 太阳系 地球 地球和月亮 月亮 

    @property (nonatomic,strong)SCNView *scnView;


    @property (nonatomic,strong)SCNNode *sunNode, *earthNode,*moonNode,*earthGroupNode,*sunHaloNode;

2.初始化

    self.scnView = [[SCNView alloc]initWithFrame:self.view.frame];

    [self.view addSubview:self.scnView];

    self.scnView.backgroundColor = [UIColor blackColor];

    self.scnView.scene = [[SCNScene alloc]init];

    self.scnView.allowsCameraControl = YES;

    

    SCNCamera *camera =  [SCNCamera camera];

    SCNNode *cameraNode = [SCNNode node];

    cameraNode.camera =camera;

    camera.fieldOfView =90;

    cameraNode.camera.zFar =200;


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

    [self.scnView.scene.rootNode addChildNode:cameraNode];


3.各node

//太阳系

    self.sunHaloNode = [SCNNode nodeWithGeometry:[SCNSphere sphereWithRadius:100]];

    self.sunHaloNode.geometry.firstMaterial.diffuse.contents = [UIColor clearColor];

    self.sunHaloNode.geometry.firstMaterial.writesToDepthBuffer=NO;

    self.sunHaloNode.geometry.firstMaterial.lightingModelName = SCNLightingModelConstant;

    self.sunHaloNode.position =SCNVector3Make(0, 0, 0);

    [self.scnView.scene.rootNode addChildNode:self.sunHaloNode];

    [self.sunHaloNode runAction:[self addRotateActionWithDuration:12]];

    //太阳

    self.sunNode = [SCNNode nodeWithGeometry:[SCNSphere sphereWithRadius:20]];

    self.sunNode.geometry.firstMaterial.diffuse.contents = [UIImage imageNamed:@"art.scnassets/earth/sun.jpg"];

    self.sunNode.geometry.firstMaterial.multiply.contents =[UIImage imageNamed:@"art.scnassets/earth/sun.jpg"];

    self.sunNode.geometry.firstMaterial.multiply.intensity = 0.5;

    //太阳自转

    self.sunNode.position =SCNVector3Make(0, 0, 0);

    [self.scnView.scene.rootNode addChildNode:self.sunNode];

    [self.sunNode runAction:[self addRotateActionWithDuration:10]];

    //地球系

    self.earthGroupNode = [SCNNode nodeWithGeometry:[SCNSphere sphereWithRadius:10]];

    self.earthGroupNode.geometry.firstMaterial.diffuse.contents = [UIColor clearColor];

    self.earthGroupNode.position=SCNVector3Make(40, 0, 0);

    [self.sunHaloNode addChildNode:self.earthGroupNode];

    [self.earthGroupNode runAction:[self addRotateActionWithDuration:4]];


 //地球

    self.earthNode = [SCNNode nodeWithGeometry:[SCNSphere sphereWithRadius:4]];

    self.earthNode.geometry.firstMaterial.diffuse.contents = [UIImage imageNamed:@"art.scnassets/earth/earth-diffuse.jpg"];

    [self.earthGroupNode addChildNode:self.earthNode];

    [self.earthNode runAction:[self addRotateActionWithDuration:6]];

    

    //月球

    self.moonNode = [SCNNode nodeWithGeometry:[SCNSphere sphereWithRadius:1]];

    self.moonNode.geometry.firstMaterial.diffuse.contents = [UIImage imageNamed:@"art.scnassets/earth/moon.jpg"];

    self.moonNode.position=SCNVector3Make(10, 0, 0);

    [self.earthGroupNode addChildNode:self.moonNode];

    [self.moonNode runAction:[self addRotateActionWithDuration:3]];


//动画衍生

    SCNNode *superNode = [SCNNode node];    

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

    superNode.eulerAngles =SCNVector3Make(0, 0, M_PI_2/2);

    [self.scnView.scene.rootNode addChildNode:superNode];

    

    SCNNode *node = [SCNScene sceneNamed:@"art.scnassets/other/SpongeBob.dae"].rootNode;

    node.scale =SCNVector3Make(10, 10, 10);

    node.position = SCNVector3Make(0, 30, 0);

    [superNode addChildNode:node];

    

    SCNAction *action = [SCNAction  rotateByAngle:-10 aroundAxis:SCNVector3Make(-1, -1, 0) duration:4];


    [superNode runAction:[SCNAction repeatActionForever:action]];

5.动画自我旋转

- (SCNAction *)addRotateActionWithDuration:(NSInteger)duration{

    SCNAction *rotateAction = [SCNAction rotateByAngle:10 aroundAxis:SCNVector3Make(0, 1, 0) duration:duration];

    SCNAction *reRotateAction = [SCNAction repeatActionForever:rotateAction];

    return reRotateAction;

}


以上就是代码部分,涉及到其他东西,如给模型贴图,几何体,动作等,将在其他节有。



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值