模型內邊緣光帶貼圖與法線

最近工作學習任務比較多,好久都沒有寫博客了。

今天寫了個“模型內邊緣光帶貼圖與法線”的Shader。在此分享給大家,有需要用到的可以直接拿去用。瑕疵是還未對點光源進行計算。

Shader "Custom/RimLight_UV_Bump" {
	Properties{
		_Color("Color Tint",Color)=(1,1,1,1)
		_MainTex("Main Tex",2D)="white"{}
		_BumpMap("Normal Map",2D)="bump"{}
		_BumpScale("Bump Scale",Float)=1
		_Specular("Specular",Color)=(1,1,1,1)
		_Gloss("Gloss",Range(8,256))=20
		_RimColor("RimColor", color) = (1,1,1,1)
		_RimStrength("RimStrength", Range(0.0001,3.0))=0.1
	}
	SubShader{
	Pass{
		Tags{"LightMode"="ForwardBase"}
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "Lighting.cginc"

		fixed4 _Color;
		sampler2D _MainTex;
		float4 _MainTex_ST;
		sampler2D _BumpMap;
		float4 _BumpMap_ST;
		float _BumpScale;
		fixed4 _Specular;
		float _Gloss;
		float3 wo;
		fixed4 _RimColor;
		float _RimStrength;

		//Appction To Vertex
		struct a2v{
			float4 vertex:POSITION;
			float3 normal:NORMAL;
			float4 tangent:TANGENT;
			float4 texcoord:TEXCOORD0;
		};
		//Vertex To Fragment
		struct v2f{
			float4 pos:SV_POSITION;
			float4 uv:TEXCOORD0;
			float4 TtoW0:TEXCOORD1;
			float4 TtoW1:TEXCOORD2;
			float4 TtoW2:TEXCOORD3;
		};

		v2f vert(a2v v){
			v2f o;
			o.pos=UnityObjectToClipPos(v.vertex);
			o.uv.xy=v.texcoord.xy*_MainTex_ST.xy+_MainTex_ST.zw;
			o.uv.zw=v.texcoord.xy*_BumpMap_ST.xy+_BumpMap_ST.zw;

			//世界座標下頂點、法線、切線、副切線位置
			float3 worldpos=mul(unity_ObjectToWorld,v.vertex).xyz;
			float3 worldNormal = UnityObjectToWorldNormal(v.normal);
			float3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
			float3 worldBinormal = cross(worldNormal,worldTangent)*v.tangent.w;

			//按列排序得到一個切線到世界空間轉換矩陣 ,位置信息也順帶存在後面了
			o.TtoW0=float4(worldTangent.x,worldBinormal.x,worldNormal.x,worldpos.x);
			o.TtoW1=float4(worldTangent.y,worldBinormal.y,worldNormal.y,worldpos.y);
			o.TtoW2=float4(worldTangent.z,worldBinormal.z,worldNormal.z,worldpos.z);

			return o;
		}

		float4 frag(v2f i):SV_TARGET{
			//世界座標下的位置、燈光方向、攝像機方向等信息
			float3 worldPos= float3(i.TtoW0.w,i.TtoW1.w,i.TtoW2.w);
			float3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));
			float3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));

			//取得紋素
			fixed3 bump = UnpackNormal(tex2D(_BumpMap,i.uv.zw));
			bump.xy*=_BumpScale;
			bump.z=sqrt(1.0-saturate(dot(bump.xy,bump.xy)));
			//用點乘與每一組相乘,得到的是每個分量位移到該空間的位置
			//這句其實相當於:mul(half3X3(i.TtoW0.xyz,i.TtoW1.xyz,(i.TtoW2.xyz),bump)
			bump=normalize(half3(dot(i.TtoW0.xyz,bump),dot(i.TtoW1.xyz,bump),dot(i.TtoW2.xyz,bump)));

			//取主圖的顏色*影響色
			fixed3 albedo=tex2D(_MainTex,i.uv).rgb*_Color.rgb;
			//環境光顏色*albedo
			fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;
			//燈光一顏色*環境色*亮度
			fixed3 diffuse=_LightColor0.rgb*albedo*max(0,dot(bump,lightDir));

			//中線:點到燈光方向+點到攝像機方向:取的是中線
			fixed3 halfDir = normalize(lightDir+viewDir);
			//中線與法線越相似則越亮
			fixed3 specular =_LightColor0.rgb*_Specular.rgb*pow(max(0,dot(bump,halfDir)),_Gloss);

			//世界座標頂點法線向量
			fixed3 worldNormal = normalize(float3(i.TtoW0.z,i.TtoW1.z,i.TtoW2.z));
			//世界座標攝像機視角
			fixed3 worldViewDir = normalize(viewDir);
			//法向量與攝像機視角越垂直邊緣光越亮
			float rim = 1 - max(0, dot(worldViewDir,worldNormal));
			fixed3 rimColor = _RimColor * pow(rim, 1 / _RimStrength);

			//得到最終色值
			return fixed4(rimColor +albedo+ambient+specular,1.0);
		}
		ENDCG
	}
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章