GL绘制

http://wiki.ceeger.com/script/unityengine/classes/gl/gl.multitexcoord?s[]=gl

GL 图形库

Namespace: UnityEngine

Description 描述

Low-level graphics library.

Use this class to manipulate active transformation matrices, issue rendering commands similar to OpenGL's immediate mode and do other low-level graphics tasks. Note that in almost all cases using Graphics.DrawMesh is more efficient than using immediate mode drawing.

这个类进行底层的矩阵变换。通过用函数命令来实现。这和OpenGL(和D3D不同的图形库)的立即模式一样。注意在大部分情况下用Graphics.DrawMesh 比这个立即模式更有效率。

GL immediate drawing functions use whatever is the “current material” set up right now. The material controls how the rendering is done (blending, textures, etc.), so unless you explicitly set it to something before using GL draw functions, the material can happen to be anything. Also, if you call any other drawing commands from inside GL drawing code, they can set material to something else, so make sure it's under control as well.

GL的立即模式用在当材质建立的时候。材质控制着物体该如何渲染(混合纹理等等)。在用GL类前你要明确的设定材质的属性,GL类可以使材质做任何改变。 (下面的实例代码看看会清楚一点)

GL drawing commands execute immediately. That means if you call them in Update(), they will be executed before the camera is rendered (and the camera will most likely clear the screen, making the GL drawing not visible).

GL函数命令会立即执行。不要在Update()函数内放置GL代码,这样会看不到效果。因为GL在 Camera 之前被执行。

The usual place to call GL drawing is most often in OnPostRender() from a script attached to a camera

常用的方法是GL代码放在OnPostRender()函数里面。OnPostRender()作为camera 脚本里的一个函数。

JavaScript:

#pragma strict
public var lineCount: int = 100;
public var radius: float = 3.0f;
static var lineMaterial: Material;
static function CreateLineMaterial() {
	if (!lineMaterial) {
		// simple colored things.
		var shader: var = Shader.Find("Hidden/Internal-Colored");
		lineMaterial = new Material(shader);
		lineMaterial.hideFlags = HideFlags.HideAndDontSave;
		// Turn on alpha blending
		lineMaterial.SetInt("_SrcBlend", intUnityEngine.Rendering.BlendMode.SrcAlpha);
		lineMaterial.SetInt("_DstBlend", intUnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
		// Turn backface culling off
		lineMaterial.SetInt("_Cull", intUnityEngine.Rendering.CullMode.Off);
		// Turn off depth writes
		lineMaterial.SetInt("_ZWrite", 0);
	}
}
// Will be called after all regular rendering is done
public function OnRenderObject() {
	CreateLineMaterial();
	// Apply the line material
	lineMaterial.SetPass(0);
	GL.PushMatrix();
	// match our transform
	GL.MultMatrix(transform.localToWorldMatrix);
	// Draw lines
	GL.Begin(GL.LINES);
	for (var i: int = 0; i < lineCount; ++i) {
		var a: float = i / floatlineCount;
		var angle: float = a * Mathf.PI * 2;
		// Vertex colors change from red to green
		GL.Color(new Color(a, 1 - a, 0, 0.8F));
		// One vertex at transform position
		GL.Vertex3(0, 0, 0);
		// Another vertex at edge of circle
		GL.Vertex3(Mathf.Cos(angle) * radius, Mathf.Sin(angle) * radius, 0);
	}
	GL.End();
	GL.PopMatrix();
}

C#:

using UnityEngine;
 
public class ExampleClass : MonoBehaviour
{
	// When added to an object, draws colored rays from the
	// transform position.
	public int lineCount = 100;
	public float radius = 3.0f;
 
	static Material lineMaterial;
	static void CreateLineMaterial ()
	{
		if (!lineMaterial)
		{
			// Unity has a built-in shader that is useful for drawing
			// simple colored things.
			var shader = Shader.Find ("Hidden/Internal-Colored");
			lineMaterial = new Material (shader);
			lineMaterial.hideFlags = HideFlags.HideAndDontSave;
			// Turn on alpha blending
			lineMaterial.SetInt ("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
			lineMaterial.SetInt ("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
			// Turn backface culling off
			lineMaterial.SetInt ("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
			// Turn off depth writes
			lineMaterial.SetInt ("_ZWrite", 0);
		}
	}
 
	// Will be called after all regular rendering is done
	public void OnRenderObject ()
	{
		CreateLineMaterial ();
		// Apply the line material
		lineMaterial.SetPass (0);
 
		GL.PushMatrix ();
		// Set transformation matrix for drawing to
		// match our transform
		GL.MultMatrix (transform.localToWorldMatrix);
 
		// Draw lines
		GL.Begin (GL.LINES);
		for (int i = 0; i < lineCount; ++i)
		{
			float a = i / (float)lineCount;
			float angle = a * Mathf.PI * 2;
			// Vertex colors change from red to green
			GL.Color (new Color (a, 1-a, 0, 0.8F));
			// One vertex at transform position
			GL.Vertex3 (0, 0, 0);
			// Another vertex at edge of circle
			GL.Vertex3 (Mathf.Cos (angle) * radius, Mathf.Sin (angle) * radius, 0);
		}
		GL.End ();
		GL.PopMatrix ();
	}
}

Note: This class is almost always used when you need to draw a couple of lines or triangles, and don't want to deal with meshes. If you want to avoid surprises the usage pattern is this:

注意:这个类使用于当你需要绘制几条线或三角形,并不想使用网格。如果你想避免意外使用模式是这样的:

JavaScript:

#pragma strict
function OnPostRender() {
	// Set your materials
	GL.PushMatrix();
	// Draw your stuff
	GL.PopMatrix();
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    void OnPostRender() {
        // Set your materials
        GL.PushMatrix();
        // yourMaterial.SetPass( );
        // Draw your stuff
        GL.PopMatrix();
    }
}

Where at the “// Draw your stuff” you should do SetPass() on some material previously declared, which will be used for drawing. If you dont call SetPass, then you'll get basically a random material (whatever was used before) which is not good. So do it.

在// Draw your stuff注释位置,你可以做 SetPass(),之前声明的一些材质,将永远绘制。如果你不调用SetPass,那么你将获得基本上是一个随机材质(不管是使用之前)这是不好的,因此要做这步。

Static Variables 静态变量

invertCulling Select whether to invert the backface culling (true) or not (false).
是否反转背面剔除(true或false)。
LINES Mode for Begin: draw lines.
从Begin模式开始后,然后绘制线。
modelview The current modelview matrix.
返回目前的模型视图矩阵。
QUADS Mode for Begin: draw quads.
从Begin模式开始后,然后绘制四边形。
TRIANGLE_STRIP Mode for Begin: draw triangle strip.
从Begin模式开始后,然后绘制三角形带。
TRIANGLES Mode for Begin: draw triangles.
从Begin模式开始后,然后绘制三角形。
wireframe Should rendering be done in wireframe?
是否渲染线框?

Static Functions 静态函数

Begin Begin drawing 3D primitives.
开始绘制3d图元。
Clear Clear the current render buffer.
清除当前的渲染缓存。
ClearWithSkybox Clear the current render buffer with camera's skybox.
用当前带有相机的天空盒清除渲染缓存。
Color Sets current vertex color.
设置当前顶点颜色。
End End drawing 3D primitives.
结束绘制3D图元。
GetGPUProjectionMatrix Compute GPU projection matrix from camera's projection matrix.
从相机的GPU投影矩阵计算GPU投影矩阵。
InvalidateState Invalidate the internally cached renderstates.
使内部渲染绘制状态无效。用来刷新内部绘制状态的统一缓存。这主要是编写本地代码时非常有用的插件,访问三维器件。
IssuePluginEvent Send a user-defined event to a native code plugin.
发送一个用户定义的事件到一个本地代码插件。
LoadIdentity Load the identity matrix to the current modelview matrix.
加载该恒等矩阵到道歉的模型视图矩阵。
LoadOrtho Helper function to set up an ortho perspective transform.
辅助函数用来做一个正交投影变换。
LoadPixelMatrix Setup a matrix for pixel-correct rendering.
设置一个矩阵用于像素矫正渲染。
LoadProjectionMatrix Load an arbitrary matrix to the current projection matrix.
加载一个任意的矩阵到当前的投影矩阵。
MultiTexCoord Sets current texture coordinate (v.x,v.y,v.z) to the actual texture unit.
设置纹理(x,y,z)坐标对于当前的纹理单元(多重纹理)。
MultiTexCoord2 Sets current texture coordinate (x,y) for the actual texture unit.
设置纹理(x,y,)坐标对于当前的纹理单元。(多重纹理)
MultiTexCoord3 Sets current texture coordinate (x,y,z) to the actual texture unit.
设置纹理(x,y,z)坐标对于当前的纹理单元(多重纹理)。
MultMatrix Multiplies the current modelview matrix with the one specified.
用当前的模型视图矩阵乘以指定的矩阵。
PopMatrix Restores both projection and modelview matrices off the top of the matrix stack.
把投影视图矩阵和模型视图矩阵从矩阵堆栈顶部恢复。
PushMatrix Saves both projection and modelview matrices to the matrix stack.
把投影视图矩阵和模型视图矩阵压入堆栈保存。
TexCoord Sets current texture coordinate (v.x,v.y,v.z) for all texture units.
设置当前纹理(v.x,v.y,v.z)坐标用于所有的纹理单元。
TexCoord2 Sets current texture coordinate (x,y) for all texture units.
设置纹理(x,y)坐标对于所有的纹理单元。
TexCoord3 Sets current texture coordinate (x,y,z) for all texture units.
设置纹理(x,y,z)坐标对于所有的纹理单元。
Vertex Submit a vertex.
绘制一个顶点。
Vertex3 Submit a vertex.
绘制一个顶点。
Viewport Set the rendering viewport.
定义渲染的视口。
L图像库主要用来绘制常见的2d和3d几何图形。使用GL图像库,可在屏幕中绘制2d几何图形,并且该几何图形将永远显示在屏幕当中,不会因为摄像机的移动而改变。

值得注意的是,绘制2d图像时,需要使用GL.LoadOrtho()方法来将图形映射在平面中;如果绘制的是3d图形,就无须使用此方法。

使用GL图像库时,需要将所有绘制相关的内容写在OnPostRender()方法中。有关GL图像库的脚本需要绑定在摄像机中。

GL图像库的平面坐标系:原点在左下角,x轴与y轴的最大值为1。


一、绘制线

[csharp] view plain copy
print ?
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class DrawLine : MonoBehaviour {  
  5.   
  6.     public Material material;  
  7.   
  8.     void OnPostRender()  
  9.     {  
  10.         material.SetPass(0);//设置该材质通道,0为默认值  
  11.         GL.LoadOrtho();//设置绘制2d图像  
  12.         GL.Begin(GL.LINES);//绘制类型为线段  
  13.         Draw(0, 0, 200, 100);  
  14.         Draw(0, 50, 200, 150);  
  15.         Draw(0, 100, 200, 200);  
  16.         GL.End();  
  17.     }  
  18.     
  19.     //将屏幕中某个点的像素坐标进行转换  
  20.     void Draw(float x1,float y1,float x2,float y2)  
  21.     {  
  22.         GL.Vertex(new Vector3(x1 / Screen.width, y1 / Screen.height, 0));  
  23.         GL.Vertex(new Vector3(x2 / Screen.width, y2 / Screen.height, 0));  
  24.     }  
  25. }  
using UnityEngine;
using System.Collections;

public class DrawLine : MonoBehaviour {

    public Material material;

    void OnPostRender()
    {
        material.SetPass(0);//设置该材质通道,0为默认值
        GL.LoadOrtho();//设置绘制2d图像
        GL.Begin(GL.LINES);//绘制类型为线段
        Draw(0, 0, 200, 100);
        Draw(0, 50, 200, 150);
        Draw(0, 100, 200, 200);
        GL.End();
    }
  
    //将屏幕中某个点的像素坐标进行转换
    void Draw(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));
    }
}



二、绘制曲线

[csharp] view plain copy
print ?
  1. using UnityEngine;  
  2. using System.Collections.Generic;  
  3.   
  4. /// <summary>  
  5. /// 记录鼠标坐标,再两两连线  
  6. /// </summary>  
  7. public class DrawCurve : MonoBehaviour {  
  8.   
  9.     public Material material;  
  10.     private List<Vector3> lineInfo = new List<Vector3>();  
  11.   
  12.     void Update ()   
  13.     {  
  14.         lineInfo.Add(Input.mousePosition);  
  15.     }  
  16.   
  17.     void OnPostRender()  
  18.     {  
  19.         material.SetPass(0);//设置该材质通道,0为默认值  
  20.         GL.LoadOrtho();//设置绘制2d图像  
  21.         GL.Begin(GL.LINES);//绘制类型为线段  
  22.   
  23.         for (int i = 0; i < lineInfo.Count - 1; i++)  
  24.         {  
  25.             Vector3 start = lineInfo[i];  
  26.             Vector3 end = lineInfo[i + 1];  
  27.             Draw(start.x,start.y,end.x,end.y);  
  28.         }  
  29.           
  30.         GL.End();  
  31.     }  
  32.   
  33.     //将屏幕中某个点的像素坐标进行转换  
  34.     void Draw(float x1, float y1, float x2, float y2)  
  35.     {  
  36.         GL.Vertex(new Vector3(x1 / Screen.width, y1 / Screen.height, 0));  
  37.         GL.Vertex(new Vector3(x2 / Screen.width, y2 / Screen.height, 0));  
  38.     }  
  39. }  
using UnityEngine;
using System.Collections.Generic;

/// <summary>
/// 记录鼠标坐标,再两两连线
/// </summary>
public class DrawCurve : MonoBehaviour {

    public Material material;
    private List<Vector3> lineInfo = new List<Vector3>();

	void Update () 
	{
        lineInfo.Add(Input.mousePosition);
	}

    void OnPostRender()
    {
        material.SetPass(0);//设置该材质通道,0为默认值
        GL.LoadOrtho();//设置绘制2d图像
        GL.Begin(GL.LINES);//绘制类型为线段

        for (int i = 0; i < lineInfo.Count - 1; i++)
        {
            Vector3 start = lineInfo[i];
            Vector3 end = lineInfo[i + 1];
            Draw(start.x,start.y,end.x,end.y);
        }
        
        GL.End();
    }

    //将屏幕中某个点的像素坐标进行转换
    void Draw(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));
    }
}



三、绘制四边形

[csharp] view plain copy
print ?
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class DrawQuad : MonoBehaviour {  
  5.   
  6.     public Material material;  
  7.   
  8.     void OnPostRender()  
  9.     {  
  10.         //绘制正四边形,提供的坐标必须是顺时针或者逆时针  
  11.         Draw(100, 100, 100, 200, 200, 200, 200, 100);  
  12.         //绘制无规则四边形  
  13.         Draw(15, 5, 10, 115, 95, 110, 90, 10);  
  14.     }  
  15.   
  16.     //绘制四边形,四个点坐标  
  17.     void Draw(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)  
  18.     {  
  19.         GL.PushMatrix();  
  20.         material.SetPass(0);//设置该材质通道,0为默认值  
  21.         GL.LoadOrtho();//设置绘制2d图像  
  22.         GL.Begin(GL.QUADS);//绘制类型为四边形  
  23.   
  24.         GL.Vertex3(x1 / Screen.width, y1 / Screen.height, 0);  
  25.         GL.Vertex3(x2 / Screen.width, y2 / Screen.height, 0);  
  26.         GL.Vertex3(x3 / Screen.width, y3 / Screen.height, 0);  
  27.         GL.Vertex3(x4 / Screen.width, y4 / Screen.height, 0);  
  28.   
  29.         GL.End();  
  30.         GL.PopMatrix();  
  31.     }  
  32. }  
using UnityEngine;
using System.Collections;

public class DrawQuad : MonoBehaviour {

    public Material material;

    void OnPostRender()
    {
        //绘制正四边形,提供的坐标必须是顺时针或者逆时针
        Draw(100, 100, 100, 200, 200, 200, 200, 100);
        //绘制无规则四边形
        Draw(15, 5, 10, 115, 95, 110, 90, 10);
    }

    //绘制四边形,四个点坐标
    void Draw(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)
    {
        GL.PushMatrix();
        material.SetPass(0);//设置该材质通道,0为默认值
        GL.LoadOrtho();//设置绘制2d图像
        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();
    }
}



四、绘制三角形

[csharp] view plain copy
print ?
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class DrawTriangle : MonoBehaviour {  
  5.   
  6.     public Material material;  
  7.   
  8.     void OnPostRender()  
  9.     {  
  10.         Draw(100,0,100,200,200,100);  
  11.     }  
  12.   
  13.     void Draw(float x1, float y1, float x2, float y2, float x3, float y3)  
  14.     {  
  15.         material.SetPass(0);//设置该材质通道,0为默认值  
  16.         GL.LoadOrtho();//设置绘制2d图像  
  17.         GL.Begin(GL.TRIANGLES);//绘制类型为三角形  
  18.   
  19.         GL.Vertex3(x1 / Screen.width, y1 / Screen.height, 0);  
  20.         GL.Vertex3(x2 / Screen.width, y2 / Screen.height, 0);  
  21.         GL.Vertex3(x3 / Screen.width, y3 / Screen.height, 0);  
  22.   
  23.         GL.End();  
  24.     }  
  25. }  
using UnityEngine;
using System.Collections;

public class DrawTriangle : MonoBehaviour {

    public Material material;

    void OnPostRender()
    {
        Draw(100,0,100,200,200,100);
    }

    void Draw(float x1, float y1, float x2, float y2, float x3, float y3)
    {
        material.SetPass(0);//设置该材质通道,0为默认值
        GL.LoadOrtho();//设置绘制2d图像
        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几何图形

操作十分简单,就是将GL.LoadOrtho();注释掉即可

原来图形不会随摄像机的移动、旋转而变化,现在则会。

GL即Graphics Library。Low-Level Graphics Library。计算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指令,否则显示会变成如下。(如何???)

3、如果是线,颜色是GL.Color( new Color(1,1,1,0.5f)设置颜色。如果是GL.TRIANGLES或者GL.QUADS,则颜色是shader里的颜色。


方法说明:

1、GL.PushMatrix():保存Matrices至matrix stack上

GL.PopMatrix():从matrix stack上读取matrices。


2、GL.LoadPixeMatrix():改变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.LoadPixeMatrix时,Vertex3的xy值不需要除上Screen.width和Screen.height


3、GL.LoadOrtho():设置ortho perspective,即水平视角。GL.Vertex3()取值范围从左下角的(0,0,0)到右上角的(1,1,0)即需要除上Screen.width和Screen.height。


4、OnPostRender():只有物体上有激活的相机时,才会调用该函数。当摄像机完成渲染场景后,绘制了所有物体再调用该方法。OnPostRender可以使用coroutine,加yield使用

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


例子1:屏幕划线

[csharp] view plain copy
  1. using UnityEngine;    
  2. using System.Collections;    
  3. using System.Collections.Generic;  
  4.   
  5. public class Line : MonoBehaviour    
  6. {     
  7.     public float z;  
  8.     private Material mat;   
  9.     private Vector3 vBeg;  
  10.     private Vector3 vEnd;  
  11.     private static IList<Vector3> _posList = new List<Vector3>();  
  12.   
  13.     private static bool _isReady = false;  
  14.     public static bool isReady  
  15.     {  
  16.         set{ _isReady = value; }  
  17.     }  
  18.   
  19.     void Start()    
  20.     {    
  21.         mat = new Material("Shader \"Lines/Colored Blended\" {" +    
  22.                                "SubShader { Pass { " +    
  23.                                "    Blend SrcAlpha OneMinusSrcAlpha " +    
  24.                                "    ZWrite Off Cull Off Fog { Mode Off } " +    
  25.                                "    BindChannels {" +    
  26.                                "      Bind \"vertex\", vertex Bind \"color\", color }" +    
  27.                                "} } }");//生成画线的材质    
  28.         mat.hideFlags = HideFlags.HideAndDontSave;    
  29.         mat.color = Color.green;   
  30.         mat.shader.hideFlags = HideFlags.HideAndDontSave;    
  31.     }      
  32.     public static void setPosList( ref IList<Vector3> posList )  
  33.     {  
  34.         if( _isReady )   
  35.         {  
  36.             return;  
  37.         }  
  38.         _posList = posList;  
  39.         if( _posList.Count > 1 )   
  40.         {  
  41.             _isReady = true;  
  42.         }  
  43.     }  
  44.     void OnPostRender()    
  45.     {    
  46.         if( _isReady )    
  47.         {    
  48.             vBeg = camera.WorldToScreenPoint( panelFullMap.instance.mainPlayer.position );  
  49.             vEnd = camera.WorldToScreenPoint( _posList[0] );  
  50.             GL.PushMatrix(); //保存当前Matirx  
  51.             mat.SetPass(0); //刷新当前材质  
  52.             GL.LoadOrtho();//设置pixelMatrix  
  53.             GL.Color(Color.yellow);  
  54.             GL.Begin(GL.LINES);  
  55.             GL.Vertex3( vBeg.x/Screen.width, vBeg.y/Screen.height, z );    
  56.             GL.Vertex3( vEnd.x/Screen.width, vEnd.y/Screen.height, z );   
  57.             GL.End();  
  58.             GL.PopMatrix();//读取之前的Matrix  
  59.   
  60.             forint i = 0; i < _posList.Count - 1; ++i )  
  61.             {  
  62.                 vBeg = camera.WorldToScreenPoint( _posList[i] );  
  63.                 vEnd = camera.WorldToScreenPoint( _posList[i+1] );  
  64.                 GL.PushMatrix();    
  65.                 mat.SetPass(0);    
  66.                 GL.LoadOrtho();    
  67. //              GL.LoadPixelMatrix();  
  68.                 GL.Begin(GL.LINES);    
  69.                 GL.Color(Color.green);    
  70.   
  71.                 GL.Vertex3( vBeg.x/Screen.width, vBeg.y/Screen.height, z );    
  72.                 GL.Vertex3( vEnd.x/Screen.width, vEnd.y/Screen.height, z );   
  73. //              GL.Vertex3( vBeg.x, vBeg.y, 0 );    
  74. //              GL.Vertex3( vEnd.x, vEnd.y, 0 );    
  75.   
  76.                 GL.End();    
  77.                 GL.PopMatrix();    
  78.             }  
  79.         }    
  80.     }  
  81. }   

例子2:截图

[csharp] view plain copy
  1. using System.IO;  
  2. using UnityEngine;  
  3. using System.Collections;  
  4.   
  5. public class ScreenShot : MonoBehaviour   
  6. {  
  7.     void Start()   
  8.     {  
  9.         StartCoroutine(UploadPNG() );  
  10.     }  
  11.     IEnumerator UploadPNG()   
  12.     {  
  13.         yield return new WaitForEndOfFrame();  
  14.         print ("yuuuuu");  
  15.         int width = Screen.width;  
  16.         int height = Screen.height;  
  17.         Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);  
  18.         tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);  
  19.         tex.Apply();  
  20.         byte[] bytes = tex.EncodeToPNG();  
  21.         File.WriteAllBytes(Application.dataPath+"/ss.png",bytes);  
  22.         UnityEditor.AssetDatabase.Refresh();  
  23.     }  
  24. }  

例子3:展示Alpha

[csharp] view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class GLTest : MonoBehaviour   
  5. {  
  6.     public Shader shader;  
  7.     public Texture2D t2d;  
  8.     private Material mat;  
  9.     void Start()  
  10.     {  
  11.         mat = new Material(shader);  
  12.         mat.mainTexture = t2d;  
  13.     }  
  14.     void OnPostRender()   
  15.     {  
  16.         if (!mat)   
  17.         {  
  18.             Debug.LogError("Please Assign a material on the inspector");  
  19.             return;  
  20.         }  
  21.         GL.PushMatrix();  
  22.         mat.SetPass(0);  
  23.         GL.LoadOrtho();  
  24.         GL.Begin(GL.QUADS);  
  25.         GL.Vertex3(0, 0, 0.1F);  
  26.         GL.Vertex3(1f, 0, 0.1F);  
  27.         GL.Vertex3(1f, 1, 0.1F);  
  28.         GL.Vertex3(0, 1, 0.1F);  
  29.         GL.End();  
  30.         GL.PopMatrix();  
  31.     }  
  32. }  
  33.   
  34.   
  35. Shader "Custom/GLDrawLine"   
  36. {  
  37.     Properties {  
  38.         _MainTex ("Base (RGB)", 2D) = "white" {}  
  39.     }  
  40.     SubShader   
  41.     {  
  42.         Pass   
  43.         {  
  44.             Cull off  
  45.                 Blend DstAlpha zero  
  46.                     Color(1,1,1,1)  
  47.         }  
  48.     }  
  49. }  

猜你喜欢

转载自blog.csdn.net/july_unity/article/details/80740776