unity绘制跟随鼠标移动的曲线(借鉴大神,仅作为笔记用)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DrawLineTest : MonoBehaviour
{
/// <summary>
/// 绘制线段的材质
/// </summary>
public Material material;
/// <summary>
/// 鼠标线段链表
/// </summary>
private List<Vector3> lineInfo;

void Start()
{
lineInfo = new List<Vector3>();
}

void Update()
{
if (Input.GetMouseButton(0))
{
//将鼠标的位置存储进链表
lineInfo.Add(Input.mousePosition);
}
}
/// <summary>
/// 系统调用的绘制方法
/// </summary>
void OnPostRender()
{
if (!material) return;
//设置材质通道,0为默认值
material.SetPass(0);
//设置绘制2D图像
GL.LoadOrtho();
//开始绘制,表示绘制线段
GL.Begin(GL.LINES);
int size = lineInfo.Count;
//遍历鼠标点的链表
for (int i = 0; i < size - 1; i++)
{
Vector3 start = lineInfo[i];
Vector3 end = lineInfo[i + 1];
//绘制线段
DrawLine(start.x, start.y, end.x, end.y);
//lineInfo.Clear();
}
GL.End();
}
void DrawLine(float x1, float y1, float x2, float y2)
{
//绘制线段,需要将屏幕中某个点的像素坐标除以屏幕宽或高
GL.Vertex(new Vector3(x1 / Screen.width, y1 / Screen.height, 0));
GL.Vertex(new Vector3(x2 / Screen.width, y2 / Screen.height, 0));
}
}

 

 

 

unity绘制跟随鼠标移动的曲线(借鉴大神,仅作为笔记用)

上一篇:uni-app 项目记录


下一篇:iOS NFC功能开发