包含光照,可处理多个光源,有光照衰减和阴影的shader,代码如下:
转载请注明出处:http://www.cnblogs.com/jietian331/p/7199311.html
Shader "Custom/Bumped Specular" { Properties { _MainTex("Main Texture", 2D) = "white" {} _NormalMap("Normal Map", 2D) = "bump" {} _Specular("Specular", Color) = (1,1,1,1) _Gloss("Gloss", Range(8, 256)) = 8 } SubShader { Tags { "Queue" = "Geometry" "RenderType" = "Opaque" } Pass { Tags { "LightMode" = "ForwardBase" } CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_fwdbase #include "UnityCG.cginc" #include "Lighting.cginc" #include "AutoLight.cginc" sampler2D _MainTex; float4 _MainTex_ST; sampler2D _NormalMap; float4 _NormalMap_ST; fixed4 _Specular; float _Gloss; struct appdata { float4 vertex : POSITION; float4 uv : TEXCOORD0; fixed4 color : COLOR; float4 tangent : TANGENT; float3 normal : NORMAL; }; struct v2f { float4 pos : SV_POSITION; float4 uv : TEXCOORD0; fixed4 color : COLOR; float4 T2W1 : TEXCOORD1; float4 T2W2 : TEXCOORD2; float4 T2W3 : TEXCOORD3; SHADOW_COORDS(4) }; v2f vert(appdata v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); float2 uv1 = v.uv.xy * _MainTex_ST.xy + _MainTex_ST.zw; float2 uv2 = v.uv.zw * _NormalMap_ST.xy + _NormalMap_ST.zw; o.uv = float4(uv1, uv2); o.color = v.color; float3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz); float3 worldNormal = UnityObjectToWorldNormal(v.normal); float3 binormal = cross(normalize(worldNormal), normalize(worldTangent)) * v.tangent.w; float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz; o.T2W1 = float4(worldTangent.x, binormal.x, worldNormal.x, worldPos.x); o.T2W2 = float4(worldTangent.y, binormal.y, worldNormal.y, worldPos.y); o.T2W3 = float4(worldTangent.z, binormal.z, worldNormal.z, worldPos.z); TRANSFER_SHADOW(o); return o; } fixed4 frag(v2f i) : SV_TARGET { float3 worldPos = float3(i.T2W1.w, i.T2W2.w, i.T2W3.w); float3 worldLight = normalize(UnityWorldSpaceLightDir(worldPos)); float3 worldView = normalize(UnityWorldSpaceViewDir(worldPos)); fixed4 albedo = tex2D(_MainTex, i.uv.xy) * i.color; float3 tangentNormal = UnpackNormal(tex2D(_NormalMap, i.uv.zw)); float3x3 tanToWorld = float3x3(i.T2W1.xyz, i.T2W2.xyz, i.T2W3.xyz); float3 worldNormal = mul(tanToWorld, tangentNormal); fixed3 ambient = albedo.xyz * UNITY_LIGHTMODEL_AMBIENT.xyz; fixed3 diffuse = albedo.xyz * _LightColor0.xyz * max(0, dot(worldLight, worldNormal)); float3 halfDir = normalize(worldLight + worldView); fixed3 specular = albedo.xyz * _Specular * pow(max(0, dot(worldNormal, halfDir)), _Gloss); UNITY_LIGHT_ATTENUATION(atten, i, worldPos); fixed3 col = ambient + (diffuse + specular) * atten; return fixed4(col, 1); } ENDCG } Pass { Tags { "LightMode" = "ForwardAdd" } Blend One One CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_fwdadd_fullshadows #include "UnityCG.cginc" #include "Lighting.cginc" #include "AutoLight.cginc" sampler2D _MainTex; float4 _MainTex_ST; sampler2D _NormalMap; float4 _NormalMap_ST; fixed4 _Specular; float _Gloss; struct appdata { float4 vertex : POSITION; float4 uv : TEXCOORD0; fixed4 color : COLOR; float4 tangent : TANGENT; float3 normal : NORMAL; }; struct v2f { float4 pos : SV_POSITION; float4 uv : TEXCOORD0; fixed4 color : COLOR; float4 T2W1 : TEXCOORD1; float4 T2W2 : TEXCOORD2; float4 T2W3 : TEXCOORD3; SHADOW_COORDS(4) }; v2f vert(appdata v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); float2 uv1 = v.uv.xy * _MainTex_ST.xy + _MainTex_ST.zw; float2 uv2 = v.uv.zw * _NormalMap_ST.xy + _NormalMap_ST.zw; o.uv = float4(uv1, uv2); o.color = v.color; float3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz); float3 worldNormal = UnityObjectToWorldNormal(v.normal); float3 binormal = cross(normalize(worldNormal), normalize(worldTangent)) * v.tangent.w; float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz; o.T2W1 = float4(worldTangent.x, binormal.x, worldNormal.x, worldPos.x); o.T2W2 = float4(worldTangent.y, binormal.y, worldNormal.y, worldPos.y); o.T2W3 = float4(worldTangent.z, binormal.z, worldNormal.z, worldPos.z); TRANSFER_SHADOW(o); return o; } fixed4 frag(v2f i) : SV_TARGET { float3 worldPos = float3(i.T2W1.w, i.T2W2.w, i.T2W3.w); float3 worldLight = normalize(UnityWorldSpaceLightDir(worldPos)); float3 worldView = normalize(UnityWorldSpaceViewDir(worldPos)); fixed4 albedo = tex2D(_MainTex, i.uv.xy) * i.color; float3 tangentNormal = UnpackNormal(tex2D(_NormalMap, i.uv.zw)); float3x3 tanToWorld = float3x3(i.T2W1.xyz, i.T2W2.xyz, i.T2W3.xyz); float3 worldNormal = mul(tanToWorld, tangentNormal); fixed3 diffuse = albedo.xyz * _LightColor0.xyz * max(0, dot(worldLight, worldNormal)); float3 halfDir = normalize(worldLight + worldView); fixed3 specular = albedo.xyz * _Specular * pow(max(0, dot(worldNormal, halfDir)), _Gloss); UNITY_LIGHT_ATTENUATION(atten, i, worldPos); fixed3 col = (diffuse + specular) * atten; return fixed4(col, 1); } ENDCG } } Fallback "Specular" }
效果如下: