ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

GL图形库

2022-08-13 20:03:39  阅读:151  来源: 互联网

标签:Screen float height width 图形库 GL void


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();
	}
}

标签:Screen,float,height,width,图形库,GL,void
来源: https://www.cnblogs.com/jyhlearning/p/16583874.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有