【Unity】GL绘制图形和网格
新建 GLSquare脚本,绑定主相机。
Helper
public class GLSquare : MonoBehaviour
{
//线材质
private Material m_material;
//在摄像机渲染场景后,将调用 OnRenderObject。
private void OnRenderObject()
{
Draw();//调用 绘制
}
//绘制调用 Begin end
void Begin()
{
if (m_material == null)
//shader选择GUI/Text Shader后打包的软件可以看见线条
m_material = new Material(Shader.Find("GUI/Text Shader");//("Unlit/Color"));
m_material.SetPass(0);
//GL.LoadOrtho();//正交视图
GL.Begin(GL.LINES);
m_material.SetColor("_Color", Color.white);//定义颜色
}
void End()
{
GL.End();
}
void Draw()
{
Begin();
//若干 GL.Vertex 绘制 两个点一条线
for (int i = 0; i < m_linePoints.Count; i++)
{
GL.Vertex(m_linePoints[i][0]);
GL.Vertex(m_linePoints[i][1]);
}
End();
}
public static GLSquare Instance;
private void Awake()
{
Instance = this;
initPoints();//定义顶点
}
}
直接取点绘制网格
//定义顶点
//此处举例: 10*10 单元格为1的网格
List<Vector3[]> m_linePoints = new List<Vector3[]>();
void initPoints()
{
(-5, 0, -5)(5, 0, -5)
(-5, 0, 5)(-5, 0, -5)
(-5, 0, -4)(5, 0, -4)
(-4, 0, 5)(-4, 0, -5)
...
for (int i = -5; i <= 5; i++)
{
Vector3[] rows = new Vector3[2];
rows[0] = new Vector3(-5, 0, i);
rows[1] = new Vector3(5, 0, i);
m_linePoints.Add(rows);
Vector3[] clos = new Vector3[2];
clos[0] = new Vector3(i, 0, 5);
clos[1] = new Vector3(i, 0, -5);
m_linePoints.Add(clos);
}
}
绘制标准网格
//调用,参数 单元格边长,网格宽度和高度(单元格个数)
//initPoints(float cellsize, int width, int height)
private bool isReady = false;
//此方法类似update是一只刷新的
private void OnRenderObject()
{
//isReady作为开关
if (isReady)
{
DrawArea();
}
}
public void clearLine() {
m_linePoints.Clear();
}
//定义顶点
public void initPoints(float cellsize, int width, int height)
{
//重新绘制时,清楚之前的绘制图案
clearLine();
for (float i = 0; i <= height; i++)
{
Vector3[] rows = new Vector3[2];
rows[0] = new Vector3(0, 0, i * cellsize);
rows[1] = new Vector3(width * cellsize, 0, i * cellsize);
m_linePoints.Add(rows);
}
for (float i = 0; i <= width; i++)
{
Vector3[] clos = new Vector3[2];
clos[0] = new Vector3(i * cellsize, 0, height * cellsize);
clos[1] = new Vector3(i * cellsize, 0, 0);
m_linePoints.Add(clos);
}
isReady = true;
}
正交视图鼠标框选矩形
private Vector3 m_startPos;
private Vector3 m_endPos;
private bool Drow = false;
private void OnRenderObject()
{
if(Drow)
{
DrawAreaByPos();
}
}
private void Update()
{
//鼠标框选:鼠标按下,抬起 点击屏幕,拖拽鼠标,绘制矩形
//鼠标按下,开始绘制
if (Input.GetMouseButtonDown(0))
{
Drow = true;
m_startPos = GetScreenTouchPos();//起点
}
if (Drow)
{
m_endPos = GetScreenTouchPos();
}
//鼠标抬起,
if (Input.GetMouseButtonUp(0))
{
Drow = false;
m_startPos = Vector3.Zero;
m_endPos = Vector3.Zero;
DrawAreaByPos();
}
}
public static Vector3 GetScreenTouchPos()
{
return GetTouchPosition();
}
public static Vector3 GetTouchPosition()
{
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
return Input.mousePosition;
#else
return Input.touches[0].position;
#endif
}
}
void DrawAreaByPos()
{
DrawArea(
new Rect(
math.min(m_startPos.x, m_endPos.x),
math.min(m_startPos.y, m_endPos.y),
math.max(m_startPos.x, m_endPos.x) - math.min(m_startPos.x, m_endPos.x),
math.max(m_startPos.y, m_endPos.y) - math.min(m_startPos.y, m_endPos.y)
)
);
}
//绘制顶点坐标
void DrawArea(Rect rect)
{
Begin();
GL.Vertex3(rect.xMin / Screen.width, rect.yMin / Screen.height, 0);
GL.Vertex3(rect.xMin / Screen.width, rect.yMax / Screen.height, 0);
GL.Vertex3(rect.xMin / Screen.width, rect.yMax / Screen.height, 0);
GL.Vertex3(rect.xMax / Screen.width, rect.yMax / Screen.height, 0);
GL.Vertex3(rect.xMax / Screen.width, rect.yMax / Screen.height, 0);
GL.Vertex3(rect.xMax / Screen.width, rect.yMin / Screen.height, 0);
GL.Vertex3(rect.xMax / Screen.width, rect.yMin / Screen.height, 0);
GL.Vertex3(rect.xMin / Screen.width, rect.yMin / Screen.height, 0);
End();
}