GL图形库
GL.PushMatrix();//将模型、视图和投影矩阵保存到矩阵堆栈顶部。
GL.PopMatrix();//出栈
mat:Material.SetPass(0);//设置材质通道
GL.LoadOrtho();//将正交投影加载到投影矩阵中,将标识加载到 模型和视图矩阵中。
GL.Begin(GL.QUADS);//绘制类型为四边形
GL.Begin(GL.TRIANGLES);//绘制类型为三角形
GL.Begin(GL.LINES);//绘制类型为直线
GL.Vertex(new Vector3(x1 / Screen.width, y1 / Screen.height, 0));//屏幕坐标为真实坐标除屏幕长宽,提供一个顶点
GL.Vertex3(x1 / Screen.width, y1 / Screen.height, 0);//提供一个顶点,上面的和下面的作用一致们就是参数不一样
绘制直线
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scripts_07_15 : MonoBehaviour
{
public Material material;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnPostRender()
{
if(!material)
{
Debug.LogError("请给资源赋值");
return ;
}
material.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.LINES);
DrawLine(0, 0, 200, 100);
DrawLine(0, 50, 200, 150);
DrawLine(0, 100, 200, 200);
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));
}
}
用鼠标在屏幕绘制线条
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scripts_07_16 : MonoBehaviour
{
public Material material;
public List<Vector3> lineInfo;
private bool draw;
// Start is called before the first frame update
void Start()
{
lineInfo = new List<Vector3>();
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButton(0))
{
lineInfo.Add(Input.mousePosition);
draw = true;
}
if(Input.GetMouseButtonUp(0))
{
lineInfo.Clear();
draw = false;
}
}
private void OnGUI()
{
GUILayout.Label("当前鼠标x轴的位置:" + Input.mousePosition.x);
GUILayout.Label("当前鼠标z轴的位置:" + Input.mousePosition.y);
}
private void OnPostRender()
{
if (!draw) return;
if(!material)
{
Debug.LogError("请给材质资源包赋值");
return;
}
material.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.LINES);
int length = lineInfo.Count;
for (int i = 0; i < length - 1; i++)
DrawLine(lineInfo[i].x, lineInfo[i].y, lineInfo[i + 1].x, lineInfo[i + 1].y);
GL.End();
}
void DrawLine(float x0, float y0, float x1, float y1)
{
GL.Vertex(new Vector3(x0 / Screen.width, y0 / Screen.height, 0));//提供一个顶点
GL.Vertex(new Vector3(x1 / Screen.width, y1 / Screen.height, 0));
}
}
绘制四边形
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scripts_07_17 : MonoBehaviour
{
public Material mat0, mat1, mat2;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnPostRender()
{
DrawRect(100, 100, 100, 100, mat0);//绘制矩形
DrawRect(250, 100, 100, 100, mat1);
DrawQuads(15, 5, 10,115, 95, 110, 90, 10, mat2);//绘制四边形
}
void DrawRect(float x, float y, float width, float height, Material mat)
{
GL.PushMatrix();//将模型、视图和投影矩阵保存到矩阵堆栈顶部。
mat.SetPass(0);//设置材质通道
GL.LoadOrtho();//将正交投影加载到投影矩阵中,将标识加载到 模型和视图矩阵中。
GL.Begin(GL.QUADS);
GL.Vertex3(x/Screen.width, y/Screen.height, 0);
GL.Vertex3(x / Screen.width, (y+height) / Screen.height, 0);
GL.Vertex3((x+width) / Screen.width, (y +height) / Screen.height, 0);
GL.Vertex3((x+width) / Screen.width, y / Screen.height, 0);
GL.End();
GL.PopMatrix();//出栈
}
void DrawQuads(float x1,float y1, float x2, float y2, float x3, float y3, float x4, float y4, Material mat)
{
GL.PushMatrix();
mat.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.QUADS);
GL.Vertex3(x1/Screen.width, y1/Screen.height, 0);
GL.Vertex3(x2/Screen.width, y2/Screen.height, 0);
GL.Vertex3(x3/Screen.width, y3/Screen.height, 0);
GL.Vertex3(x4/Screen.width, y4/Screen.height, 0);
GL.End();
GL.PopMatrix();
}
}
绘制三角形
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scripts_07_18 : MonoBehaviour
{
public Material mat;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnPostRender()
{
DrawTringle(100, 0, 100, 200, 200, 100, mat);
}
void DrawTringle(float x1, float y1 ,float x2, float y2, float x3, float y3, Material mat)
{
mat.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.TRIANGLES);
GL.Vertex3(x1 / Screen.width, y1 / Screen.height, 0);//提供一个顶点
GL.Vertex3(x2 / Screen.width, y2 / Screen.height, 0);
GL.Vertex3(x3 / Screen.width, y3 / Screen.height, 0);
GL.End();
}
}
绘制3D图形(尚存在问题)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scripts_07_19 : MonoBehaviour
{
public Material mat0;
public Material mat1;
public Material mat3;
private void Start()
{
}
void OnPostRender()
{
//绘制正四边形
DrawRect(100, 100, 100, 100, mat0);
DrawRect(250, 100, 100, 100, mat1);
//绘制无规则四边形
DrawQuads(15, 5, 10, 115, 95, 110, 90, 10, mat3);
}
void DrawRect(float x, float y, float width, float height, Material mat)
{
GL.PushMatrix();
mat.SetPass(0);
//绘制类型为四边形
GL.Begin(GL.QUADS);
GL.Vertex3(x / Screen.width, y / Screen.height, 0);
GL.Vertex3(x / Screen.width, (y + height) / Screen.height, 0);
GL.Vertex3((x + width) / Screen.width, (y + height) / Screen.height, 0);
GL.Vertex3((x + width) / Screen.width, y / Screen.height, 0);
GL.End();
GL.PopMatrix();
}
void DrawQuads(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, Material mat)
{
GL.PushMatrix();
mat.SetPass(0);
//绘制类型为四边形
GL.Begin(GL.QUADS);
GL.Vertex3(x1 / Screen.width, y1 / Screen.height, 0);
GL.Vertex3(x2 / Screen.width, y2 / Screen.height, 0);
GL.Vertex3(x3 / Screen.width, y3 / Screen.height, 0);
GL.Vertex3(x4 / Screen.width, y4 / Screen.height, 0);
GL.End();
GL.PopMatrix();
}
}
posted on 2022-08-13 19:39 jyhlearning 阅读(42) 评论(0) 编辑 收藏 举报