Unity 简单的屏幕特效样例
2016-02-25 本文已影响1474人
2b75747cf703
脚本挂在相机组件上即可。
Paste_Image.png Paste_Image.png Paste_Image.pngusing UnityEngine;
[ExecuteInEditMode]
public class ImageEffectExample : MonoBehaviour
{
public Shader shader;
private Material _material;
private Material material
{
get
{
if (shader == null)
return null;
if (_material == null)
{
_material = new Material(shader);
_material.hideFlags = HideFlags.HideAndDontSave;
}
return _material;
}
}
void Start()
{
if (!SystemInfo.supportsImageEffects)
{
enabled = false;
return;
}
if (shader == null || !shader.isSupported)
{
enabled = false;
}
}
void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture)
{
if (material != null)
Graphics.Blit(sourceTexture, destTexture, material);
else
Graphics.Blit(sourceTexture, destTexture);
}
void OnDisable()
{
if (_material)
{
DestroyImmediate(_material);
}
}
}
Shader "Hidden/ImageEffectShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex :
POSITION;
float2 uv :
TEXCOORD0;
};
struct v2f
{
float2 uv :
TEXCOORD0;
float4 vertex :
SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
col.rgb = (col.r + col.g + col.b) / 3;
return col;
}
ENDCG
}
}
}