不知道为什么有色差??!
留坑以后再填
Shader "Unlit/normal_worldspace"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_BumpMap("Normal Map",2D) ="bump"{}
_Color("Color Tint", Color) = (1,1,1,1)
_BumpScalr("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"
float _Gloss;
fixed4 _Color;
sampler2D _MainTex;
sampler2D _BumpMap;
float4 _MainTex_ST;
float4 _BumpMap_ST;
float _BumpScalr;
fixed4 _Specular;
struct a2v
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float3 normal:NORMAL;
float4 tangent :TANGENT;
};
struct v2f
{
float4 vertex : SV_POSITION;
float4 uv:TEXCOORD0;
float4 TtoW0 : TEXCOORD1 ;
float4 TtoW1 : TEXCOORD2 ;
float4 TtoW2 : TEXCOORD3 ;
//TtoW 是一个3x3的矩阵,每一个存储了从切线空间到世界空间变换矩阵的每一行
};
v2f vert (a2v v)
{
v2f o;
o.vertex = 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;
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
fixed3 worldBinormal = cross(worldNormal,worldTangent)* v.tangent.w;
//compute the matrix that transform directions from tangent space to world space
//put the world position in w component for optimization
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);
//这是计算世界空间下的顶点切线、副切线和法线的矢量表示
// 并把他们按列排放!重点:按列 :代表从切线空间到世界空间
//所以这是个3x3的变换矩阵,但是把世界空间下的顶点位置存储在这些变量的w分量中
//为了使充分利用插值寄存器的存储空间
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//Get the position in world space
float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);
//Compute the light and view dir in world space
fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));
//Get the normal in tangent in tangent space
fixed3 bump= UnpackNormal(tex2D(_BumpMap,i.uv.zw));
//将法线贴图中的切线空间下的法向量反映射出来
bump.xy *= _BumpScalr;
bump.z = sqrt(1.0 - saturate(dot(bump.xy,bump.xy)));
//Transform the normal from tangent space to world space
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;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo ;
fixed3 diffuse = _LightColor0.rbg * albedo *saturate(dot(bump, lightDir));
//fixed3 diffuse = _LightColor0.rbg * albedo *max(0,dot(bump,lightDir));
fixed3 halfdir = normalize( lightDir + viewDir);
fixed3 specular = _LightColor0.rgb * _Specular.rgb* pow(saturate(dot(bump,halfdir)),_Gloss);
//fixed3 specular = _LightColor0.rgb * _Specular.rgb *pow(max(0,dot(bump,halfdir)),_Gloss);
return fixed4(ambient+ diffuse +specular ,1.0 );
}
ENDCG
}
}
FallBack"Specular"
}