下面是轉載別人的,缺點是隻能在xz平面上畫線,可以添加一個地板來測試,鼠標點擊地板進行畫線
當再次看這篇文章時,還是覺得非常喫力,因爲距離上一次轉載的時間有點長了,上次是看懂了,但沒有記錄下來,所以這次看時還是需要費點腦子纔再次看懂了,所以這次我在後面詳細的記錄了下來,最好自己能夠看懂原來作者的代碼,不能完全看懂也沒有關係,在最後我會解釋一下核心的代碼和核心的算法。 (2019年7月13日新增)
這兩天在研究畫線的東西。直到昨天才小有成效。拿出來和大家分享一下。先上圖:
以前曾經嘗試過用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米作爲頂面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不用畫,畫了也沒有意義
(2019年7月13日新增)
其中PointA爲線的起點,PointB爲線的終點
上面的代碼:
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);
Point1對應於上圖中的A點,Point2對應上圖的B點,Point3對應上圖中的C點,Point4對應上圖的D點
想要繪製出面 ABCD,在Unity3d中只能繪製出三角形,這時我們可以把面ABCD分成兩個三角面 : 面ABC和麪ADC
把點Point3、Point2、Point1兩兩相連,再渲染(至於如何把三角形渲染成面,不需要你管,Unity3D做了),就成面ABC了
把點Point1、Point4、Point3兩兩相連,再渲染,就成面ADC了
當繪製出面ABC和麪ADC後,把這兩個面一組合,就形成了面ABCD,這時就有了下面的代碼:
mesh1.vertices = new Vector3[]{Point1,Point2,Point3,Point4};
mesh1.triangles = new int[]{2,1,0,0,3,2};
其中代碼 mesh1.triangles = new int[]{2,1,0,0,3,2}; 總共有6個數,連續三個數組成一組,這樣可以分成兩組 (2,1,0)和(0,3,2)
至於爲什麼是三個數爲一組,我猜你還缺乏Mesh基礎,請參考:https://blog.csdn.net/zxy13826134783/article/details/80114487
再看 mesh1.vertices[2]=Point3,mesh1.vertices[1]=Point2,mesh.vertices[0]=Point1, 數組下標一組合爲 (2,1,0), 把數組下標對應的點Point3、Point2、Point1 兩兩相連,不就成了三角形ABC了嗎,Unity3D自動把三角形ABC渲染成面ABC
同理mesh1.vertices[0]=Pont1,mesh1.vertices[3]=Point4,mesh1.vertices[2]=Point3,數組下標一組合爲 (0,3,2),把數組下標對應的點Point1、Point4、Point3 兩兩相連,不就成了三角形ADC了嗎,Unity3D自動把三角形ADC渲染成面ADC
把面ABC和麪ADC一組合就完成了面ABCD的繪製
同理,再把 面EFGH、面BCGF、面ADHE繪製,最後把四個面一組合,就組成了空心的三維模型線(算是線吧),如下圖: