SHADER 灰阶公式快捷使用

Basic

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

Shader "Unlit/NewUnlitShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_DesatValue("DesatValue",Range(0,1)) = 0.5
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100

CGPROGRAM
#pragma surface surf Lambert

sampler2D _MainTex;
fixed _DesatValue;

struct Input
{
float2 uv_MainTex;
};

void surf(Input IN,inout SurfaceOutput o)
{
float4 c = tex2D(_MainTex,IN.uv_MainTex);
c.rgb = lerp(c.rgb,Luminance(c.rgb),_DesatValue);
o.Albedo = c.rgb;
o.Alpha = c.a;
}


ENDCG
}
}

参数详情

1.Luminance
简单的说Luminance就是实现灰阶公式的函数。位于UnityCG.glslinc

具体实现如下

1
2
3
4
5
 // Converts color to luminance (grayscale)
float Luminance( vec3 c )
{
return dot( c, vec3(0.22, 0.707, 0.071) );
}

WARNING

1.能用函数尽量用函数,自己也可以将自定义的函数封装到.cginc文件中。