认识shader:
创建自定义光照模型,创建各种效果:
创建一个Half Lambert光照模型
将上述例子的inline float4 LightingBasicDiffuse
更改为:
2、在CGPROGRAM 中增加_RampTex变量
3、将纹理texture类型图片与_RampTex变量相关联;
4、更改光照函数LightingBasicDiffuse为:
更改原始生成的shader代码,更改为:
Shader "CookbookShaders/BasicDiffuse" {
Properties {
_EmissiveColor ("Emissive Color",Color) = (1,1,1,1)
_AmbientColor ("Ambient Color",Color) = (1,1,1,1)
_MySliderValue ("This is a Slider",Range(0,10)) = 2.5
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
float4 _EmissiveColor;
float4 _AmbientColor;
float _MySliderValue;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
float4 c;
c = pow( (_EmissiveColor + _AmbientColor), _MySliderValue);
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
创建自定义光照模型,创建各种效果:
Shader "CookbookShaders/BasicDiffuse" {
Properties {
_EmissiveColor ("Emissive Color",Color) = (1,1,1,1)
_AmbientColor ("Ambient Color",Color) = (1,1,1,1)
_MySliderValue ("This is a Slider",Range(0,10)) = 2.5
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf BasicDiffuse
float4 _EmissiveColor;
float4 _AmbientColor;
float _MySliderValue;
inline float4 LightingBasicDiffuse (SurfaceOutput s,fixed3
lightDir, fixed atten)
{
float difLight = max(0, dot (s.Normal , lightDir));
float4 col;
col.rgb = s.Albedo * _LightColor0.rgb * (difLight * atten * 2);
col.a = s.Alpha;
return col;
}
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
float4 c;
c = pow( (_EmissiveColor + _AmbientColor), _MySliderValue);
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
创建一个Half Lambert光照模型
将上述例子的inline float4 LightingBasicDiffuse
更改为:
inline float4 LightingBasicDiffuse (SurfaceOutput s,fixed3
lightDir, fixed atten)
{
//float difLight = max(0, dot (s.Normal , lightDir));
float difLight = dot(s.Normal,lightDir);
float hLambert = difLight * 0.5 + 0.5;
float4 col;
col.rgb = s.Albedo * _LightColor0.rgb * (hLambert * atten * 2);
col.a = s.Alpha;
return col;
}
创建渐变纹理控制漫反射着色
1、在Propertites中,增加
_RampTex("MyTexture",2D) = "white"{}
2、在CGPROGRAM 中增加_RampTex变量
sampler2D _RampTex;
3、将纹理texture类型图片与_RampTex变量相关联;
4、更改光照函数LightingBasicDiffuse为:
inline float4 LightingBasicDiffuse (SurfaceOutput s,fixed3
lightDir, fixed atten)
{
float difLight = max(0, dot (s.Normal , lightDir));
float hLambert = difLight * 0.5 + 0.5;
float3 ramp = tex2D(_RampTex,float2(hLambert)).rgb;
float4 col;
col.rgb = s.Albedo * _LightColor0.rgb * (ramp);
//col.rgb = s.Albedo * _LightColor0.rgb * (hLambert * atten * 2);
col.a = s.Alpha;
return col;
}