SCNGeometry
學習目標
1.了解SceneKit 游戲框架中系統(tǒng)包含的幾何對象.
2.學習如何將幾何形狀物體綁定的節(jié)點上,顯示到視圖中.
系統(tǒ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];
創(chuàng)建平面
代碼如下:
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];
金子塔
代碼如下:
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];
球體
代碼如下:
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];
圓柱體
代碼如下:
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];
圓錐體
代碼如下:
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];
管道
代碼如下:
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];
環(huán)面
代碼如下:
SCNTorus *torus = [SCNTorus torusWithRingRadius:1 pipeRadius:0.5];
torus.firstMaterial.diffuse.contents = [UIImage imageNamed:@"1.PNG"];
SCNNode *torusNode = [SCNNode nodeWithGeometry:torus];
torusNode.position = SCNVector3Make(0, 0, 0);
[scnView.scene.rootNode addChildNode:torusNode];
地板
代碼如下:
SCNFloor *floor = [SCNFloor floor];
floor.firstMaterial.diffuse.contents = [UIImage imageNamed:@"1.PNG"];
SCNNode *floorNode = [SCNNode nodeWithGeometry:floor];
floorNode.position = SCNVector3Make(0, -5, 0);
[scnView.scene.rootNode addChildNode:floorNode];
立體文字
代碼如下:
SCNText *text = [SCNText textWithString:@"好好學習" extrusionDepth:0.5];
text.firstMaterial.diffuse.contents = [UIImage imageNamed:@"1.PNG"];
text.font = [UIFont systemFontOfSize:1];
SCNNode *textNode =[SCNNode nodeWithGeometry:text];
textNode.position = SCNVector3Make(-2, 0, 0);
[scnView.scene.rootNode addChildNode:textNode];
自定義形狀
代碼如下:
// 定義貝塞爾曲線
SCNShape *shape = [SCNShape shapeWithPath:[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 1, 1) cornerRadius:0.5] extrusionDepth:3];
shape.firstMaterial.diffuse.contents = [UIImage imageNamed:@"1.PNG"];
SCNNode *shapdeNode =[SCNNode nodeWithGeometry:shape];
[scnView.scene.rootNode addChildNode:shapdeNode];
手把手教你寫代碼
第一步.創(chuàng)建工程
第二步.添加游戲框架
第三步.添加游戲專用視圖SCNView
SCNView *scnView = [[SCNView alloc]initWithFrame:self.view.bounds];
scnView.backgroundColor = [UIColor blackColor];
[self.view addSubview:scnView];
scnView.allowsCameraControl = TRUE;
第四步.創(chuàng)建游戲場景
scnView.scene = [SCNScene scene];
第五步.添加照相機
SCNNode *cameraNode =[SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.position = SCNVector3Make(0, 0, 5);
[scnView.scene.rootNode addChildNode:cameraNode];
第六步.添加節(jié)點并且綁定幾何形狀物體
// 創(chuàng)建幾何對象
SCNTorus *torus = [SCNTorus torusWithRingRadius:1 pipeRadius:0.5];
torus.firstMaterial.diffuse.contents = [UIImage imageNamed:@"1.PNG"];
// 綁定到節(jié)點上
SCNNode *torusNode = [SCNNode nodeWithGeometry:torus];
// 設置節(jié)點的位置
torusNode.position = SCNVector3Make(0, 0, 0);
// 添加到場景中去
[scnView.scene.rootNode addChildNode:torusNode];
運行結果:
問題:有人問我SegmentCount屬性到底干了什么事情?
下面舉個例子演示
創(chuàng)建一個有切面的正方體
let box = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0.5)
設置一下下面屬性
box.chamferSegmentCount = 20
上面的現(xiàn)象就是答案,不需要我解釋了吧!
本節(jié)內容比較重要,請大家務必掌握,后面需要內容都建立在今天的內容之上!