前言
SCNView
是一个集成自UIView
的视图,有个成员变量是scene
,scene
相当于VC
,scene.rootNode
相当于self.view
在UIKit
中添加子视图是使用[self.view addSubview: xxx]
,那么在SceneView
里,添加子节点使用[scene.rootNode addChildNode: xxNode]
,以下是几个简单的使用。更多戳这里
1.正方体:
-(void)viewDidLoad {
SCNView *scnView = [[SCNView alloc]initWithFrame:self.view.bounds];
scnView.backgroundColor = [UIColor blackColor];
[self.view addSubview:scnView];
scnView.allowsCameraControl = TRUE;
scnView.scene = [SCNScene scene];
// 1.创建照相机
SCNNode *cameraNode =[SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.position = SCNVector3Make(0, 0, 8);
[scnView.scene.rootNode addChildNode:cameraNode];
[self test];
}
- (void)test{
// 创建正方体
SCNBox *box = [SCNBox boxWithWidth:1 height:1 length:1 chamferRadius:0];
box.firstMaterial.diffuse.contents = [UIImage imageNamed:@"1.PNG"];
SCNNode *boxNode = [SCNNode node];
boxNode.position = SCNVector3Make(0, 0, 0);
boxNode.geometry = box;
[scnView.scene.rootNode addChildNode:boxNode];
}
效果图:
2.平面:
-(void)test {
SCNPlane *plane =[SCNPlane planeWithWidth:2 height:2];
plane.firstMaterial.diffuse.contents = [UIImage imageNamed:@"2.PNG"];
SCNNode *planeNode = [SCNNode nodeWithGeometry:plane];
planeNode.position = SCNVector3Make(0, 0, 0);
[scnView.scene.rootNode addChildNode:planeNode];
}
效果图:
3.金字塔
-(void)test {
SCNPyramid *pyramid = [SCNPyramid pyramidWithWidth:1 height:1 length:1];
pyramid.firstMaterial.diffuse.contents = [UIImage imageNamed:@"1.PNG"];
SCNNode *pyramidNode = [SCNNode nodeWithGeometry:pyramid];
pyramidNode.position = SCNVector3Make(0, 0, 0);
[scnView.scene.rootNode addChildNode:pyramidNode];
}
效果图:
4.球体:
-(void)test {
SCNSphere *sphere = [SCNSphere sphereWithRadius:1];
sphere.firstMaterial.diffuse.contents = [UIImage imageNamed:@"1.PNG"];
SCNNode *sphereNode =[SCNNode nodeWithGeometry:sphere];
sphereNode.position = SCNVector3Make(0, 0, 0);
[scnView.scene.rootNode addChildNode:sphereNode];
}
效果图:
5.圆柱体:
-(void)test {
SCNCylinder *cylinder = [SCNCylinder cylinderWithRadius:1 height:2];
cylinder.firstMaterial.diffuse.contents = [UIImage imageNamed:@"1.PNG"];
SCNNode *cylinderNode =[SCNNode nodeWithGeometry:cylinder];
cylinderNode.position = SCNVector3Make(0, 0, 0);
[scnView.scene.rootNode addChildNode:cylinderNode];
}
效果图:
6.圆锥体:
-(void)test {
SCNCone *cone = [SCNCone coneWithTopRadius:0 bottomRadius:1 height:1];
cone.firstMaterial.diffuse.contents = [UIImage imageNamed:@"1.PNG"];
SCNNode *coneNode = [SCNNode nodeWithGeometry:cone];
coneNode.position = SCNVector3Make(0,0, 0);
[scnView.scene.rootNode addChildNode:coneNode];
}
效果图:
7.管道:
-(void)test {
SCNTube *tube = [SCNTube tubeWithInnerRadius:1 outerRadius:1.2 height:2];
tube.firstMaterial.diffuse.contents = [UIImage imageNamed:@"1.PNG"];
SCNNode *tubeNode =[SCNNode nodeWithGeometry:tube];
tubeNode.position = SCNVector3Make(0, 0, 0);
[scnView.scene.rootNode addChildNode:tubeNode];
}
效果图:
8.胶囊
-(void)test {
SCNCapsule *capsule = [SCNCapsule capsuleWithCapRadius:2 height:2 ];
capsule.firstMaterial.diffuse.contents = [UIImage imageNamed:@"2.PNG"];
SCNNode *capsuleNode = [SCNNode nodeWithGeometry:capsule];
capsuleNode.position = SCNVector3Make(0, 0, 0);
[scnView.scene.rootNode addChildNode:capsuleNode];
}
效果图:
9.环面
-(void)test {
SCNTorus *torus = [SCNTorus torusWithRingRadius:1 pipeRadius:0.2];
torus.firstMaterial.diffuse.contents = [UIImage imageNamed:@"1.PNG"];
SCNNode *torusNode = [SCNNode nodeWithGeometry:torus];
torusNode.position = SCNVector3Make(0, 0, 0);
[scnView.scene.rootNode addChildNode:torusNode];
}
效果图:
10:绘制任意形状
-(void)test {
SCNShape *shape = [SCNShape shapeWithPath:[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2, 2) cornerRadius:0.5] extrusionDepth:3];
shape.firstMaterial.diffuse.contents = [UIImage imageNamed:@"1.PNG"];
SCNNode *shapdeNode =[SCNNode nodeWithGeometry:shape];
[scnView.scene.rootNode addChildNode:shapdeNode];
}
效果图: