Ejemplo n.º 1
0
    // will also remove any triangles which are using the verts
    public static void RemoveVerts(List <int> vertsToRemove, Mesh mesh)
    {
        EditedMesh meshEdit = new EditedMesh();

        Dictionary <int, int> vertRemapping = new Dictionary <int, int>();

        for (int vert = 0; vert < mesh.vertices.Length; ++vert)
        {
            if (!vertsToRemove.Contains(vert))
            {
                vertRemapping.Add(vert, meshEdit.verts.Count);
                meshEdit.verts.Add(mesh.vertices[vert]);
            }
        }

        for (int triangleIndex = 0; triangleIndex < mesh.triangles.Length; triangleIndex += NumVertsPerTriangle)
        {
            if (!vertsToRemove.Contains(mesh.triangles[triangleIndex]) &&
                !vertsToRemove.Contains(mesh.triangles[triangleIndex + 1]) &&
                !vertsToRemove.Contains(mesh.triangles[triangleIndex + 2]))                 // if none of this triangles verts are being removed, keep triangle
            {
                meshEdit.triangleIndecis.Add(vertRemapping[mesh.triangles[triangleIndex]]);
                meshEdit.triangleIndecis.Add(vertRemapping[mesh.triangles[triangleIndex + 1]]);
                meshEdit.triangleIndecis.Add(vertRemapping[mesh.triangles[triangleIndex + 2]]);
            }
        }
        meshEdit.CopyTo(mesh);
    }
Ejemplo n.º 2
0
    // triangleVertIndecis must be a multiple of three, each triangle must have three vert indecis
    public static void AddTriangles(List <int> triangleVertIndecis, Mesh mesh)
    {
        EditedMesh meshEdit = new EditedMesh(mesh);

        meshEdit.AddTriangles(triangleVertIndecis);
        meshEdit.CopyTo(mesh);
    }
Ejemplo n.º 3
0
    public static void DuplicateVerts(List <int> vertsToDuplicate, Mesh mesh)
    {
        EditedMesh meshEdit = new EditedMesh(mesh);

        for (int index = 0; index < vertsToDuplicate.Count; ++index)
        {
            meshEdit.verts.Add(mesh.vertices[vertsToDuplicate[index]]);
        }
        meshEdit.CopyTo(mesh);
    }
Ejemplo n.º 4
0
    // snap the vert to a length along the axis
    public static void SnapVertsOnAxis(List <int> vertsToMove, Vector3 snapAxis, float snapLength, Mesh mesh)
    {
        EditedMesh meshEdit           = new EditedMesh(mesh);
        Vector3    snapAxisNormalized = snapAxis.normalized;

        for (int index = 0; index < vertsToMove.Count; ++index)
        {
            float fixUpLength = snapLength - Vector3.Dot(meshEdit.verts[vertsToMove[index]], snapAxisNormalized);
            meshEdit.verts[vertsToMove[index]] = meshEdit.verts[vertsToMove[index]] + snapAxisNormalized * fixUpLength;
        }
        meshEdit.CopyTo(mesh);
    }
Ejemplo n.º 5
0
    public static void MoveVerts(List <int> vertsToMove, Vector3 delta, Mesh mesh)
    {
        EditedMesh meshEdit = new EditedMesh(mesh);

        int vertsToMoveCount = vertsToMove.Count;

        for (int index = 0; index < vertsToMoveCount; ++index)
        {
            meshEdit.verts[vertsToMove[index]] += delta;
        }
        meshEdit.CopyTo(mesh);
    }
Ejemplo n.º 6
0
    // reverse the winding order of the triangle
    public static void ChangeTrianglesWinding(List <int> trianglesToChange, Mesh mesh)
    {
        EditedMesh meshEdit = new EditedMesh(mesh);

        for (int triangle = 0; triangle < trianglesToChange.Count; ++triangle)
        {
            int firstTriangleIndex = trianglesToChange[triangle] * NumVertsPerTriangle;

            int temp = meshEdit.triangleIndecis[firstTriangleIndex];
            meshEdit.triangleIndecis[firstTriangleIndex]     = meshEdit.triangleIndecis[firstTriangleIndex + 2];
            meshEdit.triangleIndecis[firstTriangleIndex + 2] = temp;
        }
        meshEdit.CopyTo(mesh);
    }
Ejemplo n.º 7
0
    public static void AddUnitTriangle(Mesh mesh)
    {
        int        currentNumVerts = mesh.vertices.Length;
        EditedMesh meshEdit        = new EditedMesh(mesh);

        meshEdit.AddVerts(new List <Vector3>()
        {
            Vector3.zero, new Vector3(-1f, 0f, 0f), new Vector3(0f, 0f, 1f)
        });

        meshEdit.AddTriangles(new List <int>()
        {
            currentNumVerts, currentNumVerts + 1, currentNumVerts + 2
        });
        meshEdit.CopyTo(mesh);
    }
Ejemplo n.º 8
0
    // triangle zero, will mean the first triangle (three triangle indecis) are deleted, triangle index 1, the second triangle deleted
    public static void RemoveTriangles(List <int> trianglesToRemove, Mesh mesh)
    {
        EditedMesh meshEdit = new EditedMesh();

        meshEdit.AddVerts(mesh.vertices);
        for (int triangle = 0; triangle < CalculateNumTriangles(mesh); ++triangle)
        {
            if (!trianglesToRemove.Contains(triangle))
            {
                int startingTriangleIndex = triangle * NumVertsPerTriangle;
                meshEdit.triangleIndecis.Add(mesh.triangles[startingTriangleIndex]);
                meshEdit.triangleIndecis.Add(mesh.triangles[startingTriangleIndex + 1]);
                meshEdit.triangleIndecis.Add(mesh.triangles[startingTriangleIndex + 2]);
            }
        }
        meshEdit.CopyTo(mesh);
    }
Ejemplo n.º 9
0
    public static void RemoveUnusedVerts(Mesh mesh)
    {
        EditedMesh            meshEdit      = new EditedMesh();
        Dictionary <int, int> vertRemapping = new Dictionary <int, int>();

        for (int triangleIndex = 0; triangleIndex < mesh.triangles.Length; ++triangleIndex)
        {
            int vert = mesh.triangles[triangleIndex];
            if (!vertRemapping.ContainsKey(vert))
            {
                vertRemapping.Add(vert, meshEdit.verts.Count);
                meshEdit.verts.Add(mesh.vertices[vert]);
            }
            meshEdit.triangleIndecis.Add(vertRemapping[vert]);
        }
        meshEdit.CopyTo(mesh);
    }
Ejemplo n.º 10
0
    public static void DuplicateTriangles(List <int> trianglesToDuplicate, Mesh mesh)
    {
        EditedMesh meshEdit = new EditedMesh(mesh);
        // go over all the trianles, making copies of all the verts which are used
        Dictionary <int, int> vertRemapping = new Dictionary <int, int>();

        for (int index = 0; index < trianglesToDuplicate.Count; ++index)
        {
            int startingTriangleIndex = trianglesToDuplicate[index] * NumVertsPerTriangle;

            for (int triangleVert = 0; triangleVert < NumVertsPerTriangle; ++triangleVert)
            {
                int vertIndex = mesh.triangles[startingTriangleIndex + triangleVert];
                if (!vertRemapping.ContainsKey(vertIndex))
                {
                    vertRemapping.Add(vertIndex, meshEdit.verts.Count);
                    meshEdit.verts.Add(mesh.vertices[vertIndex]);
                }
                meshEdit.triangleIndecis.Add(vertRemapping[vertIndex]);
            }
        }
        meshEdit.CopyTo(mesh);
    }
Ejemplo n.º 11
0
    // all triangles will be updated
    public static int FuseVerts(List <int> vertsToFuse, Mesh mesh)
    {
        if (vertsToFuse.Count > 1)
        {
            EditedMesh meshEdit      = new EditedMesh();
            List <int> vertsToRemove = new List <int>();
            int        vertToKeep    = mesh.vertices.Length;
            for (int vert = 0; vert < vertsToFuse.Count; ++vert)
            {
                vertsToRemove.Add(vertsToFuse[vert]);
                if (vertsToFuse[vert] < vertToKeep)
                {
                    vertToKeep = vertsToFuse[vert];
                }
            }
            vertsToRemove.Remove(vertToKeep);

            Dictionary <int, int> vertRemapping = new Dictionary <int, int>();
            for (int vert = 0; vert < mesh.vertices.Length; ++vert)
            {
                if (vertsToRemove.Contains(vert))
                {
                    vertRemapping.Add(vert, vertToKeep);
                }
                else
                {
                    vertRemapping.Add(vert, meshEdit.verts.Count);
                    meshEdit.verts.Add(mesh.vertices[vert]);
                }
            }

            meshEdit.RemapTriangleIndecis(mesh, vertRemapping);
            meshEdit.CopyTo(mesh);
            return(vertToKeep);
        }
        return(IndexInvalid);
    }
Ejemplo n.º 12
0
    public static void MoveTriangles(List <int> trianglesToMove, Vector3 delta, Mesh mesh)
    {
        EditedMesh    meshEdit    = new EditedMesh(mesh);
        HashSet <int> editedVerts = new HashSet <int>();

        int trianglesToMoveCount = trianglesToMove.Count;

        for (int index = 0; index < trianglesToMoveCount; ++index)
        {
            int triangle = trianglesToMove[index];
            int startingTriangleIndex = triangle * NumVertsPerTriangle;

            for (int triangleIndex = 0; triangleIndex < NumVertsPerTriangle; ++triangleIndex)
            {
                int vertIndex = meshEdit.triangleIndecis[startingTriangleIndex + triangleIndex];
                if (!editedVerts.Contains(vertIndex))
                {
                    editedVerts.Add(vertIndex);
                    meshEdit.verts[vertIndex] += delta;
                }
            }
        }
        meshEdit.CopyTo(mesh);
    }
Ejemplo n.º 13
0
    public static void FuseCloseVerts(List <int> vertsToFuse, Mesh mesh, float distSqr)
    {
        EditedMesh            meshEdit      = new EditedMesh();
        Dictionary <int, int> vertRemapping = new Dictionary <int, int>();

        for (int vert = 0; vert < mesh.vertices.Length; ++vert)
        {
            if (vertRemapping.ContainsKey(vert))             // already been remapped
            {
                continue;
            }
            vertRemapping.Add(vert, meshEdit.verts.Count);
            meshEdit.verts.Add(mesh.vertices[vert]);

            if (!vertsToFuse.Contains(vert))
            {
                continue;                 // not interested in fusing this vert
            }

            Vector3 vertPos = mesh.vertices[vert];
            for (int vertToCompare = vert + 1; vertToCompare < mesh.vertices.Length; ++vertToCompare)
            {
                if (!vertsToFuse.Contains(vertToCompare))
                {
                    continue;                     // not interested in fusing this vert
                }

                if ((vertPos - mesh.vertices[vertToCompare]).sqrMagnitude < distSqr)
                {
                    vertRemapping.Add(vertToCompare, vertRemapping[vert]);
                }
            }
        }
        meshEdit.RemapTriangleIndecis(mesh, vertRemapping);
        meshEdit.CopyTo(mesh);
    }