法线贴图原理实例 漫反射加高光
Shader "chicai/7"{
	Properties{
		_Specular("Specular",Color)=(1,1,1,1)
		_Glass("Glass",Range(8,20))=8
		_MainTex("MainTex",2D)="white"{}
		_NormalTex("NormalTex",2D)="white"{}
		_NormalIndexd("AoTuQiangDu",float)=1
	}

	SubShader{
		Pass{
			Tags{"RenderMode"="ForwardBase"}

			CGPROGRAM
			fixed4 _Diffuse;
			fixed4 _Specular;
			float _Glass;
			sampler2D _MainTex;
			float4 _MainTex_ST;
			sampler2D _NormalTex;
			float4 _NormalTex_ST;
			float _NormalIndexd;

			#include "Lighting.cginc"
			#pragma vertex vert
			#pragma fragment frag
			struct a2v{
				float4 vertex:POSITION;
				float3 normal:NORMAL;
				float4 tangent:TANGENT;
				float4 texcoord:TEXCOORD0;
			};

			struct v2f{
				float4 position:SV_POSITION;
				fixed3 nor:COLOR0;
				fixed3 lightDir:COLOR1;
				fixed3 viewDir:COLOR2;
				float4 uv:TEXCOORD0;
			};

			v2f vert(a2v v){
				v2f f;
				f.position = UnityObjectToClipPos(v.vertex);
				f.nor = normalize(mul(unity_ObjectToWorld,v.normal));

				TANGENT_SPACE_ROTATION;
				f.lightDir = normalize(mul(rotation,ObjSpaceLightDir(v.vertex))).xyz;
				f.viewDir = normalize(mul(rotation,ObjSpaceViewDir(v.vertex))).xyz;
				f.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
				f.uv.zw = v.texcoord.xy * _NormalTex_ST.xy + _NormalTex_ST.zw;
				return f;
			}

			fixed4 frag(v2f f):SV_Target{

				fixed3 tangentNormal = UnpackNormal(tex2D(_NormalTex,f.uv.wz));
				tangentNormal.xy = tangentNormal.xy * _NormalIndexd;
				tangentNormal = normalize(tangentNormal);

				fixed3 texCol = tex2D(_MainTex,f.uv);//计算改uv上贴图的颜色,当物体本身颜色——Diffuse
				fixed3 diffuseCol = _LightColor0.rgb * texCol  * (dot(tangentNormal,f.lightDir)/2+0.5);

				fixed3 aveDir = normalize(f.lightDir+f.viewDir);
				fixed3 specularCol = _LightColor0.rgb * _Specular * pow(max(dot(aveDir,tangentNormal),0),_Glass);

				fixed3 allColor = diffuseCol + specularCol;

				return fixed4(allColor,1);
			}

			ENDCG
		}
	}
}



首页 我的博客
粤ICP备17103704号