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

下面是转载别人的,缺点是只能在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绘制,最后把四个面一组合,就组成了空心的三维模型线(算是线吧),如下图:

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值