
Shader学习笔记3
上一篇发现太多了,在加一份:
深度Shader,被遮挡的物体透明变色
透明的写法:1.Tags{"RenderType"="Transparent" "Queue"= "Transparent"}
2.在光照模型后面加上alpha(非大写A)
3.在surf中给o.Alpha赋值
Shader "Custom/ZTestShader" {
Properties {
_Color ( "Color" , Color ) = (1,1,1,1)
_MainTex ( "Albedo (RGB)" , 2D ) = "white" {}
}
SubShader {
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
LOD 200
//如果没有遮挡 (和其他物体深度相等)
ZTest LEqual
CGPROGRAM
#pragma surface surf Standard fullforwardshadows alpha
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
fixed4 _Color;
void surf (Input IN , inout SurfaceOutputStandard o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
//如果被遮挡住了(深度大于遮挡物体)
ZTest Greater
CGPROGRAM
#pragma surface surf Standard fullforwardshadows alpha
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
fixed4 _Color;
void surf (Input IN , inout SurfaceOutputStandard o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = fixed3 (1,1,0);
o.Alpha = 0.1;
}
ENDCG
}
FallBack "Diffuse"
}
溶解效果Shader
Shader "Dissolve/Dissolve_TexturCoords" {
Properties {
_Color ( "主颜色" , Color ) = (1,1,1,1) // 主色
_MainTex ( "模型贴图" , 2D ) = "white" {} // 主材质
_DissolveText ( "溶解贴图" , 2D ) = "white" {} // 溶解贴图
_Tile( "溶解贴图的平铺大小" , Range (0, 1)) = 1 // 平铺值,设置溶解贴图大小
_Amount ( "溶解值" , Range (0, 1)) = 0.5 // 溶解度
_DissSize( "溶解大小" , Range (0, 1)) = 0.1 // 溶解范围大小
_DissColor ( "溶解主色" , Color ) = (1,1,1,1) // 溶解颜色
_AddColor ( "叠加色,与主色叠加为开始色[R|G|B>0表示启用]" , Color ) = (1,1,1,1) // 改色与溶解色融合形成开始色
}
SubShader {
Tags { "RenderType" = "Opaque" }
LOD 200
Cull off
CGPROGRAM
#pragma target 3.0
#pragma surface surf BlinnPhong
sampler2D _MainTex;
sampler2D _DissolveText;
fixed4 _Color; // 主色
half _Tile; // 平铺值
half _Amount; // 溶解度
half _DissSize; // 溶解范围
half4 _DissColor; // 溶解颜色
half4 _AddColor; // 叠加色
// 最终色
static half3 finalColor = float3 (1,1,1);
struct Input {
float2 uv_MainTex; // 只需要主材质的UV信息
};
void surf (Input IN , inout SurfaceOutput o) {
// 对主材质进行采样
fixed4 tex = tex2D (_MainTex, IN.uv_MainTex);
// 设置主材质和颜色
o.Albedo = b * _b;
// 对裁剪材质进行采样,取R色值
float ClipTex = tex2D (_DissolveText, IN.uv_MainTex/_Tile).r;
// 裁剪量 = 裁剪材质R - 外部设置量
float ClipAmount = ClipTex - _Amount;
if (_Amount > 0)
{
// 如果裁剪材质的R色值 < 设置的裁剪值 那么此点将被裁剪
if (ClipAmount < 0)
{
//如果输入向量中的值小于1 ,丢弃此向量
clip (-0.1);
}
// 然后处理没有被裁剪的值
else
{
// 针对没有被裁剪的点,【裁剪量】小于【裁剪大小】的做处理
// 如果设置了叠加色,那么该色为ClipAmount/_DissSize(这样会形成渐变效果)
if (ClipAmount < _DissSize)
{
if (_AddColor.x == 0)
finalColor.x = _DissColor.x;
else
finalColor.x = ClipAmount/_DissSize;
if (_AddColor.y == 0)
finalColor.y = _DissColor.y;
else
finalColor.y = ClipAmount/_DissSize;
if (_AddColor.z == 0)
finalColor.z = _DissColor.z;
else
finalColor.z = ClipAmount/_DissSize;
// 融合
o.Albedo = o.Albedo * finalColor * 2;
}
}
}
o.Alpha = tex.a * _Color.a;
}
ENDCG
} //endsubshader
}
附:溶解贴图网上有
水流效果Shader
Shader "Custom/waterShader" { //------------------------------------【属性值】------------------------------------ Properties { //主纹理 _MainTex ( "Albedo (RGB)" , 2D ) = "white" {} //左右偏移量和流动速度 _XScorllSpeed( "XScorllSpeed" , Range (0,1))=0 _YScorllSpeed( "YScorllSpeed" , Range (0,1))=0 } SubShader { Tags { "RenderType" = "Opaque" } LOD 200 CGPROGRAM #pragma surface surf Standard fullforwardshadows
//编译指令: 指定着色器编译目标为Shader Model 3.0 #pragma target 3.0
//变量的声明 sampler2D _MainTex; fixed _XScorllSpeed; fixed _YScorllSpeed; //表面输入结构 struct Input { float2 uv_MainTex; //纹理坐标 };
void surf(Input IN , inout SurfaceOutputStandard o) { fixed2 scrollUV = IN.uv_MainTex; fixed XScorllSpeed = _XScorllSpeed* _Time ; fixed YScorllSpeed = _YScorllSpeed* _Time ; scrollUV+= fixed2 (XScorllSpeed,YScorllSpeed); fixed4 c = tex2D (_MainTex, scrollUV); o.Albedo = c.rgb; o.Alpha = c.a; } ENDCG } //备胎为漫反射 FallBack "Diffuse" } 自定义光照模型,边缘光,卡通效果搭配使用(综合)
Shader "MyShader/DavidShader" {
Properties { _MainTex( "Main Texture" , 2D ) = "white" {} _BumpTex( "Bump Texture" , 2D ) = "bump" {} _Emission( "Emission" , Color ) = (1,1,0,1) IsEnter( "IsEnter" , int ) = 0 _Amount( "Amount" , Range (-1,1))= 0 } SubShader { Tags { "RenderType" = "Opaque" } LOD 200 CGPROGRAM #pragma surface surf ZZW vertex:vert #pragma target 3.0
//自定义光照模型,Output结构,光照方向,光照衰减度 half4 LightingZZW( SurfaceOutput o, half3 lightDir, half atten){ //向量的点乘,光强度 half4 NDot = dot (o.Normal,lightDir); 这是半Lambert光照模型的公式,加亮 //NDot = NDot*0.5+0.5;
half4 result ; //_LightColor0光照颜色的影响,不乘就不收影响 b = o.Albedo*_b*(NDot*atten); result.a = o.Alpha; return result; } struct Input{ fixed2 uv_MainTex; fixed2 uv_BumpTex; float3 viewDir; //视角方向 }; fixed _Amount; void vert( inout appdata_full v){ += v.normal*_Amount; } sampler2D _MainTex; sampler2D _BumpTex; fixed4 _Emission; fixed IsEnter; void surf(Input IN , inout SurfaceOutput o){ fixed4 albedo = tex2D (_MainTex,IN.uv_MainTex); o.Albedo = b; fixed4 normal = tex2D (_BumpTex,IN.uv_BumpTex); o.Normal = UnpackNormal (normal); if (IsEnter>0){ //将视角方向归一化 float3 view = normalize (IN.viewDir); //点乘 float value = dot (view,o.Normal); //限制在0,1之间 value = 1- saturate (value); o.Emission = _Emission*value*value; } } ENDCG }
FallBack "Diffuse"
}