序言

移动应用创新赛马上决赛了,我们队伍还在为模型的生成而苦恼,在集训时有老师建议我们使用Object-Capture技术,通过拍照来生成模型,于是我去调研并切实地试了一下这个技术。

调研与使用

截止目前而言(2022.8月),该技术只支持测试版的使用,按照官方提供的操作流程,需要下载并安装macOS Monterey betaXcode 13 beta,这两个东西全部安装完毕后大约会占去电脑100G左右,并且在使用模拟器Simulator-Beta时,macbook会有明显的cpu使用率过高和发烫(我才买半年的macbook啥时候遭过这个罪)

如果只是想借助该工具生成模型,那大可不必去阅读文档和源码,更不用自己去用PhotogrammetrySession这个类去写一个Command-Line App,只需要使用官方提供的这个命令行工具(下载链接),打开之后在本机上运行。如果你的macbook能够Build Successfully,就会生成Products目录下的HelloPhotoframmetry这个Unix可执行文件,如下图所示,

截屏2022-08-26 15.45.39

而后点击Show in Finder,并在该文件目录下打开终端,如下图所示,

截屏2022-08-26 15.51.36

然后运行该可执行文件,如果输入的参数不合法,会提示你使用方法,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ ./HelloPhotogrammetry
Error: Missing expected argument '<input-folder>'

OVERVIEW: Reconstructs 3D USDZ model from a folder of images.

USAGE: hello-photogrammetry <input-folder> <output-filename> [--detail <detail>] [--sample-ordering <sample-ordering>] [--feature-sensitivity <feature-sensitivity>]

ARGUMENTS:
<input-folder> The local input file folder of images.
<output-filename> Full path to the USDZ output file.

OPTIONS:
-d, --detail <detail> detail {preview, reduced, medium, full, raw}
Detail of output model in terms of mesh size
and texture size . (default: nil)
-o, --sample-ordering <sample-ordering>
sampleOrdering {unordered, sequential}
Setting to sequential may speed up computation
if images are captured in a spatially
sequential pattern.
-f, --feature-sensitivity <feature-sensitivity>
featureSensitivity {normal, high} Set to high
if the scanned object does not contain a lot
of discernible structures, edges or textures.
-h, --help Show help information.

这里的参数说明已经讲得足够明白了,我也不再多赘述,只需要输入input-folderoutput-filename即可运行成功。当然,模型生成的质量取决于你拍照的质量,参考官网的一些demo模型,学习他们的拍照方式。

当成功生成模型后,我们会发现是带了贴图纹理的.usda格式文件,而.usdz格式正是把贴图纹理整合到了一个文件之中,我们使用Xcode打开这个.usda格式的文件,在File-->Export中选择.usdz格式导出即可。

如果想要在SceneKit中使用该模型,只要将文件移动至项目目录之中,然后类似如下代码获取即可。

1
let scene = SCNScene(named: "xxx.usdz")

最后,如果觉得生成的模型过大,放在项目文件目录中占用了太多的空间,可能可以选择将其放置在iCloud或者阿里云OSS等云端之中,这方面我们目前调研如何在Xcode中实现该功能。

总结

其实这个技术在很多地方都已经有了,比如这个网站就提供了3D模型生成的功能,最后生成的格式是.glb,但也可以通过Reality-Converter的方式转换为usdz的格式;相比庞大而又冗余的Beta版操作系统,以及使macbook发热发烫的Simulator而言,属实有点不值得。当然我们并没有去挖掘beta版的其他功能。在这给大家提个醒,beta版安装要慎重。

截屏2022-08-26 16.02.07