前言
SCNView
是一個(gè)集成自UIView
的視圖匆浙,有個(gè)成員變量是scene
粹淋,scene
相當(dāng)于VC
,scene.rootNode
相當(dāng)于self.view
在UIKit
中添加子視圖是使用[self.view addSubview: xxx]
,那么在SceneView
里高镐,添加子節(jié)點(diǎn)使用[scene.rootNode addChildNode: xxNode]
,以下是幾個(gè)簡單的使用埠忘。更多戳這里
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.創(chuàng)建照相機(jī)
SCNNode *cameraNode =[SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.position = SCNVector3Make(0, 0, 8);
[scnView.scene.rootNode addChildNode:cameraNode];
[self test];
}
- (void)test{
// 創(chuàng)建正方體
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.環(huán)面
-(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];
}
效果圖: