- Shader:
Shader "_MyShader/9_PostScreenEffect/0_BriSatCon"
{
Properties
{
_MainTex ("MainTex", 2D) = "white" {}
//_Brightness ("Brightness", float) = 1
//_Saturation ("Saturation", float) = 1
//_Contrast ("Contrast", float) = 1
//_RadiusSquared ("RadiusSquared", Range(0,1)) = 1
}
SubShader
{
Pass
{
ZTest Always
ZWrite off
Cull off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float y : TEXCOORD1;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Brightness;
float _Saturation;
float _Contrast;
float _RadiusSquared;
v2f vert (appdata_img v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.y = v.vertex.y;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
fixed4 col2 = col;
//float leng = (i.uv.x - 0.5) * (i.uv.x - 0.5) + (i.uv.y - 0.5) * (i.uv.y - 0.5);
float leng = (i.uv.x - 0.5) * (i.uv.x - 0.5) + (i.uv.y / 2 - 0.5) * (i.uv.y / 2 - 0.5) + i.uv.y / 4;
clip(_RadiusSquared - leng);
col.rgb *= _Brightness;
fixed luminance = 0.2125 * col.r + 0.7154 * col.g + 0.0721 * col.b;
fixed3 luminanceColor = fixed3(luminance,luminance,luminance);
col.rgb = lerp(luminanceColor,col,_Saturation);
fixed3 avgColor = fixed3(0.5,0.5,0.5);
col.rgb = lerp(avgColor,col,_Contrast);
return col;
}
ENDCG
}
}
FallBack off
}
- C#
基类:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class ScreenEffectBase : MonoBehaviour {
protected void Start(){
CheckResources ();
}
protected void CheckResources(){
bool isSupport = CheckSupport ();
if (!isSupport)
this.enabled = false;
}
protected bool CheckSupport(){
if (!SystemInfo.supportsImageEffects || !SystemInfo.supportsRenderTextures)
return false;
return true;
}
protected Material CheckShaderAndCreateMaterial(Shader shader, Material mat){
if (shader == null || !shader.isSupported)
return null;
if (mat && mat.shader == shader)
return mat;
mat = new Material (shader);
mat.hideFlags = HideFlags.DontSave;
if (mat)
return mat;
else
return null;
}
}
BrightnessSaturationContrast:
using UnityEngine;
using System.Collections;
public class BrightnessSaturationContrast : ScreenEffectBase {
[Range(0,3.0f)]
public float brightness = 1;
[Range(0,3.0f)]
public float saturation = 1;
[Range(0,3.0f)]
public float contrast = 1;
[Range(0,1.0f)]
public float radiusSquared = 1;
public Shader briSatConShader;
private Material briSatConMat;
public Material material {
get {
briSatConMat = CheckShaderAndCreateMaterial (briSatConShader, briSatConMat);
return briSatConMat;
}
}
void OnRenderImage(RenderTexture src, RenderTexture dest){
if (material != null) {
material.SetFloat ("_Brightness", brightness);
material.SetFloat ("_Saturation", saturation);
material.SetFloat ("_Contrast", contrast);
material.SetFloat ("_RadiusSquared", radiusSquared);
Graphics.Blit (src, dest, material);
} else {
Graphics.Blit (src, dest);
}
}
}