您的位置:首页 > 其它

Curve Bezier

2016-04-04 20:09 267 查看
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DrawLine : MonoBehaviour {

// Use this for initialization

private List<Vector3> list;
private bool IsDraw = false;
private LineRenderer lineRenderer;
void Start () {
lineRenderer = GetComponent<LineRenderer>();
}

// Update is called once per frame
void Update()
{

if (Input.GetMouseButtonDown(0))
{
if (list == null)
list = new List<Vector3>();

list.Clear();
IsDraw = true;
lineRenderer.SetVertexCount(0);
}
if (Input.GetMouseButton(0))//记录划线点
{

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000))
{
Vector3 point = hit.point;
if (!hit.collider.name.Equals("Terrain"))
{
return;
}
list.Add(point);
print(list.Count);
}

}

if (Input.GetMouseButtonUp(0))
{
IsDraw = false;
}
drawBezierCurve();
//  drawInputPointCurve();

}

private void drawBezierCurve()
{
if(IsDraw&&list.Count>0){
List<Vector3> bcList;
BezierCurve bc= new BezierCurve();
bcList = bc.CreateCurve(list);//  通过贝塞尔曲线 平滑划线点
lineRenderer.SetVertexCount(bcList.Count); //maxVertices < 65536 && maxIndices < 65536*3  点的密度 可以降低 太多会超过可设顶点数
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);
}

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: