Unity GL 绘制线

2017-05-15  本文已影响0人  木子才
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestGL001 : MonoBehaviour {

    //绘制线段的材质
    public Material material;

    //此绘制方法由系统调用
    void OnPostRender() {
        if (!material) {
            Debug.LogError ("请给材质资源赋值");
            return;
        }
        //设置该材质通道,0为默认值
        material.SetPass (0);
        //设置绘制2D图像
        GL.LoadOrtho ();
        //表示开始绘制,绘制类型为线段
        GL.Begin (GL.LINES);
        //绘制线段0
        DrawLine (0, 0, 200, 100);
        //绘制线段1
        DrawLine (0, 50, 200, 150);
        //绘制线段2
        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));
    }
}
0E6283E3-33C2-435C-8CBA-8FBD39DCF48C.png

将脚本挂在摄像头上面~

上一篇下一篇

猜你喜欢

热点阅读