Ejemplo n.º 1
0
    public static void SliceHorz(GameObject sourceGameObject, GameObject clipperPlane, Mesh targetMesh, Mesh[] slicedMeshes, int idx, int slices, Bounds bounds, bool cap)
    {
        Vector3 minBounds = bounds.min;
        Vector3 maxBounds = bounds.max;

        Mesh cuttingMesh = targetMesh;

        Mesh[] slicedHorzMeshes = new Mesh[slices];
        for (int i = 0; i < slices; i++)
        {
            slicedHorzMeshes[i] = new Mesh();
        }

        clipperPlane.transform.rotation = Quaternion.Euler(0, 0, 90);
        for (int i = 0; i < slices - 1; i++)
        {
            clipperPlane.transform.position = new Vector3(Mathf.Lerp(minBounds.x, maxBounds.x, (float)(i + 1) / (float)slices), 0, 0);

            Mesh[] horzClipMeshes = new Mesh[2];
            horzClipMeshes[0] = slicedHorzMeshes[i];
            horzClipMeshes[1] = slicedHorzMeshes[i + 1];

            if (MeshSlicer.CutTriangleMesh(horzClipMeshes, cuttingMesh, new Plane(clipperPlane.transform.up, clipperPlane.transform.position), sourceGameObject.transform, clipperPlane.transform, cap))
            {
                cuttingMesh = horzClipMeshes[1];
            }
        }

        for (int i = 0; i < slices; i++)
        {
            slicedMeshes[idx + i].Clear();
            slicedMeshes[idx + i] = slicedHorzMeshes[i];
        }
    }
Ejemplo n.º 2
0
    public static Mesh[] ChunkMesh(GameObject sourceGameObject, Mesh[] slicedMeshes, int verticalSlices, int horizontalSlices, bool cap)
    {
        Mesh sourceMesh = sourceGameObject.GetComponent <MeshFilter>().mesh;

        Bounds  bounds    = sourceGameObject.renderer.bounds;
        Vector3 minBounds = bounds.min;
        Vector3 maxBounds = bounds.max;

        GameObject clipperPlane = GameObject.CreatePrimitive(PrimitiveType.Plane);

        if (verticalSlices == 1)
        {
            MeshSlicer.SliceHorz(sourceGameObject, clipperPlane, sourceMesh, slicedMeshes, 0, horizontalSlices, bounds, cap);
        }
        else
        {
            for (int j = 0; j < verticalSlices - 1; j++)
            {
                clipperPlane.transform.rotation = Quaternion.Euler(0, 0, 0);
                clipperPlane.transform.position = new Vector3(0, Mathf.Lerp(maxBounds.y, minBounds.y, (float)(j + 1) / (float)verticalSlices), 0);

                Mesh[] vertClipMeshes = new Mesh[2];
                vertClipMeshes[0] = slicedMeshes[j * horizontalSlices];
                vertClipMeshes[1] = slicedMeshes[(j + 1) * horizontalSlices];

                MeshSlicer.CutTriangleMesh(vertClipMeshes, sourceMesh, new Plane(clipperPlane.transform.up, clipperPlane.transform.position), sourceGameObject.transform, clipperPlane.transform, cap);

                MeshSlicer.SliceHorz(sourceGameObject, clipperPlane, slicedMeshes[j * horizontalSlices], slicedMeshes, j * horizontalSlices, horizontalSlices, bounds, cap);

                sourceMesh = vertClipMeshes[1];
            }
            MeshSlicer.SliceHorz(sourceGameObject, clipperPlane, slicedMeshes[(verticalSlices - 1) * horizontalSlices], slicedMeshes, (verticalSlices - 1) * horizontalSlices, horizontalSlices, bounds, cap);
        }

        Destroy(clipperPlane);
        return(slicedMeshes);
    }