UniversalRPMaterialMapper.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System;
  2. using TriLibCore.General;
  3. using TriLibCore.Utils;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. #if UNITY_EDITOR
  7. using UnityEditor;
  8. #endif
  9. namespace TriLibCore.Mappers
  10. {
  11. /// <summary>Represents a Material Mapper that converts TriLib Materials into Unity UniversalRP Materials.</summary>
  12. [Serializable]
  13. [CreateAssetMenu(menuName = "TriLib/Mappers/Material/Universal RP Material Mapper", fileName = "UniversalRPMaterialMapper")]
  14. #if UNITY_EDITOR
  15. [InitializeOnLoad]
  16. #endif
  17. public class UniversalRPMaterialMapper : MaterialMapper
  18. {
  19. static UniversalRPMaterialMapper()
  20. {
  21. AddToRegisteredMappers();
  22. }
  23. [RuntimeInitializeOnLoadMethod]
  24. private static void AddToRegisteredMappers()
  25. {
  26. if (RegisteredMappers.Contains("UniversalRPMaterialMapper"))
  27. {
  28. return;
  29. }
  30. RegisteredMappers.Add("UniversalRPMaterialMapper");
  31. }
  32. public override Material MaterialPreset => Resources.Load<Material>("Materials/UniversalRP/TriLibUniversalRP");
  33. public override Material AlphaMaterialPreset => Resources.Load<Material>("Materials/UniversalRP/TriLibUniversalRPAlphaCutout");
  34. public override Material AlphaMaterialPreset2 => Resources.Load<Material>("Materials/UniversalRP/TriLibUniversalRPAlpha");
  35. public override Material SpecularMaterialPreset => Resources.Load<Material>("Materials/UniversalRP/TriLibUniversalRPSpecular");
  36. public override Material SpecularAlphaMaterialPreset => Resources.Load<Material>("Materials/UniversalRP/TriLibUniversalRPAlphaCutoutSpecular");
  37. public override Material SpecularAlphaMaterialPreset2 => Resources.Load<Material>("Materials/UniversalRP/TriLibUniversalRPAlphaSpecular");
  38. public override Material LoadingMaterial => Resources.Load<Material>("Materials/UniversalRP/TriLibUniversalRPLoading");
  39. ///<inheritdoc />
  40. public override bool IsCompatible(MaterialMapperContext materialMapperContext)
  41. {
  42. return GraphicsSettingsUtils.IsUsingUniversalPipeline;
  43. }
  44. ///<inheritdoc />
  45. public override void Map(MaterialMapperContext materialMapperContext)
  46. {
  47. materialMapperContext.VirtualMaterial = new VirtualMaterial();
  48. CheckDiffuseMapTexture(materialMapperContext);
  49. }
  50. private void CheckDiffuseMapTexture(MaterialMapperContext materialMapperContext)
  51. {
  52. var diffuseTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.DiffuseTexture);
  53. if (materialMapperContext.Material.HasProperty(diffuseTexturePropertyName))
  54. {
  55. LoadTexture(materialMapperContext, TextureType.Diffuse, materialMapperContext.Material.GetTextureValue(diffuseTexturePropertyName), ApplyDiffuseMapTexture);
  56. }
  57. else
  58. {
  59. ApplyDiffuseMapTexture(materialMapperContext, TextureType.Diffuse, null);
  60. }
  61. }
  62. private void ApplyDiffuseMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  63. {
  64. materialMapperContext.VirtualMaterial.SetProperty("_BaseMap", texture);
  65. CheckGlossinessValue(materialMapperContext);
  66. }
  67. private void CheckGlossinessValue(MaterialMapperContext materialMapperContext)
  68. {
  69. var value = materialMapperContext.Material.GetGenericPropertyValueMultiplied(GenericMaterialProperty.Glossiness, materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.Glossiness));
  70. materialMapperContext.VirtualMaterial.SetProperty("_Glossiness", value);
  71. CheckMetallicValue(materialMapperContext);
  72. }
  73. private void CheckMetallicValue(MaterialMapperContext materialMapperContext)
  74. {
  75. var value = materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.Metallic);
  76. materialMapperContext.VirtualMaterial.SetProperty("_Metallic", value);
  77. CheckEmissionMapTexture(materialMapperContext);
  78. }
  79. private void CheckEmissionMapTexture(MaterialMapperContext materialMapperContext)
  80. {
  81. var emissionTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.EmissionTexture);
  82. if (materialMapperContext.Material.HasProperty(emissionTexturePropertyName))
  83. {
  84. LoadTexture(materialMapperContext, TextureType.Emission, materialMapperContext.Material.GetTextureValue(emissionTexturePropertyName), ApplyEmissionMapTexture);
  85. }
  86. else
  87. {
  88. ApplyEmissionMapTexture(materialMapperContext, TextureType.Emission, null);
  89. }
  90. }
  91. private void ApplyEmissionMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  92. {
  93. materialMapperContext.VirtualMaterial.SetProperty("_EmissionMap", texture);
  94. if (texture)
  95. {
  96. materialMapperContext.VirtualMaterial.EnableKeyword("_EMISSION");
  97. materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
  98. }
  99. else
  100. {
  101. materialMapperContext.VirtualMaterial.DisableKeyword("_EMISSION");
  102. materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.EmissiveIsBlack;
  103. }
  104. CheckNormalMapTexture(materialMapperContext);
  105. }
  106. private void CheckNormalMapTexture(MaterialMapperContext materialMapperContext)
  107. {
  108. var normalMapTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.NormalTexture);
  109. if (materialMapperContext.Material.HasProperty(normalMapTexturePropertyName))
  110. {
  111. LoadTexture(materialMapperContext, TextureType.NormalMap, materialMapperContext.Material.GetTextureValue(normalMapTexturePropertyName), ApplyNormalMapTexture);
  112. }
  113. else
  114. {
  115. ApplyNormalMapTexture(materialMapperContext, TextureType.NormalMap, null);
  116. }
  117. }
  118. private void ApplyNormalMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  119. {
  120. materialMapperContext.VirtualMaterial.SetProperty("_BumpMap", texture);
  121. if (texture != null)
  122. {
  123. materialMapperContext.VirtualMaterial.EnableKeyword("_NORMALMAP");
  124. materialMapperContext.VirtualMaterial.SetProperty("_NormalScale", materialMapperContext.Material.GetGenericPropertyValueMultiplied(GenericMaterialProperty.NormalTexture, 1f));
  125. }
  126. else
  127. {
  128. materialMapperContext.VirtualMaterial.DisableKeyword("_NORMALMAP");
  129. }
  130. CheckSpecularTexture(materialMapperContext);
  131. }
  132. private void CheckSpecularTexture(MaterialMapperContext materialMapperContext)
  133. {
  134. var specularTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.SpecularTexture);
  135. if (materialMapperContext.Material.HasProperty(specularTexturePropertyName))
  136. {
  137. LoadTexture(materialMapperContext, TextureType.Specular, materialMapperContext.Material.GetTextureValue(specularTexturePropertyName), ApplySpecGlossMapTexture);
  138. }
  139. else
  140. {
  141. ApplySpecGlossMapTexture(materialMapperContext, TextureType.Specular, null);
  142. }
  143. }
  144. private void ApplySpecGlossMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  145. {
  146. materialMapperContext.VirtualMaterial.SetProperty("_SpecGlossMap", texture);
  147. if (texture != null)
  148. {
  149. materialMapperContext.VirtualMaterial.EnableKeyword("_METALLICSPECGLOSSMAP");
  150. }
  151. else
  152. {
  153. materialMapperContext.VirtualMaterial.DisableKeyword("_METALLICSPECGLOSSMAP");
  154. }
  155. CheckOcclusionMapTexture(materialMapperContext);
  156. }
  157. private void CheckOcclusionMapTexture(MaterialMapperContext materialMapperContext)
  158. {
  159. var occlusionMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.OcclusionTexture);
  160. if (materialMapperContext.Material.HasProperty(occlusionMapTextureName))
  161. {
  162. LoadTexture(materialMapperContext, TextureType.Occlusion, materialMapperContext.Material.GetTextureValue(occlusionMapTextureName), ApplyOcclusionMapTexture);
  163. }
  164. else
  165. {
  166. ApplyOcclusionMapTexture(materialMapperContext, TextureType.Occlusion, null);
  167. }
  168. }
  169. private void ApplyOcclusionMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  170. {
  171. materialMapperContext.VirtualMaterial.SetProperty("_OcclusionMap", texture);
  172. if (texture != null)
  173. {
  174. materialMapperContext.VirtualMaterial.EnableKeyword("_OCCLUSIONMAP");
  175. }
  176. else
  177. {
  178. materialMapperContext.VirtualMaterial.DisableKeyword("_OCCLUSIONMAP");
  179. }
  180. CheckParallaxMapTexture(materialMapperContext);
  181. }
  182. private void CheckParallaxMapTexture(MaterialMapperContext materialMapperContext)
  183. {
  184. var parallaxMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.ParallaxMap);
  185. if (materialMapperContext.Material.HasProperty(parallaxMapTextureName))
  186. {
  187. LoadTexture(materialMapperContext, TextureType.Parallax, materialMapperContext.Material.GetTextureValue(parallaxMapTextureName), ApplyParallaxMapTexture);
  188. }
  189. else
  190. {
  191. ApplyParallaxMapTexture(materialMapperContext, TextureType.Parallax, null);
  192. }
  193. }
  194. private void ApplyParallaxMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  195. {
  196. materialMapperContext.VirtualMaterial.SetProperty("_ParallaxMap", texture);
  197. if (texture)
  198. {
  199. materialMapperContext.VirtualMaterial.EnableKeyword("_PARALLAXMAP");
  200. }
  201. else
  202. {
  203. materialMapperContext.VirtualMaterial.DisableKeyword("_PARALLAXMAP");
  204. }
  205. CheckMetallicGlossMapTexture(materialMapperContext);
  206. }
  207. private void CheckMetallicGlossMapTexture(MaterialMapperContext materialMapperContext)
  208. {
  209. var metallicGlossMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.MetallicGlossMap);
  210. if (materialMapperContext.Material.HasProperty(metallicGlossMapTextureName))
  211. {
  212. LoadTexture(materialMapperContext, TextureType.Metalness, materialMapperContext.Material.GetTextureValue(metallicGlossMapTextureName), ApplyMetallicGlossMapTexture);
  213. }
  214. else
  215. {
  216. ApplyMetallicGlossMapTexture(materialMapperContext, TextureType.Metalness, null);
  217. }
  218. }
  219. private void ApplyMetallicGlossMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  220. {
  221. materialMapperContext.VirtualMaterial.SetProperty("_MetallicGlossMap", texture);
  222. if (texture != null)
  223. {
  224. materialMapperContext.VirtualMaterial.EnableKeyword("_METALLICGLOSSMAP");
  225. }
  226. else
  227. {
  228. materialMapperContext.VirtualMaterial.DisableKeyword("_METALLICGLOSSMAP");
  229. }
  230. CheckEmissionColor(materialMapperContext);
  231. }
  232. private void CheckEmissionColor(MaterialMapperContext materialMapperContext)
  233. {
  234. var value = materialMapperContext.Material.GetGenericColorValue(GenericMaterialProperty.EmissionColor) * materialMapperContext.Material.GetGenericPropertyValueMultiplied(GenericMaterialProperty.EmissionColor, 1f);
  235. materialMapperContext.VirtualMaterial.SetProperty("_EmissionColor", value);
  236. if (value != Color.black)
  237. {
  238. materialMapperContext.VirtualMaterial.EnableKeyword("_EMISSION");
  239. materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
  240. }
  241. else
  242. {
  243. materialMapperContext.VirtualMaterial.DisableKeyword("_EMISSION");
  244. materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.EmissiveIsBlack;
  245. }
  246. CheckDiffuseColor(materialMapperContext);
  247. }
  248. private void CheckDiffuseColor(MaterialMapperContext materialMapperContext)
  249. {
  250. var value = materialMapperContext.Material.GetGenericColorValue(GenericMaterialProperty.DiffuseColor) * materialMapperContext.Material.GetGenericPropertyValueMultiplied(GenericMaterialProperty.DiffuseColor, 1f);
  251. value.a *= materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.AlphaValue);
  252. if (!materialMapperContext.VirtualMaterial.HasAlpha && value.a < 1f)
  253. {
  254. materialMapperContext.VirtualMaterial.HasAlpha = true;
  255. }
  256. materialMapperContext.VirtualMaterial.SetProperty("_BaseColor", value);
  257. BuildMaterial(materialMapperContext);
  258. }
  259. }
  260. }