LayaBlinnPhongFB.cginc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "Lighting.cginc"
  2. #include "AutoLight.cginc"
  3. #include "UnityGlobalIllumination.cginc"
  4. float4 _Color;
  5. sampler2D _MainTex;
  6. float _AlbedoIntensity;
  7. float4 _MainTex_ST;
  8. sampler2D _BumpMap;
  9. sampler2D _SpecGlossMap;
  10. half _Shininess;
  11. half _Cutoff;
  12. struct a2v {
  13. float4 vertex : POSITION;
  14. float3 normal : NORMAL;
  15. float4 tangent : TANGENT;
  16. float4 texcoord : TEXCOORD0;
  17. float4 texcoord1: TEXCOORD1;
  18. float4 color : COLOR;
  19. };
  20. struct v2f {
  21. float4 pos : SV_POSITION;
  22. float2 uv : TEXCOORD0;
  23. float2 uv2: TEXCOORD1;
  24. float3 worldPos : TEXCOORD2;
  25. float3 lightDir: TEXCOORD3;
  26. float3 viewDir : TEXCOORD4;
  27. float4 color : COLOR;
  28. SHADOW_COORDS(5)
  29. UNITY_FOG_COORDS(6)
  30. float3 tangentToWorldAndPackedData[3]:TEXCOORD7;
  31. };
  32. inline void layaBlinnPhongLighting (in fixed3 lightDir, in fixed3 viewDir, in fixed3 normal, out fixed4 diffuse, out fixed4 specular)
  33. {
  34. half3 h = normalize (lightDir + viewDir);
  35. fixed diff = max (0, dot (normal, lightDir));
  36. float nh = max (0, dot (normal, h));
  37. diffuse = _LightColor0 * max(0, dot(normal, lightDir));
  38. specular = _LightColor0 * pow (nh, _Shininess * 128.0);
  39. }
  40. v2f vert(a2v v) {
  41. v2f o;
  42. o.pos = UnityObjectToClipPos(v.vertex);
  43. o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
  44. o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  45. o.uv2 = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
  46. o.color = v.color;
  47. fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
  48. fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
  49. fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;
  50. float3x3 worldToTangent = float3x3(worldTangent, worldBinormal, worldNormal);
  51. o.lightDir = mul(worldToTangent, WorldSpaceLightDir(v.vertex));
  52. o.viewDir = mul(worldToTangent, WorldSpaceViewDir(v.vertex));
  53. o.tangentToWorldAndPackedData[0] = worldTangent;
  54. o.tangentToWorldAndPackedData[1] = worldBinormal;
  55. o.tangentToWorldAndPackedData[2] = worldNormal;
  56. // Pass shadow coordinates to pixel shader
  57. TRANSFER_SHADOW(o);
  58. UNITY_TRANSFER_FOG(o, o.pos);
  59. return o;
  60. }
  61. fixed4 frag(v2f i) : SV_Target {
  62. fixed4 mainTexColor = tex2D(_MainTex, i.uv);
  63. fixed4 albedo = mainTexColor * _Color * _AlbedoIntensity;
  64. #if EnableAlphaCutoff
  65. clip (albedo.a - _Cutoff);
  66. #endif
  67. half3 tangent = i.tangentToWorldAndPackedData[0];
  68. half3 binormal = i.tangentToWorldAndPackedData[1];
  69. half3 normals = i.tangentToWorldAndPackedData[2];
  70. fixed3 normal = UnpackNormal(tex2D(_BumpMap, i.uv));
  71. float3 worldNormal=normalize((float3)(tangent * normal.x + binormal * normal.y + normals * normal.z));
  72. fixed4 giAmbient = fixed4(0.0,0.0,0.0,1.0);
  73. #if defined(LIGHTMAP_ON)
  74. giAmbient = fixed4(DecodeLightmap (UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv2)), 1.0);
  75. #else
  76. #if UNITY_LIGHT_PROBE_PROXY_VOLUME
  77. giAmbient.rgb = ShadeSHPerPixel(worldNormal, UNITY_LIGHTMODEL_AMBIENT, i.worldPos);
  78. half3 ambient_contrib = 0.0;
  79. ambient_contrib = SHEvalLinearL0L1(half4(worldNormal, 1.0));
  80. half3 ambient_L2 = 0.0;
  81. ambient_L2 = SHEvalLinearL2(half4(worldNormal, 1.0));
  82. giAmbient.rgb = ambient_L2 + ambient_contrib;
  83. #else
  84. giAmbient = UNITY_LIGHTMODEL_AMBIENT;
  85. #endif
  86. #ifdef UNITY_COLORSPACE_GAMMA
  87. giAmbient.rgb = LinearToGammaSpace(giAmbient.rgb);
  88. #endif
  89. //giAmbient =UNITY_LIGHTMODEL_AMBIENT;
  90. #endif
  91. fixed4 diffuse;
  92. fixed4 specular;
  93. #if EnableLighting
  94. fixed3 lightDir = normalize(i.lightDir);
  95. fixed3 viewDir = normalize(i.viewDir);
  96. layaBlinnPhongLighting(lightDir, viewDir, normal, diffuse, specular);
  97. #else
  98. diffuse = fixed4(0.0,0.0,0.0,0.0);
  99. specular = fixed4(0.0,0.0,0.0,0.0);
  100. #endif
  101. fixed4 ambient = giAmbient * albedo;
  102. diffuse = diffuse * albedo;
  103. #if defined(SpecularTexture)
  104. specular = specular * tex2D(_SpecGlossMap, i.uv) * _SpecColor;
  105. #else
  106. specular = specular * mainTexColor.a * _SpecColor;
  107. #endif
  108. UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
  109. fixed4 color;
  110. #if ENABLEVERTEXCOLOR
  111. color = fixed4(ambient.rgb + (diffuse + specular).rgb * atten, albedo.a)*i.color;
  112. #else
  113. color = fixed4(ambient.rgb + (diffuse + specular).rgb * atten, albedo.a);
  114. #endif
  115. UNITY_APPLY_FOG(i.fogCoord, color);
  116. color = clamp(color, 0, 1);
  117. //return giAmbient;
  118. return color;
  119. }