问题:今天接到一个项目,负责弄需求的美眉跟我讲能不能做一个原型能够加载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. sqlite3 转义字符

    SqLite数据库的单引号转义是用单引号转义,并不是常用的"/" 参考:http://blog.csdn.net/qingflyer/article/details/6372498 ...

  2. Go语言开发 Eclipse插件安装

    UpdateSite: http://goclipse.github.io/releases/

  3. vc++ 程序开机自启动和取消启动

    //开机启动 int CMainWnd::CreateRun() { //添加以下代码 HKEY hKey; }; //得到程序自身的全路径 DWORD dwRet = GetModuleFileNa ...

  4. ARCGIS Server 发布服务时出现的问题解决

    target='CFH.ConfigurationFactoryHost'  machine='IBM3850X5'  thread='24072'  elapsed='0.31200'>Ser ...

  5. python之路-Mysql&&ORM

    1. 数据库介绍 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库, 每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据. 我们也可以 ...

  6. Vue.JS入门学习随笔

    PS:先说说学习Vue的缘由吧,学习完了React之后,突然发现又出了一款叫做vue的框架,而且据说可以引领又一波新框架的潮流,我容易吗我!!!   Vue.js(读音 /vjuː/, 类似于view ...

  7. nullcom HackIM2016 -- Programming Question 4

    One of the NullCon vidoes talked about a marvalous Russian Gift. The Vidoe was uploaded on [May of 2 ...

  8. man ascii

    Linux 2.6 - man page for ascii (linux section 7) - Unix & Linux Commands Linux 2.6 - man page fo ...

  9. 通过RGB灯输出七色

    本文由博主原创,如有不对之处请指明,转载请说明出处. /********************************* 代码功能:输出模拟信号,控制RGB灯的颜色 使用函数: pinMode(引脚 ...

  10. 利用Service bus中的queue中转消息

    有需求就有对策就有市场. 由于公司global的policy,导致对公司外发邮件的service必须要绑定到固定的ip地址,所以别的程序需要调用发邮件程序时,问题就来了,如何在azure上跨servi ...