1. 程式人生 > 程式設計 >Unity Shader實現紋理遮罩效果

Unity Shader實現紋理遮罩效果

紋理遮罩用處很多,簡單來講就是允許我們可以保護模型中的某些區域不受某些因素的影響。例如我們有時希望模型表面某些區域的反光強烈一些,而某些區域弱一些。為了得到更加細膩的結果,我們需要用一張遮罩紋理來控制該光照。還有一些情況就是某些模型需要多張紋理混合時,此時使用遮罩紋理可以控制如何混合這些紋理。

具體流程為:通過取樣得到遮罩紋理的紋素值,然後使用其中某個或者幾個通道的值來與某種表面屬性進行相乘。當該通道的值為0時,此時該表面屬性不受遮罩紋理的影響。

shader程式碼如下:

Shader "Custom/MaskTexture"
{
  Properties
  {
    _Color ("Color",Color) = (1,1,1)
    _MainTex ("Main Tex",2D) = "white" {}
 _BumpMap("Normal Map",2D) = "white" {}
 _BumpScale("Bump Scale",Float) = 1.0
 _SpecularMask("Specular Mask",2D) = "white" {}
 _SpecularScale("Specular Scale",Float) = 1.0
 _Specular("Specular",1)
 _Gloss("Gloss",Range(8.0,256)) = 20
 }
 
 SubShader
 {
 Pass
 {
  Tags{"LightMode" = "ForwardBase"}
 
  CGPROGRAM
 
  #pragma vertex vert
  #pragma fragment frag
  #include "UnityCG.cginc"
  #include "Lighting.cginc"
 
  fixed4 _Color;
  sampler2D _MainTex;
  float4 _MainTex_ST;   //紋理變數名+_ST為unity內建巨集,儲存了該紋理的縮放大小和偏移量,分別對應.xy和.zw屬性
  sampler2D _BumpMap;
  float _BumpScale;    //控制凹凸程度的變數
  sampler2D _SpecularMask; //遮罩紋理
  float _SpecularScale;  //控制遮罩紋理的可見度
  fixed4 _Specular;
  float _Gloss;
 
  struct a2v {
  float4 vertex : POSITION;
  float3 normal : NORMAL;
  float4 tangent : TANGENT;
  float4 texcoord : TEXCOORD0;
  };
 
  struct v2f {
  float4 pos : SV_POSITION;
  float2 uv : TEXCOORD0;
  float3 lightDir : TEXCOORD1;
  float3 viewDir : TEXCOORD2;
  };
 
  v2f vert(a2v v)
  {
  v2f o;
 
  o.pos = UnityObjectToClipPos(v.vertex);
  //將_MainTex紋理資訊(縮放大小和偏移量以及座標資訊)儲存到o.uv.xy中
  o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
 
  //unity內建巨集,計算並得到從模型空間到切線空間的變換矩陣rotation
  TANGENT_SPACE_ROTATION;
  //獲取切線空間下的光照方向
  o.lightDir = 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.lightDir);
  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)));
 
  //獲取片元上的主紋理,並和變數_Color相乘得到其混合結果
  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));
 
  //高光反射計算,其計算方式跟前文的計算一樣,這裡只能另外跟specularMask的遮罩紋理相乘得到其與遮罩紋理的混合結果
  fixed3 halfDir = normalize(tangentLightDir + tangentViewDir);
  fixed specularMask = tex2D(_SpecularMask,i.uv).r * _SpecularScale;
  fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0,halfDir)),_Gloss) * specularMask;
 
  return fixed4(ambient + diffuse + specular,1.0);
  }
 
  ENDCG
 }
  }
  FallBack "Specular"
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。