1 Star 0 Fork 17

antonetwo / 3d-model-convert-to-gltf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

3DModelConvertToGltf 统一模型格式转换工具

此项目产生的主要原因是工作中遇到了需要在Web中展示 STEP 和 IGES 模型的场景,但是市面上的web3d类库均不支持此格式,并且用户上传的STL文件直接展示会占用大量带宽或CDN流量,转换为压缩后的gltf会比较合适。

样例文件压缩效果如下表:

文件类型 文件名 转换时间 原大小 转换后大小
stl assets/test.stl 2368.890ms 7.6 MB 86 KB
iges assets/test.iges 1641.226ms 1 M 111 KB
stp assets/test.stp 2969.200ms 5.1 MB 217 KB
fbx assets/Samba Dancing.fbx <1000ms 3.7 MB 614 KB

支持输入格式: STL/IGES/STP/OBJ/FBX

支持输出格式: GLTF/GLB

本项目即采用了博客中总结的思路:STEP和IGES模型转换为适用Web的glb格式

项目状态: 维护中

文档

中文|English

待完成任务

  • 基本项目结构规划及接口设计
  • 转换及压缩代码实现
  • 增加 obj 的格式转换
  • 相关API接口实现(考虑了下模型转换功能的特性,单纯提供API用处不大)
  • docker镜像打包
  • 一键转换脚本封装
  • 在线转换预览
  • [bug] stp 转 gltf 最终文件太大

为什么不用 assmip

我尝试用过 assimp,但是在 stl/iges/obj 转换场景测试下结果不大理想,我使用的 https://hub.docker.com/r/dylankenneally/assimp 打包好的环境进行测试,感兴趣的也可以试一试。

为什么不直接在本项目提供API

模型转换是一个非常消耗性能并且速度不快的服务,模型的上传和下载都会非常的耗费带宽,如果直接部署在自己的服务器上会是一个非常耗费带宽和CPU的工作,针对这种大型文件上传和下载比较通用的方式是引入 OSS 和 CDN 配合队列和后端服务动态扩容来做,但是部署成本和实施成本都会比较高,有商业需求请联系 admin@wj2015.com 获取商业API支持。

快速上手

由于环境配置麻烦等原因,命令行模式依旧需要依赖docker,命令行模式适合服务端简单调用,转换过程阻塞进程同步进行,无法分布式部署增加并发量等

PS:命令行模式同步转换模型过多或者单个模型过大时,有把提供Web服务的服务器卡住的风险

在线转换预览

可以使用 modelbox-sdk 在线转换模型(<100MB),链接:https://wangerzi.gitee.io/modelbox-sdk/examples/index.html

命令行模式

下载代码中的 convert.sh,赋予执行权限,执行如下指令即可,第二个参数可支持 stl|stp|iges|obj|fbx,根据文件类型而定。

脚本依赖于docker环境,所以 Docker 环境先准备好吧。

convert.sh stl inputpath.stl outputpath.glb # 生成二进制glb文件
convert.sh stl inputpath.stl outputpath.gltf # 非单一二进制文件 gltf

assets 目录中,有五个测试文件 test.stl test.stp test.igs E 45 Aircraft_obj.obj Samba Dancing.fbx,将其复制到项目路径下,按照上述指令执行即可看到生成了对应结果。

如果在运行过程中遇到如下错误,可以执行如下指令,将其他语言执行器对应用户加到 docker 用户组中。

报错信息:

docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/create: dial unix /var/run/docker.sock: connect: permission denied.

执行指令:

usermod -a -G docker nginx

通过其他语言调用可同步判断输出文件是否存在,来判断是否转换成功,如:

<?php
$out = 'out.glb';
$input = 'test.stl';
$type = 'stl';
// 请注意安全,小心任意执行漏洞
shell_exec('convert.sh '.$type.' '.$input.' '.$out);
if (file_exists($out)) {
    echo "convert result:" . $out;
} else {
    echo "convert failed";
}

配置说明

下列为默认配置 server/config/app.yaml ,请按需更改,如果使用docker需要映射配置文件

app:
    # 保存临时文件(原模型文件)
    save_upload_temp_file: 1
    # 保存转换过程文件(模型格式转换文件)
    save_convert_temp_file: 0
    # 后台并发处理数量(仅api)
    background_process_num: 3
# 上传路径配置(仅 API)
upload:
    path: uploads/
    # 单位: Mb
    maxsize: 30

Docker运行

在宿主机安装好 docker 的条件下,运行如下指令获取镜像(大约4G)

docker pull wj2015/3d-model-convert-to-gltf

在容器内执行 conda run -n pythonocc python convert.py stl input.stl out.glb 可同步转换文件

简单负载示意图

如果有多机负载的需求,可借助 nginx 的反向代理做一下简单的负载均衡或者辅助消息队列以及生产者消费者来做,HTTP 服务或队列部分需要自己实现逻辑。

1583754967257

参与开发

docker开发环境

首先把 dockerdocker-compose 安装好,参考官方文档:Docker Desktop

随后,进入 environment/ 文件夹,执行 docker-compose up,运行结果如下即表示成功

user@MacBook-Pro environment % docker-compose up
Recreating 3d-model-convert-to-gltf-app ... done
Starting 3d-model-convert-to-gltf-redis ... done
Attaching to 3d-model-convert-to-gltf-redis, 3d-model-convert-to-gltf-app
3d-model-convert-to-gltf-redis | 1:C 09 Oct 2020 03:03:29.150 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3d-model-convert-to-gltf-redis | 1:C 09 Oct 2020 03:03:29.150 # Redis version=6.0.1, bits=64, commit=00000000, modified=0, pid=1, just started
3d-model-convert-to-gltf-redis | 1:C 09 Oct 2020 03:03:29.150 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
3d-model-convert-to-gltf-redis | 1:M 09 Oct 2020 03:03:29.152 * Running mode=standalone, port=6379.
3d-model-convert-to-gltf-redis | 1:M 09 Oct 2020 03:03:29.152 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3d-model-convert-to-gltf-redis | 1:M 09 Oct 2020 03:03:29.152 # Server initialized

如果出现端口冲突,初始化失败等异常情况,请根据错误信息查找资料进行排查。

新开一个终端,执行 docker ps 指令查看目前运行的镜像,查看到如下类似的镜像结果表示运行成功

user@MacBook-Pro 3d-model-convert-to-gltf % docker ps
CONTAINER ID        IMAGE                             COMMAND                  CREATED             STATUS              PORTS               NAMES
69b684ed7755        wj2015/3d-model-convert-to-gltf   "conda run -n python…"   3 seconds ago       Up 2 seconds                            3d-model-convert-to-gltf-app
20eb8ede5da7        redis                             "docker-entrypoint.s…"   2 hours ago         Up 2 seconds        6379/tcp            3d-model-convert-to-gltf-redis

接下来进入容器执行命令,并进入 pythonocc 环境,在此环境下执行脚本可以方便的进行更改代码和调试

wangjie@MacBook-Pro 3d-model-convert-to-gltf % docker exec -it 3d-model-convert-to-gltf-app /bin/bash
(base) root@5efd6ef96814:/opt/3d-model-convert-to-gltf# conda activate pythonocc
(pythonocc) root@69b684ed7755:/opt/3d-model-convert-to-gltf# python server/convert.py 
Params not found, format: python convert.py [type] [file path] [out file path]

非 docker 搭建环境

主要针对无法运行 docker 的开发者,可以尝试使用此方法搭建一个开发环境

首先创建一下 conda 虚拟环境

conda create -n 3d-model-convert-to-gltf-pythonocc -c dlr-sc -c pythonocc pythonocc-core=7.4.0rc1
conda activate 3d-model-convert-to-gltf-pythonocc
pip install -r server/requirements.txt

本地的 node 版本需要是 12.0.0,否则 gltf-pipeline 无法运行,需要安装 gltf-pipelineobj2gltf 两个 npm 包。

本地调试环境安装指引

可以使用如下指令安装 nvm(MacOs or Linux),Windows需下载可执行文件 https://github.com/coreybutler/nvm-windows

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

安装 12.0.0,以及安装依赖包

nvm install 12.0.0
nvm use 12.0.0
npm install -g gltf-pipeline obj2gltf

再去 FBX2glTF 下载对应系统的可执行文件,放到环境变量文件夹中,地址: https://github.com/facebookincubator/FBX2glTF

PS: 为保证调用命名统一,需要将指令重命名为 fbx2gltf,否则转换 FBX 出错

简单了解下代码结构,修改完毕后提交PR即可,欢迎邮箱 admin@wj2015.com 与我讨论

开源协议

3DModelConvertToGltf is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

Convert 3d model (STL/IGES/STEP/OBJ/FBX) to gltf and compression 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/antonetwo_admin/model-convert-to-gltf.git
git@gitee.com:antonetwo_admin/model-convert-to-gltf.git
antonetwo_admin
model-convert-to-gltf
3d-model-convert-to-gltf
master

搜索帮助