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

效果图:


SceneKit_第1张图片
正方体.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];
}

效果图:


SceneKit_第2张图片
平面.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];
}

效果图:


SceneKit_第3张图片
金字塔.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];
}

效果图:


SceneKit_第4张图片
球体.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];
}

效果图:


SceneKit_第5张图片
圆柱体.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];
}

效果图:


SceneKit_第6张图片
圆锥体.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];
}

效果图:


SceneKit_第7张图片
管道.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];
}

效果图:


SceneKit_第8张图片
胶囊.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];
}

效果图:


SceneKit_第9张图片
环面.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];
}

效果图:


SceneKit_第10张图片
任意.gif

你可能感兴趣的:(SceneKit)