问题:今天接到一个项目,负责弄需求的美眉跟我讲能不能做一个原型能够加载Collada文件,流程如下:

  1. 用户用app下载Collada 压缩包(如内购项目)
  2. 压缩包解压
  3. 展示Collada文件里的内容

我开始google各种能够快速搞定需求的工具以及类库,看了下Unity3D,感觉这胖子挺臃肿的,对胖子没兴趣。苹果的SceneKit好像做3D还不错,性能高还是原生,原生态的东西味道应该不错,下面有食用方法。

步骤一:

打开不是给人看的Apple Doucmentation,经过两眼球左右互搏后有重大发现就是苹果没有教会我什么魔法能够直接搞定这个需求,我表示很沮丧。

最终决定使用简单粗暴的方法硬把.dae文件下载到沙盒然后在runtime中读取

结果Xcode爆出:

scenekit COLLADA files are not supported on this platform

我也想爆粗。。。。。

看来没办法了,只能老老实实的自己写。

步骤二:

新建项目选择Game template,SceneKit

首先,要搞个Collada文件(.dae),没弄Maya很久了,下载了个免费的blender,把弄好的的dae文件丢到art.scnassets里边去。

步骤三:

先搞两盘盘炉石或者去斗鱼看看露半球

然后呢。。。

没然后了。

步骤四:

有的时候思路会自己送上门的,想读取文件在runtime又不让我操作compile-time,那种只能看不能摸的感觉你懂的。

既然苹果能够在scnasets目录下读取.dae那肯定用了些魔法,看看build logs到底做了些什么。

显然发现了什么,嗯似乎调用了copySceneKitAsets,正是这玩意最终调用了scntool去优化.dae文件

步骤五:

要挖坑了,要搞清楚为什么请找到这个目录去挖

(/Applications/Xcode/Contents/Developer/usr/bin)

找找发现这两个宝物 copySceneKitAsets、 scntool。

步骤六:

用户付费后下一步应该调用Api加载.dae

先建一个名叫product.scnassets的文件夹

运行下边脚本

./copySceneKitAssets product-1.scnassets/ -o product-1-optimized.scnassets

压缩打包生成的文件然后丢到服务器去

步骤六:

接下来是重体力活

我是用AFNetworkingSSZipArchive

在GameViewController.m 中施工

流程大概是这样,在viewDidLoad:中加入下载zip压缩包,解压的代码

- (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 = NSSearchPathForDirectoriesInDomains(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];
}

东西下载完了然后就是加载了

// Load the downloaded scene

NSURL *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 node

SCNNode *theCube = [sceneSource entryWithIdentifier:@"Cube" withClass:[SCNNode class]];

简单说一下就是是用SCNSceneSource、SCNNode加载,好像等于没说看代码就好。

接下来就是流氓导演最熟悉的步骤,布景、灯光、摄像机。

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

这是我最喜欢的部分模特上场

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

习惯性总结:

  1. 从服务器拿到Collada zip的压缩包
  2. 解压
  3. 读到内存里
  4. 展示
  5. 这个总结好像可有可无

如何在SCENEKIT使用SWIFT RUNTIME动态加载COLLADA文件的更多相关文章

  1. Java_Java中动态加载jar文件和class文件

    转自:http://blog.csdn.net/mousebaby808/article/details/31788325 概述 诸如tomcat这样的服务器,在启动的时候会加载应用程序中lib目录下 ...

  2. Android应用安全之外部动态加载DEX文件风险

    1. 外部动态加载DEX文件风险描述 Android 系统提供了一种类加载器DexClassLoader,其可以在运行时动态加载并解释执行包含在JAR或APK文件内的DEX文件.外部动态加载DEX文件 ...

  3. js动态加载css文件和js文件的方法

    今天研究了下js动态加载js文件和css文件的方法. 网上发现一个动态加载的方法.摘抄下来,方便自己以后使用 [code lang="html"] <html xmlns=& ...

  4. 两种动态加载JavaScript文件的方法

    两种动态加载JavaScript文件的方法 第一种便是利用ajax方式,第二种是,动静创建一个script标签,配置其src属性,经过把script标签拔出到页面head来加载js,感乐趣的网友可以看 ...

  5. QUiLoader 动态加载.ui文件

    动态加载UI文件是指,用 Qt Designer 通过拖拽的方式生产.ui 文件.不用 uic工具把.ui 文件变成等价的 c++代码,而是在程序运行过程中需要用到UI文件时,用 QUiLoader ...

  6. 动态加载JS文件,并根据JS文件的加载状态来执行自己的回调函数

    动态加载JS文件,并根据JS文件的加载状态来执行自己的回调函数, 在很多场景下,我们需要在动态加载JS文件的时候,根据加载的状态来进行后续的操作,需要在JS加载成功后,执行另一方法,这个方法是依托在加 ...

  7. 详解QUiLoader 动态加载.ui文件

    http://blog.chinaunix.net/uid-13838881-id-3652523.html 1.适用情况: 动态加载UI文件是指,用 Qt Designer 通过拖拽的方式生产.ui ...

  8. ExtJS4.x动态加载js文件

    动态加载js文件是ext4.x的一个新特性,可以有效的减少浏览器的压力,提高渲染速度.如动态加载自定义组件 1.在js/extjs/ux目录下,建立自定义组件的js文件. 2.编写MyWindow.j ...

  9. Ext JS学习第十天 Ext基础之动态加载JS文件(补充)

    此文用来记录学习笔记: •Ext4.x版本提供的一大亮点就是Ext.Loader这个类的动态加载机制!只要遵循路径规范,即可动态加载js文件,方便把自己扩展组件动态加载进来,并且减轻浏览器的压力. • ...

随机推荐

  1. convert return char from sql server 2008 r2 or below version to c#

    C# string.Replace((char)13, ' ') //newline char; string.Replace((char)10, ' ') //return char;

  2. CentOS平台部署vsftp(基于虚拟用户)

    1. 安装FTP 1 2 [root@task ~]# yum install vsftpd –y [root@task ~]# chkconfig vsftpd on          # 配置开机 ...

  3. 修改server 2008远程桌面端口

    在“开始”-"运行"菜单里,输入regedit HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Terminal Serve ...

  4. C#窗体文件的操作

    文件的创建使用File类下的AppendAllText("要保存的数据","文件完整路径")方法. string fileMove = @"C:\te ...

  5. x-editable 的使用方法

    1.首先在html网页中定义一个a标签(如下<%#%>是asp.net的语法) <a href="#"  data-pk="<%#Eval(&qu ...

  6. PHP开启cURL功能

    PHP开启cURL功能 在php.ini中开启 确定php扩展目录下有php_curl.dll类库 在php.int中找到扩展库所在目录 判断目录下是否有php_curl.dll 没有的话去搜索下载 ...

  7. iOS 沙盒(sandbox)结构 使用 实例

    声明:该文档是经过自己查找网上的资料以及自己多年的经验后而总结出来的,希望对大家有所帮助,有什么不恰当支出还请大家多指点! iOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为 ...

  8. Appium移动自动化测试之安装Android SDK和JDK

    安装好Appium后,我们来继续安装Android SDK和JDK,JDK的安装以及环境变量配置这边就不再多说了,毕竟都是从事自动化的,这个应该是so easy.闲言少续,我们来操作Android S ...

  9. 嵌入式linux学习笔记1—内存管理MMU之虚拟地址到物理地址的转化

    一.内存管理基本知识 1.S3C2440最多会用到两级页表:以段的方式进行转换时只用到一级页表,以页的方式进行转换时用到两级页表.页的大小有三种:大页(64KB),小页(4KB),极小页(1KB).条 ...

  10. JAVA 之print,printf,println

    print:将它的参数显示在命令窗口,并将输出光标定位在所显示的最后一个字符之后. println: 将它的参数显示在命令窗口,并在结尾加上换行符,将输出光标定位在下一行的开始. printf:是格式 ...