SceneKit

前言

SCNView是一个集成自UIView的视图,有个成员变量是scenescene相当于VC,scene.rootNode相当于self.viewUIKit中添加子视图是使用[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];
}

效果图:


正方体.gif

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];
}

效果图:


平面.gif

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];
}

效果图:


金字塔.gif

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];
}

效果图:


球体.gif

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];
}

效果图:


圆柱体.gif

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];
}

效果图:


圆锥体.gif

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];
}

效果图:


管道.gif

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];
}

效果图:


胶囊.gif

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];
}

效果图:


环面.gif

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];
}

效果图:


任意.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Scene Kit是一个苹果Cocoa风格的3D渲染框架,该框架被引入OS X是在WWDC 2012 (那时 OS...
    rectinajh阅读 2,771评论 0 2
  • 本系列文章的学习首先要感谢史前图腾的共享精神,在他的系列文章中有详细的解释,但是他的代码都是Swift写的.并且代...
    严兵胜阅读 1,427评论 0 1
  • 1、创建一个sceneScene Kit 内建了几种简单的几何模型,如盒子、球体、平面、圆锥体等,但对于游戏来说,...
    光明程辉阅读 588评论 1 0
  • 使用SceneKit如何播放视频 scenekit 播放视频的方式有很多种,今天我就给大家介绍一种最简单的播放视频...
    笑啥风云阅读 913评论 0 0
  • 说明 本系列文章是对<3D Apple Games by Tutorials>一书的学习记录和体会此书对应的代码地...
    苹果API搬运工阅读 2,189评论 0 3