初心
一開始的時候是想在一張圖上進行涂鴉官硝,經(jīng)過尋找矗蕊,在unity中,可以通過GL圖形庫傻咖,來繪制線條,以達到涂鴉的效果岖研。
代碼實現(xiàn)
簡單的添加了改變線條顏色和線條粗細的功能卿操,在一定程度上可以撤銷操作和斷點繪制
ps:裝13的英文注釋請忽略
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DrawLineByCursor: MonoBehaviour
{
// the line's Material
private Material m_LineMaterial;
public Material LineMaterial
{
get { return m_LineMaterial; }
}
// MousePosition Data
private List<List<Vector3>> m_LineInfo;
// the line's Width
public int m_Width = 1;
public Color LineColor = Color.black;
// MouseButtonUp Count
private int currentID;
// Use this for initialization
void Start ()
{
currentID = 0;
// Create List
m_LineInfo = new List<List<Vector3>>();
}
// Update is called once per frame
void Update ()
{
// the Left mouseButton Up stop recording the mousePosition to this list
if (Input.GetMouseButtonUp(0))
{
currentID += 1;
}
// the left mouseButton Down create new mousePosition list
if (Input.GetMouseButtonDown(0))
{
List<Vector3> tmp = new List<Vector3>();
m_LineInfo.Add(tmp);
}
if (Input.GetMouseButton(0))
{
m_LineInfo[currentID].Add(Input.mousePosition);
}
if (Input.GetKeyDown(KeyCode.C))
ClearAll();
if (Input.GetKeyDown(KeyCode.Z))
Undo();
}
// Draw Line by system
void OnPostRender()
{
// create the line's shader
if (m_LineMaterial == null)
CreateLineShader();
// set the Ortho
GL.LoadOrtho();
// filtration the material
m_LineMaterial.SetPass(0);
// strat Draw line
GL.Begin(GL.LINES);
// Get the need draw line Count
int size = m_LineInfo.Count;
if (size != 0)
{
// draw line one by one
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < m_LineInfo[i].Count - 1; ++j)
{
Vector3 start = m_LineInfo[i][j];
Vector3 end = m_LineInfo[i][j + 1];
GL.Color(LineColor);
DrawLine(start.x, start.y, start.z, end.x, end.y, end.z, m_Width);
}
}
}
GL.End();
}
// DrawLine procedure
void DrawLine(float x1, float y1, float z1, float x2, float y2, float z2, int width)
{
for (int i = 0; i < width; ++i)
{
int iTmp = width / 2;
// Draw current line
GL.Vertex3(x1 / Screen.width, y1 / Screen.height, z1);
GL.Vertex3(x2 / Screen.width, y2 / Screen.height, z2);
// Draw Left
if (i % 2 == 0)
{
for (int j = -iTmp; j < iTmp; ++j)
{
GL.Vertex3((x1 - (i / 2)) / Screen.width, (y1 + j) / Screen.height, z1);
GL.Vertex3((x2 - (i / 2)) / Screen.width, (y2 + j) / Screen.height, z2);
}
}
// Draw Right
else
{
for (int j = -iTmp; j < iTmp; ++j)
{
GL.Vertex3((x1 + (i / 2)) / Screen.width, (y1 + j) / Screen.height, z1);
GL.Vertex3((x2 + (i / 2)) / Screen.width, (y2 + j) / Screen.height, z2);
}
}
}
}
// Create LineShader
private void CreateLineShader()
{
Shader shader = Shader.Find("Hidden/Internal-Colored");
m_LineMaterial = new Material(shader);
m_LineMaterial.hideFlags = HideFlags.HideAndDontSave;
m_LineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
m_LineMaterial.SetInt("_DstBlend",(int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
m_LineMaterial.SetInt("_Cull",(int)UnityEngine.Rendering.CullMode.Off);
m_LineMaterial.SetInt("_ZWrite", 0);
}
// ClearAll
public void ClearAll()
{
m_LineInfo.Clear();
currentID = 0;
}
// Undo
public void Undo()
{
if (currentID > 0)
currentID--;
m_LineInfo.RemoveAt(currentID);
}
}
附注
在使用過程中,嘗試使用unity創(chuàng)建完成的材質(zhì)球孙援,發(fā)現(xiàn)無法改變線條的顏色害淤,所以動態(tài)創(chuàng)建了材質(zhì),如果有大神知道如何使用通過unity創(chuàng)建的材質(zhì)球拓售,煩請指點