凹凸映射7.2

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
//7.2凹凸映射
Shader "Unlit/Chapter7-NormalMapTangentSpace"
{
Properties
{
	//定义相应属性
_Color("Color Tint",Color) = (1,1,1,1)
_MainTex("Main Tex", 2D) = "white" {}
//法线纹理,bump是unity内置的法线纹理,对应模型自带的法线信息
_BumpMap("Normal Map",2D) = "bump"{}
//法线纹理凹凸程度
_BumpScale("Bump Scale",Float) = 1.0
_Specular("Specular",Color) = (1,1,1,1)
_Gloss("Gloss",Range(8.0,256)) = 20
}

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;

struct a2v {
	float4 vertex:POSITION;
	float3 normal:NORMAL;
	//tanggent顶点切线方向
	//是float4而非float3是因为需要使用tangent.w分量来决定切线空间中的第三个坐标轴—副切线方向
	float4 tangent:TANGENT;
	float4 texcoord:TEXCOORD0;
};
struct v2f {
	float4 pos : SV_POSITION;
	float4 uv:TEXCOORD0;
	//光照方向
	float3 lightDir: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;
	o.uv.zw = v.texcoord.xy*_BumpMap_ST.xy + _BumpMap_ST.zw;

	//Compute the binormal   (计算副法线)
	//v.tangent.w决定我们选择其中哪一个方向
	//float3  binormal=cross(normalize(v.normal),normalize(v.tangent.xyz))*v.tangent.w;
	/*cross:叉积,输入俩向量,得出垂直于这俩个向量的第三个向量,用于求法线,副法线等*/
	//Construct a matrix which transform vectors from object space to tangent space  (构造一个矩阵,将向量从目标空间变换到切空间)
	//把模型空间下  切线方向, 副切线方向, 法线方向,  按行排列来得到从模型空间到切线空间的变换矩阵Rotation
	//float3x3 rotation=float3x3(v.tangent.xyz,binormal,v.normal);
	//or just use thebuilt-in macro  (或者使用内置宏)

	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);
//Get the texel in the normal map
//利用tex2D对法线纹理_BumpMap进行采样
fixed4 packedNormal = tex2D(_BumpMap, i.uv.zw);
fixed3 tangentNormal;
//if the texture is not marked as "normal map"(如果纹理没有被标记为“法线贴图”)
//tangentNormal.xy=(packedNormal.xy*2-1)*_BumpScale;
//tangentNormal.z=sqrt(1.0-saturate(dot(tangentNormal.xy,tangentNormal.xy)));

//or mark the texture as "Normal map",ang use the built-in funciton(或者将纹理标记为“法线贴图”,使用内置函数)
tangentNormal = UnpackNormal(packedNormal);
tangentNormal.xy *= _BumpScale;
//sqrt函数,非负实数的平方根
tangentNormal.z = sqrt(1.0 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));

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));

fixed3 halfDir = normalize(tangentLightDir + tangentViewDir);
fixed3 specular = _LightColor0.rgb*_Specular.rgb*pow(max(0, dot(tangentNormal, halfDir)), _Gloss);

return fixed4(ambient + diffuse + specular, 1.0);
}

ENDCG
}

}
Fallback"specular"
}

猜你喜欢

转载自blog.csdn.net/ABigDeal/article/details/82256881
7.2