Unity中新建一个样式如下
Shader "Custom/NewShader 1" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
修改上述代码去掉 o.Albedo = c.rgb;增加新的一行代码,标示输出的效果不受光照影响
Shader "Custom/NewShader 1" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Emission = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
颜色的加法:
RGB的加法,表示颜色的叠加,颜色越加越亮。
(1,0,0) + (0,1,0)=(1,1,0) 红加绿等于黄
(0.5,0,0)+(0.7,0,0)=(1.2,0,0)=(1,0,0);两个暗红相加就是更亮的红色,超出1.0的就作为1.0,到顶了再加还是红色
对图片来说,加法会不规则的改变原来的颜色,有不协调感,像是加了一层上去。
一般只有光效才会采用加法混合,就是为了不协调。
我们先来做一个颜色叠加
首先在属性中增加一个添加的颜色属性,可以在Inspector窗口中修改,然后在CG中添加这个变量,最后修改输出的颜色
Shader "Custom/NewShader 1" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_AddColor("Add Color",Color) = (0,0,0,0)
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
float4 _AddColor;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Emission = c.rgb + _AddColor.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
下一步增加一张图片,将两张图片加在一起,需要增加一张贴图,在surf函数中增加新家图片的的颜色采样。
Shader "Custom/NewShader 1" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_AddTex("Add (RGB)",2D) = "white" {}
_AddColor("Add Color",Color) = (0,0,0,0)
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
sampler2D _AddTex;
float4 _AddColor;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
half4 c1 = tex2D(_AddTex,IN.uv_MainTex);
o.Emission = c.rgb + c1.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
乘法
RGB的乘法,表示对亮度的改变,对三个分量乘以相同的值,则三个分量一起变化亮度,色调不变。
对三个分量乘以不同的值,则三个分量分别变化,色调会改变。
对图片来说,图片乘以图片没有什么意义,一般采用图片乘以一个颜色值,可以表示对整个图片的亮度进行修改。
修改之前的代码,将添加的颜色变量由“+”改为“*”。
Shader "Custom/NewShader 1" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_AddTex("Add (RGB)",2D) = "white" {}
_AddColor("Add Color",Color) = (0,0,0,0)
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
sampler2D _AddTex;
float4 _AddColor;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
half4 c1 = tex2D(_AddTex,IN.uv_MainTex);
o.Emission = c.rgb * _AddColor.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
AlphaBlend
透明混合 是加法和乘法的组合运用。
NewColor =SrcColor*(1-Alpha)+Color*Alpha;
将底图的颜色根据当前图的(1-alpha)降低亮度
将当前图的颜色根据当前图的alpha 降低亮度,再合并到一起。
若alpha越低,底图的亮度降低的越少。若alpha越高,当前图的亮度降低的越少。
将最终的颜色计算代入上面的公式,添加的贴图用有Alpha通道的图片
Shader "Custom/NewShader 1" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_AddTex("Add (RGB)",2D) = "white" {}
_AddColor("Add Color",Color) = (0,0,0,0)
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
sampler2D _AddTex;
float4 _AddColor;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
half4 c1 = tex2D(_AddTex,IN.uv_MainTex);
o.Emission = c.rgb*(1-c1.a)+ c1.rgb*c1.a;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
最后是颜色看起来变为黑白的方法:
去色就是把饱和度拉下来,让图片看起来是黑白的
我们前面讲过,rgb三分量差距越小,则饱和度越低,三分量相等,就是黑白的。
由于人眼对绿色最为敏感,所以有个简单的方法,让rgb全部都等于绿色的值。
Shader "Custom/NewShader 1" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_AddTex("Add (RGB)",2D) = "white" {}
_AddColor("Add Color",Color) = (0,0,0,0)
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
sampler2D _AddTex;
float4 _AddColor;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
half4 c1 = tex2D(_AddTex,IN.uv_MainTex);
c.r = c.g;
c.b = c.g;
o.Emission = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
更准确的方式是按照人眼的敏感度求权出亮度:
Shader "Custom/NewShader" {
Properties {
//以下属性显示在Inspector窗口
_MainTex ("Base (RGB)", 2D) = "white" {}
_AddTex("Add (RGB)",2D) = "white" {} //新增的图片
_AddColor("Add Color",Color) = (0,0,0,0)//新增的颜色
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
//声明对应的变量
sampler2D _MainTex;
sampler2D _AddTex;
float4 _AddColor;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
//这两个方法是对图片取颜色
half4 c = tex2D (_MainTex, IN.uv_MainTex);
half4 c1 = tex2D(_AddTex,IN.uv_MainTex);
//o.Albedo = c.rgb; //颜色与光照的影响
//Alpha混合
//o.Emission = c.rgb*(1-c1.a)+ c1.rgb*c1.a;
//颜色相乘
//o.Emission = c.rgb * _AddColor.rgb;
//两张图片颜色相加效果比较糟糕
//o.Emission = c.rgb +c1.rgb;
//通过全部变为绿色通道去色
//c.r = c.g;
//c.b = c.g;
// o.Emission = c.rgb;
float gray = c.r*0.3+c.g*0.59+c.b*0.11;
o.Emission = gray;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
学习参考链接http://www.manew.com/thread-23533-1-1.html