unity3D 教程宝典之GL

原创文章如需转载请注明:转载自风宇冲Unity3D教程学院


             GL

即Graphics Library。Low-Level GraphicsLibrary。计算matrices,发出类似OpenGL的immediate模式的渲染指令,和其他低级图像任务。Graphic.DrawMesh()比GL更高效。

GL立即绘制函数只用当前material的设置。因此除非你显示指定mat,否则mat可以是任何材质。并且GL可能会改变材质。

GL是立即执行的,如果你在Update()里调用,它们将在相机渲染前执行,相机渲染将会清空屏幕,GL效果将无法看到。

通常GL用法是,在camera上贴脚本,并在OnPostRender()里执行。
注意: 
1.GL的线等基本图元并没有uv.所有是没有贴图纹理影射的,shader里仅仅做的是单色计算或者对之前的影像加以处理。
2.GL所使用的shader里必须有Cull off指令,否则显示会变成如下
【风宇冲】Unity3D教程宝典之GL
3. 如果是线,颜色是GL .Color ( newColor ( 1 , 1 , 1 , 0.5f ));设置的颜色
  如果是GL.TRIANGLES或者是 GL.QUADS,则颜色是shader里的颜色。

1.
GL.PushMatrix()
保存matrices至matrix stack上。
GL.PopMatrix()
从matrix stack上读取matrices。

2.
GL.LoadPixelMatrix()
改变MVP矩阵,使得transform里的xy 直接对应像素,(0,0)表示屏幕viewport的左下角,z的范围是(-1,1),该函数改变camera的参数,所以需要GL.PushMatrix()保存和GL.PopMatrix()读取。
GL.Vertex3()的取值范围从左下角的(0,0,0)至右上角的(Screen.width,Screen.height,0)

GL.LoadOrtho()
设置ortho perspective,即水平视角。After calling LoadOrtho, the viewingfrustum goes from (0,0,-1) to (1,1,100). 主要用于在纯2D里绘制图元。
GL .Vertex3()的取值范围从左下角的(0,0,0)至右上角的(1,1,0)

3.
OnPostRender()
只有物体上有激活的摄像机时,才会调用的函数,当摄像机完成渲染场景,绘制了所有物体以后调用。
OnPostRender可以变成co-routine,加yield语句即可。

WaitForEndOfFrame()
等待至 所有绘制之后,end of frame,就在展示frame到屏幕之前。可以做截图。可以在任何物体上使用该函数。

例1:屏幕画线

 

  1. using UnityEngine;
  2. using System.Collections;

  3. public class GLTest : MonoBehaviour{

  4.   publicMaterial mat;
  5.    voidOnPostRender() {
  6.        if (!mat) {
  7.           Debug.LogError("PleaseAssign a material on the inspector");
  8.           return;
  9.        }
  10.        GL.PushMatrix();//保存当前Matirx
  11.        mat.SetPass(0);//刷新当前材质
  12.        GL.LoadPixelMatrix();//设置pixelMatrix
  13.        GL.Color(Color.yellow);
  14.        GL.Begin(GL.LINES);
  15.        GL.Vertex3(0,0, 0);
  16.        GL.Vertex3(Screen.width,Screen.height, 0);
  17.        GL.End();
  18.        GL.PopMatrix();//读取之前的Matrix
  19.     }
  20. }


例2:截图

 

  1. using System.IO;
  2. using UnityEngine;
  3. using System.Collections;

  4. public class ScreenShot : MonoBehaviour{
  5.    void Start(){
  6.        StartCoroutine(UploadPNG());
  7.     }
  8.    IEnumerator UploadPNG() {
  9.        yield return newWaitForEndOfFrame();
  10. print ("yuuuuu");
  11.        int width =Screen.width;
  12.        int height =Screen.height;
  13.        Texture2D tex =new Texture2D(width, height,TextureFormat.RGB24, false);
  14.        tex.ReadPixels(newRect(0, 0, width, height),0, 0);
  15.        tex.Apply();
  16.        byte[] bytes =tex.EncodeToPNG();
  17. File.WriteAllBytes(Application.dataPath+"/ss.png",bytes);
  18. UnityEditor.AssetDatabase.Refresh();
  19.     }
  20. }
例3:展示Alpha

 

  1. using UnityEngine;
  2. using System.Collections;

  3. public class GLTest : MonoBehaviour{
  4. public Shader shader;
  5. public Texture2D t2d;
  6.   privateMaterial mat;
  7. void Start()
  8. {
  9. mat = new Material(shader);
  10. mat.mainTexture = t2d;
  11. }
  12.    voidOnPostRender() {
  13.        if (!mat) {
  14.           Debug.LogError("PleaseAssign a material on the inspector");
  15.           return;
  16.        }
  17.        GL.PushMatrix();
  18.        mat.SetPass(0);
  19.        GL.LoadOrtho();
  20.        GL.Begin(GL.QUADS);
  21.        GL.Vertex3(0,0, 0.1F);
  22.        GL.Vertex3(1f,0, 0.1F);
  23.        GL.Vertex3(1f,1, 0.1F);
  24.        GL.Vertex3(0,1, 0.1F);
  25.        GL.End();
  26.        GL.PopMatrix();
  27.     }
  28. }

 

 

 



 

  1. Shader "Custom/GLDrawLine" {
  2. Properties {
  3. _MainTex ("Base (RGB)", 2D) ="white" {}
  4. }
  5. SubShader {
  6.    Pass {
  7. Cull off
  8. Blend DstAlpha zero
  9. Color(1,1,1,1)
  10.     }
  11. }
  12. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值