在我们ARKit 项目中,尝尝需要导入模型到项目中,但模型dae文件大小,会导致项目包变大。
这时候就考虑到需要网络下载模型。
我的做法步骤如下:
1. 先测试模型在沙盒中是否直接可以读取。
2.不能读取,查看xcode 的build 是否对art.scnassets文件里的模型进行处理了。
3. xcode确实对art.scnassets文件里的模型并用到了xcode里的2个脚本进行处理,分别是:scntool和copySceneKitAssets
4.运行后生成文件,压缩文件放在服务器上,供下载。
5.项目下载解压,发在沙盒中,读出模型。
以上是实现的步骤。
具体操作:
1.scntool和copySceneKitAssets文件路径:
/Applications/Xcode.app/Contents/Developer/usr/bin
2.新建文件夹 model 在里面在创建art.scnassets文件夹(文件夹必须要有.scnassets文件后缀),art.scnassets里存放模型dea和模型贴图。(注意⚠️:贴图png/jpg和dae文件必须在一个同目录文件下)
最后将scntool和copySceneKitAssets文件放入model目录下。
3.执行命令:如下
1. cd /Users/tanhusheng/Desktop/model
2. ./copySceneKitAssets art.scnassets -o model.scnassets
执行结果:生成出了一个文件夹model.scnassets
4. 压缩 model.scnassets 文件 model.zip 放服务器
模型下载地址:
http://123.57.35.196:9090/CJH/freela/download/huaban.zip(不供下载)
5.最好先用虚拟机调试,这样能查看沙盒里的下载的zip。
具体操作就不演示了,直接提供代码:
#import
#import
@interface ViewController ()
@property (nonatomic,strong)SCNView *scnView;
@property (nonatomic,strong)SCNNode *node;
@property (nonatomic, strong) UIButton *btnAction;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self makeScene];//1.初始化场景
[self makeNode];//2.初始化节点
// [self rotation];//3,旋转节点
// Do any additional setup after loading the view, typically from a nib.
}
- (void)makeScene{
self.scnView = [[SCNView alloc]initWithFrame:self.view.frame];
[self.viewaddSubview:self.scnView];
self.scnView.backgroundColor = [UIColor blackColor];
self.scnView.scene= [[SCNScenealloc]init];
}
- (void)makeNode{
SCNNode*camersNode = [SCNNodenode];
camersNode.camera= [SCNCameracamera];
[self.scnView.scene.rootNodeaddChildNode:camersNode];
self.scnView.allowsCameraControl = YES;
camersNode.position=SCNVector3Make(0,0,0);
self.btnAction = [UIButton buttonWithType:UIButtonTypeCustom];
self.btnAction.frame =CGRectMake(0, self.view.bounds.size.height-40, self.view.bounds.size.width, 40);
self.btnAction.backgroundColor = [UIColor redColor];
[self.btnAction addTarget:self action:@selector(goBtnAction) forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:self.btnAction];
}
- (void)goBtnAction{
dispatch_queue_t queue = dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(queue, ^{
NSURL *url = [NSURL URLWithString:@"http://123.57.35.196:9090/CJH/freela/download/huaban.zip"];
NSError*error =nil;
// 2
NSData*data = [NSDatadataWithContentsOfURL:urloptions:0error:&error];
if(!error)
{
// 3
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString*path = [pathsobjectAtIndex:0];
//压缩后的文件名
NSString*zipPath = [pathstringByAppendingPathComponent:@"zipfile.zip"];
[datawriteToFile:zipPathoptions:0error:&error];
if(!error)
{
// TODO: Unzip
[selfzipArchiveWithZipPath:zipPathandPath:path];
}
else
{
NSLog(@"Error saving file %@",error);
}
}
else
{
NSLog(@"Error downloading zip file: %@", error);
}
});
}
- (void)zipArchiveWithZipPath:(NSString*)zipPath andPath:(NSString*)path{
ZipArchive *za = [[ZipArchive alloc] init];
if([zaUnzipOpenFile: zipPath]) {
BOOLret = [zaUnzipFileTo: pathoverWrite:YES];
if(NO== ret){} [zaUnzipCloseFile];
dispatch_async(dispatch_get_main_queue(), ^{
[selfaddNodeWithPath:path];
});
}
}
- (void)addNodeWithPath:(NSString*)path{
NSFileManager * fileManger = [NSFileManager defaultManager];
NSString*newPath = [NSStringstringWithFormat:@"%@/model.scnassets",path];
NSArray* dirArray = [fileMangercontentsOfDirectoryAtPath:newPatherror:nil];
SCNNode*secendNode = [SCNNode node];
NSError*ERROR =nil;
SCNScene *scene = [SCNScene sceneWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",newPath,@"dazhaohu.DAE"]] options:nil error:&ERROR];
secendNode = [scene.rootNodeclone];
[self.scnView.scene.rootNodeaddChildNode:secendNode];
for(SCNNode *node in self.scnView.scene.rootNode.childNodes){
node.scale=SCNVector3Make(2,2,2);
// node.position =SCNVector3Make(0, -3, -3);
node.eulerAngles=SCNVector3Make(0, -M_PI, 0);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
以上就是 网络下载模型并加载显示。如有问题请留言。