//一般在渲染函数里实现,比如:OnPostRender()/*相机后处理*/,OnRenderImage()/*图像处理*/
void OnRenderImage(RenderTexture src, RenderTexture dst)
{
Vector3 toRight = cameraTransform.right * halfHeight * aspect;
Vector3 toTop = cameraTransform.up * halfHeight;
Vector3 topLeft = (myCamera.transform.forward - toRight + toTop);
float scale = topLeft.magnitude / near;
Vector3 topRight = cameraTransform.forward * near + toRight + toTop;
topRight.Normalize();
topRight *= scale;
Vector3 bottomLeft = cameraTransform.forward * near - toTop - toRight;
bottomLeft.Normalize();
bottomLeft *= scale;
Vector3 bottomRight = cameraTransform.forward * near + toRight - toTop;
bottomRight.Normalize();
bottomRight *= scale;
// Custom Blit, encoding Frustum Corners as additional Texture Coordinates
RenderTexture.active = dst;
mat.SetTexture("_MainTex", src);
GL.PushMatrix();
GL.LoadOrtho();
//必须设置material pass通道,不然无法生效
mat.SetPass(0);
GL.Begin(GL.QUADS);
GL.MultiTexCoord2(0, 0.0f, 0.0f);
GL.MultiTexCoord(1, bottomLeft);
GL.Vertex3(0.0f, 0.0f, 0.0f);
GL.MultiTexCoord2(0, 1.0f, 0.0f);
GL.MultiTexCoord(1, bottomRight);
GL.Vertex3(1.0f, 0.0f, 0.0f);
GL.MultiTexCoord2(0, 1.0f, 1.0f);
GL.MultiTexCoord(1, topRight);
GL.Vertex3(1.0f, 1.0f, 0.0f);
GL.MultiTexCoord2(0, 0.0f, 1.0f);
GL.MultiTexCoord(1, topLeft);
GL.Vertex3(0.0f, 1.0f, 0.0f);
GL.End();
GL.PopMatrix();
}
官方脚本UnityEngine.GL - Unity 脚本 API
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.
Shader 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();
}
}
调用示例:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
void OnPostRender()
{
// Set your materials
GL.PushMatrix();
// yourMaterial.SetPass( );
// Draw your stuff
GL.PopMatrix();
}
}
简而言之使用方法就是:
挂一个脚本在camera上, 重写OnRenderImg()/OnPostRender(),设置material 绑定 shader, 实现屏幕后效果