unity关于lineRender平滑画线问题

来源:互联网 发布:tensorflow官方 编辑:程序博客网 时间:2024/05/16 10:18

使用lineRender的时候需要在摄像机上加上组件linerender 可以调节粗细颜色之类

可以简单下面方法测试

void Start () {lineRenderer = gameObject.GetComponent<LineRenderer>(); lineRenderer.SetVertexCount(5);}    void Update () {        lineRenderer.SetPosition (0, new Vector3 (-1, 1, 0));        lineRenderer.SetPosition (1, new Vector3 (1, 1, 0));        lineRenderer.SetPosition (2, new Vector3 (1, -1, 0));        lineRenderer.SetPosition (3, new Vector3 (-1, -1, 0));        lineRenderer.SetPosition (4, new Vector3 (-1, 1, 0));        } 
可能画面里面看不到,因为在组件上没有材质
一定要加sprite/default之类的材质的材质  不然可能看不见或者视觉效果不正常


下面是简单画线代码

public class drawLine : MonoBehaviour {LineRenderer lineRenderer;int i = 0;void Start () {lineRenderer = gameObject.GetComponent<LineRenderer>(); }void Update () {if(Input.GetMouseButton(0)) { i++;lineRenderer.SetVertexCount(i); lineRenderer.SetPosition(i-1,Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,10))); }}}
但是可以发现严重的问题就是有锯齿,所以这里最好用Vectrosity来解决

可以使用这里的

http://blog.csdn.net/taotaoah/article/details/50607444

可以在drawlinestouch的例子上加line.joins = Joins.Weld;

但是Vectrosity是使用另一个摄像机的,所以要截屏的比较困难


所以下面要用的是贝塞尔曲线方式

using UnityEngine;using System.Collections;using System.Collections.Generic;public class BezierPath{public List<Vector3> pathPoints;private int segments;public int pointCount;public BezierPath(){pathPoints = new List<Vector3>();pointCount = 100;//最大点数}public void DeletePath(){pathPoints.Clear ();}//t就是两点之间几分之几的位置Vector3 BezierPathCalculation(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t){float tt = t * t;float ttt = t * tt;float u = 1.0f - t;float uu = u * u;float uuu = u * uu;Vector3 B = new Vector3();B = uuu * p0;B += 3.0f * uu * t * p1;B += 3.0f * u * tt * p2;B += ttt * p3;return B;}public List<Vector3> CreateCurve(List<Vector3> controlPoints){segments = controlPoints.Count / 3;//以3为间隔进行平滑 算出分段数量pointCount = controlPoints.Count;//这里最大点数就是本身for (int s = 0; s < controlPoints.Count -3; s+=3)//以3为间隔遍历所有点{Vector3 p0 = controlPoints[s];Vector3 p1 = controlPoints[s+1];Vector3 p2 = controlPoints[s+2]; Vector3 p3 = controlPoints[s+3];//第一个点的处理if(s == 0){pathPoints.Add(BezierPathCalculation(p0, p1, p2, p3, 0.0f));}    //for (int p = 0; p < (pointCount/segments); p++) {float t = (1.0f / (pointCount/segments)) * p;Vector3 point = new Vector3 ();point = BezierPathCalculation (p0, p1, p2, p3, t);pathPoints.Add (point);}}return pathPoints;}}

using UnityEngine;using System.Collections;using System.Collections.Generic;public class DrawLine : MonoBehaviour {// Use this for initializationprivate List<Vector3> list;private bool IsDraw = false;private LineRenderer lineRenderer;void Start () {lineRenderer = gameObject.GetComponent<LineRenderer>();}// Update is called once per framevoid Update(){if (Input.GetMouseButtonDown(1)){if (list == null)list = new List<Vector3>();list.Clear();IsDraw = true;lineRenderer.SetVertexCount(0);}if (Input.GetMouseButton(1)){list.Add(Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,10)));}if (Input.GetMouseButtonUp(1)){IsDraw = false;}drawBezierCurve();//drawInputPointCurve();}private void drawBezierCurve(){if(IsDraw&&list.Count>0){List<Vector3> bcList;//BezierCurve bc= new BezierCurve();BezierPath bc= new BezierPath();//bcList = bc.CreateCurve(list);//  通过贝塞尔曲线 平滑bcList = bc.CreateCurve(list);//  通过贝塞尔曲线 平滑lineRenderer.SetVertexCount(bcList.Count); for (int i = 0; i < bcList.Count; i++){Vector3 v = bcList[i];v += new Vector3(0, 0.5f, 0);lineRenderer.SetPosition(i, v);}}}//普通没有使用贝塞尔的情况private void drawInputPointCurve(){if (IsDraw && list.Count > 0){lineRenderer.SetVertexCount(list.Count);for (int i = 0; i < list.Count; i++){Vector3 v = list[i];v += new Vector3(0, 0.5f, 0);lineRenderer.SetPosition(i, v);}}}}


可以看到图片那种锯齿会少很多,当然还有很多改进的地方,比如加上抗锯齿啊,使用贴图或者rendertexture之类

但是由于项目不同,不是每个方法都能用,要看自己斟酌

最后吐糟下unity的linerenderer,也没有2D版的,没法设置corner或者joint

其实还有的方法就是放弃unity自带的linerenderer而使用自己写mesh的方式进行,这样可以更好平滑

0 0