一、SenceKit简介
SenceKit是苹果自己封装的一个3D框架。是苹果自己基于opengl进行的封装。据说之前一直无人问津。后来直到苹果出了ARKit。大家才发现ARKit是基于SenceKit框架实现的。所以学习ARKit。首先要了解SenceKit
二、SenceKit坐标系
SenceKit的坐标系是左手坐标系。如图:
- 这里很重要的一点是,SenceKit的坐标系原点是屏幕中央(划重点)。
三、SenceKit常用类
3.1 SCNView
SCNView 是一个用来显示3D图像的View容器。可以用 addSubView 方法添加到UiView 中。
SCNView *scView = [SCNView new];
scView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
[self.view addSubview:scView];
复制代码
这个类提供了一些代理方法<SCNSceneRenderer, SCNTechniqueSupport>
。
3.2 SCNScene
SCNScene 翻译过来是场景。这个场景提供了一个3D图像显示的环境。SCNView中有一个sence属性。实例化该属性才能显示3D图像。
/**view*/
SCNView *scView = [SCNView new];
/**场景*/
SCNScene *sence = [SCNScene new];
scView.scene = sence;
复制代码
实际使用过程中。SCNScene 可以直接加载.scn文件
SCNScene *scene = [SCNScene sceneNamed:@"art.scnassets/ship.scn"];
复制代码
也可以通过SCNNode加载3D建模工具生成的.dae文件.
SCNNode *ship = [scene.rootNode childNodeWithName:@"ship" recursively:YES];
复制代码
- 在SCNScene中有一个只读的rootNode 属性,其他node都是把自己作为子node添加到rootNode上
3.3 SCNNode
SCNNode 节点。一个场景的基本构建块。
在SenceKit中,包括在ARKit中。节点都是一个很重要的概念。有句话说在这两个框架中。万物皆节点。包括几何物体、摄像机(透视点)、灯光等。都是以节点的形式出现。SCNNode包括多种属性。通过实例化这些属性。节点可以成为各种部件。例如:
/*! 灯光
@property light
@abstract Determines the light attached to the receiver.
*/
@property(nonatomic, retain, nullable) SCNLight *light;
/*!摄像机
@property camera
@abstract Determines the camera attached to the receiver.
*/
@property(nonatomic, retain, nullable) SCNCamera *camera;
/*! 几何图形
@property geometry
@abstract Returns the geometry attached to the receiver.
*/
@property(nonatomic, retain, nullable) SCNGeometry *geometry;
复制代码
节点通过addChildNode:
方法添加子节点。例如:
SCNNode *node = [SCNNode node];
SCNNode *secNode = [SCNNode node];
[node addChildNode:secNode];
复制代码
在渲染场景的时候,sceneKit会遍历所有的子节点。当某个节点上添加了动画。这个节点上的子节点均会随着动画运转。
- 关于节点的一点个人理解。节点本身更类似于一个抽象的概念。理论上来说。节点是有无限大小的。你可以随意在哪个位置为节点添加子节点。即使这个节点看起来是一个有固定大小的几何图形。但是子节点的范围并不局限于这个几何图形。通常。这种方式都用于。把多个子节点设置到一个父节点。当父节点运动的时候。上面的子节点也随之运动。这个时候子节点可以在父节点内部。做自己的轨迹运动。实现多个运动效果叠加
3.4 SCNGeometry
SCNGeometry 一个可以连接到一个节点的三维几何体。
例如添加一个球体
SCNNode *SphereNode = [SCNNode node];/**节点 */
SCNSphere *Sphere = [SCNSphere sphereWithRadius:1];/**球体 */
SphereNode.geometry = Sphere;
/**具体灯光、位置设置和详细代码见简单实现模块 */
复制代码
SCeneKit提供了即中常见的几何体。也可以从.dea文件中加载一个几何体。还可以将2D几何体转化成一个具有深度的3D几何体。
- SceneKit中,定义了几种常见的几何图形
SCNPlane /** 平面(plane:飞机,平面*/
SCNBox /** 立方体*/
SCNPyramid /**角锥体 (Pyramid:金字塔,角锥体)*/
SCNSphere /**球体(Sphere:球体)*/
SCNCylinder /**圆柱体 (Cylinder 圆柱体)*/
SCNCone /**角锥体 (Cone角锥体)*/
SCNTube /**管道形状*/
SCNCapsule /**胶囊形状*/
SCNTorus /**圆环*/
SCNFloor /** 无限平面*/
SCNText /**文字*/
SCNShape /**2D平面图形*/
复制代码
3.5 SCNMaterial
SCNMaterial 材料
设置几何体的 颜色、贴图类。
3.6 SCNLight
光源。为渲染场景中提供光照着色。光源可以附加到节点上
光源的几种类型
- SCNLightTypeAmbient 周边光
- SCNLightTypeOmni 全方向光源
- SCNLightTypeDirectional 平行光
- SCNLightTypeSpot 聚焦光
可以添加一个或者多个光源节点。
SCNNode *lightNode = [SCNNode new];
lightNode.light = [SCNLight new];
lightNode.light.type = SCNLightTypeOmni;
lightNode.light.color = UIColor.whiteColor;
lightNode.position =SCNVector3Make(0, 3, 10);
[sence.rootNode addChildNode:lightNode];
复制代码
3.7 SCNCamera
SCNCamera 虚拟摄像机
摄像机的概念,即观察点。也就是从某个点看去。图形是什么样子的。一般做3D图像的时候,一定要设置这个类。如果不做设置,可能最终看到的是这样的。
这个效果相当于贴近球体去看的效果。 另外通过设置虚拟摄像机位置。也为3D场景提供了一个透视角
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.position = SCNVector3Make(0, 3, 10);
cameraNode.camera.zNear = 3;
[sence.rootNode addChildNode:cameraNode];
复制代码
简单实现
首先导入#import <SceneKit/SceneKit.h>
头文件
完整代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
/**view */
SCNView *scView = [SCNView new];
scView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
[self.view addSubview:scView];
/**场景*/
SCNScene *sence = [SCNScene new];
scView.scene = sence;
/**无限平面*/
SCNNode *fllorNode = [SCNNode node];
SCNFloor *floor = [SCNFloor floor];
floor.firstMaterial.diffuse.contents = UIColor.greenColor;
fllorNode.geometry = floor;
[sence.rootNode addChildNode:fllorNode];
/**球形*/
SCNNode *SphereNode = [SCNNode node];
SCNSphere *Sphere = [SCNSphere sphereWithRadius:1];
Sphere.firstMaterial.diffuse.contents = UIColor.redColor;
SphereNode.geometry = Sphere;
SphereNode.position = SCNVector3Make(0, 0, 0);
[sence.rootNode addChildNode:SphereNode];
/**相机节点*/
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.position = SCNVector3Make(0, 3, 10);
cameraNode.camera.zNear = 3;
[sence.rootNode addChildNode:cameraNode];
/**光源节点*/
SCNNode *lightNode = [SCNNode new];
lightNode.light = [SCNLight new];
lightNode.light.type = SCNLightTypeOmni;
lightNode.light.color = UIColor.whiteColor;
lightNode.position =SCNVector3Make(0, 3, 3);
[sence.rootNode addChildNode:lightNode];
/**动画*/
// SCNVector3 position = SCNVector3Make(0, 0, 0);
// SCNAction *move = [SCNAction moveTo:position duration:5];
// [cameraNode runAction:move];
/**动画2*/
// SCNVector3 lightPosition = SCNVector3Make(0, 0, 10);
// SCNAction *lightMove = [SCNAction moveTo:lightPosition duration:5];
// [lightNode runAction:lightMove];
}
复制代码
参考文档
https://blog.youkuaiyun.com/pzhtpf/article/category/6366595 ios 3D引擎 SceneKit 开发(1-8)
https://www.cnblogs.com/jukaiit/archive/2016/08/04/5737242.html https://www.jianshu.com/p/37908e6ec7b8 https://blog.youkuaiyun.com/biangabiang/article/details/77096619