ChannelPacker.shader 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License.
  3. Shader "Hidden/ChannelPacker"
  4. {
  5. Properties
  6. {
  7. _MetallicMap("Metallic Map", 2D) = "black" {}
  8. _MetallicMapChannel("Metallic Map Channel", Int) = 0 // Red.
  9. _MetallicUniform("Metallic Uniform", Float) = -0.01
  10. _OcclusionMap("Occlusion Map", 2D) = "white" {}
  11. _OcclusionMapChannel("Occlusion Map Channel", Int) = 1 // Green.
  12. _OcclusionUniform("Occlusion Uniform", Float) = -0.01
  13. _EmissionMap("Emission Map", 2D) = "black" {}
  14. _EmissionMapChannel("Emission Map Channel", Int) = 4 // RGBAverage.
  15. _EmissionUniform("Emission Uniform", Float) = -0.01
  16. _SmoothnessMap("Smoothness Map", 2D) = "gray" {}
  17. _SmoothnessMapChannel("Smoothness Map Channel", Int) = 3 // Alpha.
  18. _SmoothnessUniform("Smoothness Uniform", Float) = -0.01
  19. }
  20. SubShader
  21. {
  22. Pass
  23. {
  24. CGPROGRAM
  25. #pragma vertex vert
  26. #pragma fragment frag
  27. #include "UnityCG.cginc"
  28. struct appdata
  29. {
  30. float4 vertex : POSITION;
  31. float2 uv : TEXCOORD0;
  32. };
  33. struct v2f
  34. {
  35. float4 vertex : SV_POSITION;
  36. float2 uv : TEXCOORD0;
  37. };
  38. sampler2D _MetallicMap;
  39. int _MetallicMapChannel;
  40. float _MetallicUniform;
  41. sampler2D _OcclusionMap;
  42. int _OcclusionMapChannel;
  43. float _OcclusionUniform;
  44. sampler2D _EmissionMap;
  45. int _EmissionMapChannel;
  46. float _EmissionUniform;
  47. sampler2D _SmoothnessMap;
  48. int _SmoothnessMapChannel;
  49. float _SmoothnessUniform;
  50. v2f vert(appdata v)
  51. {
  52. v2f o;
  53. o.vertex = UnityObjectToClipPos(v.vertex);
  54. o.uv = v.uv;
  55. return o;
  56. }
  57. fixed4 ToGrayScale(fixed4 color)
  58. {
  59. return color.r * 0.21 + color.g * 0.71 + color.b * 0.08;
  60. }
  61. fixed Sample(fixed4 color, int channel, float uniformValue)
  62. {
  63. if (uniformValue >= 0.0)
  64. {
  65. return uniformValue;
  66. }
  67. if (channel == 4)
  68. {
  69. return ToGrayScale(color);
  70. }
  71. return color[channel];
  72. }
  73. fixed4 frag(v2f i) : SV_Target
  74. {
  75. fixed4 output;
  76. output.r = Sample(tex2D(_MetallicMap, i.uv), _MetallicMapChannel, _MetallicUniform);
  77. output.g = Sample(tex2D(_OcclusionMap, i.uv), _OcclusionMapChannel, _OcclusionUniform);
  78. output.b = Sample(tex2D(_EmissionMap, i.uv), _EmissionMapChannel, _EmissionUniform);
  79. output.a = Sample(tex2D(_SmoothnessMap, i.uv), _SmoothnessMapChannel, _SmoothnessUniform);
  80. return output;
  81. }
  82. ENDCG
  83. }
  84. }
  85. }