UniversalRPMaterialMapper.cs 15 KB

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