Unity3D点击绘制二维模型线和三维模型线

下面是转载别人的,缺点是只能在xz平面上画线,可以添加一个地板来测试,鼠标点击地板进行画线



这两天在研究画线的东西。直到昨天才小有成效。拿出来和大家分享一下。先上图:




以前曾经尝试过用LineRender写画线的功能,但是在拐弯的时候会出现变形和扭曲。所以才决定用绘制Mesh的方法写一个画线的功能。

东西其实很简单,稍微有一点数学的知识,用到了三角函数。还有一些关于构造Mesh的相关代码。下面有草图一张:



黑色的线框代表画出来的模型线,PointA和PointB代表获取的起始点和终结点,A,B,C,D为Mesh的四个顶点。

已知起始点和终结点和线宽,就可以求出四个顶点的坐标。并根据这四个顶点绘制出Mesh.

至于具体的实现细节,大家看代码吧。


DrawMeshLine类:

[csharp]  view plain  copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4.   
  5. public class DrawMeshLine : MonoBehaviour   
  6. {  
  7.     //线容器  
  8.     List<Vector3> LinePointContainer;  
  9.     //线宽  
  10.     public float LineWidth = 0.5f;  
  11.     //地形层  
  12.     public int Layer = 8;  
  13.     //线物体父物体  
  14.     GameObject LineObj;  
  15.       
  16.     void Start ()   
  17.     {  
  18.         LinePointContainer = new List<Vector3>();  
  19.         LineObj = new GameObject("Lines");  
  20.     }  
  21.       
  22.     void Update ()   
  23.     {  
  24.         if(Input.GetMouseButtonDown(0))  
  25.         {  
  26.             Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);  
  27.             RaycastHit hit;  
  28.             if(Physics.Raycast(ray,out hit))  
  29.             {  
  30.                 if(hit.transform.gameObject.layer != Layer)return;  
  31.                 LinePointContainer.Add(hit.point);  
  32.             }  
  33.             if(LinePointContainer.Count == 1)  
  34.             {  
  35.                 //二维初始面片圆  
  36. //              GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);  
  37. //              sphere.transform.parent = LineObj.transform;  
  38. //              sphere.transform.position = hit.point;  
  39. //              sphere.transform.localScale = new Vector3(LineWidth * 2,0.01f,LineWidth * 2);  
  40. //              sphere.renderer.material.shader = Shader.Find("GUI/Text Shader");  
  41. //              sphere.renderer.material.SetColor("_Color",new Color(255,0,0,255) / 255);  
  42.                   
  43.                 //三维初始球  
  44.                 GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);  
  45.                 sphere.transform.parent = LineObj.transform;  
  46.                 sphere.transform.position = new Vector3(hit.point.x,hit.point.y + LineWidth,hit.point.z);  
  47.                 sphere.transform.localScale = new Vector3(LineWidth * Mathf.Sqrt(8.0f),LineWidth * Mathf.Sqrt(8.0f),LineWidth * Mathf.Sqrt(8.0f));  
  48.             }  
  49.             if(LinePointContainer.Count > 1)  
  50.             {  
  51.                 //画二维面片线  
  52.                 //DrawLine2D(LinePointContainer[LinePointContainer.Count - 2],LinePointContainer[LinePointContainer.Count - 1]);  
  53.                 //画三维立体线  
  54.                 DrawLine3D(LinePointContainer[LinePointContainer.Count - 2],LinePointContainer[LinePointContainer.Count - 1]);                   //第一个参数是起点,第二个参数是终点
  55.             }  
  56.         }  
  57.         if(Input.GetMouseButtonDown(1))  
  58.         {  
  59.             //清空线容器  
  60.             if(LinePointContainer.Count < 3)return;  
  61.             DrawLine3D(LinePointContainer[LinePointContainer.Count - 1],LinePointContainer[0]);  
  62.             LinePointContainer.Clear();  
  63.         }  
  64.         if(Input.GetKeyDown(KeyCode.Escape))  
  65.         {  
  66.             //清除所有线物体  
  67.             LinePointContainer.Clear();  
  68.             ClearLineObjects();  
  69.         }  
  70.     }  
  71.       
  72.     /// <summary>  
  73.     /// 二维线  
  74.     /// </summary>  
  75.     /// <param name='PointA'>  
  76.     /// 初始点  
  77.     /// </param>  
  78.     /// <param name='PointB'>  
  79.     /// 结束点  
  80.     /// </param>  
  81.     void DrawLine2D(Vector3 PointA,Vector3 PointB)  
  82.     {  
  83.         float HorDisABx = PointB.x - PointA.x;  
  84.         float HorDisABz = PointB.z - PointA.z;  
  85.         float HorDisAB = Mathf.Sqrt(Mathf.Pow(HorDisABx,2) + Mathf.Pow(HorDisABz,2));  
  86.           
  87.         float offsetX = HorDisABz * LineWidth / HorDisAB;  
  88.         float offsetZ = HorDisABx * LineWidth / HorDisAB;  
  89.           
  90.         Vector3 Point1 = new Vector3(PointA.x - offsetX,PointA.y,PointA.z + offsetZ);  
  91.         Vector3 Point2 = new Vector3(PointA.x + offsetX,PointA.y,PointA.z - offsetZ);  
  92.         Vector3 Point3 = new Vector3(PointB.x + offsetX,PointB.y,PointB.z - offsetZ);  
  93.         Vector3 Point4 = new Vector3(PointB.x - offsetX,PointB.y,PointB.z + offsetZ);  
  94.           
  95.  

  96.           
  97.         GameObject go = new GameObject((LinePointContainer.Count - 1).ToString());  
  98.         go.transform.parent = LineObj.transform;  
  99.         Mesh mesh = go.AddComponent<MeshFilter>().mesh;  
  100.         go.AddComponent<MeshRenderer>();  
  101.         mesh.vertices = new Vector3[]{Point1,Point2,Point3,Point4};  
  102.         mesh.triangles = new int[]{2,1,0,0,3,2};  
  103.           
  104.         GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);  
  105.         sphere.transform.parent = LineObj.transform;  
  106.         sphere.transform.position = PointB;  
  107.         sphere.transform.localScale = new Vector3(LineWidth * 2,0.01f,LineWidth * 2);  
  108.     }  
  109.       
  110.     /// <summary>  
  111.     /// 三维线  
  112.     /// </summary>  
  113.     /// <param name='PointA'>  
  114.     /// 初始点  
  115.     /// </param>  
  116.     /// <param name='PointB'>  
  117.     /// 结束点  
  118.     /// </param>  
  119.     void DrawLine3D(Vector3 PointA,Vector3 PointB)  
  120.     {  
  121.         float HorDisABx = PointB.x - PointA.x;  
  122.         float HorDisABz = PointB.z - PointA.z;  
  123.         float HorDisAB = Mathf.Sqrt(Mathf.Pow(HorDisABx,2) + Mathf.Pow(HorDisABz,2));  //求起点和终点的模长
  124.           
  125.         float offsetX = HorDisABz * LineWidth / HorDisAB;  
  126.         float offsetZ = HorDisABx * LineWidth / HorDisAB;  
  127.           
  128.         Vector3 Point1 = new Vector3(PointA.x - offsetX,PointA.y,PointA.z + offsetZ);  
  129.         Vector3 Point2 = new Vector3(PointA.x + offsetX,PointA.y,PointA.z - offsetZ);  
  130.         Vector3 Point3 = new Vector3(PointB.x + offsetX,PointB.y,PointB.z - offsetZ);  
  131.         Vector3 Point4 = new Vector3(PointB.x - offsetX,PointB.y,PointB.z + offsetZ);  
  132.           
  133.         GameObject go1 = new GameObject((LinePointContainer.Count - 1).ToString() + "_1");  
  134.         go1.transform.parent = LineObj.transform;  
  135.         Mesh mesh1 = go1.AddComponent<MeshFilter>().mesh;  
  136.         go1.AddComponent<MeshRenderer>();  
  137.         mesh1.vertices = new Vector3[]{Point1,Point2,Point3,Point4};  
  138.         mesh1.triangles = new int[]{2,1,0,0,3,2};  
  139.           
  140.         Vector3 Point5 = new Vector3(PointA.x - offsetX,PointA.y + 2 * LineWidth,PointA.z + offsetZ);  
  141.         Vector3 Point6 = new Vector3(PointA.x + offsetX,PointA.y + 2 * LineWidth,PointA.z - offsetZ);  
  142.         Vector3 Point7 = new Vector3(PointB.x + offsetX,PointB.y + 2 * LineWidth,PointB.z - offsetZ);  
  143.         Vector3 Point8 = new Vector3(PointB.x - offsetX,PointB.y + 2 * LineWidth,PointB.z + offsetZ);  
  144.           
  145.         GameObject go2 = new GameObject((LinePointContainer.Count - 1).ToString() + "_2");  
  146.         go2.transform.parent = LineObj.transform;  
  147.         Mesh mesh2 = go2.AddComponent<MeshFilter>().mesh;  
  148.         go2.AddComponent<MeshRenderer>();  
  149.         mesh2.vertices = new Vector3[]{Point5,Point6,Point7,Point8};  
  150.         mesh2.triangles = new int[]{2,1,0,0,3,2};  
  151.           
  152.         GameObject go3 = new GameObject((LinePointContainer.Count - 1).ToString() + "_3");  
  153.         go3.transform.parent = LineObj.transform;  
  154.         Mesh mesh3 = go3.AddComponent<MeshFilter>().mesh;  
  155.         go3.AddComponent<MeshRenderer>();  
  156.         mesh3.vertices = new Vector3[]{Point6,Point2,Point3,Point7};  
  157.         mesh3.triangles = new int[]{2,1,0,0,3,2};  
  158.           
  159.         GameObject go4 = new GameObject((LinePointContainer.Count - 1).ToString() + "_4");  
  160.         go4.transform.parent = LineObj.transform;  
  161.         Mesh mesh4 = go4.AddComponent<MeshFilter>().mesh;  
  162.         go4.AddComponent<MeshRenderer>();  
  163.         mesh4.vertices = new Vector3[]{Point1,Point5,Point8,Point4};  
  164.         mesh4.triangles = new int[]{2,1,0,0,3,2};  
  165.           
  166.         GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);  
  167.         sphere.transform.parent = LineObj.transform;  
  168.         sphere.transform.position = new Vector3(PointB.x,PointB.y + LineWidth,PointB.z);  
  169.         sphere.transform.localScale = new Vector3(LineWidth * Mathf.Sqrt(8.0f),LineWidth * Mathf.Sqrt(8.0f),LineWidth * Mathf.Sqrt(8.0f));  
  170.     }  
  171.       
  172.     void ClearLineObjects ()  
  173.     {  
  174.         for(int i = 0 ; i < LineObj.transform.childCount;i ++)  
  175.         {  
  176.             GameObject go = LineObj.transform.GetChild(i).gameObject;  
  177.             Destroy(go);  
  178.         }  
  179.     }  
  180. }  

个人理解:

想象成一个几何体,以ABCD为地面,把地面抬高2 * LineWidth,PointA.z米作为顶面EFGH

1 GameObject go1 = new GameObject((LinePointContainer.Count - 1).ToString() + "_1"); 这个是画面ABCD

GameObject go2 = new GameObject((LinePointContainer.Count - 1).ToString() + "_2");  这个是画面EFGH

GameObject go3 = new GameObject((LinePointContainer.Count - 1).ToString() + "_3");  这个是画面BCGF

 GameObject go4 = new GameObject((LinePointContainer.Count - 1).ToString() + "_4");  这个是画面ADHE

其中面ABFE和面DCGH不用画,画了也没有意义





猜你喜欢

转载自blog.csdn.net/zxy13826134783/article/details/80114727
今日推荐