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

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

问题描述

我正在尝试将网格物体统一导出到obj中,我发现有2个脚本可以做到这一点,但是它们导出三角形的方式却大不相同,我想了解每个三角形的原因:

第一:

        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}\n",-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}\n", 
                    triangles[i]+1 + vertexOffset, triangles[i+1]+1 + normalOffset, triangles[i+2]+1 + uvOffset));
            }

(我保留了评论), 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}\n",v.x,v.y,-v.z));
        }
        sb.Append("\n");
        foreach(Vector3 nn in m.normals) 
        {
            Vector3 v = r * nn;
            sb.Append(string.Format("vn {0} {1} {2}\n",-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}\n", 
                    triangles[i]+1+StartIndex, triangles[i+1]+1+StartIndex, triangles[i+2]+1+StartIndex));
            }

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

第一种方法效果很好,第二种方法效果也很好,但它已沿Up轴旋转了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}\n",-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}\n", 
                    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}\n",v.x,v.y,-v.z));
        }
        sb.Append("\n");
        foreach(Vector3 nn in m.normals) 
        {
            Vector3 v = r * nn;
            sb.Append(string.Format("vn {0} {1} {2}\n",-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}\n", 
                    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天全站免登陆