欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

详述在iOS中利用assimpKit加载FBX模型的步骤

最编程 2024-08-04 09:45:34
...
研究背景
体积:一个.dae模型大概有1M那么大  而.fbx模型0.5M
现状
xcode现在不能直接读取.fbx模型
方案
1.使用assimpKit加载
2.使用Wish3D加载
结果
1.使用assimpKit--可行
2.使用Wish3D-- 官方文档有问题,然后咨询还不回复----直接丢弃
原理

原理篇

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

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


image.png
image.png

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


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:

有两种解决方法

image.png

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

image.png

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

5.设置bitcode为no


image.png
开发中遇到的问题
1.加载fbx模型的动画 只有部分节点有动画
2.控制动画的播放但动画结束的时候会很突兀
解决办法

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

image.png

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


image.png

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

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

推荐阅读