HDRPMaterialMapper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using System;
  2. using TriLibCore.General;
  3. using TriLibCore.Mappers;
  4. using UnityEngine;
  5. using UnityEngine.Experimental.Rendering;
  6. namespace TriLibCore.HDRP.Mappers
  7. {
  8. /// <summary>Represents a Material Mapper that converts TriLib Materials into Unity HDRP Materials.</summary>
  9. [Serializable]
  10. [CreateAssetMenu(menuName = "TriLib/Mappers/Material/HDRP Material Mapper", fileName = "HDRPMaterialMapper")]
  11. public class HDRPMaterialMapper : MaterialMapper
  12. {
  13. private bool _isCompatible;
  14. #region Standard
  15. public override Material MaterialPreset => Resources.Load<Material>("Materials/HDRP/Standard/TriLibHDRP");
  16. public override Material CutoutMaterialPreset => Resources.Load<Material>("Materials/HDRP/Standard/TriLibHDRPAlphaCutout");
  17. public override Material TransparentMaterialPreset => Resources.Load<Material>("Materials/HDRP/Standard/TriLibHDRPAlpha");
  18. public override Material TransparentComposeMaterialPreset => Resources.Load<Material>("Materials/HDRP/Standard/TriLibHDRPAlpha");
  19. #endregion
  20. public override Material LoadingMaterial => Resources.Load<Material>("Materials/HDRP/TriLibHDRPLoading");
  21. public override bool IsCompatible(MaterialMapperContext materialMapperContext)
  22. {
  23. return _isCompatible;
  24. }
  25. private void Awake()
  26. {
  27. _isCompatible = TriLibSettings.GetBool("HDRPMaterialMapper");
  28. }
  29. public override void Map(MaterialMapperContext materialMapperContext)
  30. {
  31. materialMapperContext.VirtualMaterial = new HDRPVirtualMaterial();
  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. BuildMaterial(materialMapperContext);
  46. //materialMapperContext.AddPostProcessingActionToMainThread(BuildMaterial, materialMapperContext);
  47. //materialMapperContext.AddPostProcessingActionToMainThread(BuildHDRPMask, materialMapperContext);
  48. }
  49. private void CheckDiffuseMapTexture(MaterialMapperContext materialMapperContext)
  50. {
  51. var diffuseTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.DiffuseMap);
  52. var textureValue = materialMapperContext.Material.GetTextureValue(diffuseTexturePropertyName);
  53. LoadTextureWithCallbacks(materialMapperContext, TextureType.Diffuse, textureValue, CheckTextureOffsetAndScaling, ApplyDiffuseMapTexture);
  54. }
  55. private void ApplyDiffuseMapTexture(TextureLoadingContext textureLoadingContext)
  56. {
  57. if (textureLoadingContext.UnityTexture != null)
  58. {
  59. textureLoadingContext.Context.AddUsedTexture(textureLoadingContext.UnityTexture);
  60. }
  61. textureLoadingContext.MaterialMapperContext.VirtualMaterial.SetProperty("_BaseColorMap", textureLoadingContext.UnityTexture, GenericMaterialProperty.DiffuseMap);
  62. }
  63. private void CheckGlossinessValue(MaterialMapperContext materialMapperContext)
  64. {
  65. var value = materialMapperContext.Material.GetGenericFloatValueMultiplied(GenericMaterialProperty.Glossiness, materialMapperContext);
  66. materialMapperContext.VirtualMaterial.SetProperty("_Smoothness", value);
  67. }
  68. private void CheckMetallicValue(MaterialMapperContext materialMapperContext)
  69. {
  70. var value = materialMapperContext.Material.GetGenericFloatValueMultiplied(GenericMaterialProperty.Metallic, materialMapperContext);
  71. materialMapperContext.VirtualMaterial.SetProperty("_Metallic", value);
  72. }
  73. private void CheckEmissionMapTexture(MaterialMapperContext materialMapperContext)
  74. {
  75. var emissionTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.EmissionMap);
  76. var textureValue = materialMapperContext.Material.GetTextureValue(emissionTexturePropertyName);
  77. LoadTextureWithCallbacks(materialMapperContext, TextureType.Emission, textureValue, CheckTextureOffsetAndScaling, ApplyEmissionMapTexture);
  78. }
  79. private void ApplyEmissionMapTexture(TextureLoadingContext textureLoadingContext)
  80. {
  81. if (textureLoadingContext.UnityTexture == null && textureLoadingContext.MaterialMapperContext.VirtualMaterial.HasEmissionColor)
  82. {
  83. textureLoadingContext.OriginalUnityTexture = textureLoadingContext.UnityTexture = TriLibCore.Textures.DefaultTextures.White;
  84. }
  85. if (textureLoadingContext.UnityTexture != null)
  86. {
  87. textureLoadingContext.Context.AddUsedTexture(textureLoadingContext.UnityTexture);
  88. }
  89. textureLoadingContext.MaterialMapperContext.VirtualMaterial.SetProperty("_EmissiveColorMap", textureLoadingContext.UnityTexture, GenericMaterialProperty.EmissionMap);
  90. if (textureLoadingContext.UnityTexture)
  91. {
  92. textureLoadingContext.MaterialMapperContext.VirtualMaterial.EnableKeyword("_EMISSIVE_COLOR_MAP");
  93. textureLoadingContext.MaterialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
  94. textureLoadingContext.MaterialMapperContext.VirtualMaterial.SetProperty("_EmissiveIntensity", 1f);
  95. }
  96. else
  97. {
  98. textureLoadingContext.MaterialMapperContext.VirtualMaterial.DisableKeyword("_EMISSIVE_COLOR_MAP");
  99. textureLoadingContext.MaterialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.EmissiveIsBlack;
  100. }
  101. }
  102. private void CheckNormalMapTexture(MaterialMapperContext materialMapperContext)
  103. {
  104. var normalMapTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.NormalMap);
  105. var textureValue = materialMapperContext.Material.GetTextureValue(normalMapTexturePropertyName);
  106. LoadTextureWithCallbacks(materialMapperContext, TextureType.NormalMap, textureValue, CheckTextureOffsetAndScaling, ApplyNormalMapTexture);
  107. }
  108. private void ApplyNormalMapTexture(TextureLoadingContext textureLoadingContext)
  109. {
  110. if (textureLoadingContext.UnityTexture != null)
  111. {
  112. textureLoadingContext.Context.AddUsedTexture(textureLoadingContext.UnityTexture);
  113. }
  114. textureLoadingContext.MaterialMapperContext.VirtualMaterial.SetProperty("_NormalMap", textureLoadingContext.UnityTexture, GenericMaterialProperty.NormalMap);
  115. if (textureLoadingContext.UnityTexture != null)
  116. {
  117. textureLoadingContext.MaterialMapperContext.VirtualMaterial.EnableKeyword("_NORMALMAP");
  118. textureLoadingContext.MaterialMapperContext.VirtualMaterial.EnableKeyword("_NORMALMAP_TANGENT_SPACE");
  119. textureLoadingContext.MaterialMapperContext.VirtualMaterial.SetProperty("_NormalScale", 1f);
  120. }
  121. else
  122. {
  123. textureLoadingContext.MaterialMapperContext.VirtualMaterial.DisableKeyword("_NORMALMAP");
  124. textureLoadingContext.MaterialMapperContext.VirtualMaterial.DisableKeyword("_NORMALMAP_TANGENT_SPACE");
  125. }
  126. }
  127. private void CheckTransparencyMapTexture(MaterialMapperContext materialMapperContext)
  128. {
  129. materialMapperContext.VirtualMaterial.HasAlpha |= materialMapperContext.Material.UsesAlpha;
  130. var transparencyTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.TransparencyMap);
  131. var textureValue = materialMapperContext.Material.GetTextureValue(transparencyTexturePropertyName);
  132. LoadTextureWithCallbacks(materialMapperContext, TextureType.Transparency, textureValue, CheckTextureOffsetAndScaling);
  133. }
  134. private void CheckSpecularMapTexture(MaterialMapperContext materialMapperContext)
  135. {
  136. var specularTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.SpecularMap);
  137. var textureValue = materialMapperContext.Material.GetTextureValue(specularTexturePropertyName);
  138. LoadTextureWithCallbacks(materialMapperContext, TextureType.Specular, textureValue, CheckTextureOffsetAndScaling);
  139. }
  140. private void CheckOcclusionMapTexture(MaterialMapperContext materialMapperContext)
  141. {
  142. var occlusionMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.OcclusionMap);
  143. var textureValue = materialMapperContext.Material.GetTextureValue(occlusionMapTextureName);
  144. LoadTextureWithCallbacks(materialMapperContext, TextureType.Occlusion, textureValue, CheckTextureOffsetAndScaling, ApplyOcclusionMapTexture);
  145. }
  146. private void ApplyOcclusionMapTexture(TextureLoadingContext textureLoadingContext)
  147. {
  148. ((HDRPVirtualMaterial)textureLoadingContext.MaterialMapperContext.VirtualMaterial).OcclusionTexture = textureLoadingContext.UnityTexture;
  149. }
  150. private void CheckGlossinessMapTexture(MaterialMapperContext materialMapperContext)
  151. {
  152. var auxiliaryMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.GlossinessOrRoughnessMap);
  153. var textureValue = materialMapperContext.Material.GetTextureValue(auxiliaryMapTextureName);
  154. LoadTextureWithCallbacks(materialMapperContext, TextureType.GlossinessOrRoughness, textureValue, CheckTextureOffsetAndScaling);
  155. }
  156. private void CheckMetallicGlossMapTexture(MaterialMapperContext materialMapperContext)
  157. {
  158. var metallicGlossMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.MetallicMap);
  159. var textureValue = materialMapperContext.Material.GetTextureValue(metallicGlossMapTextureName);
  160. LoadTextureWithCallbacks(materialMapperContext, TextureType.Metalness, textureValue, CheckTextureOffsetAndScaling, ApplyMetallicGlossMapTexture);
  161. }
  162. private void ApplyMetallicGlossMapTexture(TextureLoadingContext textureLoadingContext)
  163. {
  164. ((HDRPVirtualMaterial)textureLoadingContext.MaterialMapperContext.VirtualMaterial).MetallicTexture = textureLoadingContext.UnityTexture;
  165. }
  166. private void CheckEmissionColor(MaterialMapperContext materialMapperContext)
  167. {
  168. var value = materialMapperContext.Material.GetGenericColorValueMultiplied(GenericMaterialProperty.EmissionColor, materialMapperContext);
  169. materialMapperContext.VirtualMaterial.SetProperty("_EmissiveColor", materialMapperContext.Context.Options.ApplyGammaCurveToMaterialColors ? value.gamma : value);
  170. materialMapperContext.VirtualMaterial.SetProperty("_EmissiveColorLDR", materialMapperContext.Context.Options.ApplyGammaCurveToMaterialColors ? value.gamma : value);
  171. if (value != Color.black)
  172. {
  173. materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
  174. materialMapperContext.VirtualMaterial.SetProperty("_EmissiveIntensity", 1f);
  175. materialMapperContext.VirtualMaterial.HasEmissionColor = true;
  176. }
  177. else
  178. {
  179. materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.EmissiveIsBlack;
  180. }
  181. }
  182. private void CheckDiffuseColor(MaterialMapperContext materialMapperContext)
  183. {
  184. var value = materialMapperContext.Material.GetGenericColorValueMultiplied(GenericMaterialProperty.DiffuseColor, materialMapperContext);
  185. value.a *= materialMapperContext.Material.GetGenericFloatValueMultiplied(GenericMaterialProperty.AlphaValue);
  186. materialMapperContext.VirtualMaterial.HasAlpha |= value.a < 1f;
  187. materialMapperContext.VirtualMaterial.SetProperty("_BaseColor", materialMapperContext.Context.Options.ApplyGammaCurveToMaterialColors ? value.gamma : value);
  188. materialMapperContext.VirtualMaterial.SetProperty("_Color", materialMapperContext.Context.Options.ApplyGammaCurveToMaterialColors ? value.gamma : value);
  189. }
  190. private void BuildHDRPMask(MaterialMapperContext materialMapperContext)
  191. {
  192. materialMapperContext.Completed = false;
  193. if (materialMapperContext.UnityMaterial == null)
  194. {
  195. return;
  196. }
  197. var hdrpVirtualMaterial = (HDRPVirtualMaterial)materialMapperContext.VirtualMaterial;
  198. var maskBaseTexture = hdrpVirtualMaterial.MetallicTexture ?? hdrpVirtualMaterial.OcclusionTexture ?? hdrpVirtualMaterial.DetailMaskTexture;
  199. if (maskBaseTexture == null)
  200. {
  201. if (materialMapperContext.Context.Options.UseMaterialKeywords)
  202. {
  203. materialMapperContext.UnityMaterial.DisableKeyword("_MASKMAP");
  204. }
  205. return;
  206. }
  207. var graphicsFormat = GraphicsFormat.R8G8B8A8_UNorm;
  208. var renderTexture = new RenderTexture(maskBaseTexture.width, maskBaseTexture.height, 0, graphicsFormat);
  209. renderTexture.name = $"{(string.IsNullOrWhiteSpace(maskBaseTexture.name) ? "Unnamed" : maskBaseTexture.name)}_Mask";
  210. renderTexture.useMipMap = false;
  211. renderTexture.autoGenerateMips = false;
  212. var material = new Material(Shader.Find("Hidden/TriLib/BuildHDRPMask"));
  213. if (hdrpVirtualMaterial.MetallicTexture != null)
  214. {
  215. material.SetTexture("_MetallicTex", hdrpVirtualMaterial.MetallicTexture);
  216. }
  217. if (hdrpVirtualMaterial.OcclusionTexture != null)
  218. {
  219. material.SetTexture("_OcclusionTex", hdrpVirtualMaterial.OcclusionTexture);
  220. }
  221. if (hdrpVirtualMaterial.DetailMaskTexture != null)
  222. {
  223. material.SetTexture("_DetailMaskTex", hdrpVirtualMaterial.DetailMaskTexture);
  224. }
  225. Graphics.Blit(null, renderTexture, material);
  226. if (renderTexture.useMipMap)
  227. {
  228. renderTexture.GenerateMips();
  229. }
  230. if (materialMapperContext.Context.Options.UseMaterialKeywords)
  231. {
  232. materialMapperContext.UnityMaterial.EnableKeyword("_MASKMAP");
  233. }
  234. materialMapperContext.UnityMaterial.SetTexture("_MaskMap", renderTexture);
  235. materialMapperContext.VirtualMaterial.TextureProperties.Add("_MaskMap", renderTexture);
  236. if (Application.isPlaying)
  237. {
  238. Destroy(material);
  239. }
  240. else
  241. {
  242. DestroyImmediate(material);
  243. }
  244. materialMapperContext.Completed = true;
  245. }
  246. public override string GetDiffuseTextureName(MaterialMapperContext materialMapperContext)
  247. {
  248. return "_BaseColorMap";
  249. }
  250. public override string GetGlossinessOrRoughnessTextureName(MaterialMapperContext materialMapperContext)
  251. {
  252. return "_MetallicGlossMap";
  253. }
  254. public override string GetDiffuseColorName(MaterialMapperContext materialMapperContext)
  255. {
  256. return "_BaseColor";
  257. }
  258. public override string GetEmissionColorName(MaterialMapperContext materialMapperContext)
  259. {
  260. return "_EmissionColor";
  261. }
  262. public override string GetGlossinessOrRoughnessName(MaterialMapperContext materialMapperContext)
  263. {
  264. return "_Smoothness";
  265. }
  266. public override string GetMetallicName(MaterialMapperContext materialMapperContext)
  267. {
  268. return "_Metallic";
  269. }
  270. public override string GetMetallicTextureName(MaterialMapperContext materialMapperContext)
  271. {
  272. return null;
  273. }
  274. }
  275. }