BuildMetallicTexture.shader 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. Shader "Hidden/TriLib/BuildMetallicTexture"
  2. {
  3. SubShader
  4. {
  5. // No culling or depth
  6. Cull Off ZWrite Off ZTest Always
  7. Pass
  8. {
  9. CGPROGRAM
  10. #pragma vertex vert
  11. #pragma fragment frag
  12. #include "UnityCG.cginc"
  13. struct appdata
  14. {
  15. float4 vertex : POSITION;
  16. float2 uv : TEXCOORD0;
  17. };
  18. struct v2f
  19. {
  20. float2 uv : TEXCOORD0;
  21. float4 vertex : SV_POSITION;
  22. };
  23. v2f vert(appdata v)
  24. {
  25. v2f o;
  26. o.vertex = UnityObjectToClipPos(v.vertex);
  27. o.uv = v.uv;
  28. return o;
  29. }
  30. sampler2D _MetallicTexture;
  31. sampler2D _DiffuseTexture;
  32. sampler2D _SpecularTexture;
  33. sampler2D _GlossinessTexture;
  34. float3 _DefaultDiffuse;
  35. float3 _DefaultSpecular;
  36. float _DefaultRoughness;
  37. float _DefaultMetallic;
  38. float _ShininessExponent;
  39. int _HasMetallicTexture;
  40. int _HasDiffuseTexture;
  41. int _HasSpecularTexture;
  42. int _HasGlossinessTexture;
  43. int _HasDefaultRoughness;
  44. int _HasDefaultMetallic;
  45. int _UsingRoughness;
  46. int _MixTextureChannelsWithColors;
  47. int _MetallicComponentIndex;
  48. int _GlossinessComponentIndex;
  49. float GetGlossiness() {
  50. return 1 - (_HasDefaultRoughness > 0 ? _DefaultRoughness : 1);
  51. }
  52. // Reference:
  53. // https://docs.microsoft.com/en-us/azure/remote-rendering/reference/material-mapping
  54. float4 frag(v2f i) : SV_Target
  55. {
  56. const float dielectricSpecular = 0.04;
  57. float3 specularBase = _HasSpecularTexture > 0 ? tex2D(_SpecularTexture, i.uv).xyz * _DefaultSpecular : _DefaultSpecular;
  58. //Metalness
  59. float metalness;
  60. if (_HasMetallicTexture > 0) {
  61. metalness = tex2D(_MetallicTexture, i.uv)[_MetallicComponentIndex];
  62. if (_HasDefaultMetallic && _MixTextureChannelsWithColors > 0) {
  63. metalness *= _DefaultMetallic;
  64. }
  65. }
  66. else if (_HasSpecularTexture > 0)
  67. {
  68. float3 diffuseBase = _DefaultDiffuse;
  69. float diffuseBrightness = 0.299 * pow(diffuseBase.r, 2) + 0.587 * pow(diffuseBase.g, 2) + 0.114 * pow(diffuseBase.b, 2);
  70. float specularBrightness = 0.299 * pow(specularBase.r, 2) + 0.587 * pow(specularBase.g, 2) + 0.114 * pow(specularBase.b, 2);
  71. float specularStrength = max(specularBase.r, max(specularBase.g, specularBase.b));
  72. float oneMinusSpecularStrength = 1 - specularStrength;
  73. float A = dielectricSpecular;
  74. float B = (diffuseBrightness * (oneMinusSpecularStrength / (1 - A)) + specularBrightness) - 2 * A;
  75. float C = A - specularBrightness;
  76. float squareRoot = sqrt(max(0, B * B - 4 * A * C));
  77. float value = (-B + squareRoot) / (2 * A);
  78. metalness = clamp(value, 0, 1);
  79. if (_HasDefaultMetallic && _MixTextureChannelsWithColors > 0) {
  80. metalness *= _DefaultMetallic;
  81. }
  82. }
  83. else
  84. {
  85. metalness = _DefaultMetallic;
  86. }
  87. //Glossiness
  88. float glossiness;
  89. if (_HasGlossinessTexture > 0) {
  90. glossiness = _UsingRoughness > 0 ? 1 - tex2D(_GlossinessTexture, i.uv)[_GlossinessComponentIndex] : tex2D(_GlossinessTexture, i.uv)[_GlossinessComponentIndex];
  91. if (_HasDefaultRoughness && _MixTextureChannelsWithColors > 0) {
  92. glossiness *= GetGlossiness();
  93. }
  94. }
  95. else if (_HasSpecularTexture > 0)
  96. {
  97. float specularIntensity = specularBase.r * 0.2125 + specularBase.g * 0.7154 + specularBase.b * 0.0721;
  98. glossiness = 1 - sqrt(2 / (_ShininessExponent * specularIntensity + 2));
  99. if (_HasDefaultRoughness && _MixTextureChannelsWithColors > 0) {
  100. glossiness *= GetGlossiness();
  101. }
  102. }
  103. else {
  104. glossiness = GetGlossiness();
  105. }
  106. return float4(metalness, metalness, metalness, glossiness);
  107. }
  108. ENDCG
  109. }
  110. }
  111. }