Recently, the company required to display THE 3D model on mobile phones and obtain the model coordinates clicked. After searching a lot of relevant materials, it was found that there are two ways to achieve this, one is to integrate Unity3D, the other is to use SceneKit provided by Apple. Because it is too troublesome to use Unity3D, I choose to use SceneKit, and this is native, the speed can be expected to run faster than U3D integration ~
SceneKit is built on top of OpenGL and includes advanced engine features such as lighting, models, textures, cameras, etc. SceneKit implements a hierarchical tree of nodes, also known as a scene graph. A scene consists of a root node that defines a coordinate space for the scene world, as well as other visual nodes. SceneKit will display the scene on a view that will process the scene data graph and perform the animation before rendering each frame efficiently on the GPU.
To get a sense of the coordinates, check the Official Apple documentation and you’ll see the following image. SceneKit uses a right-handed coordinate system, and the default view orientation is along the negative Z axis. It’s easy to remember that red is the X-axis, green is the Y-axis, and blue is the z-axis.
The fledgling
Let’s start by creating a game project
Game Technology chooses SceneKit
The directory structure of the project is as follows. Compared with the project of APP development, there is an ScnAssets folder, which contains a SCN suffix file, which is the scene file that Xcode can identify. It supports a model file with the suffix DAE, which we will use later.
A first look way
Code flow from top to bottom:
- Create a scene. The scene itself is not visible and needs to be added to the scene in sceneView
- Create a camera node and set the position of the camera. The position of the camera is the position of the perspective, which can be associated with the eye perspective
- Create light nodes to set the placement of the light to give texture to the object to be rendered.
- Get the airplane model without making a recursive query on any of the child nodes.
- Set the airplane model to rotate around the Y-axis using SCNAction, which is also very simple to use.
- Get the SCNView and set the SCNView.
The wheel test
Let’s try a project of our own
Introduce SceneKit in Link Binary With Libraries
#import <SceneKit/SceneKit.h>
Copy the code
Next, create an art folder on your desktop and add the suffix [. Scnassets]. Put our asset Menchi
// Initialize a scene SCNScene *scene = [SCNScene sceneNamed:@"art.scnassets/Menchi.dae"]; / / remove the root node of the first node in the scene (directory root nodes is a child, is our material in the Menchi) SCNNode * node = scene. The rootNode..childnodes. FirstObject; SCNAction *action = [SCNAction repeatActionForever:[SCNAction rotateByX:0 y:1 z:0 duration:1]]; [node runAction:action]; Node. transform = SCNMatrix4MakeScale(5, 5, 5); // Create a camera and place it in the scene SCNNode *cameraNode = [SCNNode node]; cameraNode.camera = [SCNCamera camera]; [scene.rootNode addChildNode:cameraNode]; Cameranode. position = SCNVector3Make(0, 5, 15); SCNNode *lightNode = [SCNNode node]; lightNode.light = [SCNLight light]; lightNode.light.type = SCNLightTypeOmni; lightNode.position = SCNVector3Make(0, 10, 10); [scene.rootNode addChildNode:lightNode]; // create and add an ambient light to the scene SCNNode *ambientLightNode = [SCNNode node]; ambientLightNode.light = [SCNLight light]; ambientLightNode.light.type = SCNLightTypeAmbient; ambientLightNode.light.color = [UIColor darkGrayColor]; [scene.rootNode addChildNode:ambientLightNode]; / / create a used to show the scene SCNView SCNView * SCNView = [[SCNView alloc] initWithFrame: self. The bounds]; [self.view addSubview:scnView]; // set scene scnview.scene = scene; BackgroundColor = [UIColor blackColor]; // Set background scnView.backgroundColor = [UIColor blackColor]; . / / allowed to control the camera position scnView allowsCameraControl = YES; // Display data console scnView.showsStatistics = YES;Copy the code
Okay, edit run. Run successfully
** BUT **, in real projects, we often need to download the model from the server and display it, however, this method can only be displayed normally if the material is put into the project in advance, [note: I say normal display], which is not in line with our needs. If we download material from the server into the sandbox, and the program reads it directly from the sandbox and initializes the scene, it will have a nil problem.
Ios-scenekit display and interactive 3D modeling (II) Finally attached DEMO LXF3DSceneDemo