前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS使用assimpKit加载FBX模型步骤详解

iOS使用assimpKit加载FBX模型步骤详解

原创
作者头像
发布2018-05-07 15:05:07
3.3K2
发布2018-05-07 15:05:07
举报
文章被收录于专栏:浅探ARKit浅探ARKit

#####研究背景

代码语言:txt
复制
体积:一个.dae模型大概有1M那么大  而.fbx模型0.5M

#####现状

代码语言:txt
复制
xcode现在不能直接读取.fbx模型

#####方案

代码语言:txt
复制
1.使用assimpKit加载
2.使用Wish3D加载

#####结果

代码语言:txt
复制
1.使用assimpKit--可行
2.使用Wish3D-- 官方文档有问题,然后咨询还不回复----直接丢弃

#####原理

原理篇

代码语言:txt
复制
1.读取模型的  顶点坐标  纹理坐标  法线  骨骼顶点  骨骼动画 

2.调用以下方法画出模型 

根据顶点坐标和模型类型画出模型,模型类型可以是点、线、三角形
为什么是三角形呢?因为三角形是最小边几何图形

+ (instancetype)geometrySourceWithVertices:(const SCNVector3 *)vertices count:(NSInteger)count;

3.调用以下方法设置纹理
+ (instancetype)geometrySourceWithNormals:(const SCNVector3 *)normals count:(NSInteger)count;
用以下方法设置模型图片 就是设置node的纹理
SCNGeometry *geo = [SCNGeometry geometryWithSources:sources elements:elements];
UIImage * image  = [UIImage imageNamed:@"xy.jpg"];
SCNMaterial *mat = [SCNMaterial material];
mat.diffuse.contents = image;
geo.firstMaterial = mat;
geo.firstMaterial.doubleSided = YES;

4.调用以下方法画出法线
+ (instancetype)geometrySourceWithTextureCoordinates:(const CGPoint *)texcoord count:(NSInteger)count;

5.调用以下方法画出骨骼顶点
+ (instancetype)geometrySourceWithData:(NSData *)data semantic:(SCNGeometrySourceSemantic)semantic vectorCount:(NSInteger)vectorCount floatComponents:(BOOL)floatComponents componentsPerVector:(NSInteger)componentsPerVector bytesPerComponent:(NSInteger)bytesPerComponent dataOffset:(NSInteger)offset dataStride:(NSInteger)stride;

6.调用以下方法设置与骨骼顶点向连接的部分
+ (instancetype)geometrySourceWithData:(NSData *)data semantic:(SCNGeometrySourceSemantic)semantic vectorCount:(NSInteger)vectorCount floatComponents:(BOOL)floatComponents componentsPerVector:(NSInteger)componentsPerVector bytesPerComponent:(NSInteger)bytesPerComponent dataOffset:(NSInteger)offset dataStride:(NSInteger)stride;

7.调用以下方法设置骨骼动画皮肤那方面的
+ (instancetype)geometrySourceWithData:(NSData *)data semantic:(SCNGeometrySourceSemantic)semantic vectorCount:(NSInteger)vectorCount floatComponents:(BOOL)floatComponents componentsPerVector:(NSInteger)componentsPerVector bytesPerComponent:(NSInteger)bytesPerComponent dataOffset:(NSInteger)offset dataStride:(NSInteger)stride

8.调用以下方法设置把骨骼相信存放到一个SCNSkinner类
+ (instancetype)skinnerWithBaseGeometry:(nullable SCNGeometry *)baseGeometry bones:(NSArray<SCNNode *> *)bones boneInverseBindTransforms:(nullable NSArray<NSValue *> *)boneInverseBindTransforms boneWeights:(SCNGeometrySource *)boneWeights boneIndices:(SCNGeometrySource *)boneIndices API_AVAILABLE(macos(10.10));

9.调用以下方法设置骨骼动画的值和类型
+ (SCNAction *)repeatActionForever:(SCNAction *)action;

10.调用以下方法运行骨骼动画
- (void)runAction:(SCNAction *)action API_AVAILABLE(macos(10.10));

#####把assimpKit接入项目

1.到 assimpKit 下载 文件

文件很大的 要找到ios的

2.把assimpKit.frame 拖入工程里

image.png
image.png

3.把下面的文件导入工程里

image.png
image.png
image.png
image.png

4.然后再设置动态库 (不是静态)

image.png
image.png

还有

image.png
image.png

如果出现了

"_compress", referenced from:

“_uncompress", referenced from:

"_compress2", referenced from:

"_inflateReset", referenced from:

"inflateInit", referenced from:

"_inflateEnd", referenced from:

"inflateInit2", referenced from:

有两种解决方法

1.

image.png
image.png

这个可以添加多个的 就是本来有其他的xxx 还是可以添加-lz的

2.

image.png
image.png

主要是assimpKit里有.a 的压缩文件 我们要添加压缩的库区解压

5.设置bitcode为no

image.png
image.png

#####开发中遇到的问题

代码语言:txt
复制
1.加载fbx模型的动画 只有部分节点有动画
2.控制动画的播放但动画结束的时候会很突兀

#####解决办法

1.查看源码 他是遍历到每个子节点然后把动画添加上去 但是有一些节点根本加不上去 所以我们需要改一下他的源码

image.png
image.png

当了这个bug他们开发团队也知道了 并且标为了bug

image.png
image.png

2.查看源码 发现他是没有用到fadeOutDuration这个属性 可能是遗留了

image.png
image.png

#####后记

代码语言:txt
复制
assimpKit这个库   功能很强   虽然不是很成熟   但我们可以在他们基础上修改一下代码  来实现我们的功能   当然了   如果发现bug    还可以和他们开发团队提一下   让这个框架帮助更多的开发人员 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档