纹理映射过程后如何合并两个 .obj 文件?
在 3D 网格上应用纹理后,会生成三个文件(包括一个 .obj、.mtl 和一个 atlas 文件).我已经在 2 个对象上映射了纹理,现在我想将这些对象合并在一起,但我不知道如何合并它们的文件.有什么方法(不是非免费工具)吗?
After applying texture on 3D mesh, three files (including a .obj, .mtl and an atlas file) are generated. I have mapped textures on 2 objects and now I want to merge these objects together, but I do not know how to merge their files. Is there any method(not non free tool)?
推荐答案
单对象波前obj文件的naive文件合并相对容易:
The naive file merging for single object wavefront obj files is relatively easy:
将第一个文件读入内存
记住每个表的条目数
将第二个文件附加到内存中(人脸除外)
将第二个文件的面追加到内存中
这是唯一需要改变的东西.所有索引都来自 1
因此您需要将每个表的大小从上一个文件添加到它.例如,如果第一个 obj 有 10 行以 v
开头,这意味着在附加第二个 obj 后将开始从 11
引用它的点.所以获取每个顶点索引并为其添加大小.
This is the only stuff that need to change a bit. All the indexes are from 1
so you need to add the size of each table from previous file to it. For example if first obj got 10 lines starting with v
that means after appending the second obj will start referencing its points from 11
instead. so take each vertex index and add the size to it.
这同样适用于您获得的任何信息,例如法线、纹理坐标或其他任何信息.
The same goes for any info you got like normals, texture coords or what ever.
将整个内容保存为单个 obj 文件
抱歉,我不使用材料扩展名,所以我不知道格式以及是否需要在 mtl 文件中进行任何更改(但我不这么认为).
Sorry I do not use the material extensions so I do not know the format and if any changes are needed inside mtl file too (but I do not think so).
如果您还想更新网格(删除不可见的相交部分),那么您需要使用一些几何方法(不是小问题).
If you want to have also the mesh updated (removing the invisible intersected part) then you need to use some geometry approach (not trivial problem).
只是为了确定这里的小例子......
Just to be sure here small example...
文件 1:
v -1.0 -1.0 -1.0
v +1.0 -1.0 -1.0
v +1.0 +1.0 -1.0
v -1.0 +1.0 -1.0
v -1.0 -1.0 +1.0
v +1.0 -1.0 +1.0
v +1.0 +1.0 +1.0
v -1.0 +1.0 +1.0
f 1 2 3 4
f 5 6 7 8
f 1 2 6 5
f 2 3 7 6
f 3 4 8 7
f 4 1 5 8
文件2:
v -1.0 -1.0 +1.0
v +1.0 -1.0 +1.0
v +1.0 +1.0 +1.0
v -1.0 +1.0 +1.0
v -2.0 -2.0 +2.0
v +2.0 -2.0 +2.0
v +2.0 +2.0 +2.0
v -2.0 +2.0 +2.0
f 1 2 3 4
f 5 6 7 8
f 1 2 6 5
f 2 3 7 6
f 3 4 8 7
f 4 1 5 8
合并:
v -1.0 -1.0 -1.0
v +1.0 -1.0 -1.0
v +1.0 +1.0 -1.0
v -1.0 +1.0 -1.0
v -1.0 -1.0 +1.0
v +1.0 -1.0 +1.0
v +1.0 +1.0 +1.0
v -1.0 +1.0 +1.0
v -1.0 -1.0 +1.0
v +1.0 -1.0 +1.0
v +1.0 +1.0 +1.0
v -1.0 +1.0 +1.0
v -2.0 -2.0 +2.0
v +2.0 -2.0 +2.0
v +2.0 +2.0 +2.0
v -2.0 +2.0 +2.0
f 1 2 3 4
f 5 6 7 8
f 1 2 6 5
f 2 3 7 6
f 3 4 8 7
f 4 1 5 8
f 9 10 11 12
f 13 14 15 16
f 9 10 14 13
f 10 11 15 14
f 11 12 16 15
f 12 9 13 16
File1 有 8
个顶点,因此 File2 的 f
中的每个顶点索引增加了 8代码>.我手动完成了整个示例(包括 File1,2),所以希望我没有犯一些愚蠢的错误,但是预览还可以,所以看起来不是这样.
File1 has 8
vertexes so each vertex index in f
from File2 is increased by 8
. I did the whole example manually (including File1,2) so hope I did not make some silly mistake but the previews are OK so looks like not the case.
如果您想消除重复条目(为了空间和速度),那么您需要为每个表重新索引表并使用它而不是仅仅添加...
If you want to eliminate duplicate entries (for space and speed) then you need to have reindexing table for each table and use it instead of just adding...
相关文章