StandardMaterialMapper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using System;
  2. using TriLibCore.General;
  3. using UnityEngine;
  4. namespace TriLibCore.Mappers
  5. {
  6. /// <summary>Represents a Material Mapper that converts TriLib Materials into Unity Standard Materials.</summary>
  7. [Serializable]
  8. [CreateAssetMenu(menuName = "TriLib/Mappers/Material/Standard Material Mapper", fileName = "StandardMaterialMapper")]
  9. public class StandardMaterialMapper : MaterialMapper
  10. {
  11. private bool _isCompatible;
  12. #region Standard
  13. public override Material MaterialPreset => Resources.Load<Material>("Materials/Standard/Standard/TriLibStandard");
  14. public override Material CutoutMaterialPreset => Resources.Load<Material>("Materials/Standard/Standard/TriLibStandardAlphaCutout");
  15. public override Material TransparentComposeMaterialPreset => Resources.Load<Material>("Materials/Standard/Standard/TriLibStandardAlphaCompose");
  16. public override Material TransparentMaterialPreset => Resources.Load<Material>("Materials/Standard/Standard/TriLibStandardAlpha");
  17. public override Material MaterialPresetNoMetallicTexture => Resources.Load<Material>("Materials/Standard/Standard/TriLibStandardNoMetallicTexture");
  18. public override Material CutoutMaterialPresetNoMetallicTexture => Resources.Load<Material>("Materials/Standard/Standard/TriLibStandardAlphaCutoutNoMetallicTexture");
  19. public override Material TransparentMaterialPresetNoMetallicTexture => Resources.Load<Material>("Materials/Standard/Standard/TriLibStandardAlphaNoMetallicTexture");
  20. public override Material TransparentComposeMaterialPresetNoMetallicTexture => TransparentComposeMaterialPreset;
  21. #endregion
  22. public override Material LoadingMaterial => Resources.Load<Material>("Materials/Standard/TriLibStandardLoading");
  23. public override bool ExtractMetallicAndSmoothness
  24. {
  25. get
  26. {
  27. return false;
  28. }
  29. }
  30. public override bool IsCompatible(MaterialMapperContext materialMapperContext)
  31. {
  32. return _isCompatible;
  33. }
  34. private void Awake()
  35. {
  36. _isCompatible = TriLibSettings.GetBool("StandardMaterialMapper");
  37. }
  38. public override void Map(MaterialMapperContext materialMapperContext)
  39. {
  40. materialMapperContext.VirtualMaterial = new VirtualMaterial();
  41. CheckTransparencyMapTexture(materialMapperContext);
  42. CheckSpecularMapTexture(materialMapperContext);
  43. CheckDiffuseMapTexture(materialMapperContext);
  44. CheckDiffuseColor(materialMapperContext);
  45. CheckNormalMapTexture(materialMapperContext);
  46. CheckEmissionColor(materialMapperContext);
  47. CheckEmissionMapTexture(materialMapperContext);
  48. CheckOcclusionMapTexture(materialMapperContext);
  49. CheckGlossinessMapTexture(materialMapperContext);
  50. CheckGlossinessValue(materialMapperContext);
  51. CheckMetallicGlossMapTexture(materialMapperContext);
  52. CheckMetallicValue(materialMapperContext);
  53. BuildMaterial(materialMapperContext);
  54. //materialMapperContext.AddPostProcessingActionToMainThread(BuildMaterial, materialMapperContext);
  55. }
  56. private void CheckDiffuseMapTexture(MaterialMapperContext materialMapperContext)
  57. {
  58. var diffuseTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.DiffuseMap);
  59. var textureValue = materialMapperContext.Material.GetTextureValue(diffuseTexturePropertyName);
  60. LoadTextureWithCallbacks(materialMapperContext, TextureType.Diffuse, textureValue, CheckTextureOffsetAndScaling, ApplyDiffuseMapTexture);
  61. }
  62. private void ApplyDiffuseMapTexture(TextureLoadingContext textureLoadingContext)
  63. {
  64. if (textureLoadingContext.UnityTexture != null)
  65. {
  66. textureLoadingContext.Context.AddUsedTexture(textureLoadingContext.UnityTexture);
  67. }
  68. textureLoadingContext.MaterialMapperContext.VirtualMaterial.SetProperty("_MainTex", textureLoadingContext.UnityTexture, GenericMaterialProperty.DiffuseMap);
  69. }
  70. private void CheckGlossinessValue(MaterialMapperContext materialMapperContext)
  71. {
  72. var value = materialMapperContext.Material.GetGenericFloatValueMultiplied(GenericMaterialProperty.Glossiness, materialMapperContext);
  73. materialMapperContext.VirtualMaterial.SetProperty("_Glossiness", value);
  74. materialMapperContext.VirtualMaterial.SetProperty("_GlossMapScale", value);
  75. }
  76. private void CheckMetallicValue(MaterialMapperContext materialMapperContext)
  77. {
  78. var value = materialMapperContext.Material.GetGenericFloatValueMultiplied(GenericMaterialProperty.Metallic, materialMapperContext);
  79. materialMapperContext.VirtualMaterial.SetProperty("_Metallic", value);
  80. }
  81. private void CheckEmissionMapTexture(MaterialMapperContext materialMapperContext)
  82. {
  83. var emissionTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.EmissionMap);
  84. var textureValue = materialMapperContext.Material.GetTextureValue(emissionTexturePropertyName);
  85. LoadTextureWithCallbacks(materialMapperContext, TextureType.Emission, textureValue, CheckTextureOffsetAndScaling, ApplyEmissionMapTexture);
  86. }
  87. private void ApplyEmissionMapTexture(TextureLoadingContext textureLoadingContext)
  88. {
  89. if (textureLoadingContext.UnityTexture == null && textureLoadingContext.MaterialMapperContext.VirtualMaterial.HasEmissionColor)
  90. {
  91. textureLoadingContext.OriginalUnityTexture = textureLoadingContext.UnityTexture = TriLibCore.Textures.DefaultTextures.White;
  92. }
  93. if (textureLoadingContext.UnityTexture != null)
  94. {
  95. textureLoadingContext.Context.AddUsedTexture(textureLoadingContext.UnityTexture);
  96. }
  97. textureLoadingContext.MaterialMapperContext.VirtualMaterial.SetProperty("_EmissionMap", textureLoadingContext.UnityTexture, GenericMaterialProperty.EmissionMap);
  98. if (textureLoadingContext.UnityTexture != null)
  99. {
  100. textureLoadingContext.MaterialMapperContext.VirtualMaterial.EnableKeyword("_EMISSION");
  101. textureLoadingContext.MaterialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
  102. }
  103. else
  104. {
  105. textureLoadingContext.MaterialMapperContext.VirtualMaterial.DisableKeyword("_EMISSION");
  106. textureLoadingContext.MaterialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.EmissiveIsBlack;
  107. }
  108. }
  109. private void CheckNormalMapTexture(MaterialMapperContext materialMapperContext)
  110. {
  111. var normalMapTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.NormalMap);
  112. var textureValue = materialMapperContext.Material.GetTextureValue(normalMapTexturePropertyName);
  113. LoadTextureWithCallbacks(materialMapperContext, TextureType.NormalMap, textureValue, CheckTextureOffsetAndScaling, ApplyNormalMapTexture);
  114. }
  115. private void ApplyNormalMapTexture(TextureLoadingContext textureLoadingContext)
  116. {
  117. if (textureLoadingContext.UnityTexture != null)
  118. {
  119. textureLoadingContext.Context.AddUsedTexture(textureLoadingContext.UnityTexture);
  120. }
  121. textureLoadingContext.MaterialMapperContext.VirtualMaterial.SetProperty("_BumpMap", textureLoadingContext.UnityTexture, GenericMaterialProperty.NormalMap);
  122. if (textureLoadingContext.UnityTexture != null)
  123. {
  124. textureLoadingContext.MaterialMapperContext.VirtualMaterial.EnableKeyword("_NORMALMAP");
  125. textureLoadingContext.MaterialMapperContext.VirtualMaterial.SetProperty("_NormalScale", 1f);
  126. }
  127. else
  128. {
  129. textureLoadingContext.MaterialMapperContext.VirtualMaterial.DisableKeyword("_NORMALMAP");
  130. }
  131. }
  132. private void CheckTransparencyMapTexture(MaterialMapperContext materialMapperContext)
  133. {
  134. materialMapperContext.VirtualMaterial.HasAlpha |= materialMapperContext.Material.UsesAlpha;
  135. var transparencyTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.TransparencyMap);
  136. var textureValue = materialMapperContext.Material.GetTextureValue(transparencyTexturePropertyName);
  137. LoadTextureWithCallbacks(materialMapperContext, TextureType.Transparency, textureValue, CheckTextureOffsetAndScaling);
  138. }
  139. private void CheckSpecularMapTexture(MaterialMapperContext materialMapperContext)
  140. {
  141. var specularTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.SpecularMap);
  142. var textureValue = materialMapperContext.Material.GetTextureValue(specularTexturePropertyName);
  143. LoadTextureWithCallbacks(materialMapperContext, TextureType.Specular, textureValue, CheckTextureOffsetAndScaling);
  144. }
  145. private void CheckOcclusionMapTexture(MaterialMapperContext materialMapperContext)
  146. {
  147. var occlusionMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.OcclusionMap);
  148. var textureValue = materialMapperContext.Material.GetTextureValue(occlusionMapTextureName);
  149. LoadTextureWithCallbacks(materialMapperContext, TextureType.Occlusion, textureValue, CheckTextureOffsetAndScaling, ApplyOcclusionMapTexture);
  150. }
  151. private void ApplyOcclusionMapTexture(TextureLoadingContext textureLoadingContext)
  152. {
  153. if (textureLoadingContext.Texture != null)
  154. {
  155. textureLoadingContext.Context.AddUsedTexture(textureLoadingContext.UnityTexture);
  156. }
  157. textureLoadingContext.MaterialMapperContext.VirtualMaterial.SetProperty("_OcclusionMap", textureLoadingContext.UnityTexture, GenericMaterialProperty.OcclusionMap);
  158. if (textureLoadingContext.UnityTexture != null)
  159. {
  160. textureLoadingContext.MaterialMapperContext.VirtualMaterial.EnableKeyword("_OCCLUSIONMAP");
  161. }
  162. else
  163. {
  164. textureLoadingContext.MaterialMapperContext.VirtualMaterial.DisableKeyword("_OCCLUSIONMAP");
  165. }
  166. }
  167. private void CheckGlossinessMapTexture(MaterialMapperContext materialMapperContext)
  168. {
  169. var auxiliaryMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.GlossinessOrRoughnessMap);
  170. var textureValue = materialMapperContext.Material.GetTextureValue(auxiliaryMapTextureName);
  171. LoadTextureWithCallbacks(materialMapperContext, TextureType.GlossinessOrRoughness, textureValue, CheckTextureOffsetAndScaling);
  172. }
  173. private void CheckMetallicGlossMapTexture(MaterialMapperContext materialMapperContext)
  174. {
  175. var metallicGlossMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.MetallicMap);
  176. var textureValue = materialMapperContext.Material.GetTextureValue(metallicGlossMapTextureName);
  177. LoadTextureWithCallbacks(materialMapperContext, TextureType.Metalness, textureValue, CheckTextureOffsetAndScaling, ApplyMetallicGlossMapTexture);
  178. }
  179. private void ApplyMetallicGlossMapTexture(TextureLoadingContext textureLoadingContext)
  180. {
  181. if (textureLoadingContext.UnityTexture != null)
  182. {
  183. textureLoadingContext.Context.AddUsedTexture(textureLoadingContext.UnityTexture);
  184. }
  185. textureLoadingContext.MaterialMapperContext.VirtualMaterial.SetProperty("_MetallicGlossMap", textureLoadingContext.UnityTexture, GenericMaterialProperty.MetallicMap);
  186. if (textureLoadingContext.UnityTexture != null)
  187. {
  188. textureLoadingContext.MaterialMapperContext.VirtualMaterial.EnableKeyword("_METALLICGLOSSMAP");
  189. }
  190. else
  191. {
  192. textureLoadingContext.MaterialMapperContext.VirtualMaterial.DisableKeyword("_METALLICGLOSSMAP");
  193. }
  194. }
  195. private void CheckEmissionColor(MaterialMapperContext materialMapperContext)
  196. {
  197. var value = materialMapperContext.Material.GetGenericColorValueMultiplied(GenericMaterialProperty.EmissionColor, materialMapperContext);
  198. materialMapperContext.VirtualMaterial.SetProperty("_EmissionColor", materialMapperContext.Context.Options.ApplyGammaCurveToMaterialColors ? value.gamma : value);
  199. if (value != Color.black)
  200. {
  201. materialMapperContext.VirtualMaterial.EnableKeyword("_EMISSION");
  202. materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
  203. materialMapperContext.VirtualMaterial.SetProperty("_EmissiveIntensity", 1f);
  204. materialMapperContext.VirtualMaterial.HasEmissionColor = true;
  205. }
  206. else
  207. {
  208. materialMapperContext.VirtualMaterial.DisableKeyword("_EMISSION");
  209. materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.EmissiveIsBlack;
  210. }
  211. }
  212. private void CheckDiffuseColor(MaterialMapperContext materialMapperContext)
  213. {
  214. var value = materialMapperContext.Material.GetGenericColorValueMultiplied(GenericMaterialProperty.DiffuseColor, materialMapperContext);
  215. value.a *= materialMapperContext.Material.GetGenericFloatValueMultiplied(GenericMaterialProperty.AlphaValue);
  216. materialMapperContext.VirtualMaterial.HasAlpha |= value.a < 1f;
  217. materialMapperContext.VirtualMaterial.SetProperty("_Color", materialMapperContext.Context.Options.ApplyGammaCurveToMaterialColors ? value.gamma : value);
  218. }
  219. public override string GetDiffuseTextureName(MaterialMapperContext materialMapperContext)
  220. {
  221. return "_MainTex";
  222. }
  223. public override string GetGlossinessOrRoughnessTextureName(MaterialMapperContext materialMapperContext)
  224. {
  225. return "_MetallicGlossMap";
  226. }
  227. public override string GetDiffuseColorName(MaterialMapperContext materialMapperContext)
  228. {
  229. return "_Color";
  230. }
  231. public override string GetEmissionColorName(MaterialMapperContext materialMapperContext)
  232. {
  233. return "_EmissionColor";
  234. }
  235. public override string GetGlossinessOrRoughnessName(MaterialMapperContext materialMapperContext)
  236. {
  237. return "_GlossMapScale";
  238. }
  239. public override string GetMetallicName(MaterialMapperContext materialMapperContext)
  240. {
  241. return "_Metallic";
  242. }
  243. public override string GetMetallicTextureName(MaterialMapperContext materialMapperContext)
  244. {
  245. return null;
  246. }
  247. }
  248. }