Пример #1
0
    void Start()
    {
        objLoader = new OBJLoader(objPath);
        material  = new Material(Shader.Find("PS1"));
        if (objLoader != null)
        {
            int shapeCounter = 0;
            foreach (OBJShape shape in objLoader.shapeList)
            {
                GameObject   go           = new GameObject("Shape" + shapeCounter);
                MeshFilter   filter       = go.AddComponent <MeshFilter>();
                MeshRenderer meshRenderer = go.AddComponent <MeshRenderer>();
                meshRenderer.material = material;
                List <Vector3> vertexList  = new List <Vector3>();
                List <Vector3> normalsList = new List <Vector3>();
                List <Vector2> uvList      = new List <Vector2>();
                List <Color>   colorList   = new List <Color>();
                Vector3        center      = Vector3.zero;
                if (shape.IndeciesVertex.Count == 3) //triangles
                {
                    Gizmos.color = new Color(1.0f, 1.0f, 0.0f, 0.2f);

                    for (int i = 0; i < shape.IndeciesVertex.Count; i++)
                    {
                        int vertexIndex = shape.IndeciesVertex[i];
                        int normalIndex = -65536;
                        int uvIndex     = -65536;

                        if (vertexIndex < 0)
                        {
                            vertexIndex = objLoader.vertexList.Count + vertexIndex;
                        }
                        if (shape.IndeciesNormal.Count > i && objLoader.normalsList.Count > 0)
                        {
                            normalIndex = shape.IndeciesNormal[i];
                            if (normalIndex != -65536 && normalIndex < 0)
                            {
                                normalIndex = objLoader.normalsList.Count + normalIndex;
                            }
                        }
                        if (shape.IndeciesUV.Count > i && objLoader.uvList.Count > 0)
                        {
                            uvIndex = shape.IndeciesUV[i];
                            if (uvIndex != -65536 && uvIndex < 0)
                            {
                                uvIndex = objLoader.uvList.Count + uvIndex;
                            }
                        }

                        center += objLoader.vertexList[vertexIndex];
                        vertexList.Add(objLoader.vertexList[vertexIndex]);
                        if (normalIndex != -65536)
                        {
                            normalsList.Add(objLoader.normalsList[normalIndex]);
                        }
                        if (uvIndex != -65536)
                        {
                            uvList.Add(objLoader.uvList[uvIndex]);
                        }

                        colorList.Add(Color.yellow);
                    }

                    center.x /= 3f;
                    center.y /= 3f;
                    center.z /= 3f;
                    if (normalsList.Count == 0)
                    {
                        Vector3 normal = GetPolygonNormal(vertexList.ToArray());
                        for (int i = 0; i < shape.IndeciesVertex.Count; i++)
                        {
                            normalsList.Add(normal);
                        }
                    }
                    if (uvList.Count == 0)
                    {
                        uvList.Add(new Vector2(0f, 0f));
                        uvList.Add(new Vector2(0f, 1f));
                        uvList.Add(new Vector2(1f, 0f));
                    }

                    filter.mesh = new Mesh
                    {
                        vertices  = vertexList.ToArray(),
                        triangles = new int[] { 0, 1, 2 },
                        colors    = colorList.ToArray(),
                        normals   = normalsList.ToArray(),
                        uv        = uvList.ToArray()
                    };
                }
                else // quad
                {
                    Gizmos.color = new Color(0.0f, 1.0f, 1.0f, 0.2f);
                    for (int i = 0; i < shape.IndeciesVertex.Count; i++)
                    {
                        int vertexIndex = shape.IndeciesVertex[i];
                        int normalIndex = -65536;
                        int uvIndex     = -65536;

                        if (vertexIndex < 0)
                        {
                            vertexIndex = objLoader.vertexList.Count + vertexIndex;
                        }
                        if (shape.IndeciesNormal.Count > i && objLoader.normalsList.Count > 0)
                        {
                            normalIndex = shape.IndeciesNormal[i];
                            if (normalIndex != -65536 && normalIndex < 0)
                            {
                                normalIndex = objLoader.normalsList.Count + normalIndex;
                            }
                        }
                        if (shape.IndeciesUV.Count > i && objLoader.uvList.Count > 0)
                        {
                            uvIndex = shape.IndeciesUV[i];
                            if (uvIndex != -65536 && uvIndex < 0)
                            {
                                uvIndex = shape.IndeciesUV.Count + uvIndex;
                            }
                        }

                        center += objLoader.vertexList[vertexIndex];
                        vertexList.Add(objLoader.vertexList[vertexIndex]);
                        if (normalIndex != -65536)
                        {
                            normalsList.Add(objLoader.normalsList[normalIndex]);
                        }
                        if (uvIndex != -65536)
                        {
                            uvList.Add(objLoader.uvList[uvIndex]);
                        }

                        colorList.Add(Color.green);
                    }
                    center.x /= 4f;
                    center.y /= 4f;
                    center.z /= 4f;
                    if (normalsList.Count == 0)
                    {
                        Vector3 normal = GetPolygonNormal(vertexList.ToArray());
                        for (int i = 0; i < vertexList.Count; i++)
                        {
                            normalsList.Add(normal);
                        }
                    }
                    if (uvList.Count == 0)
                    {
                        if (vertexList.Count == 4)
                        {
                            uvList.Add(new Vector2(0f, 0f));
                            uvList.Add(new Vector2(0f, 1f));
                            uvList.Add(new Vector2(1f, 0f));
                            uvList.Add(new Vector2(1f, 1f));
                        }
                        else
                        {
                            Debug.Log("huh");
                        }
                    }

                    filter.mesh = new Mesh
                    {
                        vertices  = vertexList.ToArray(),
                        triangles = new int[] { 0, 1, 2, 0, 2, 3 },
                        colors    = colorList.ToArray(),
                        normals   = normalsList.ToArray(),
                        uv        = uvList.ToArray()
                    };
                }
                go.transform.parent = transform;

                ModelQuadVisualiser quadVisualiser = go.AddComponent <ModelQuadVisualiser>();
                quadVisualiser.mesh     = filter.mesh;
                quadVisualiser.Renderer = meshRenderer;
                quadVisualiser.Center   = center;
                if (normalsList.Count > 0)
                {
                    quadVisualiser.Normal = normalsList[0];
                }
                else
                {
                    quadVisualiser.Normal = GetPolygonNormal(vertexList.ToArray());
                }
                MeshCollider collider = go.AddComponent <MeshCollider>();
                collider.sharedMesh     = filter.mesh;
                quadVisualiser.Collider = collider;
                if (shape.IndeciesNormal.Count > 0)
                {
                    for (int i = 0; i < normalsList.Count; i++)
                    {
                        normalsList[i] = -normalsList[i];
                    }

                    Mesh reverseMesh = new Mesh
                    {
                        vertices  = vertexList.ToArray(),
                        normals   = normalsList.ToArray(),
                        triangles = filter.mesh.triangles
                    };
                    MeshCollider collider2 = go.AddComponent <MeshCollider>();
                    collider2.sharedMesh = reverseMesh;
                }

                shapeCounter++;
            }
        }
    }
Пример #2
0
    private void Start()
    {
        Mesh mesh = GetComponent <MeshFilter>().mesh;

        int[]     triangles = mesh.triangles;
        Vector3[] vertices  = mesh.vertices;
        Vector3[] normals   = mesh.normals;
        triangles = mesh.triangles;
        //var neighbours = MeshQuadNeighbors.GetNeighbors(mesh).ToArray();

        Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, transform.rotation, transform.lossyScale);

        for (int i = 0; i < (int)Mathf.Floor((triangles.Length / 6.0f)) * 6.0f; i += 6)
        {
            int i1 = triangles[i + 0];
            int i2 = triangles[i + 1];
            int i3 = triangles[i + 2];
            int i4 = -4;
            for (int j = 3; j < 6; j++)
            {
                if (triangles[i + j] != i1 && triangles[i + j] != i2 && triangles[i + j] != i3)
                {
                    i4 = triangles[i + j];
                }
            }

            if (i1 < 0)
            {
                continue;
            }
            if (i2 < 0)
            {
                continue;
            }
            if (i3 < 0)
            {
                continue;
            }
            if (i4 < 0)
            {
                continue;
            }
            Vector3 p1 = matrix.MultiplyPoint(vertices[i1]) + transform.position;
            Vector3 p2 = matrix.MultiplyPoint(vertices[i2]) + transform.position;
            Vector3 p3 = matrix.MultiplyPoint(vertices[i3]) + transform.position;
            Vector3 p4 = matrix.MultiplyPoint(vertices[i4]) + transform.position;
            Vector3 n1 = normals[i1];
            Vector3 n2 = normals[i2];
            Vector3 n3 = normals[i3];
            Vector3 n4 = normals[i4];

            Mesh quadMesh = new Mesh();

            Vector3[] meshVertices = new Vector3[4]
            {
                p1, p2, p3, p4
            };
            Vector3[] meshNormals = new Vector3[4]
            {
                n1, n2, n3, n4
            };
            quadMesh.vertices = meshVertices;
            quadMesh.normals  = meshNormals;

            int[] meshTris = new int[6]
            {
                0, 1, 2,
                0, 2, 3
            };
            quadMesh.triangles = meshTris;

            Vector3 c = new Vector3((p1.x + p2.x + p3.x + p4.x) / 4f, (p1.y + p2.y + p3.y + p4.y) / 4f,
                                    (p1.z + p2.z + p3.z + p4.z) / 4f);

            GameObject meshGo = new GameObject("Quad" + i / 4);
            meshGo.transform.parent = transform;
            //meshGo.transform.position = c;
            ModelQuadVisualiser quadVisualiser = meshGo.AddComponent <ModelQuadVisualiser>();
            quadVisualiser.mesh = quadMesh;
            MeshCollider collider = meshGo.AddComponent <MeshCollider>();
            collider.sharedMesh = quadMesh;
            MeshFilter meshFilter = meshGo.AddComponent <MeshFilter>();
            meshFilter.mesh = quadMesh;
            MeshRenderer meshRenderer = meshGo.AddComponent <MeshRenderer>();
            meshRenderer.material   = material;
            quadVisualiser.Renderer = meshRenderer;
            quadVisualiser.Center   = c;
            quadVisualiser.Collider = collider;
        }
    }