TextureBlenderFallback.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. namespace DigitalOpus.MB.Core
  6. {
  7. public class TextureBlenderFallback : TextureBlender
  8. {
  9. bool m_doTintColor = false;
  10. Color m_tintColor;
  11. Color m_defaultColor = Color.white;
  12. public bool DoesShaderNameMatch(string shaderName)
  13. {
  14. return true; //matches everything
  15. }
  16. public void OnBeforeTintTexture(Material sourceMat, string shaderTexturePropertyName)
  17. {
  18. if (shaderTexturePropertyName.Equals("_MainTex"))
  19. {
  20. m_doTintColor = true;
  21. m_tintColor = Color.white;
  22. if (sourceMat.HasProperty("_Color"))
  23. {
  24. m_tintColor = sourceMat.GetColor("_Color");
  25. }
  26. else if (sourceMat.HasProperty("_TintColor"))
  27. {
  28. m_tintColor = sourceMat.GetColor("_TintColor");
  29. }
  30. } else
  31. {
  32. m_doTintColor = false;
  33. }
  34. }
  35. public Color OnBlendTexturePixel(string shaderPropertyName, Color pixelColor)
  36. {
  37. if (m_doTintColor)
  38. {
  39. return new Color(pixelColor.r * m_tintColor.r, pixelColor.g * m_tintColor.g, pixelColor.b * m_tintColor.b, pixelColor.a * m_tintColor.a);
  40. }
  41. return pixelColor;
  42. }
  43. public bool NonTexturePropertiesAreEqual(Material a, Material b)
  44. {
  45. if (a.HasProperty("_Color"))
  46. {
  47. if (_compareColor(a, b, m_defaultColor, "_Color"))
  48. {
  49. return true;
  50. }
  51. //return false;
  52. }
  53. else if (a.HasProperty("_TintColor"))
  54. {
  55. if (_compareColor(a, b, m_defaultColor, "_TintColor"))
  56. {
  57. return true;
  58. }
  59. //return false;
  60. }
  61. return false;
  62. }
  63. public void SetNonTexturePropertyValuesOnResultMaterial(Material resultMaterial)
  64. {
  65. if (resultMaterial.HasProperty("_Color"))
  66. {
  67. resultMaterial.SetColor("_Color", m_defaultColor);
  68. }
  69. else if (resultMaterial.HasProperty("_TintColor"))
  70. {
  71. resultMaterial.SetColor("_TintColor", m_defaultColor);
  72. }
  73. }
  74. public Color GetColorIfNoTexture(Material mat, ShaderTextureProperty texProperty)
  75. {
  76. if (texProperty.isNormalMap)
  77. {
  78. return new Color(.5f, .5f, 1f);
  79. }
  80. else if (texProperty.name.Equals("_MainTex"))
  81. {
  82. if (mat != null && mat.HasProperty("_Color"))
  83. {
  84. try
  85. { //need try because can't garantee _Color is a color
  86. return mat.GetColor("_Color");
  87. }
  88. catch (Exception) { }
  89. }
  90. else if (mat != null && mat.HasProperty("_TintColor"))
  91. {
  92. try
  93. { //need try because can't garantee _TintColor is a color
  94. return mat.GetColor("_TintColor");
  95. }
  96. catch (Exception) { }
  97. }
  98. }
  99. else if (texProperty.name.Equals("_SpecGlossMap"))
  100. {
  101. if (mat != null && mat.HasProperty("_SpecColor"))
  102. {
  103. try
  104. { //need try because can't garantee _Color is a color
  105. Color c = mat.GetColor("_SpecColor");
  106. if (mat.HasProperty("_Glossiness"))
  107. {
  108. try
  109. {
  110. c.a = mat.GetFloat("_Glossiness");
  111. }
  112. catch (Exception) { }
  113. }
  114. Debug.LogWarning(c);
  115. return c;
  116. }
  117. catch (Exception) { }
  118. }
  119. }
  120. else if (texProperty.name.Equals("_MetallicGlossMap"))
  121. {
  122. if (mat != null && mat.HasProperty("_Metallic"))
  123. {
  124. try
  125. { //need try because can't garantee _Metallic is a float
  126. float v = mat.GetFloat("_Metallic");
  127. Color c = new Color(v, v, v);
  128. if (mat.HasProperty("_Glossiness"))
  129. {
  130. try
  131. {
  132. c.a = mat.GetFloat("_Glossiness");
  133. }
  134. catch (Exception) { }
  135. }
  136. return c;
  137. }
  138. catch (Exception) { }
  139. }
  140. }
  141. else if (texProperty.name.Equals("_ParallaxMap"))
  142. {
  143. return new Color(0f, 0f, 0f, 0f);
  144. }
  145. else if (texProperty.name.Equals("_OcclusionMap"))
  146. {
  147. return new Color(1f, 1f, 1f, 1f);
  148. }
  149. else if (texProperty.name.Equals("_EmissionMap"))
  150. {
  151. if (mat != null)
  152. {
  153. if (mat.HasProperty("_EmissionScaleUI"))
  154. {
  155. //Standard shader has weird behavior if EmissionMap has never
  156. //been set then no EmissionColorUI color picker. If has ever
  157. //been set then is EmissionColorUI color picker.
  158. if (mat.HasProperty("_EmissionColor") &&
  159. mat.HasProperty("_EmissionColorUI"))
  160. {
  161. try
  162. {
  163. Color c1 = mat.GetColor("_EmissionColor");
  164. Color c2 = mat.GetColor("_EmissionColorUI");
  165. float f = mat.GetFloat("_EmissionScaleUI");
  166. if (c1 == new Color(0f, 0f, 0f, 0f) &&
  167. c2 == new Color(1f, 1f, 1f, 1f))
  168. {
  169. //is virgin Emission values
  170. return new Color(f, f, f, f);
  171. }
  172. else { //non virgin Emission values
  173. return c2;
  174. }
  175. }
  176. catch (Exception) { }
  177. }
  178. else {
  179. try
  180. { //need try because can't garantee _Color is a color
  181. float f = mat.GetFloat("_EmissionScaleUI");
  182. return new Color(f, f, f, f);
  183. }
  184. catch (Exception) { }
  185. }
  186. }
  187. }
  188. }
  189. else if (texProperty.name.Equals("_DetailMask"))
  190. {
  191. return new Color(0f, 0f, 0f, 0f);
  192. }
  193. return new Color(1f, 1f, 1f, 0f);
  194. }
  195. public static bool _compareColor(Material a, Material b, Color defaultVal, string propertyName)
  196. {
  197. Color aColor = defaultVal;
  198. Color bColor = defaultVal;
  199. if (a.HasProperty(propertyName))
  200. {
  201. aColor = a.GetColor(propertyName);
  202. }
  203. if (b.HasProperty(propertyName))
  204. {
  205. bColor = b.GetColor(propertyName);
  206. }
  207. if (aColor != bColor)
  208. {
  209. return false;
  210. }
  211. return true;
  212. }
  213. public static bool _compareFloat(Material a, Material b, float defaultVal, string propertyName)
  214. {
  215. float aFloat = defaultVal;
  216. float bFloat = defaultVal;
  217. if (a.HasProperty(propertyName))
  218. {
  219. aFloat = a.GetFloat(propertyName);
  220. }
  221. if (b.HasProperty(propertyName))
  222. {
  223. bFloat = b.GetFloat(propertyName);
  224. }
  225. if (aFloat != bFloat)
  226. {
  227. return false;
  228. }
  229. return true;
  230. }
  231. }
  232. }