第九節 - SCNGeometry用法詳解
SCNGeometry
學習目標
1.瞭解SceneKit 遊戲框架中系統包含的幾何物件.
2.學習如何將幾何形狀物體繫結的節點上,顯示到檢視中.
系統提供的幾何形狀講解
正方體
程式碼如下:
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];
建立平面
程式碼如下:
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];
環面
程式碼如下:
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];
手把手教你寫程式碼
第一步.建立工程
第二步.新增遊戲框架
第三步.新增遊戲專用檢視SCNView
SCNView *scnView = [[SCNView alloc]initWithFrame:self.view.bounds];
scnView.backgroundColor = [UIColor blackColor];
[self.view addSubview:scnView];
scnView.allowsCameraControl = TRUE;
第四步.建立遊戲場景
scnView.scene = [SCNScene scene];
第五步.新增照相機
SCNNode *cameraNode =[SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.position = SCNVector3Make(0, 0, 5);
[scnView.scene.rootNode addChildNode:cameraNode];
第六步.新增節點並且繫結幾何形狀物體
// 建立幾何物件
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];
執行結果:
問題:有人問我SegmentCount屬性到底幹了什麼事情?
下面舉個例子演示
建立一個有切面的正方體
let box = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0.5)
設定一下下面屬性
box.chamferSegmentCount = 20
上面的現象就是答案,不需要我解釋了吧!
本節內容比較重要,請大家務必掌握,後面需要內容都建立在今天的內容之上!
相關文章
- extern用法詳解
- Metasploit用法詳解
- xargs用法詳解
- Nmap用法詳解
- mount用法詳解
- C++17 std::variant 詳解:概念、用法和實現細節C++
- Flutter ListView 用法詳解FlutterView
- MyBatis Generator 用法詳解MyBatis
- iconfont用法詳解
- Promise用法詳解(一)Promise
- StringTie用法詳解
- SVG <markers>用法詳解SVG
- Elasticsearch SQL用法詳解ElasticsearchSQL
- git stash用法詳解Git
- JSONP用法詳解JSON
- Generator用法詳解+co
- appendChild()用法詳解APP
- jQuery 事件用法詳解jQuery事件
- SVG transform用法詳解SVGORM
- expdp/impdp 用法詳解
- expdp/impdp用法詳解
- awk sed 用法詳解
- 第九小節 函式函式
- Ubuntu mount命令用法詳解Ubuntu
- axios的用法詳解iOS
- react-dnd 用法詳解React
- golang package time 用法詳解GolangPackage
- c++ vector用法詳解C++
- dataTransfer.setData() 用法詳解
- struct的匿名用法詳解Struct
- Python self用法詳解Python
- fcntl函式用法詳解函式
- eval()函式用法詳解函式
- jQuery stop()方法用法詳解jQuery
- STL中set用法詳解
- background屬性用法詳解
- inline用法詳解inline
- document.all用法詳解