How to dynamically load Collada files using Swift runtime in Scenekit

Source: Internet
Author: User
Tags what magic

Question: Today received a project, responsible for the needs of the Meimei told me to do a prototype can be loaded Collada file, the process is as follows:

    1. Users to download Collada package (for example, in-app purchase)
    2. Compress Package Decompression
    3. Show content in the Collada file

I started a variety of Google can quickly take care of the needs of the tool and class library, looked at the next unity3d, feeling that the fat man is very bloated, not interested in fat. Apple's scenekit seems to do 3D is good, high performance or original, the original ecological things taste should be good, there are edible methods.

Step One:

To open an Apple doucmentation that's not for the eye, after two eyeballs and a big discovery that Apple didn't teach me what magic was going to directly take care of the demand, I was frustrated.

Finally decided to use a simple brute method to download the. Dae file to the sandbox and read it in runtime

Results Xcode burst out:

Scenekit COLLADA files is not supported on this platform

I also want to explode .....

There seems to be no way, can only be honest to write their own.

Step Two:

New Project Select Game Template,scenekit

First of all, to get a Collada file (. Dae), not Maya for a long time, downloaded a free blender, the finished Dae file into the art.scnassets inside.

Step Three:

Let's start with two plates of stone or a fish to see the open hemisphere.

And then what...

No, then.

Step Four:

Sometimes thinking will be delivered to the door, want to read the file in runtime and do not let me operate compile-time, the kind can only see can not touch the feeling you understand.

Now that Apple is able to read the. Dae in the Scnasets directory, it must have used some magic to see what the build logs did.

Apparently found out what, uh, seems to have called copyscenekitasets, and it was this thing that finally called Scntool to optimize the. Dae File

Step Five:

To dig a hole, to figure out why please find this directory to dig

(/applications/xcode/contents/developer/usr/bin)

Find these two treasures Copyscenekitasets, Scntool.

Step Six:

After the user pays, the next step should call the API to load. Dae

First build a folder called Product.scnassets.

Run the script below

./copyscenekitassets product-1.scnassets/-O product-1-optimized.scnassets

Compress package generated files and drop them on the server.

Step Six:

Next is the heavy physical activity

I was using afnetworking and ssziparchive.

Construction in the GAMEVIEWCONTROLLER.M

The process is probably like this, in viewdidload: Add download Zip tarball, unzip the code

-(void) Viewdidload {[Super viewdidload]; [Self downloadzip];} -(void) Downloadzip {Nsurlsessionconfiguration *configuration =[Nsurlsessionconfiguration defaultsessionconfiguration]; Afurlsessionmanager *manager =[[Afurlsessionmanager alloc] initwithsessionconfiguration:configuration]; Nsurl *url = [Nsurl urlwithstring:@ "Http://www.XXXXXXX.com/product-1-optimized.scnassets.zip"]; Nsurlrequest *request =[Nsurlrequest Requestwithurl:url]; Nsurlsessiondownloadtask *downloadtask = [Manager downloadtaskwithrequest:request progress:nil Destination:^NSURL * ( Nsurl *targetpath, Nsurlresponse *Response) {Nsurl *documentsdirectoryurl =[[Nsfilemanager Defaultmanager] urlfordirectory:nsdocumentdirectory indomain:nsuserdomainmask AppropriateForURL: Nil Create:no Error:nil]; Return [Documentsdirectoryurl Urlbyappendingpathcomponent:[response Suggestedfilename]; } completionhandler:^ (Nsurlresponse *response, Nsurl *filepath, Nserror *error) {NSLog (@ "File downloaded to:%@", FilePath);//Unzip the archive nsarray *paths = nssearchpathfordirectories Indomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString *documentsdirectory = [paths objectatindex:0]; NSString *inputpath = [documentsdirectory stringbyappendingpathcomponent:@ "/product-1-optimized.scnassets.zip"  ]; Nserror *ziperror = nil; [Ssziparchive unzipfileatpath:inputpath todestination:documentsdirectory overwrite:yes password:nil error:&  Ziperror]; If(ziperror) {NSLog (@ "[GAMEVC] Something went wrong while unzipping:%@", ziperror.debugdescription);} else {NSLog (@ "[GAMEVC] Archive unzipped successfully"); [Self startscene]; } }]; [Downloadtask resume];}           

The stuff is downloaded and then it's loaded.

Load the downloaded scenensurl *documentsdirectoryurl = [[Nsfilemanager Defaultmanager] Urlfordirectory: NSDocumentDirectory indomain:nsuserdomainmask appropriateforurl:nil create:no Error:nil];   Documentsdirectoryurl = [Documentsdirectoryurl urlbyappendingpathcomponent:@ "product-1-optimized.scnassets/ Cube.dae "]; Scnscenesource *scenesource = [Scnscenesource scenesourcewithurl:documentsdirectoryurl options:nil];//Get Reference to the cube nodescnnode *thecube = [scenesource entrywithidentifier:@ "Cube" Withclass:[scnnode class]];
           

Simply say is to use Scnscenesource, scnnode load, as if not to say look at the code is good.

The next step is the most familiar steps of the rogue director, the scene, the lights, the camera.

Create a new sceneScnscene *scene = [Scnscene scene]; Create and add a camera to the scene  scnnode *cameranode =  [Scnnode node];cameranode.camera =  [scncame RA camera]; [Scene.rootnode Addchildnode:cameranode]; Place the camera  cameranode.position = scnvector3make (0, 0, + );//create and add a light to the Scenescnnode *lightnode =  [Scnnode node];lightnode.light =  [Scnlight light];lightnode.light.type =  scnlighttypeomni;lightnode.position = Scnvector3make (0, ten, ); [ 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];    
                       

This is my favorite part of the model.

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

Habitual Summary:

    1. Get the Collada zip from the server
    2. Extract
    3. Read into memory.
    4. Show
    5. This summary seems dispensable.

How to dynamically load Collada files using Swift runtime in Scenekit

Related Article

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.