After iOS8, Apple introduced a 3D model rendering framework. SceneKit. But there are not many tutorials in the country for this. Two days ago was confused, finally the most basic content to understand, write down this essay as the opening of cnblogs, hope to keep writing down.
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 3Nsurlsessionconfiguration *configuration =[Nsurlsessionconfiguration defaultsessionconfiguration];4Afurlsessionmanager *manager =[[Afurlsessionmanager alloc] initwithsessionconfiguration:configuration];5Here we replace with local links, you can use any URL link6Nsurl *url = [Nsurl urlwithstring:@ "File:///User/name/Desktop/model.scnassets.zip"];7Nsurlrequest *request =[Nsurlrequest Requestwithurl:url];8 9Nsurlsessiondownloadtask *downloadtask = [Manager downloadtaskwithrequest:request progress:nil Destination:^NSURL * ( Nsurl *targetpath, Nsurlresponse *response) {TenNsurl *documentsdirectoryurl =[[Nsfilemanager Defaultmanager] Urlfordirectory:nsdocumentdirectory indomain:nsuserdomainmask Appropriateforurl:nil Create:no Error:nil]; One return[Documentsdirectoryurl urlbyappendingpathcomponent:[response suggestedfilename]; A} completionhandler:^ (Nsurlresponse *response, Nsurl *filepath, Nserror *error) { -NSLog (@"File downloaded to:%@", FilePath); - the //Unzip the file -Nsarray *paths =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); -NSString *documentsdirectory = [Paths Objectatindex:0]; -NSString *inputpath = [Documentsdirectory stringbyappendingpathcomponent:@"/product-1-optimized.scnassets.zip"]; + -Nserror *ziperror =Nil; + A[Ssziparchive unzipfileatpath:inputpath todestination:documentsdirectory overwrite:yes password:nil error:&Ziperror]; at - if(ziperror) { -NSLog (@"[GAMEVC] Something went wrong while unzipping:%@", ziperror.debugdescription); -}Else { -NSLog (@"[GAMEVC] Archive unzipped successfully"); - [self startscene]; in } - to }]; + [Downloadtask resume]; -}
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 that we defined when we exported it, 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"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 sceneScnscene *scene =[Scnscene scene]; //Create and add a camera to the sceneScnnode *cameranode =[Scnnode Node];cameranode.camera=[Scncamera camera]; [Scene.rootnode Addchildnode:cameranode]; //Place the cameraCameranode.position = Scnvector3make (0,0, the); //Create and add a light to the sceneScnnode *lightnode =[Scnnode node];lightnode.light=[Scnlight Light];lightnode.light.type=scnlighttypeomni;lightnode.position= Scnvector3make (0,Ten,Ten); [Scene.rootnode Addchildnode:lightnode]; //Create and add an ambient light to the sceneScnnode *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 ScnviewScnview *scnview = (Scnview *) Self.view;//set the scene to the viewScnview.scene =scene;//allows the user to manipulate the cameraScnview.allowscameracontrol =YES;//show statistics such as FPS and timing informationScnview.showsstatistics =YES;//Configure the ViewScnview.backgroundcolor = [Uicolor blackcolor];
This allows us to dynamically download a Dae file and display it.
Note: Original article, reproduced please indicate the original author.
PostScript: Just started iOS development, the article must have a lot of incorrect and missing places, there are a lot of superficial understanding places, I hope you have more advice.
Convert. stl files to. Dae and dynamically load to Scenekit display (3d model shown in iOS)