До:
После:
Скрипт:
Синтаксис:
Используется csharp
using UnityEngine;
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
[AddComponentMenu("Image Effects/SaturationPostEffect")]
public class SaturationPostEffect : MonoBehaviour
{
public Shader shaderRGB;
private Material m_MaterialRGB;
public float saturation = 1.0f;
protected void Start()
{
// Disable if we don't support image effects
if (!SystemInfo.supportsImageEffects)
{
enabled = false;
return;
}
if (shaderRGB == null)
{
Debug.Log("Sat shaders are not set up! Disabling saturation effect.");
enabled = false;
}
else
{
if (!shaderRGB.isSupported) // disable effect if RGB shader is not supported
enabled = false;
//else if( !shaderYUV.isSupported ) // fallback to RGB if YUV is not supported
// rgbFallback = true;
}
}
protected Material material
{
get
{
if (m_MaterialRGB == null)
{
m_MaterialRGB = new Material(shaderRGB);
m_MaterialRGB.hideFlags = HideFlags.HideAndDontSave;
}
return m_MaterialRGB;
}
}
protected void OnDisable()
{
if (m_MaterialRGB)
DestroyImmediate(m_MaterialRGB);
}
// Called by the camera to apply the image effect
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Material mat = material;
mat.SetFloat("_Sat", saturation);
Graphics.Blit(source, destination, mat);
}
}
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
[AddComponentMenu("Image Effects/SaturationPostEffect")]
public class SaturationPostEffect : MonoBehaviour
{
public Shader shaderRGB;
private Material m_MaterialRGB;
public float saturation = 1.0f;
protected void Start()
{
// Disable if we don't support image effects
if (!SystemInfo.supportsImageEffects)
{
enabled = false;
return;
}
if (shaderRGB == null)
{
Debug.Log("Sat shaders are not set up! Disabling saturation effect.");
enabled = false;
}
else
{
if (!shaderRGB.isSupported) // disable effect if RGB shader is not supported
enabled = false;
//else if( !shaderYUV.isSupported ) // fallback to RGB if YUV is not supported
// rgbFallback = true;
}
}
protected Material material
{
get
{
if (m_MaterialRGB == null)
{
m_MaterialRGB = new Material(shaderRGB);
m_MaterialRGB.hideFlags = HideFlags.HideAndDontSave;
}
return m_MaterialRGB;
}
}
protected void OnDisable()
{
if (m_MaterialRGB)
DestroyImmediate(m_MaterialRGB);
}
// Called by the camera to apply the image effect
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Material mat = material;
mat.SetFloat("_Sat", saturation);
Graphics.Blit(source, destination, mat);
}
}
Шейдер:
Синтаксис:
Используется glsl
Shader "Hidden/Saturation" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Sat("Sat", Float) = 0
}
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off Fog { Mode off }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct v2f {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
uniform sampler2D _MainTex;
uniform float _Sat;
v2f vert (appdata_img v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = MultiplyUV (UNITY_MATRIX_TEXTURE0, v.texcoord);
return o;
}
half4 frag (v2f i) : COLOR
{
half4 c = tex2D(_MainTex, i.uv);
float Lum = dot(c, float3(0.2126, 0.7152, 0.0722));
half4 color = half4(lerp(Lum.xxx, c, _Sat), 1);
return color;
}
ENDCG
}
}
Fallback off
}
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Sat("Sat", Float) = 0
}
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off Fog { Mode off }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct v2f {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
uniform sampler2D _MainTex;
uniform float _Sat;
v2f vert (appdata_img v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = MultiplyUV (UNITY_MATRIX_TEXTURE0, v.texcoord);
return o;
}
half4 frag (v2f i) : COLOR
{
half4 c = tex2D(_MainTex, i.uv);
float Lum = dot(c, float3(0.2126, 0.7152, 0.0722));
half4 color = half4(lerp(Lum.xxx, c, _Sat), 1);
return color;
}
ENDCG
}
}
Fallback off
}