Unity GL

画线:

DrawLine.cs 脚本挂到一个新的 GameObject 上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 屏幕坐标画线
/// </summary>
public class DrawLine : MonoBehaviour {

    private static Material s_lineMaterial;

    private static void CreateLineMaterial () {
        // 如果材质球不存在
        if (!s_lineMaterial) {
            // 用代码的方式实例一个材质球
            Shader shader = Shader.Find("Hidden/Internal-Colored");
            s_lineMaterial = new Material(shader);
            s_lineMaterial.hideFlags = HideFlags.HideAndDontSave;
            // 设置参数
            s_lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            s_lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            // 设置参数
            s_lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
            // 设置参数
            s_lineMaterial.SetInt("_ZWrite", 0);
        }
    }

    private void OnRenderObject () {
        // 创建材质球
        CreateLineMaterial();

        // 激活第一个着色器 Pass(本例中,它是唯一的 Pass)
        s_lineMaterial.SetPass(0);

        // 渲染入栈  在Push——Pop之间写GL代码
        GL.PushMatrix();
        {
            // 1.用屏幕坐标绘图,移动对象时图像保持不动
            GL.LoadPixelMatrix();
            
            // 2.使用局部坐标绘图,移动对象时图像也会移动
            // 矩阵相乘,将物体坐标转化为世界坐标
            //GL.MultMatrix(transform.localToWorldMatrix);
			
			// 1,2 都不使用时,使用世界坐标绘图,移动对象时图像保持不动

            // 开始画线  在Begin——End之间写画线方式
            // GL.LINES 画线
            GL.Begin(GL.LINES);
            {
                GL.Color(Color.green);
                GL.Vertex3(0, 0, 0);
                GL.Vertex3(500, 500, 0);
            }
            GL.End();
        }
        // 渲染出栈
        GL.PopMatrix();

    }

}
原文地址:https://www.cnblogs.com/kingBook/p/15136434.html