3d模型任意切割的思路(續)

上一篇講到3d模型切割我遇到的問題(切面的紋理會混亂),經過這段時間的琢磨,有了解決方案,當然我這裏只給出我的解決思路,投入到實際項目中還需要做許多工作,比如我在上一篇中切割模型固定寫死了切平面方程是y=0.1。實際項目中,我們應該是根據手指滑動來得出空間平面方程式。紋理之所以會混亂,根本上的原因是因爲我們切割模型後生成出來的新的頂點是混亂無序的,所以我在這片文章裏做的,就是把新生成的紋理重新排序,事實上,模型切割的關鍵點就只有上一篇講的生成橫切面的新頂點以及本篇解決的切口紋理,理論上解決了這兩點,就近乎可以做到以假亂真的程度了。這裏只是提供了思路,因爲解決了紋理混亂的問題。如需下載完整插件,請到http://www.demodashi.com/demo/11343.html 


//重新排序新生成的頂點,按照角度
        List<SortAngle> SortAngleList = new List<SortAngle>();
        for (int verticeIndex = verticeCount + 1; verticeIndex < verticeList.Count; verticeIndex++)
        {
            //計算角度,以0-1爲參照
            Vector3 vec1to0 = verticeList[verticeCount + 1] - verticeList[verticeCount];
            Vector3 indexTo0 = verticeList[verticeIndex] - verticeList[verticeCount];

            float angle = Vector3.Angle(indexTo0.normalized, vec1to0.normalized);
            bool isExis = false;
            for (int i = 0; i < SortAngleList.Count; ++i)
            {
                //同樣角度,距離近的被剔除
                if (SortAngleList[i].Angle == angle)
                {
                    float dis1 = Vector3.Distance(verticeList[SortAngleList[i].Index], verticeList[verticeCount]);
                    float dis2 = Vector3.Distance(verticeList[verticeIndex], verticeList[verticeCount]);
                    if (dis2 > dis1)
                    {
                        SortAngleList[i].Index = verticeIndex;
                    }
                    isExis = true;
                    break;
                }
            }
            if (!isExis)
            {
                Debug.Log(angle);
                SortAngle sortAngle = new SortAngle();
                sortAngle.Index = verticeIndex;
                sortAngle.Angle = angle;
                SortAngleList.Add(sortAngle);
            }
        }
        SortAngleList.Sort();
        //縫合切口
        for (int verticeIndex = 0; verticeIndex < SortAngleList.Count - 1;)
        {
            triangles1.Add(verticeCount);
            triangles1.Add(SortAngleList[verticeIndex].Index);
            triangles1.Add(SortAngleList[verticeIndex + 1].Index);

            triangles2.Add(verticeCount);
            triangles2.Add(SortAngleList[verticeIndex].Index);
            triangles2.Add(SortAngleList[verticeIndex + 1].Index);
            

            verticeIndex ++;
        }

以上爲重新排序了頂點的代碼。說一下是怎麼排序的吧。

首先,切出來的模型新生成的頂點是無序的,但是我們可以連接任意兩個無序頂點定爲參考向量,然後其他任意頂點與參考向量中的起點連接形成新的向量,求得這兩個向量之間的夾角,利用這個夾角大小來排序,如圖所示:



以下是完整代碼

/*
 * @authors: liangjian
 * @desc:	
*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ClipMesh : MonoBehaviour {

    float ClipPlaneY = 0.01f;
	void Start () {
        MeshFilter mf = this.gameObject.GetComponent<MeshFilter>();

        //頂點數組轉頂點容器
        List<Vector3> verticeList = new List<Vector3>();
        int verticeCount = mf.mesh.vertices.Length;
        for (int verticeIndex = 0; verticeIndex < verticeCount; ++verticeIndex)
        {
            verticeList.Add(mf.mesh.vertices[verticeIndex]);
        }
        //三角形數組轉三角形容器
        List<int> triangleList = new List<int>();
        int triangleCount = mf.mesh.triangles.Length;
        for (int triangleIndex = 0; triangleIndex < triangleCount; ++triangleIndex)
        {
            triangleList.Add(mf.mesh.triangles[triangleIndex]);
        }
        //uv座標數組轉uv座標容器
        List<Vector2> uvList = new List<Vector2>();
        int uvCount = mf.mesh.uv.Length;
        for (int uvIndex = 0; uvIndex < uvCount; ++uvIndex)
        {
            uvList.Add(mf.mesh.uv[uvIndex]);
        }
        //頂點顏色數組轉頂點顏色容器
        List<Vector3> normalList = new List<Vector3>();
        int normalCount = mf.mesh.normals.Length;
        for (int normalIndex = 0; normalIndex < normalCount; ++normalIndex)
        {
            normalList.Add(mf.mesh.normals[normalIndex]);
        }

        //檢查每個三角面,是否存在兩個頂點連接正好在直線上
        for (int triangleIndex = 0; triangleIndex < triangleList.Count;)
        {
            int trianglePoint0 = triangleList[triangleIndex];
            int trianglePoint1 = triangleList[triangleIndex + 1];
            int trianglePoint2 = triangleList[triangleIndex + 2];
            
            Vector3 point0 = verticeList[trianglePoint0];
            Vector3 point1 = verticeList[trianglePoint1];
            Vector3 point2 = verticeList[trianglePoint2];

            float planeY = ClipPlaneY;
            //0-1,1-2相連線段被切割
            if ((point0.y - planeY)* (point1.y - planeY) < 0 && (point1.y - planeY) * (point2.y - planeY) < 0)
            {
                //截斷0-1之間的頂點
                float k01 = (point1.y - point0.y) / (planeY - point0.y);
                float newPointX01 = (point1.x - point0.x) / k01 + point0.x;
                float newPointZ01 = (point1.z - point0.z) / k01 + point0.z;
                Vector3 newPoint0_1 = new Vector3(newPointX01, planeY, newPointZ01);
                verticeList.Add(newPoint0_1);
                //uv
                if(uvList.Count > 0)
                {
                    Vector2 uv0 = uvList[trianglePoint0];
                    Vector2 uv1 = uvList[trianglePoint1];
                    float newUV_x = (uv1.x - uv0.x) / k01 + uv0.x;
                    float newUV_y = (uv1.y - uv0.y) / k01 + uv0.y;
                    uvList.Add(new Vector2(newUV_x, newUV_y));
                }
                //法向量
                Vector3 normalX0 = normalList[trianglePoint0];
                Vector3 normalX1 = normalList[trianglePoint1];
                Vector3 normalX2 = normalList[trianglePoint2];
                float newNoramlX01 = (normalX1.x - normalX0.x) / k01 + normalX0.x;
                float newNoramlY01 = (normalX1.y - normalX0.y) / k01 + normalX0.y;
                float newNoramlZ01 = (normalX1.z - normalX0.z) / k01 + normalX0.z;
                normalList.Add(new Vector3(newNoramlX01, newNoramlY01, newNoramlZ01));
                //截斷1-2之間的頂點
                float k12 = (point2.y - point1.y) / (planeY - point1.y);
                float newPointX12 = (point2.x - point1.x) / k12 + point1.x;
                float newPointZ12 = (point2.z - point1.z) / k12 + point1.z;
                Vector3 newPoint1_2 = new Vector3(newPointX12, planeY, newPointZ12);
                verticeList.Add(newPoint1_2);
                if (uvList.Count > 0)
                {
                    Vector2 uv1 = uvList[trianglePoint1];
                    Vector2 uv2 = uvList[trianglePoint2];
                    float newUV_x = (uv2.x - uv1.x) / k12 + uv1.x;
                    float newUV_y = (uv2.y - uv1.y) / k12 + uv1.y;
                    uvList.Add(new Vector2(newUV_x, newUV_y));
                }
                //法向量
                float newNoramlX12 = (normalX2.x - normalX1.x) / k12 + normalX1.x;
                float newNoramlY12 = (normalX2.y - normalX1.y) / k12 + normalX1.y;
                float newNoramlZ12 = (normalX2.z - normalX1.z) / k12 + normalX1.z;
                normalList.Add(new Vector3(newNoramlX12, newNoramlY12, newNoramlZ12));

                int newVerticeCount = verticeList.Count;
                //插入頂點索引,以此構建新三角形
                triangleList.Insert(triangleIndex + 1, newVerticeCount - 2);
                triangleList.Insert(triangleIndex + 2, newVerticeCount - 1);

                triangleList.Insert(triangleIndex + 3, newVerticeCount - 1);
                triangleList.Insert(triangleIndex + 4, newVerticeCount - 2);

                triangleList.Insert(triangleIndex + 6, trianglePoint0);
                triangleList.Insert(triangleIndex + 7, newVerticeCount - 1);
            }
            //1-2,2-0相連線段被切割
            else if ((point1.y - planeY) * (point2.y - planeY) < 0 && (point2.y - planeY) * (point0.y - planeY) < 0)
            {
                //截斷1-2之間的頂點
                float k12 = (point2.y - point1.y) / (planeY - point1.y);
                float newPointX12 = (point2.x - point1.x) / k12 + point1.x;
                float newPointZ12 = (point2.z - point1.z) / k12 + point1.z;
                Vector3 newPoint1_2 = new Vector3(newPointX12, planeY, newPointZ12);
                verticeList.Add(newPoint1_2);
                if (uvList.Count > 0)
                {
                    Vector2 uv1 = uvList[trianglePoint1];
                    Vector2 uv2 = uvList[trianglePoint2];
                    float newUV_x = (uv2.x - uv1.x) / k12 + uv1.x;
                    float newUV_y = (uv2.y - uv1.y) / k12 + uv1.y;
                    uvList.Add(new Vector2(newUV_x, newUV_y));
                }
                //法向量
                Vector3 normalX0 = normalList[trianglePoint0];
                Vector3 normalX1 = normalList[trianglePoint1];
                Vector3 normalX2 = normalList[trianglePoint2];
                float newNoramlX12 = (normalX2.x - normalX1.x) / k12 + normalX1.x;
                float newNoramlY12 = (normalX2.y - normalX1.y) / k12 + normalX1.y;
                float newNoramlZ12 = (normalX2.z - normalX1.z) / k12 + normalX1.z;
                normalList.Add(new Vector3(newNoramlX12, newNoramlY12, newNoramlZ12));

                //截斷0-2之間的頂點
                float k02 = (point2.y - point0.y) / (planeY - point0.y);
                float newPointX02 = (point2.x - point0.x) / k02 + point0.x;
                float newPointZ02 = (point2.z - point0.z) / k02 + point0.z;
                Vector3 newPoint0_2 = new Vector3(newPointX02, planeY, newPointZ02);
                verticeList.Add(newPoint0_2);
                //uv
                if (uvList.Count > 0)
                {
                    Vector2 uv0 = uvList[trianglePoint0];
                    Vector2 uv2 = uvList[trianglePoint2];
                    float newUV_x = (uv2.x - uv0.x) / k02 + uv0.x;
                    float newUV_y = (uv2.y - uv0.y) / k02 + uv0.y;
                    uvList.Add(new Vector2(newUV_x, newUV_y));
                }
                //法向量
                float newNoramlX02 = (normalX1.x - normalX0.x) / k02 + normalX0.x;
                float newNoramlY02 = (normalX1.y - normalX0.y) / k02 + normalX0.y;
                float newNoramlZ02 = (normalX1.z - normalX0.z) / k02 + normalX0.z;
                normalList.Add(new Vector3(newNoramlX02, newNoramlY02, newNoramlZ02));
                
                int newVerticeCount = verticeList.Count;
                //插入頂點索引,以此構建新三角形

                //{0}
                //{1}
                triangleList.Insert(triangleIndex + 2, newVerticeCount - 2);

                triangleList.Insert(triangleIndex + 3, newVerticeCount - 1);
                triangleList.Insert(triangleIndex + 4, newVerticeCount - 2);
                //{2}
                
                triangleList.Insert(triangleIndex + 6, newVerticeCount - 1);
                triangleList.Insert(triangleIndex + 7, trianglePoint0);
                triangleList.Insert(triangleIndex + 8, newVerticeCount - 2);
            }
            //0-1,2-0相連線段被切割
            else if((point0.y - planeY) * (point1.y - planeY) < 0 && (point2.y - planeY) * (point0.y - planeY) < 0)
            {
                //截斷0-1之間的頂點
                float k01 = (point1.y - point0.y) / (planeY - point0.y);
                float newPointX01 = (point1.x - point0.x) / k01 + point0.x;
                float newPointZ01 = (point1.z - point0.z) / k01 + point0.z;
                Vector3 newPoint0_1 = new Vector3(newPointX01, planeY, newPointZ01);
                verticeList.Add(newPoint0_1);
                //uv
                if (uvList.Count > 0)
                {
                    Vector2 uv0 = uvList[trianglePoint0];
                    Vector2 uv1 = uvList[trianglePoint1];
                    float newUV_x = (uv1.x - uv0.x) / k01 + uv0.x;
                    float newUV_y = (uv1.y - uv0.y) / k01 + uv0.y;
                    uvList.Add(new Vector2(newUV_x, newUV_y));
                }
                //法向量
                Vector3 normalX0 = normalList[trianglePoint0];
                Vector3 normalX1 = normalList[trianglePoint1];
                Vector3 normalX2 = normalList[trianglePoint2];
                float newNoramlX01 = (normalX1.x - normalX0.x) / k01 + normalX0.x;
                float newNoramlY01 = (normalX1.y - normalX0.y) / k01 + normalX0.y;
                float newNoramlZ01 = (normalX1.z - normalX0.z) / k01 + normalX0.z;
                normalList.Add(new Vector3(newNoramlX01, newNoramlY01, newNoramlZ01));

                //截斷0-2之間的頂點
                float k02 = (point2.y - point0.y) / (planeY - point0.y);
                float newPointX02 = (point2.x - point0.x) / k02 + point0.x;
                float newPointZ02 = (point2.z - point0.z) / k02 + point0.z;
                Vector3 newPoint0_2 = new Vector3(newPointX02, planeY, newPointZ02);
                verticeList.Add(newPoint0_2);
                //uv
                if (uvList.Count > 0)
                {
                    Vector2 uv0 = uvList[trianglePoint0];
                    Vector2 uv2 = uvList[trianglePoint2];
                    float newUV_x = (uv2.x - uv0.x) / k02 + uv0.x;
                    float newUV_y = (uv2.y - uv0.y) / k02 + uv0.y;
                    uvList.Add(new Vector2(newUV_x, newUV_y));
                }
                //法向量
                float newNoramlX02 = (normalX1.x - normalX0.x) / k02 + normalX0.x;
                float newNoramlY02 = (normalX1.y - normalX0.y) / k02 + normalX0.y;
                float newNoramlZ02 = (normalX1.z - normalX0.z) / k02 + normalX0.z;
                normalList.Add(new Vector3(newNoramlX02, newNoramlY02, newNoramlZ02));
                
                int newVerticeCount = verticeList.Count;
                //插入頂點索引,以此構建新三角形

                //{0}
                triangleList.Insert(triangleIndex + 1, newVerticeCount - 2);
                triangleList.Insert(triangleIndex + 2, newVerticeCount - 1);

                triangleList.Insert(triangleIndex + 3, newVerticeCount - 2);
                //{1}
                //{2}
                
                triangleList.Insert(triangleIndex + 6, trianglePoint2);
                triangleList.Insert(triangleIndex + 7, newVerticeCount - 1);
                triangleList.Insert(triangleIndex + 8, newVerticeCount - 2);
            }
            //只有0-1被切
            else if((point0.y - planeY) * (point1.y - planeY) < 0)
            {
                Debug.Log("只有01被切");
            }
            //只有1-2被切
            else if ((point1.y - planeY) * (point2.y - planeY) < 0)
            {
                Debug.Log("只有12被切");
            }
            //只有2-0被切
            else if ((point2.y - planeY) * (point0.y - planeY) < 0)
            {
                Debug.Log("只有02被切");
            }
            triangleIndex += 3;
        }

        //篩選出切割面兩側的頂點索引
        List<int> triangles1 = new List<int>();
        List<int> triangles2 = new List<int>();
        for (int triangleIndex = 0; triangleIndex < triangleList.Count; triangleIndex += 3)
        {
            int trianglePoint0 = triangleList[triangleIndex];
            int trianglePoint1 = triangleList[triangleIndex + 1];
            int trianglePoint2 = triangleList[triangleIndex + 2];
            
            Vector3 point0 = verticeList[trianglePoint0];
            Vector3 point1 = verticeList[trianglePoint1];
            Vector3 point2 = verticeList[trianglePoint2];
            //切割面
            float planeY = ClipPlaneY;
            if(point0.y > planeY || point1.y > planeY || point2.y > planeY)
            {
                triangles1.Add(trianglePoint0);
                triangles1.Add(trianglePoint1);
                triangles1.Add(trianglePoint2);
            }
            else
            {
                triangles2.Add(trianglePoint0);
                triangles2.Add(trianglePoint1);
                triangles2.Add(trianglePoint2);
            }
        }
        //重新排序新生成的頂點,按照角度
        List<SortAngle> SortAngleList = new List<SortAngle>();
        for (int verticeIndex = verticeCount + 1; verticeIndex < verticeList.Count; verticeIndex++)
        {
            //計算角度,以0-1爲參照
            Vector3 vec1to0 = verticeList[verticeCount + 1] - verticeList[verticeCount];
            Vector3 indexTo0 = verticeList[verticeIndex] - verticeList[verticeCount];
<span style="white-space:pre">	float moIndexto0 = indexTo0.magnitude;
            float mo1to0 = vec1to0.magnitude;
            float dotRes = Vector3.Dot(indexTo0, vec1to0);</span>     
            float angle = Mathf.Acos(dotRes/(mo1to0* moIndexto0));
            bool isExis = false;
            for (int i = 0; i < SortAngleList.Count; ++i)
            {
                //同樣角度,距離近的被剔除
                if (SortAngleList[i].Angle == angle)
                {
                    float dis1 = Vector3.Distance(verticeList[SortAngleList[i].Index], verticeList[verticeCount]);
                    float dis2 = Vector3.Distance(verticeList[verticeIndex], verticeList[verticeCount]);
                    if (dis2 > dis1)
                    {
                        SortAngleList[i].Index = verticeIndex;
                    }
                    isExis = true;
                    break;
                }
            }
            if (!isExis)
            {
                Debug.Log(angle);
                SortAngle sortAngle = new SortAngle();
                sortAngle.Index = verticeIndex;
                sortAngle.Angle = angle;
                SortAngleList.Add(sortAngle);
            }
        }
        SortAngleList.Sort();
        //縫合切口
        for (int verticeIndex = 0; verticeIndex < SortAngleList.Count - 1;)
        {
            triangles1.Add(verticeCount);
            triangles1.Add(SortAngleList[verticeIndex].Index);
            triangles1.Add(SortAngleList[verticeIndex + 1].Index);

            triangles2.Add(verticeCount);
            triangles2.Add(SortAngleList[verticeIndex].Index);
            triangles2.Add(SortAngleList[verticeIndex + 1].Index);
            

            verticeIndex ++;
        }


        mf.mesh.vertices = verticeList.ToArray();
        mf.mesh.triangles = triangles1.ToArray();
        if (uvList.Count > 0)
        {
            mf.mesh.uv = uvList.ToArray();
        }
        mf.mesh.normals = normalList.ToArray();


        //分割模型
        GameObject newModel = new GameObject("New Model");
        MeshFilter meshFilter = newModel.AddComponent<MeshFilter>();
        meshFilter.mesh.vertices = mf.mesh.vertices;
        meshFilter.mesh.triangles = triangles2.ToArray();
        meshFilter.mesh.uv = mf.mesh.uv;
        meshFilter.mesh.normals = mf.mesh.normals;
        Renderer newRenderer = newModel.AddComponent<MeshRenderer>();
        newRenderer.material = this.gameObject.GetComponent<MeshRenderer>().material;
    }
}
/*
 * @authors: liangjian
 * @desc:	
*/
using UnityEngine;
using System.Collections;
using System;

public class SortAngle : IComparable<SortAngle> {
    public int Index;
    public float Angle;
    public int CompareTo(SortAngle item)
    {
        return item.Angle.CompareTo(Angle);
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章