//7.4遮罩纹理
Shader "Unlit/Chapter7-MaskTexture"
{
Properties{
_Color("Color Tint",Color) = (1,1,1,1)
//主纹理
_MainTex("Main Tex",2D) = "white"{}
//法线纹理,bump是unity内置的法线纹理,对应模型自带的法线信息
_BumpMap("Bamp Map",2D) = "bump"{}
//法线纹理凹凸程度
_BumpScale("Bump Scale",Float)=2.0
//遮罩纹理
_SpecularMask("Specular Mask",2D) = "white"{}
//遮罩影响度系数
_SpecularScale("Specular Scale",Float)=1.0
//高光颜色
_Specular("Specular",Color) = (1,1,1,1)
//高光程度
_Gloss("Gloss",Range(8.0,256)) = 20
}
SubShader{
pass {
//指明光照模式
Tags{ "LighitMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include"Lighting.cginc"
fixed4 _Color;
sampler2D _MainTex;
/*主纹理,法线纹理,遮罩纹理共同使用属性变量_MainTex_ST
在材质面板中修改主纹理的平铺系数和偏移系数会同时影响三个纹理采样*/
float4 _MainTex_ST;
sampler2D _BumpMap;
float _BumpScale;
sampler2D _SpecularMask;
float _SpecularScale;
fixed4 _Specular;
float _Gloss;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
//tanggent顶点切线方向
float4 tangent:TANGENT;
float4 texcoord:TEXCOORD0;
};
struct v2f {
float4 pos:SV_POSITION;
float2 uv:TEXCOORD0;
float3 lighitDir:TEXCOORD1;
float3 viewDir:TEXCOORD2;
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv.xy = v.texcoord.xy*_MainTex_ST.xy + _MainTex_ST.zw;
//使用内置宏,宏内定义了rotation
TANGENT_SPACE_ROTATION;
//使用rotation将光线和视角从模型空间转换到切线空间
o.lighitDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;
o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz;
return o;
}
//使用遮罩纹理的地方是片元着色器,我们使用它来控制模型表面的高光反射强度;
fixed4 frag(v2f i) :SV_Target{
//光照方向归一化
fixed3 tangentLightDir = normalize(i.lighitDir);
//视角方向归一化
fixed3 tangentViewDir = normalize(i.viewDir);
//将纹理标记为“法线贴图”,使用内置函数
fixed3 tangentNormal = UnpackNormal(tex2D(_BumpMap, i.uv));
//法线纹理凹凸程度
tangentNormal.xy *= _BumpScale;
//法线纹理公式应用 深度
tangentNormal.z = sqrt(1.0 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));
//反射率=tex2D(被采样的纹理,float2类型的纹理坐标).rgb*颜色属性
fixed3 albedo = tex2D(_MainTex, i.uv).rgb*_Color.rgb;
//环境光部分=环境光照*反射率
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;
//漫反射=光照颜色*反射率*(法线纹理,入射光)
fixed3 diffuse = _LightColor0.rgb*albedo*max(0, dot(tangentNormal, tangentLightDir));
//Blinn-Phong光照模式
fixed3 halfDir = normalize(tangentLightDir + tangentViewDir);
//先对遮罩贴图进行采样(使用R分量来计算掩码值),然后乘以高光强度系数,一起控制高光反射强度
fixed specularMask = tex2D(_SpecularMask, i.uv).r*_SpecularScale;
//高光反射=光照颜色*
fixed3 specular = _LightColor0.rgb*_Specular.rgb*pow(max(0, dot(tangentNormal, halfDir)), _Gloss)*specularMask;
return fixed4(ambient + diffuse + specular, 1.0);
}
ENDCG
}
}
Fallback"Specular"
}
遮罩纹理7.4
猜你喜欢
转载自blog.csdn.net/ABigDeal/article/details/82256962
今日推荐
周排行