HDRPMaterialMapper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using System;
  2. using TriLibCore.General;
  3. using TriLibCore.Utils;
  4. using UnityEngine;
  5. using UnityEngine.Experimental.Rendering;
  6. using UnityEngine.Rendering;
  7. #if UNITY_EDITOR
  8. using UnityEditor;
  9. #endif
  10. namespace TriLibCore.Mappers
  11. {
  12. /// <summary>Represents a Material Mapper that converts TriLib Materials into Unity HDRP Materials.</summary>
  13. [Serializable]
  14. [CreateAssetMenu(menuName = "TriLib/Mappers/Material/HDRP Material Mapper", fileName = "HDRPMaterialMapper")]
  15. #if UNITY_EDITOR
  16. [InitializeOnLoad]
  17. #endif
  18. public class HDRPMaterialMapper : MaterialMapper
  19. {
  20. static HDRPMaterialMapper()
  21. {
  22. AddToRegisteredMappers();
  23. }
  24. [RuntimeInitializeOnLoadMethod]
  25. private static void AddToRegisteredMappers()
  26. {
  27. if (RegisteredMappers.Contains("HDRPMaterialMapper"))
  28. {
  29. return;
  30. }
  31. RegisteredMappers.Add("HDRPMaterialMapper");
  32. }
  33. public override Material MaterialPreset => Resources.Load<Material>("Materials/HDRP/TriLibHDRP");
  34. public override Material AlphaMaterialPreset => Resources.Load<Material>("Materials/HDRP/TriLibHDRPAlphaCutout");
  35. public override Material AlphaMaterialPreset2 => Resources.Load<Material>("Materials/HDRP/TriLibHDRPAlpha");
  36. public override Material SpecularMaterialPreset => Resources.Load<Material>("Materials/HDRP/TriLibHDRP");
  37. public override Material SpecularAlphaMaterialPreset => Resources.Load<Material>("Materials/HDRP/TriLibHDRPAlphaCutout");
  38. public override Material SpecularAlphaMaterialPreset2 => Resources.Load<Material>("Materials/HDRP/TriLibHDRPAlpha");
  39. public override Material LoadingMaterial => Resources.Load<Material>("Materials/HDRP/TriLibHDRPLoading");
  40. ///<inheritdoc />
  41. public override bool IsCompatible(MaterialMapperContext materialMapperContext)
  42. {
  43. return GraphicsSettingsUtils.IsUsingHDRPPipeline;
  44. }
  45. ///<inheritdoc />
  46. public override void Map(MaterialMapperContext materialMapperContext)
  47. {
  48. materialMapperContext.VirtualMaterial = new HDRPVirtualMaterial();
  49. CheckDiffuseMapTexture(materialMapperContext);
  50. }
  51. private void CheckDiffuseMapTexture(MaterialMapperContext materialMapperContext)
  52. {
  53. var diffuseTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.DiffuseTexture);
  54. if (materialMapperContext.Material.HasProperty(diffuseTexturePropertyName))
  55. {
  56. LoadTexture(materialMapperContext, TextureType.Diffuse, materialMapperContext.Material.GetTextureValue(diffuseTexturePropertyName), ApplyDiffuseMapTexture);
  57. }
  58. else
  59. {
  60. ApplyDiffuseMapTexture(materialMapperContext, TextureType.Diffuse, null);
  61. }
  62. }
  63. private void ApplyDiffuseMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  64. {
  65. materialMapperContext.VirtualMaterial.SetProperty("_BaseColorMap", texture);
  66. CheckGlossinessValue(materialMapperContext);
  67. }
  68. private void CheckGlossinessValue(MaterialMapperContext materialMapperContext)
  69. {
  70. var value = materialMapperContext.Material.GetGenericPropertyValueMultiplied(GenericMaterialProperty.Glossiness, materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.Glossiness));
  71. materialMapperContext.VirtualMaterial.SetProperty("_Smoothness", value);
  72. CheckMetallicValue(materialMapperContext);
  73. }
  74. private void CheckMetallicValue(MaterialMapperContext materialMapperContext)
  75. {
  76. var value = materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.Metallic);
  77. materialMapperContext.VirtualMaterial.SetProperty("_Metallic", value);
  78. CheckEmissionMapTexture(materialMapperContext);
  79. }
  80. private void CheckEmissionMapTexture(MaterialMapperContext materialMapperContext)
  81. {
  82. var emissionTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.EmissionTexture);
  83. if (materialMapperContext.Material.HasProperty(emissionTexturePropertyName))
  84. {
  85. LoadTexture(materialMapperContext, TextureType.Emission, materialMapperContext.Material.GetTextureValue(emissionTexturePropertyName), ApplyEmissionMapTexture);
  86. }
  87. else
  88. {
  89. ApplyEmissionMapTexture(materialMapperContext, TextureType.Emission, null);
  90. }
  91. }
  92. private void ApplyEmissionMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  93. {
  94. materialMapperContext.VirtualMaterial.SetProperty("_EmissiveColorMap", texture);
  95. if (texture)
  96. {
  97. materialMapperContext.VirtualMaterial.EnableKeyword("_EMISSIVE_COLOR_MAP");
  98. materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
  99. materialMapperContext.VirtualMaterial.SetProperty("_EmissiveIntensity", materialMapperContext.Material.GetGenericPropertyValueMultiplied(GenericMaterialProperty.EmissionColor, 1f));
  100. }
  101. else
  102. {
  103. materialMapperContext.VirtualMaterial.DisableKeyword("_EMISSIVE_COLOR_MAP");
  104. materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.EmissiveIsBlack;
  105. }
  106. CheckNormalMapTexture(materialMapperContext);
  107. }
  108. private void CheckNormalMapTexture(MaterialMapperContext materialMapperContext)
  109. {
  110. var normalMapTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.NormalTexture);
  111. if (materialMapperContext.Material.HasProperty(normalMapTexturePropertyName))
  112. {
  113. LoadTexture(materialMapperContext, TextureType.NormalMap, materialMapperContext.Material.GetTextureValue(normalMapTexturePropertyName), ApplyNormalMapTexture);
  114. }
  115. else
  116. {
  117. ApplyNormalMapTexture(materialMapperContext, TextureType.NormalMap, null);
  118. }
  119. }
  120. private void ApplyNormalMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  121. {
  122. materialMapperContext.VirtualMaterial.SetProperty("_NormalMap", texture);
  123. if (texture != null)
  124. {
  125. materialMapperContext.VirtualMaterial.EnableKeyword("_NORMALMAP");
  126. materialMapperContext.VirtualMaterial.EnableKeyword("_NORMALMAP_TANGENT_SPACE");
  127. materialMapperContext.VirtualMaterial.SetProperty("_NormalScale", materialMapperContext.Material.GetGenericPropertyValueMultiplied(GenericMaterialProperty.NormalTexture, 1f));
  128. }
  129. else
  130. {
  131. materialMapperContext.VirtualMaterial.DisableKeyword("_NORMALMAP");
  132. materialMapperContext.VirtualMaterial.DisableKeyword("_NORMALMAP_TANGENT_SPACE");
  133. }
  134. CheckSpecularTexture(materialMapperContext);
  135. }
  136. private void CheckSpecularTexture(MaterialMapperContext materialMapperContext)
  137. {
  138. var specularTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.SpecularTexture);
  139. if (materialMapperContext.Material.HasProperty(specularTexturePropertyName))
  140. {
  141. LoadTexture(materialMapperContext, TextureType.Specular, materialMapperContext.Material.GetTextureValue(specularTexturePropertyName), ApplySpecGlossMapTexture);
  142. }
  143. else
  144. {
  145. ApplySpecGlossMapTexture(materialMapperContext, TextureType.Specular, null);
  146. }
  147. }
  148. private void ApplySpecGlossMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  149. {
  150. ((HDRPVirtualMaterial)materialMapperContext.VirtualMaterial).SmoothnessTexture = texture;
  151. CheckOcclusionMapTexture(materialMapperContext);
  152. }
  153. private void CheckOcclusionMapTexture(MaterialMapperContext materialMapperContext)
  154. {
  155. var occlusionMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.OcclusionTexture);
  156. if (materialMapperContext.Material.HasProperty(occlusionMapTextureName))
  157. {
  158. LoadTexture(materialMapperContext, TextureType.Occlusion, materialMapperContext.Material.GetTextureValue(occlusionMapTextureName), ApplyOcclusionMapTexture);
  159. }
  160. else
  161. {
  162. ApplyOcclusionMapTexture(materialMapperContext, TextureType.Occlusion, null);
  163. }
  164. }
  165. private void ApplyOcclusionMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  166. {
  167. ((HDRPVirtualMaterial)materialMapperContext.VirtualMaterial).OcclusionTexture = texture;
  168. CheckParallaxMapTexture(materialMapperContext);
  169. }
  170. private void CheckParallaxMapTexture(MaterialMapperContext materialMapperContext)
  171. {
  172. var parallaxMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.ParallaxMap);
  173. if (materialMapperContext.Material.HasProperty(parallaxMapTextureName))
  174. {
  175. LoadTexture(materialMapperContext, TextureType.Parallax, materialMapperContext.Material.GetTextureValue(parallaxMapTextureName), ApplyParallaxMapTexture);
  176. }
  177. else
  178. {
  179. ApplyParallaxMapTexture(materialMapperContext, TextureType.Parallax, null);
  180. }
  181. }
  182. private void ApplyParallaxMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  183. {
  184. CheckMetallicGlossMapTexture(materialMapperContext);
  185. }
  186. private void CheckMetallicGlossMapTexture(MaterialMapperContext materialMapperContext)
  187. {
  188. var metallicGlossMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.MetallicGlossMap);
  189. if (materialMapperContext.Material.HasProperty(metallicGlossMapTextureName))
  190. {
  191. LoadTexture(materialMapperContext, TextureType.Metalness, materialMapperContext.Material.GetTextureValue(metallicGlossMapTextureName), ApplyMetallicGlossMapTexture);
  192. }
  193. else
  194. {
  195. ApplyMetallicGlossMapTexture(materialMapperContext, TextureType.Metalness, null);
  196. }
  197. }
  198. private void ApplyMetallicGlossMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  199. {
  200. ((HDRPVirtualMaterial)materialMapperContext.VirtualMaterial).MetallicTexture = texture;
  201. CheckEmissionColor(materialMapperContext);
  202. }
  203. private void CheckEmissionColor(MaterialMapperContext materialMapperContext)
  204. {
  205. var value = materialMapperContext.Material.GetGenericColorValue(GenericMaterialProperty.EmissionColor);
  206. materialMapperContext.VirtualMaterial.SetProperty("_EmissiveColor", value);
  207. if (value != Color.black)
  208. {
  209. materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
  210. materialMapperContext.VirtualMaterial.SetProperty("_EmissiveIntensity", materialMapperContext.Material.GetGenericPropertyValueMultiplied(GenericMaterialProperty.EmissionColor, 1f));
  211. }
  212. else
  213. {
  214. materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.EmissiveIsBlack;
  215. }
  216. CheckDiffuseColor(materialMapperContext);
  217. }
  218. private void CheckDiffuseColor(MaterialMapperContext materialMapperContext)
  219. {
  220. var value = materialMapperContext.Material.GetGenericColorValue(GenericMaterialProperty.DiffuseColor) * materialMapperContext.Material.GetGenericPropertyValueMultiplied(GenericMaterialProperty.DiffuseColor, 1f);
  221. value.a *= materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.AlphaValue);
  222. if (!materialMapperContext.VirtualMaterial.HasAlpha && value.a < 1f)
  223. {
  224. materialMapperContext.VirtualMaterial.HasAlpha = true;
  225. }
  226. materialMapperContext.VirtualMaterial.SetProperty("_BaseColor", value);
  227. BuildMaterial(materialMapperContext);
  228. BuildHDRPMask(materialMapperContext);
  229. }
  230. private void BuildHDRPMask(MaterialMapperContext materialMapperContext)
  231. {
  232. if (materialMapperContext.UnityMaterial == null)
  233. {
  234. return;
  235. }
  236. var hdrpVirtualMaterial = (HDRPVirtualMaterial)materialMapperContext.VirtualMaterial;
  237. var maskBaseTexture = hdrpVirtualMaterial.MetallicTexture ?? hdrpVirtualMaterial.OcclusionTexture ?? hdrpVirtualMaterial.DetailMaskTexture ?? hdrpVirtualMaterial.SmoothnessTexture;
  238. if (maskBaseTexture == null)
  239. {
  240. materialMapperContext.UnityMaterial.DisableKeyword("_MASKMAP");
  241. return;
  242. }
  243. var material = new Material(Shader.Find("Hidden/TriLib/BuildHDRPMask"));
  244. material.SetTexture("_MetallicTex", hdrpVirtualMaterial.MetallicTexture);
  245. material.SetTexture("_OcclusionTex", hdrpVirtualMaterial.OcclusionTexture);
  246. material.SetTexture("_DetailMaskTex", hdrpVirtualMaterial.DetailMaskTexture);
  247. material.SetTexture("_SmoothnessTex", hdrpVirtualMaterial.SmoothnessTexture);
  248. var graphicsFormat = materialMapperContext.Context.Options.Enforce16BitsTextures ? GraphicsFormat.R16G16B16A16_SFloat : GraphicsFormat.R8G8B8A8_SRGB;
  249. var renderTexture = RenderTexture.GetTemporary(maskBaseTexture.width, maskBaseTexture.height, 0, graphicsFormat);
  250. renderTexture.useMipMap = true;
  251. renderTexture.autoGenerateMips = false;
  252. Graphics.Blit(null, renderTexture, material);
  253. var texture2D = new Texture2D(
  254. maskBaseTexture.width,
  255. maskBaseTexture.height,
  256. graphicsFormat,
  257. materialMapperContext.Context.Options.GenerateMipmaps ? TextureCreationFlags.MipChain : TextureCreationFlags.None)
  258. {
  259. name = "Mask"
  260. };
  261. Graphics.CopyTexture(renderTexture, texture2D);
  262. materialMapperContext.UnityMaterial.EnableKeyword("_MASKMAP");
  263. materialMapperContext.UnityMaterial.SetTexture("_MaskMap", texture2D);
  264. materialMapperContext.Context.Allocations.Add(texture2D);
  265. Graphics.SetRenderTarget(null);
  266. renderTexture.Release();
  267. if (Application.isPlaying)
  268. {
  269. Destroy(material);
  270. }
  271. else
  272. {
  273. DestroyImmediate(material);
  274. }
  275. }
  276. }
  277. }