本文共 3074 字,大约阅读时间需要 10 分钟。
Shader "Unlit/WorldNormalTex"{ Properties { _MainTex ("Texture", 2D) = "white" {} _MainNormalTex("NormalTex",2D) = "white"{} } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { Tags{ "LightMode"="ForwardBase" } CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" #include "Lighting.cginc" struct a2v { float4 vertex : POSITION; float2 uv : TEXCOORD0; float3 normal:NORMAL; float4 tangent:TANGENT; }; struct v2f { float4 uv : TEXCOORD0; float4 vertex : SV_POSITION; float4 T2W0 :TEXCOORD1 ; float4 T2W1 :TEXCOORD2 ; float4 T2W2 :TEXCOORD3 ; }; sampler2D _MainTex; float4 _MainTex_ST; sampler2D _MainNormalTex; float4 _MainNormalTex_ST; v2f vert (a2v v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); // 缩放+偏移 o.uv.xy = TRANSFORM_TEX(v.uv,_MainTex); o.uv.zw = TRANSFORM_TEX(v.uv,_MainNormalTex); float3 worldPos = mul(unity_ObjectToWorld,v.vertex); float3 worldNormal = UnityObjectToWorldNormal(v.normal); float3 worldTangent = UnityObjectToWorldDir(v.tangent); float3 worldBitangent = cross(worldNormal ,worldTangent) * v.tangent.w; // 一个插值寄存器最多存储float4大小的变量 o.T2W0 = float4 (worldTangent.x,worldBitangent.x,worldNormal.x,worldPos .x); o.T2W1 = float4 (worldTangent.y,worldBitangent.y,worldNormal.y,worldPos .y); o.T2W2 = float4 (worldTangent.z,worldBitangent.z,worldNormal.z,worldPos .z); return o; } fixed4 frag (v2f i) : SV_Target { float3 worldPos = float3(i.T2W0.w,i.T2W1.w,i.T2W2.w); fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos)); fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos)); fixed4 tangentNormal = tex2D(_MainNormalTex,i.uv.zw); fixed3 bump = UnpackNormal(tangentNormal); fixed3 worldBump = normalize(half3( dot(i.T2W0.xyz,bump), dot(i.T2W1.xyz,bump), dot(i.T2W2.xyz,bump))); float3 albedo = tex2D(_MainTex,i.uv.xy).rgb; fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT; fixed3 diffuse = _LightColor0.rgb * albedo* max(0,dot(worldBump,lightDir)); fixed3 halfDir = normalize(lightDir+viewDir); fixed3 specular = _LightColor0.rgb * pow(max(0,dot(worldBump,halfDir)),20); return fixed4( diffuse+ambient+specular,1.0); } ENDCG } }}
转载地址:http://ndkii.baihongyu.com/