Example #1
0
        public static void ApplyTransformToMesh(MeshFilter meshFilter)
        {
            Transform transform = meshFilter.transform;
            //Matrix4x4 matrix = transform.localToWorldMatrix;
            Mesh mesh = meshFilter.mesh;

            Vector3[] vertices  = mesh.vertices;
            Vector3[] normals   = mesh.normals;
            Vector2[] uvs       = mesh.uv;
            var       triangles = new TriangleArray(mesh.triangles);

            for (int i = 0; i < vertices.Length; ++i)
            {
                vertices[i] = transform.TransformPoint(vertices[i]);
                normals[i]  = transform.TransformVector(normals[i]);
            }

            // Flip triangle order if needed
            {
                Vector3 s = transform.localScale;
                if (s.x * s.y * s.z < 0)
                {
                    triangles.ReverseTriangles();
                }
            }

            transform.ResetTransform();
            mesh.Clear(false);
            mesh.vertices  = vertices;
            mesh.normals   = normals;
            mesh.uv        = uvs;
            mesh.triangles = (int[])triangles;
            mesh.UploadMeshData(false);
        }
Example #2
0
        public static RawTriangle GetRawTriangleByIndex(this Mesh mesh, int index)
        {
            Vector3[]       vertices = mesh.vertices;
            IndexedTriangle t        = new TriangleArray(mesh.triangles)[index];

            return(new RawTriangle(
                       vertices[t.i1],
                       vertices[t.i2],
                       vertices[t.i3]
                       ));
        }
Example #3
0
        public MeshWalker(Mesh mesh, MeshIndex meshIndex)
        {
            if (mesh == null || meshIndex == null)
            {
                throw new System.ArgumentNullException();
            }

            this.mesh      = mesh;
            this.meshIndex = meshIndex;

            triangles = new TriangleArray(mesh.triangles);
            vertices  = mesh.vertices;
            normals   = mesh.normals;

            triangleCoords = new Vector2[3];
        }
Example #4
0
        private void GenerateIndex(Mesh mesh)
        {
            TriangleArray triangles = mesh.GetSaneTriangles(0);

            for (int i = 0; i < triangles.Length; ++i)
            {
                IndexedTriangle triangle = triangles[i];

                for (int j = 0; j < 3; ++j)
                {
                    var edge = new IndexedEdge(
                        triangle[j],
                        triangle[(j + 1) % 3]
                        );
                    edgeToTriangleMap.Add(edge, i);
                }
            }
        }
Example #5
0
        public static void FlipNormals(this Mesh mesh, int submeshIndex = 0)
        {
            // Triangle orientation
            {
                TriangleArray triangles = mesh.GetSaneTriangles(submeshIndex);
                for (int i = 0; i < triangles.Length; ++i)
                {
                    triangles[i] = triangles[i].Reversed;
                }

                mesh.triangles = (int[])triangles;
            }

            // Normals
            {
                Vector3[] normals = mesh.normals;
                for (int i = 0; i < normals.Length; ++i)
                {
                    normals[i] = -normals[i];
                }

                mesh.normals = normals;
            }
        }