コード例 #1
0
ファイル: WMesh.cs プロジェクト: mehmetsirin34/Slice
    public WMesh(Mesh unityMesh)
    {
        bodyTris = new List <WTriangle>();
        int[] tris = unityMesh.GetTriangles(0);

        if (unityMesh.subMeshCount == 3)
        {
            m1.AddRange(unityMesh.GetTriangles(0));
            m1.AddRange(unityMesh.GetTriangles(1));
            m1.AddRange(unityMesh.GetTriangles(2));

            tris = new int[m1.Count];

            for (int i = 0; i < m1.Count; i++)
            {
                tris[i] = m1[i];
            }
        }

        Vector3[] vertices = unityMesh.vertices;
        for (int i = 0; i < tris.Length; i += 3)
        {
            WTriangle tri = new WTriangle(
                vertices[tris[i]],
                vertices[tris[i + 1]],
                vertices[tris[i + 2]]);
            bodyTris.Add(tri);
        }
    }
コード例 #2
0
 public WMesh(Mesh unityMesh)
 {
     bodyTris = new List <WTriangle>();
     int[]     tris     = unityMesh.GetTriangles(0);
     Vector3[] vertices = unityMesh.vertices;
     for (int i = 0; i < tris.Length; i += 3)
     {
         WTriangle tri = new WTriangle(
             vertices[tris[i]],
             vertices[tris[i + 1]],
             vertices[tris[i + 2]]);
         bodyTris.Add(tri);
     }
 }
コード例 #3
0
    private bool ClipByPlane(Plane plane, WMesh originMesh, out WMesh slicedMesh, out WMesh remainedMesh)
    {
        slicedMesh   = null;
        remainedMesh = null;

        List <WTriangle> bodyTris = originMesh.GetBodyTris();

        List <WTriangle> slicedTris   = new List <WTriangle>();
        List <WTriangle> remainedTris = new List <WTriangle>();

        List <WClipEdge> clipedEdges = new List <WClipEdge>();

        //Plane clipPlane = new Plane(plane.normal, Vector3.zero);
        Plane clipPlane = plane;

        for (int i = 0; i < bodyTris.Count; i++)
        {
            WTriangle tri = bodyTris[i];

            Vector3 worldPos = Target.GetPosition();
            // Using modified Sutherland-Hodgman
            Vector3 a = tri.a + worldPos;
            Vector3 b = tri.b + worldPos;
            Vector3 c = tri.c + worldPos;

            float da = clipPlane.GetDistanceToPoint(a);
            float db = clipPlane.GetDistanceToPoint(b);
            float dc = clipPlane.GetDistanceToPoint(c);


            if (WMath.InFront(da) && WMath.InFront(db) && WMath.InFront(dc))
            {
                slicedTris.Add(tri);
            }
            else if (WMath.Behind(da) && WMath.Behind(db) && WMath.Behind(dc))
            {
                remainedTris.Add(tri);
            }
            else if (WMath.InFront(da) && WMath.InFront(db) && WMath.Behind(dc))
            {
                Vector3 ac = WMath.Intersect(a, c, da, dc);
                Vector3 bc = WMath.Intersect(b, c, db, dc);

                slicedTris.Add(new WTriangle(ac, a, b, worldPos));
                slicedTris.Add(new WTriangle(b, bc, ac, worldPos));
                remainedTris.Add(new WTriangle(ac, bc, c, worldPos));

                clipedEdges.Add(new WClipEdge(ac, bc, worldPos));
            }
            else if (WMath.Behind(da) && WMath.Behind(db) && WMath.InFront(dc))
            {
                Vector3 ac = WMath.Intersect(a, c, da, dc);
                Vector3 bc = WMath.Intersect(b, c, db, dc);

                remainedTris.Add(new WTriangle(ac, a, b, worldPos));
                remainedTris.Add(new WTriangle(b, bc, ac, worldPos));
                slicedTris.Add(new WTriangle(ac, bc, c, worldPos));

                clipedEdges.Add(new WClipEdge(bc, ac, worldPos));
            }
            else if (WMath.InFront(da) && WMath.Behind(db) && WMath.InFront(dc))
            {
                Vector3 ab = WMath.Intersect(a, b, da, db);
                Vector3 bc = WMath.Intersect(b, c, db, dc);

                slicedTris.Add(new WTriangle(a, ab, c, worldPos));
                slicedTris.Add(new WTriangle(ab, bc, c, worldPos));
                remainedTris.Add(new WTriangle(ab, b, bc, worldPos));

                clipedEdges.Add(new WClipEdge(bc, ab, worldPos));
            }
            else if (WMath.Behind(da) && WMath.InFront(db) && WMath.Behind(dc))
            {
                Vector3 ab = WMath.Intersect(a, b, da, db);
                Vector3 bc = WMath.Intersect(b, c, db, dc);

                remainedTris.Add(new WTriangle(a, ab, c, worldPos));
                remainedTris.Add(new WTriangle(ab, bc, c, worldPos));
                slicedTris.Add(new WTriangle(ab, b, bc, worldPos));

                clipedEdges.Add(new WClipEdge(ab, bc, worldPos));
            }
            else if (WMath.Behind(da) && WMath.InFront(db) && WMath.InFront(dc))
            {
                Vector3 ab = WMath.Intersect(a, b, da, db);
                Vector3 ac = WMath.Intersect(a, c, da, dc);

                slicedTris.Add(new WTriangle(ab, b, c, worldPos));
                slicedTris.Add(new WTriangle(c, ac, ab, worldPos));
                remainedTris.Add(new WTriangle(ac, a, ab, worldPos));

                clipedEdges.Add(new WClipEdge(ab, ac, worldPos));
            }
            else if (WMath.InFront(da) && WMath.Behind(db) && WMath.Behind(dc))
            {
                Vector3 ab = WMath.Intersect(a, b, da, db);
                Vector3 ac = WMath.Intersect(a, c, da, dc);

                remainedTris.Add(new WTriangle(ab, b, c, worldPos));
                remainedTris.Add(new WTriangle(c, ac, ab, worldPos));
                slicedTris.Add(new WTriangle(ac, a, ab, worldPos));

                clipedEdges.Add(new WClipEdge(ac, ab, worldPos));
            }
        }

        MakeCap(clipedEdges, out List <WTriangle> slicedCap, out List <WTriangle> remainedCap);

        if (slicedTris.Count > 0 && remainedTris.Count > 0)
        {
            slicedMesh   = new WMesh(slicedTris, slicedCap);
            remainedMesh = new WMesh(remainedTris, remainedCap);
            return(true);
        }
        else
        {
            return(false);
        }
    }
コード例 #4
0
    public Mesh ToUnityMesh(string meshName)
    {
        Mesh mesh = new Mesh();

        mesh.name         = meshName;
        mesh.subMeshCount = 2;

        List <WTriangle> allTriangles = new List <WTriangle>(bodyTris);

        if (capTris != null)
        {
            allTriangles.AddRange(capTris);
        }


        // 1. Set All Vertices
        int vertCount = allTriangles.Count * 3;

        Vector3[] vertices = new Vector3[vertCount];
        int       vertIdx  = 0;

        for (int i = 0; i < allTriangles.Count; i++)
        {
            WTriangle tri = allTriangles[i];
            vertices[vertIdx]     = tri.a;
            vertices[vertIdx + 1] = tri.b;
            vertices[vertIdx + 2] = tri.c;

            vertIdx += 3;
        }

        mesh.vertices = vertices;


        // 2. Set Body Triangle : Submesh 0
        int bodyArrLen = bodyTris.Count * 3;

        int[] bodyArr = new int[bodyArrLen];

        for (int i = 0; i < bodyArrLen; i += 3)
        {
            bodyArr[i]     = i;
            bodyArr[i + 1] = i + 1;
            bodyArr[i + 2] = i + 2;
        }

        mesh.SetTriangles(bodyArr, 0);

        // 3. Set Cap Triangle : Submesh 1
        int capStartIdx = bodyArrLen;

        int capArrLen = capTris.Count * 3;

        int[] capArr = new int[capArrLen];

        for (int i = 0; i < capArrLen; i += 3)
        {
            capArr[i]     = capStartIdx + i;
            capArr[i + 1] = capStartIdx + i + 1;
            capArr[i + 2] = capStartIdx + i + 2;
        }

        mesh.SetTriangles(capArr, 1);


        // 4. Finish
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();

        return(mesh);
    }