1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| Shader "Unlit/ForwardLighting" { Properties { _MainColor("MainColor",Color) = (1,1,1,1) _SpecularColor("SpecularColor",Color) = (1,1,1,1) _SpecularNum("SpecularNum",Range(0,20)) = 0.5 } SubShader { Pass { Tags {"LightMode"="ForwardBase"} CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_fwdbase #include "UnityCG.cginc" #include "Lighting.cginc" struct v2f { float4 pos:SV_POSITION; float3 normal:NORMAL; float3 wPos:TEXCOORD0; }; v2f vert (appdata_base v) { v2f data; data.pos = UnityObjectToClipPos(v.vertex); data.normal = UnityObjectToWorldNormal(v.normal); data.wPos = mul(UNITY_MATRIX_M,v.vertex).xyz; return data; }
float3 _MainColor; fixed3 GetLambertFColor(in float3 normal) { float3 lightDir = normalize(_WorldSpaceLightPos0.xyz); fixed3 color = _MainColor.rgb * _LightColor0.rgb * max(0,dot(lightDir,normal)); return color; }
float3 _SpecularColor; float _SpecularNum; fixed3 GetBlinnSpecularColor(in float3 wPos,in float3 normal) { float3 viewDir = normalize(_WorldSpaceCameraPos.xyz-wPos);
float3 lightDir = normalize(_WorldSpaceLightPos0).xyz;
float3 halfA = normalize(viewDir + lightDir);
fixed3 color = _LightColor0.rgb * _SpecularColor.rgb *pow(max(0,dot(halfA,normal)),_SpecularNum); return color; } fixed4 frag (v2f i) : SV_Target { fixed3 lambertColor = GetLambertFColor(i.normal); fixed3 SpecularColor = GetBlinnSpecularColor(i.wPos,i.normal);
fixed attenuation = 1; fixed3 color = UNITY_LIGHTMODEL_AMBIENT.rgb + (lambertColor + SpecularColor)*attenuation; return fixed4(color.rgb , 1); } ENDCG } Pass { Tags {"LightMode"="ForwardAdd"} Blend One One CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_fwdadd #include "UnityCG.cginc" #include "Lighting.cginc" struct v2f { float4 pos:SV_POSITION; float3 normal:NORMAL; float3 wPos:TEXCOORD0; }; float3 _MainColor; float3 _SpecularColor; float _SpecularNum; v2f vert (appdata_base v) { v2f data; data.pos = UnityObjectToClipPos(v.vertex); data.normal = UnityObjectToWorldNormal(v.normal); data.wPos = mul(UNITY_MATRIX_M,v.vertex).xyz; return data; }
fixed4 frag (v2f i) : SV_Target { fixed3 worldNormal = normalize(i.normal); #if defined(_DIRECTIONAL_LIGHT) fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz); #else fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz - i.wPos.xyz); #endif fixed3 diffuse = _LightColor0.rgb * _MainColor.rgb * max(0,dot(worldNormal,worldLightDir));
fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.wPos.xyz); fixed3 halfDir = normalize(worldLightDir + viewDir); fixed3 specular = _LightColor0.rgb * _SpecularColor.rgb * pow(max(0,dot(worldNormal,halfDir)),_SpecularNum);
#if defined(_Direction_LIGHT) fixed atten = 1; #elif defined(_Point_LIGHT) float3 lightCoord = mul(unity_worldToLight,float4(i.wPos,1)).xyz; fixed atten = tex2D(_LightTexture0,dot(lightCoord,lightCoord).xx).UNITY_ATTEN_CHANNEL; #elif defined(_SPOT_LIGHT) float4 lightCoord = mul(unity_worldToLight,float4(i.wPos,1)); fixed atten = (lightCoord.z > 0) * tex2D(_LIghtTexture0,lightCoord.xy / lightCoord.w + 0.5).w * tex2D(_LightTextureB0,dot(lightCoord,lightCoord).xx).UNITY_ATTEN_CHANNEL; #else fixed atten = 1; #endif
return fixed4((diffuse + specular) * atten, 1); } ENDCG } } }
|