之前说的了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;
}
以上就是代码部分,涉及到其他东西,如给模型贴图,几何体,动作等,将在其他节有。