先自定义SCNView和SCNScene用来作为模型展示的view和场景。
@property (nonatomic,strong) SCNView *QFSceneView;
@property (nonatomic,strong) SCNScene *QFSenePlace;
//声明3D场景
self.QFSceneView = [[SCNView alloc]init];
_QFSceneView.allowsCameraControl = YES;//用户可以控制摄像机
_QFSceneView.showsStatistics = YES;//控制下方的统计栏是否显示
[self.view addSubview:_QFSceneView];
_QFSceneView.sd_layout.spaceToSuperView(UIEdgeInsetsMake(NavBarHeight, 0, 0, 0));
//声明场景中的模型对象
self.QFSenePlace = [SCNScene scene];
_QFSceneView.scene = _QFSenePlace;
_QFSceneView.autoenablesDefaultLighting = true;
将系统的3D模型控件放到根节点处展示出来
//构建3D模型
self.createNode = [[CreateTreeNode alloc]init];
SCNNode *_model = [_createNode createTreeModel];
[_QFSenePlace.rootNode addChildNode:model];
下面开始介绍绘制的模型结构
1.绘制方块
SCNNode *present = [SCNNode nodeWithGeometry:[SCNBox boxWithWidth:1 height:1 length:1 chamferRadius:0]];
present.geometry.firstMaterial.diffuse.contents = QFBlueColor;
present.position = SCNVector3Make(2, -2, 2);
present.name = @"树下面的方块";
[_model addChildNode:present];
2.绘制SCNPyramid
SCNNode *pyramidNode = [SCNNode nodeWithGeometry:[SCNPyramid pyramidWithWidth:2 height:2 length:2]];
pyramidNode.geometry.firstMaterial.diffuse.contents = QFYellowColor;
pyramidNode.position = SCNVector3Make(-2, -2, -2);
pyramidNode.name = @"三角锥";
[_model addChildNode:pyramidNode];
3.绘制SCNSphere
SCNNode *sphereNode = [SCNNode nodeWithGeometry:[SCNSphere sphereWithRadius:1]];
sphereNode.geometry.firstMaterial.diffuse.contents = QFRedColor;
sphereNode.position = SCNVector3Make(-2, -1, 2);
sphereNode.name = @"球体";
[_model addChildNode:sphereNode];
4.绘制SCNCone
SCNNode *coneNode = [SCNNode nodeWithGeometry:[SCNCone coneWithTopRadius:0.5 bottomRadius:1 height:2]];
coneNode.geometry.firstMaterial.diffuse.contents = QFBlackColor;
coneNode.position = SCNVector3Make(2, -1, -2);
coneNode.name = @"锥柱体";
[_model addChildNode:coneNode];
5.绘制SCNTube
SCNNode *tubeNode = [SCNNode nodeWithGeometry:[SCNTube tubeWithInnerRadius:0.5 outerRadius:1 height:2]];
tubeNode.geometry.firstMaterial.diffuse.contents = QFGrayColor;
tubeNode.position = SCNVector3Make(3, 3, 3);
tubeNode.name = @"环柱体";
[_model addChildNode:tubeNode];
6.绘制SCNCapsule
SCNNode *capsuleNode = [SCNNode nodeWithGeometry:[SCNCapsule capsuleWithCapRadius:1 height:2]];
capsuleNode.geometry.firstMaterial.diffuse.contents = QFDarkGrayColor;
capsuleNode.position = SCNVector3Make(3, 3, -3);
capsuleNode.name = @"跟球看着一样";
[_model addChildNode:capsuleNode];
7.绘制SCNTorus
SCNNode *torusNode = [SCNNode nodeWithGeometry:[SCNTorus torusWithRingRadius:1 pipeRadius:0.5]];
torusNode.geometry.firstMaterial.diffuse.contents = QFLightGrayColor;
torusNode.position = SCNVector3Make(-3, 3, 3);
torusNode.name = @"游泳圈";
[_model addChildNode:torusNode];
8.绘制SCNText
SCNText *scnText = [SCNText textWithString:@"自构3D模型" extrusionDepth:0.5];
scnText.font = [UIFont systemFontOfSize:2];
SCNNode *textNode = [SCNNode nodeWithGeometry:scnText];
textNode.geometry.firstMaterial.diffuse.contents = QFOrangeColor;
textNode.position = SCNVector3Make(-5, 8, -5);
textNode.name = @"3D文字";
[_model addChildNode:textNode];