TextureBlenderStandardMetallicRoughness.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. namespace DigitalOpus.MB.Core
  6. {
  7. public class TextureBlenderStandardMetallicRoughness : TextureBlender
  8. {
  9. static Color NeutralNormalMap = new Color(.5f, .5f, 1f);
  10. private enum Prop{
  11. doColor,
  12. doMetallic,
  13. doRoughness,
  14. doEmission,
  15. doBump,
  16. doNone,
  17. }
  18. // This is used to cache the non texture property values. If all non-texutre property values are the same for a property for all source textures
  19. // then the source value will be re-used
  20. TextureBlenderMaterialPropertyCacheHelper sourceMaterialPropertyCache = new TextureBlenderMaterialPropertyCacheHelper();
  21. // These are cached values read in OnBeforeTintTexture and used when blending pixels.
  22. Color m_tintColor;
  23. float m_roughness;
  24. float m_metallic;
  25. bool m_hasMetallicGlossMap;
  26. bool m_hasSpecGlossMap;
  27. float m_bumpScale;
  28. bool m_shaderDoesEmission;
  29. Color m_emissionColor;
  30. // This just makes things more efficient so we arn't doing a string comparison for each pixel.
  31. Prop propertyToDo = Prop.doNone;
  32. // These are the property values that will be assigned to the result material if
  33. // generating an atlas for those properties.
  34. Color m_generatingTintedAtlasColor = Color.white;
  35. float m_generatingTintedAtlasMetallic = 0f;
  36. float m_generatingTintedAtlasRoughness = .5f;
  37. float m_generatingTintedAtlasBumpScale = 1f;
  38. Color m_generatingTintedAtlasEmission = Color.white;
  39. // These are the default property values that will be assigned to the result materials if
  40. // none of the source materials have a value for these properties.
  41. Color m_notGeneratingAtlasDefaultColor = Color.white;
  42. float m_notGeneratingAtlasDefaultMetallic = 0;
  43. float m_notGeneratingAtlasDefaultGlossiness = .5f;
  44. Color m_notGeneratingAtlasDefaultEmisionColor = Color.black;
  45. public bool DoesShaderNameMatch(string shaderName)
  46. {
  47. return shaderName.Equals("Standard (Roughness setup)");
  48. }
  49. public void OnBeforeTintTexture(Material sourceMat, string shaderTexturePropertyName)
  50. {
  51. if (shaderTexturePropertyName.Equals("_MainTex"))
  52. {
  53. propertyToDo = Prop.doColor;
  54. if (sourceMat.HasProperty("_Color"))
  55. {
  56. m_tintColor = sourceMat.GetColor("_Color");
  57. } else
  58. {
  59. m_tintColor = m_generatingTintedAtlasColor;
  60. }
  61. } else if (shaderTexturePropertyName.Equals("_MetallicGlossMap"))
  62. {
  63. propertyToDo = Prop.doMetallic;
  64. m_metallic = m_generatingTintedAtlasMetallic;
  65. if (sourceMat.GetTexture("_MetallicGlossMap") != null)
  66. {
  67. m_hasMetallicGlossMap = true;
  68. } else
  69. {
  70. m_hasMetallicGlossMap = false;
  71. }
  72. if (sourceMat.HasProperty("_Metallic"))
  73. {
  74. m_metallic = sourceMat.GetFloat("_Metallic");
  75. } else
  76. {
  77. m_metallic = 0f;
  78. }
  79. }
  80. else if (shaderTexturePropertyName.Equals("_SpecGlossMap"))
  81. {
  82. propertyToDo = Prop.doRoughness;
  83. m_roughness = m_generatingTintedAtlasRoughness;
  84. if (sourceMat.GetTexture("_SpecGlossMap") != null)
  85. {
  86. m_hasSpecGlossMap = true;
  87. }
  88. else
  89. {
  90. m_hasSpecGlossMap = false;
  91. }
  92. if (sourceMat.HasProperty("_Glossiness"))
  93. {
  94. m_roughness = sourceMat.GetFloat("_Glossiness");
  95. }
  96. else
  97. {
  98. m_roughness = 1f;
  99. }
  100. }
  101. else if (shaderTexturePropertyName.Equals("_BumpMap"))
  102. {
  103. propertyToDo = Prop.doBump;
  104. if (sourceMat.HasProperty(shaderTexturePropertyName))
  105. {
  106. if (sourceMat.HasProperty("_BumpScale"))
  107. m_bumpScale = sourceMat.GetFloat("_BumpScale");
  108. }
  109. else
  110. {
  111. m_bumpScale = m_generatingTintedAtlasBumpScale;
  112. }
  113. } else if (shaderTexturePropertyName.Equals("_EmissionMap"))
  114. {
  115. propertyToDo = Prop.doEmission;
  116. m_shaderDoesEmission = sourceMat.IsKeywordEnabled("_EMISSION");
  117. if (sourceMat.HasProperty("_EmissionColor"))
  118. {
  119. m_emissionColor = sourceMat.GetColor("_EmissionColor");
  120. }
  121. else
  122. {
  123. m_emissionColor = m_notGeneratingAtlasDefaultEmisionColor;
  124. }
  125. } else
  126. {
  127. propertyToDo = Prop.doNone;
  128. }
  129. }
  130. public Color OnBlendTexturePixel(string propertyToDoshaderPropertyName, Color pixelColor)
  131. {
  132. if (propertyToDo == Prop.doColor)
  133. {
  134. return new Color(pixelColor.r * m_tintColor.r, pixelColor.g * m_tintColor.g, pixelColor.b * m_tintColor.b, pixelColor.a * m_tintColor.a);
  135. } else if (propertyToDo == Prop.doMetallic)
  136. {
  137. if (m_hasMetallicGlossMap)
  138. {
  139. return pixelColor;
  140. }
  141. else
  142. {
  143. return new Color(m_metallic, 0, 0, m_roughness);
  144. }
  145. } else if (propertyToDo == Prop.doRoughness)
  146. {
  147. if (m_hasSpecGlossMap)
  148. {
  149. return pixelColor;
  150. } else
  151. {
  152. return new Color(m_roughness, 0, 0, 0);
  153. }
  154. } else if (propertyToDo == Prop.doBump)
  155. {
  156. return Color.Lerp(NeutralNormalMap, pixelColor, m_bumpScale);
  157. } else if (propertyToDo == Prop.doEmission)
  158. {
  159. if (m_shaderDoesEmission)
  160. {
  161. return new Color(pixelColor.r * m_emissionColor.r, pixelColor.g * m_emissionColor.g, pixelColor.b * m_emissionColor.b, pixelColor.a * m_emissionColor.a);
  162. }
  163. else
  164. {
  165. return Color.black;
  166. }
  167. }
  168. return pixelColor;
  169. }
  170. public bool NonTexturePropertiesAreEqual(Material a, Material b)
  171. {
  172. if (!TextureBlenderFallback._compareColor(a, b, m_notGeneratingAtlasDefaultColor, "_Color"))
  173. {
  174. return false;
  175. }
  176. bool aHasMetallicTex = a.HasProperty("_MetallicGlossMap") && a.GetTexture("_MetallicGlossMap") != null;
  177. bool bHasMetallicTex = b.HasProperty("_MetallicGlossMap") && b.GetTexture("_MetallicGlossMap") != null;
  178. if (!aHasMetallicTex && !bHasMetallicTex)
  179. {
  180. if (!TextureBlenderFallback._compareFloat(a, b, m_notGeneratingAtlasDefaultMetallic, "_Metallic"))
  181. {
  182. return false;
  183. }
  184. }
  185. else
  186. {
  187. return false;
  188. }
  189. bool aHasSpecTex = a.HasProperty("_SpecGlossMap") && a.GetTexture("_SpecGlossMap") != null;
  190. bool bHasSpecTex = b.HasProperty("_SpecGlossMap") && b.GetTexture("_SpecGlossMap") != null;
  191. if (!aHasSpecTex && !bHasSpecTex)
  192. {
  193. if (!TextureBlenderFallback._compareFloat(a, b, m_generatingTintedAtlasRoughness, "_Glossiness"))
  194. {
  195. return false;
  196. }
  197. }
  198. else
  199. {
  200. return false;
  201. }
  202. if (!TextureBlenderFallback._compareFloat(a, b, m_generatingTintedAtlasBumpScale, "_bumpScale"))
  203. {
  204. return false;
  205. }
  206. if (!TextureBlenderFallback._compareFloat(a, b, m_generatingTintedAtlasRoughness, "_Glossiness"))
  207. {
  208. return false;
  209. }
  210. if (a.IsKeywordEnabled("_EMISSION") != b.IsKeywordEnabled("_EMISSION"))
  211. {
  212. return false;
  213. }
  214. if (a.IsKeywordEnabled("_EMISSION"))
  215. {
  216. if (!TextureBlenderFallback._compareColor(a, b, m_generatingTintedAtlasEmission, "_EmissionColor"))
  217. {
  218. return false;
  219. }
  220. }
  221. return true;
  222. }
  223. public void SetNonTexturePropertyValuesOnResultMaterial(Material resultMaterial)
  224. {
  225. if (resultMaterial.GetTexture("_MainTex") != null)
  226. {
  227. resultMaterial.SetColor("_Color", m_generatingTintedAtlasColor);
  228. }
  229. else
  230. {
  231. resultMaterial.SetColor("_Color", (Color)sourceMaterialPropertyCache.GetValueIfAllSourceAreTheSameOrDefault("_Color", m_notGeneratingAtlasDefaultColor));
  232. }
  233. if (resultMaterial.GetTexture("_MetallicGlossMap") != null)
  234. {
  235. resultMaterial.SetFloat("_Metallic", m_generatingTintedAtlasMetallic);
  236. }
  237. else
  238. {
  239. resultMaterial.SetFloat("_Metallic", (float)sourceMaterialPropertyCache.GetValueIfAllSourceAreTheSameOrDefault("_Metallic", m_notGeneratingAtlasDefaultMetallic));
  240. }
  241. if (resultMaterial.GetTexture("_SpecGlossMap") != null)
  242. {
  243. }
  244. else
  245. {
  246. resultMaterial.SetFloat("_Glossiness", (float)sourceMaterialPropertyCache.GetValueIfAllSourceAreTheSameOrDefault("_Glossiness", m_notGeneratingAtlasDefaultGlossiness));
  247. }
  248. if (resultMaterial.GetTexture("_BumpMap") != null)
  249. {
  250. resultMaterial.SetFloat("_BumpScale", m_generatingTintedAtlasBumpScale);
  251. }
  252. else
  253. {
  254. resultMaterial.SetFloat("_BumpScale", m_generatingTintedAtlasBumpScale);
  255. }
  256. if (resultMaterial.GetTexture("_EmissionMap") != null)
  257. {
  258. resultMaterial.EnableKeyword("_EMISSION");
  259. resultMaterial.SetColor("_EmissionColor", Color.white);
  260. }
  261. else
  262. {
  263. resultMaterial.DisableKeyword("_EMISSION");
  264. resultMaterial.SetColor("_EmissionColor", (Color)sourceMaterialPropertyCache.GetValueIfAllSourceAreTheSameOrDefault("_EmissionColor", m_notGeneratingAtlasDefaultEmisionColor));
  265. }
  266. }
  267. public Color GetColorIfNoTexture(Material mat, ShaderTextureProperty texPropertyName)
  268. {
  269. if (texPropertyName.name.Equals("_BumpMap"))
  270. {
  271. return new Color(.5f, .5f, 1f);
  272. }
  273. else if (texPropertyName.name.Equals("_MainTex"))
  274. {
  275. if (mat != null && mat.HasProperty("_Color"))
  276. {
  277. try
  278. { //need try because can't garantee _Color is a color
  279. Color c = mat.GetColor("_Color");
  280. sourceMaterialPropertyCache.CacheMaterialProperty(mat, "_Color", c);
  281. }
  282. catch (Exception) { }
  283. return Color.white;
  284. }
  285. }
  286. else if (texPropertyName.name.Equals("_MetallicGlossMap"))
  287. {
  288. if (mat != null && mat.HasProperty("_Metallic"))
  289. {
  290. try
  291. { //need try because can't garantee _Metallic is a float
  292. float v = mat.GetFloat("_Metallic");
  293. sourceMaterialPropertyCache.CacheMaterialProperty(mat, "_Metallic", v);
  294. }
  295. catch (Exception) { }
  296. return new Color(0f, 0f, 0f, .5f);
  297. } else
  298. {
  299. return new Color(0f,0f,0f,.5f);
  300. }
  301. }
  302. else if (texPropertyName.name.Equals("_SpecGlossMap"))
  303. {
  304. bool success = false;
  305. try
  306. { //need try because can't garantee _Color is a color
  307. Color c = new Color(0f, 0f, 0f, .5f);
  308. if (mat.HasProperty("_Glossiness"))
  309. {
  310. try
  311. {
  312. success = true;
  313. c.a = mat.GetFloat("_Glossiness");
  314. }
  315. catch (Exception) { }
  316. }
  317. sourceMaterialPropertyCache.CacheMaterialProperty(mat, "_Glossiness", c.a);
  318. return new Color(0f, 0f, 0f, .5f);
  319. }
  320. catch (Exception) { }
  321. if (!success)
  322. {
  323. return new Color(0f, 0f, 0f, .5f);
  324. }
  325. }
  326. else if (texPropertyName.name.Equals("_ParallaxMap"))
  327. {
  328. return new Color(0f, 0f, 0f, 0f);
  329. }
  330. else if (texPropertyName.name.Equals("_OcclusionMap"))
  331. {
  332. return new Color(1f, 1f, 1f, 1f);
  333. }
  334. else if (texPropertyName.name.Equals("_EmissionMap"))
  335. {
  336. if (mat != null)
  337. {
  338. if (mat.IsKeywordEnabled("_EMISSION"))
  339. {
  340. if (mat.HasProperty("_EmissionColor"))
  341. {
  342. try
  343. {
  344. Color c = mat.GetColor("_EmissionColor");
  345. sourceMaterialPropertyCache.CacheMaterialProperty(mat, "_EmissionColor", c);
  346. }
  347. catch (Exception) { }
  348. }
  349. else
  350. {
  351. return Color.black;
  352. }
  353. }
  354. else
  355. {
  356. return Color.black;
  357. }
  358. }
  359. }
  360. else if (texPropertyName.name.Equals("_DetailMask"))
  361. {
  362. return new Color(0f, 0f, 0f, 0f);
  363. }
  364. return new Color(1f, 1f, 1f, 0f);
  365. }
  366. }
  367. }