返回介绍

GL 图形库

发布于 2019-12-18 15:37:43 字数 12770 浏览 1292 评论 0 收藏 0

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 静态变量

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

Static Functions 静态函数

BeginBegin drawing 3D primitives.
开始绘制3d图元。
ClearClear the current render buffer.
清除当前的渲染缓存。
ClearWithSkyboxClear the current render buffer with camera's skybox.
用当前带有相机的天空盒清除渲染缓存。
ColorSets current vertex color.
设置当前顶点颜色。
EndEnd drawing 3D primitives.
结束绘制3D图元。
GetGPUProjectionMatrixCompute GPU projection matrix from camera's projection matrix.
从相机的GPU投影矩阵计算GPU投影矩阵。
InvalidateStateInvalidate the internally cached renderstates.
使内部渲染绘制状态无效。用来刷新内部绘制状态的统一缓存。这主要是编写本地代码时非常有用的插件,访问三维器件。
IssuePluginEventSend a user-defined event to a native code plugin.
发送一个用户定义的事件到一个本地代码插件。
LoadIdentityLoad the identity matrix to the current modelview matrix.
加载该恒等矩阵到道歉的模型视图矩阵。
LoadOrthoHelper function to set up an ortho perspective transform.
辅助函数用来做一个正交投影变换。
LoadPixelMatrixSetup a matrix for pixel-correct rendering.
设置一个矩阵用于像素矫正渲染。
LoadProjectionMatrixLoad an arbitrary matrix to the current projection matrix.
加载一个任意的矩阵到当前的投影矩阵。
MultiTexCoordSets current texture coordinate (v.x,v.y,v.z) to the actual texture unit.
设置纹理(x,y,z)坐标对于当前的纹理单元(多重纹理)。
MultiTexCoord2Sets current texture coordinate (x,y) for the actual texture unit.
设置纹理(x,y,)坐标对于当前的纹理单元。(多重纹理)
MultiTexCoord3Sets current texture coordinate (x,y,z) to the actual texture unit.
设置纹理(x,y,z)坐标对于当前的纹理单元(多重纹理)。
MultMatrixMultiplies the current modelview matrix with the one specified.
用当前的模型视图矩阵乘以指定的矩阵。
PopMatrixRestores both projection and modelview matrices off the top of the matrix stack.
把投影视图矩阵和模型视图矩阵从矩阵堆栈顶部恢复。
PushMatrixSaves both projection and modelview matrices to the matrix stack.
把投影视图矩阵和模型视图矩阵压入堆栈保存。
TexCoordSets current texture coordinate (v.x,v.y,v.z) for all texture units.
设置当前纹理(v.x,v.y,v.z)坐标用于所有的纹理单元。
TexCoord2Sets current texture coordinate (x,y) for all texture units.
设置纹理(x,y)坐标对于所有的纹理单元。
TexCoord3Sets current texture coordinate (x,y,z) for all texture units.
设置纹理(x,y,z)坐标对于所有的纹理单元。
VertexSubmit a vertex.
绘制一个顶点。
Vertex3Submit a vertex.
绘制一个顶点。
ViewportSet the rendering viewport.
定义渲染的视口。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文