BlendMode.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using UnityEngine;
  2. using NativeBlendMode = UnityEngine.Rendering.BlendMode;
  3. namespace FairyGUI
  4. {
  5. /*关于BlendMode.Off, 这种模式相当于Blend Off指令的效果。当然,在着色器里使用Blend Off指令可以获得更高的效率,
  6. 但因为Image着色器本身就有多个关键字,复制一个这样的着色器代价太大,所有为了节省Shader数量便增加了这样一种模式,也是可以接受的。
  7. */
  8. /// <summary>
  9. ///
  10. /// </summary>
  11. public enum BlendMode
  12. {
  13. Normal,
  14. None,
  15. Add,
  16. Multiply,
  17. Screen,
  18. Erase,
  19. Mask,
  20. Below,
  21. Off,
  22. One_OneMinusSrcAlpha,
  23. Custom1,
  24. Custom2,
  25. Custom3
  26. }
  27. /// <summary>
  28. ///
  29. /// </summary>
  30. public class BlendModeUtils
  31. {
  32. public class BlendFactor
  33. {
  34. public NativeBlendMode srcFactor;
  35. public NativeBlendMode dstFactor;
  36. public bool pma;
  37. public BlendFactor(NativeBlendMode srcFactor, NativeBlendMode dstFactor, bool pma = false)
  38. {
  39. this.srcFactor = srcFactor;
  40. this.dstFactor = dstFactor;
  41. this.pma = pma;
  42. }
  43. }
  44. //Source指的是被计算的颜色,Destination是已经在屏幕上的颜色。
  45. //混合结果=Source * factor1 + Destination * factor2
  46. public static BlendFactor[] Factors = new BlendFactor[] {
  47. //Normal
  48. new BlendFactor(NativeBlendMode.SrcAlpha, NativeBlendMode.OneMinusSrcAlpha),
  49. //None
  50. new BlendFactor(NativeBlendMode.One, NativeBlendMode.One),
  51. //Add
  52. new BlendFactor(NativeBlendMode.SrcAlpha, NativeBlendMode.One),
  53. //Multiply
  54. new BlendFactor(NativeBlendMode.DstColor, NativeBlendMode.OneMinusSrcAlpha, true),
  55. //Screen
  56. new BlendFactor(NativeBlendMode.One, NativeBlendMode.OneMinusSrcColor, true),
  57. //Erase
  58. new BlendFactor(NativeBlendMode.Zero, NativeBlendMode.OneMinusSrcAlpha),
  59. //Mask
  60. new BlendFactor(NativeBlendMode.Zero, NativeBlendMode.SrcAlpha),
  61. //Below
  62. new BlendFactor(NativeBlendMode.OneMinusDstAlpha, NativeBlendMode.DstAlpha),
  63. //Off
  64. new BlendFactor(NativeBlendMode.One, NativeBlendMode.Zero),
  65. //One_OneMinusSrcAlpha
  66. new BlendFactor(NativeBlendMode.One, NativeBlendMode.OneMinusSrcAlpha),
  67. //Custom1
  68. new BlendFactor(NativeBlendMode.SrcAlpha, NativeBlendMode.OneMinusSrcAlpha),
  69. //Custom2
  70. new BlendFactor(NativeBlendMode.SrcAlpha, NativeBlendMode.OneMinusSrcAlpha),
  71. //Custom3
  72. new BlendFactor(NativeBlendMode.SrcAlpha, NativeBlendMode.OneMinusSrcAlpha)
  73. };
  74. /// <summary>
  75. ///
  76. /// </summary>
  77. /// <param name="mat"></param>
  78. /// <param name="blendMode"></param>
  79. public static void Apply(Material mat, BlendMode blendMode)
  80. {
  81. BlendFactor bf = Factors[(int)blendMode];
  82. mat.SetFloat(ShaderConfig.ID_BlendSrcFactor, (float)bf.srcFactor);
  83. mat.SetFloat(ShaderConfig.ID_BlendDstFactor, (float)bf.dstFactor);
  84. if (bf.pma)
  85. mat.SetFloat(ShaderConfig.ID_ColorOption, 1);
  86. else
  87. mat.SetFloat(ShaderConfig.ID_ColorOption, 0);
  88. }
  89. /// <summary>
  90. ///
  91. /// </summary>
  92. /// <param name="blendMode"></param>
  93. /// <param name="srcFactor"></param>
  94. /// <param name="dstFactor"></param>
  95. public static void Override(BlendMode blendMode, NativeBlendMode srcFactor, NativeBlendMode dstFactor)
  96. {
  97. BlendFactor bf = Factors[(int)blendMode];
  98. bf.srcFactor = srcFactor;
  99. bf.dstFactor = dstFactor;
  100. }
  101. }
  102. }