Unity3D点击绘制二维模型线和三维模型线
下面是转载别人的,缺点是只能在xz平面上画线,可以添加一个地板来测试,鼠标点击地板进行画线
这两天在研究画线的东西。直到昨天才小有成效。拿出来和大家分享一下。先上图:
以前曾经尝试过用LineRender写画线的功能,但是在拐弯的时候会出现变形和扭曲。所以才决定用绘制Mesh的方法写一个画线的功能。
东西其实很简单,稍微有一点数学的知识,用到了三角函数。还有一些关于构造Mesh的相关代码。下面有草图一张:
黑色的线框代表画出来的模型线,PointA和PointB代表获取的起始点和终结点,A,B,C,D为Mesh的四个顶点。
已知起始点和终结点和线宽,就可以求出四个顶点的坐标。并根据这四个顶点绘制出Mesh.
至于具体的实现细节,大家看代码吧。
DrawMeshLine类:
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class DrawMeshLine : MonoBehaviour
- {
- //线容器
- List<Vector3> LinePointContainer;
- //线宽
- public float LineWidth = 0.5f;
- //地形层
- public int Layer = 8;
- //线物体父物体
- GameObject LineObj;
- void Start ()
- {
- LinePointContainer = new List<Vector3>();
- LineObj = new GameObject("Lines");
- }
- void Update ()
- {
- if(Input.GetMouseButtonDown(0))
- {
- Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if(Physics.Raycast(ray,out hit))
- {
- if(hit.transform.gameObject.layer != Layer)return;
- LinePointContainer.Add(hit.point);
- }
- if(LinePointContainer.Count == 1)
- {
- //二维初始面片圆
- // GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- // sphere.transform.parent = LineObj.transform;
- // sphere.transform.position = hit.point;
- // sphere.transform.localScale = new Vector3(LineWidth * 2,0.01f,LineWidth * 2);
- // sphere.renderer.material.shader = Shader.Find("GUI/Text Shader");
- // sphere.renderer.material.SetColor("_Color",new Color(255,0,0,255) / 255);
- //三维初始球
- GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- sphere.transform.parent = LineObj.transform;
- sphere.transform.position = new Vector3(hit.point.x,hit.point.y + LineWidth,hit.point.z);
- sphere.transform.localScale = new Vector3(LineWidth * Mathf.Sqrt(8.0f),LineWidth * Mathf.Sqrt(8.0f),LineWidth * Mathf.Sqrt(8.0f));
- }
- if(LinePointContainer.Count > 1)
- {
- //画二维面片线
- //DrawLine2D(LinePointContainer[LinePointContainer.Count - 2],LinePointContainer[LinePointContainer.Count - 1]);
- //画三维立体线
- DrawLine3D(LinePointContainer[LinePointContainer.Count - 2],LinePointContainer[LinePointContainer.Count - 1]); //第一个参数是起点,第二个参数是终点
- }
- }
- if(Input.GetMouseButtonDown(1))
- {
- //清空线容器
- if(LinePointContainer.Count < 3)return;
- DrawLine3D(LinePointContainer[LinePointContainer.Count - 1],LinePointContainer[0]);
- LinePointContainer.Clear();
- }
- if(Input.GetKeyDown(KeyCode.Escape))
- {
- //清除所有线物体
- LinePointContainer.Clear();
- ClearLineObjects();
- }
- }
- /// <summary>
- /// 二维线
- /// </summary>
- /// <param name='PointA'>
- /// 初始点
- /// </param>
- /// <param name='PointB'>
- /// 结束点
- /// </param>
- void DrawLine2D(Vector3 PointA,Vector3 PointB)
- {
- float HorDisABx = PointB.x - PointA.x;
- float HorDisABz = PointB.z - PointA.z;
- float HorDisAB = Mathf.Sqrt(Mathf.Pow(HorDisABx,2) + Mathf.Pow(HorDisABz,2));
- float offsetX = HorDisABz * LineWidth / HorDisAB;
- float offsetZ = HorDisABx * LineWidth / HorDisAB;
- Vector3 Point1 = new Vector3(PointA.x - offsetX,PointA.y,PointA.z + offsetZ);
- Vector3 Point2 = new Vector3(PointA.x + offsetX,PointA.y,PointA.z - offsetZ);
- Vector3 Point3 = new Vector3(PointB.x + offsetX,PointB.y,PointB.z - offsetZ);
- Vector3 Point4 = new Vector3(PointB.x - offsetX,PointB.y,PointB.z + offsetZ);
- GameObject go = new GameObject((LinePointContainer.Count - 1).ToString());
- go.transform.parent = LineObj.transform;
- Mesh mesh = go.AddComponent<MeshFilter>().mesh;
- go.AddComponent<MeshRenderer>();
- mesh.vertices = new Vector3[]{Point1,Point2,Point3,Point4};
- mesh.triangles = new int[]{2,1,0,0,3,2};
- GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- sphere.transform.parent = LineObj.transform;
- sphere.transform.position = PointB;
- sphere.transform.localScale = new Vector3(LineWidth * 2,0.01f,LineWidth * 2);
- }
- /// <summary>
- /// 三维线
- /// </summary>
- /// <param name='PointA'>
- /// 初始点
- /// </param>
- /// <param name='PointB'>
- /// 结束点
- /// </param>
- void DrawLine3D(Vector3 PointA,Vector3 PointB)
- {
- float HorDisABx = PointB.x - PointA.x;
- float HorDisABz = PointB.z - PointA.z;
- float HorDisAB = Mathf.Sqrt(Mathf.Pow(HorDisABx,2) + Mathf.Pow(HorDisABz,2)); //求起点和终点的模长
- float offsetX = HorDisABz * LineWidth / HorDisAB;
- float offsetZ = HorDisABx * LineWidth / HorDisAB;
- Vector3 Point1 = new Vector3(PointA.x - offsetX,PointA.y,PointA.z + offsetZ);
- Vector3 Point2 = new Vector3(PointA.x + offsetX,PointA.y,PointA.z - offsetZ);
- Vector3 Point3 = new Vector3(PointB.x + offsetX,PointB.y,PointB.z - offsetZ);
- Vector3 Point4 = new Vector3(PointB.x - offsetX,PointB.y,PointB.z + offsetZ);
- GameObject go1 = new GameObject((LinePointContainer.Count - 1).ToString() + "_1");
- go1.transform.parent = LineObj.transform;
- Mesh mesh1 = go1.AddComponent<MeshFilter>().mesh;
- go1.AddComponent<MeshRenderer>();
- mesh1.vertices = new Vector3[]{Point1,Point2,Point3,Point4};
- mesh1.triangles = new int[]{2,1,0,0,3,2};
- Vector3 Point5 = new Vector3(PointA.x - offsetX,PointA.y + 2 * LineWidth,PointA.z + offsetZ);
- Vector3 Point6 = new Vector3(PointA.x + offsetX,PointA.y + 2 * LineWidth,PointA.z - offsetZ);
- Vector3 Point7 = new Vector3(PointB.x + offsetX,PointB.y + 2 * LineWidth,PointB.z - offsetZ);
- Vector3 Point8 = new Vector3(PointB.x - offsetX,PointB.y + 2 * LineWidth,PointB.z + offsetZ);
- GameObject go2 = new GameObject((LinePointContainer.Count - 1).ToString() + "_2");
- go2.transform.parent = LineObj.transform;
- Mesh mesh2 = go2.AddComponent<MeshFilter>().mesh;
- go2.AddComponent<MeshRenderer>();
- mesh2.vertices = new Vector3[]{Point5,Point6,Point7,Point8};
- mesh2.triangles = new int[]{2,1,0,0,3,2};
- GameObject go3 = new GameObject((LinePointContainer.Count - 1).ToString() + "_3");
- go3.transform.parent = LineObj.transform;
- Mesh mesh3 = go3.AddComponent<MeshFilter>().mesh;
- go3.AddComponent<MeshRenderer>();
- mesh3.vertices = new Vector3[]{Point6,Point2,Point3,Point7};
- mesh3.triangles = new int[]{2,1,0,0,3,2};
- GameObject go4 = new GameObject((LinePointContainer.Count - 1).ToString() + "_4");
- go4.transform.parent = LineObj.transform;
- Mesh mesh4 = go4.AddComponent<MeshFilter>().mesh;
- go4.AddComponent<MeshRenderer>();
- mesh4.vertices = new Vector3[]{Point1,Point5,Point8,Point4};
- mesh4.triangles = new int[]{2,1,0,0,3,2};
- GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- sphere.transform.parent = LineObj.transform;
- sphere.transform.position = new Vector3(PointB.x,PointB.y + LineWidth,PointB.z);
- sphere.transform.localScale = new Vector3(LineWidth * Mathf.Sqrt(8.0f),LineWidth * Mathf.Sqrt(8.0f),LineWidth * Mathf.Sqrt(8.0f));
- }
- void ClearLineObjects ()
- {
- for(int i = 0 ; i < LineObj.transform.childCount;i ++)
- {
- GameObject go = LineObj.transform.GetChild(i).gameObject;
- Destroy(go);
- }
- }
- }
个人理解:
想象成一个几何体,以ABCD为地面,把地面抬高2 * LineWidth,PointA.z米作为顶面EFGH
1 GameObject go1 = new GameObject((LinePointContainer.Count - 1).ToString() + "_1"); 这个是画面ABCD
2 GameObject go2 = new GameObject((LinePointContainer.Count - 1).ToString() + "_2"); 这个是画面EFGH
3 GameObject go3 = new GameObject((LinePointContainer.Count - 1).ToString() + "_3"); 这个是画面BCGF
4 GameObject go4 = new GameObject((LinePointContainer.Count - 1).ToString() + "_4"); 这个是画面ADHE
其中面ABFE和面DCGH不用画,画了也没有意义