Ios-scenekit3d Engine-Import model + upload server + download and simple setup

Source: Internet
Author: User



Scenekit was ios8 after Apple introduced a 3D model rendering framework.

Scenekit can now support a limited number of models, as far as I am writing this article seems to be only. Dae and. ABC after a model I have not used. This article is written only for the. Dae model.
First, if you want to load an existing DAE model that does not need to be dynamically added when the program is running. Then we can create a new game-type project directly. Select Scenekit in the options and replace the model name with the one in the program that loads the model. This article focuses on how to export the DAE model and dynamically download and display it on the server side.
First of all, we have a. STL or other model file that converts the model file to a. dae file I use blender.
(1) Create a new scene in Blender
(2) Delete the auto-generated cube, camera and 3 objects in the upper right sidebar
(3) Import our existing model files
(4) Adjust the direction and size of our model files
(5) in the upper right sidebar change the name of the model file and sub-file for the Dae file you want to export (this step is important!) )
(6) In the left column, click Smooth in the edit options.
(7) File->export->dae
(8) On the next page, we select the location of the export and the name of the file, and select include material texture in the left option texture (just as important!). )
Next we create a new folder on the desktop, temporarily named model, change the suffix to. scnassets, and copy our generated model files. Scenekit has written two scripts for dynamically added folders. It's not clear what the principle of action is, let's study it later. Just know how to use it for the time being. Copy the Copyscenekitassets, scntool files to the directory where the model.scnassets resides, enter the terminal and CD to the directory, run
1./copyscenekitassets Model.scnassets-o model-o.scnassets

If the terminal does not have an error and the model-o.scnassets is generated, the operation succeeds.
We then packaged the generated model-o.scnassets file as a zip file to make it smaller when the iphone client downloaded it.
Then upload to the server when you're done packing.
Two executable file download link http://download.csdn.net/detail/u013588047/8937773

Next is the play, how to download, unzip, and display it in the program.
Download unzip I used two open-source frameworks afnetworking and ssziparchive, and friends can see how to use them on their own.
Step-by-step, first download, unzip
1-(void) Downloadzip {
2
3 Nsurlsessionconfiguration *configuration = [Nsurlsessionconfiguration defaultsessionconfiguration];
4 Afurlsessionmanager *manager = [[Afurlsessionmanager alloc] initwithsessionconfiguration:configuration];
5//Here we replace it with a local link, you can use any URL link
6 Nsurl *url = [Nsurl urlwithstring:@ "File:///User/name/Desktop/model.scnassets.zip"];
7 Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];
8
9 Nsurlsessiondownloadtask *downloadtask = [Manager downloadtaskwithrequest:request progress:nil Destination:^NSURL * ( Nsurl *targetpath, Nsurlresponse *response) {
Ten Nsurl *documentsdirectoryurl = [[Nsfilemanager Defaultmanager] Urlfordirectory:nsdocumentdirectory InDomain:NSU Serdomainmask Appropriateforurl:nil Create:no Error:nil];
return [Documentsdirectoryurl Urlbyappendingpathcomponent:[response Suggestedfilename]];
completionhandler:^ (Nsurlresponse *response, Nsurl *filepath, Nserror *error) {
NSLog (@ "File downloaded to:%@", FilePath);
14
15//Unzip the file
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *documentsdirectory = [Paths objectatindex:0];
NSString *inputpath = [documentsdirectory stringbyappendingpathcomponent:@ "/product-1-optimized.scnassets.zip"] ;
19
Nserror *ziperror = nil;
21st
[Ssziparchive unzipfileatpath:inputpath todestination:documentsdirectory overwrite:yes password:nil error:& Ziperror];
23
if (ziperror) {
NSLog (@ "[GAMEVC] Something went wrong while unzipping:%@", ziperror.debugdescription);
}else {
NSLog (@ "[GAMEVC] Archive unzipped successfully");
[Self startscene];
29}
30
31}];
[Downloadtask resume];
33}

For 3d model scene creation, we use Scnscenesource, the code is as follows
1 Nsurl *documentsdirectoryurl = [[Nsfilemanager Defaultmanager] urlfordirectory:nsdocumentdirectory inDomain: Nsuserdomainmask Appropriateforurl:nil Create:no Error:nil];
2//The Dae file name here is the file name we defined at the time of export, and the scnnode we loaded in the following code is the model name we changed in the panel
3 Documentsdirectoryurl = [Documentsdirectoryurl urlbyappendingpathcomponent:@ "Model.scnassets/cube.dae"];
4
5 Scnscenesource *scenesource = [Scnscenesource scenesourcewithurl:documentsdirectoryurl options:nil];
Then we load the model in the. Dae file, as a scnnode, with the name of the model we changed in the first place.
1 Scnnode *thecube = [Scenesource entrywithidentifier:@ "Cube" Withclass:[scnnode class]];

Finally, we set the lighting and other effects, is actually set up in the new game file, we have to do is to load the Scnnode *thecube into the scene
Create a new scene
Scnscene *scene = [Scnscene scene];

Create and add a camera to the scene
Scnnode *cameranode = [Scnnode node];
Cameranode.camera = [Scncamera camera];
[Scene.rootnode Addchildnode:cameranode];

Place the camera
cameranode.position = scnvector3make (0, 0, 15);

Create and add a light to the scene
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];

ADD our cube to the scene
[Scene.rootnode Addchildnode:thecube];

Retrieve the Scnview
Scnview *scnview = (Scnview *) Self.view;

Set the scene to the view
Scnview.scene = scene;

Allows the user to manipulate the camera
Scnview.allowscameracontrol = YES;

Show statistics such as FPS and timing information
Scnview.showsstatistics = YES;

Configure the View
Scnview.backgroundcolor = [Uicolor blackcolor];

This allows us to dynamically download a Dae file and display it.

Ios-scenekit3d Engine-Import model + upload server + download and simple setup

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.