导出obj时如何对三角形进行排序 [英] how to order triangles when exporting obj

查看:26
本文介绍了导出obj时如何对三角形进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将网格导出到具有统一性的 obj 中,我找到了 2 个执行此操作的脚本,但是它们导出三角形的方式完全不同,我想了解每个三角形的原因:

第一:

 foreach(Vector3 lv in m.vertices){Vector3 wv = mf.transform.TransformPoint(lv);//这有点难看 - 反转 x 分量,因为我们在//与每个人"习惯"不同的坐标系.sb.Append(string.Format("v {0} {1} {2}
",-wv.x,wv.y,wv.z));}foreach(m.normals 中的 Vector3 lv){Vector3 wv = mf.transform.TransformDirection(lv);sb.Append}for (int i=0;i

(我保留了评论),http://wiki.unity3d.com/index.php?title=ObjExporter

这是第一种方法,下面是第二种:

foreach(Vector3 vv in m.vertices){Vector3 v = t.TransformPoint(vv);numVertices++;sb.Append(string.Format("v {0} {1} {2}
",v.x,v.y,-v.z));}sb.Append("
");foreach(m.normals 中的 Vector3 nn){Vector3 v = r * nn;sb.Append(string.Format("vn {0} {1} {2}
",-v.x,-v.y,v.z));}for (int i=0;i

链接:http://wiki.unity3d.com/index.php?title=ExportOBJ

第一种方法效果很好,第二种方法也可以,但它在向上轴上旋转了 180 度.问题是我不明白为什么他们需要对第一种方法中的三角形组件重新排序并且仍然有效,以及为什么第二种方法中的正常组件符号与位置组件符号不匹配但仍然有效

解决方案

我不明白为什么他们需要重新排列三角形组件

许多渲染器使用三角形点的顺序来指示多边形的哪一边是三角形的前"或后".如果启用背面剔除,这一点尤其重要.

您可以尝试在网格上混合顺时针和逆时针三角形;Unity 的默认行为将剔除背面,您最终会看到一堆洞!

在导入/导出几何数据时,翻转一两个轴是很常见的.如果每种文件格式都同意这种事情会很方便,但有时他们不同意.

I'm trying to export a mesh into an obj with unity and I found 2 script that does it but the way they export the triangles is quite different and I would like to understand the reason for each one of them:

first:

        foreach(Vector3 lv in m.vertices) 
        {
            Vector3 wv = mf.transform.TransformPoint(lv);

            //This is sort of ugly - inverting x-component since we're in
            //a different coordinate system than "everyone" is "used to".
            sb.Append(string.Format("v {0} {1} {2}
",-wv.x,wv.y,wv.z));
        }

    foreach(Vector3 lv in m.normals) 
    {
        Vector3 wv = mf.transform.TransformDirection(lv);

        sb.Append}

            for (int i=0;i<triangles.Length;i+=3) 
            {
                //Because we inverted the x-component, we also needed to alter the triangle winding.
                sb.Append(string.Format("f {1}/{1}/{1} {0}/{0}/{0} {2}/{2}/{2}
", 
                    triangles[i]+1 + vertexOffset, triangles[i+1]+1 + normalOffset, triangles[i+2]+1 + uvOffset));
            }

(I kept the comments), http://wiki.unity3d.com/index.php?title=ObjExporter

this is the first method, and the second below:

foreach(Vector3 vv in m.vertices)
        {
            Vector3 v = t.TransformPoint(vv);
            numVertices++;
            sb.Append(string.Format("v {0} {1} {2}
",v.x,v.y,-v.z));
        }
        sb.Append("
");
        foreach(Vector3 nn in m.normals) 
        {
            Vector3 v = r * nn;
            sb.Append(string.Format("vn {0} {1} {2}
",-v.x,-v.y,v.z));
        }
        for (int i=0;i<triangles.Length;i+=3) {
                sb.Append(string.Format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}
", 
                    triangles[i]+1+StartIndex, triangles[i+1]+1+StartIndex, triangles[i+2]+1+StartIndex));
            }

link: http://wiki.unity3d.com/index.php?title=ExportOBJ

The first method works perfectly and the second one aswell but it's rotated 180degres in the Up axis. The thing is I don't understand why they need to reorder the triangle components on the first method and still works and why the normal components sign in the second method doesn't match with the position components sign but still works

解决方案

I don't understand why they need to reorder the triangle components

Many renderers use the ordering of triangle points to indicate which side of a polygon is the "front" or "back" of the triangle. That's especially important if back-face culling is enabled.

You can try mixing clockwise and counter-clockwise triangles on a mesh; Unity's default behavior will cull the back faces and you'll end up seeing a bunch of holes!

Flipping an axis or two is fairly common when importing/exporting geometry data. It would be convenient if every file format agreed on that sort of thing, but sometimes they don't.

这篇关于导出obj时如何对三角形进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆