HardSurface.cginc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //Hard Surface Shader Package, Written for the Unity engine by Bruno Rime: http://www.behance.net/brunorime brunorime@gmail.com
  2. #ifndef HARD_SURFACE_INCLUDED
  3. #define HARD_SURFACE_INCLUDED
  4. samplerCUBE _Cube;
  5. #ifdef HardsurfaceNormal
  6. sampler2D _BumpMap;
  7. #endif
  8. #ifdef HardsurfaceDiffuse
  9. sampler2D _MainTex;
  10. #endif
  11. #ifdef HardsurfaceSpecular
  12. sampler2D _Spec_Gloss_Reflec_Masks;
  13. #endif
  14. fixed4 _Color;
  15. #ifdef ShaderModel3
  16. fixed _Shininess;
  17. fixed _Gloss;
  18. #endif
  19. fixed _Reflection;
  20. fixed _FrezPow;
  21. fixed _FrezFalloff;
  22. fixed _EdgeAlpha;
  23. fixed _Metalics;
  24. struct Input {
  25. float3 worldNormal;
  26. float2 uv_MainTex;
  27. # ifdef HardsurfaceNormal
  28. float2 uv_BumpMap;
  29. #endif
  30. float3 worldRefl;
  31. INTERNAL_DATA
  32. };
  33. void surf (Input IN, inout SurfaceOutput o) {
  34. #ifdef HardsurfaceSpecular
  35. fixed3 SpecGlossRefMask = tex2D(_Spec_Gloss_Reflec_Masks, IN.uv_BumpMap).rgb;
  36. #endif
  37. #ifdef HardsurfaceSpecular
  38. #ifdef ShaderModel3
  39. _Shininess *= pow(SpecGlossRefMask.r,3);
  40. _Shininess = max (_Shininess , .005);
  41. _Gloss *= SpecGlossRefMask.g;
  42. #endif
  43. _Reflection *= SpecGlossRefMask.b;
  44. #endif
  45. #ifdef HardsurfaceNormal
  46. #ifdef HardsurfaceBackface
  47. #ifdef SHADER_API_D3D9
  48. #ifdef ShaderModel3
  49. o.Normal = UnpackNormal(tex2Dlod(_BumpMap, float4(IN.uv_MainTex,1,(1-_Shininess)*3)));
  50. #else
  51. o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
  52. #endif
  53. #else
  54. o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
  55. #endif
  56. #else
  57. o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
  58. #endif
  59. #else
  60. o.Normal = fixed3(0,0,1);
  61. #endif
  62. #ifdef HardsurfaceDiffuse
  63. _Color.rgba *= tex2D(_MainTex, IN.uv_MainTex).rgba;
  64. #endif
  65. //Claculate Reflection vector
  66. half3 worldRefl = normalize(WorldReflectionVector (IN, o.Normal));
  67. half3 worldNormal = WorldNormalVector (IN, o.Normal);
  68. fixed SurfAngle= abs(dot (worldRefl,worldNormal));
  69. fixed frez = pow(1-SurfAngle,_FrezFalloff) ;
  70. _Color.a *= (1-(frez * _EdgeAlpha ));
  71. frez*=_FrezPow * 0.0009765625;
  72. #ifdef HardsurfaceBackface
  73. fixed Diffusion = (1-_Color.a);
  74. Diffusion *= Diffusion;
  75. _Reflection *= Diffusion;
  76. frez *= Diffusion;
  77. #ifdef ShaderModel3
  78. _Shininess *= Diffusion ;
  79. _Gloss *= Diffusion;
  80. _SpecColor.rgb = lerp(_Color.rgb, _SpecColor.rgb, Diffusion);
  81. #endif
  82. #endif
  83. // Decalre variables for platform specific variations;
  84. fixed4 CubeTex;
  85. #ifdef SHADER_API_D3D9
  86. #ifdef ShaderModel3
  87. CubeTex = texCUBElod(_Cube,float4(worldRefl,(1-_Shininess)*6));
  88. #else
  89. CubeTex = texCUBE(_Cube,worldRefl);
  90. #endif
  91. #else
  92. CubeTex = texCUBE(_Cube,worldRefl);
  93. #endif
  94. // Add Fresnel Falloff to Reflection & calculate Reflection Luminace for Blending with diffuse
  95. #ifdef HardsurfaceSpecular
  96. fixed Reflection = saturate(_Reflection + (frez * SpecGlossRefMask.b));
  97. #else
  98. fixed Reflection = saturate(_Reflection + frez);
  99. #endif
  100. CubeTex.rgb *= Reflection;
  101. fixed ReflectiveLum = Luminance(CubeTex.rgb);
  102. ReflectiveLum *= ReflectiveLum;
  103. #ifdef ShaderModel3
  104. o.Specular = _Shininess ;
  105. #ifdef HardsurfaceSpecular
  106. o.Gloss = saturate(_Gloss + (frez * SpecGlossRefMask.g));
  107. #else
  108. o.Gloss = saturate(_Gloss + frez);
  109. #endif
  110. #endif
  111. o.Emission = lerp (CubeTex.rgb ,CubeTex.rgb*lerp(_Color.rgb ,1,ReflectiveLum),_Metalics);
  112. o.Alpha = saturate(_Color.a + ReflectiveLum);
  113. o.Albedo = _Color.rgb * (1-Reflection);
  114. }
  115. #endif