StandardMaterialMapper.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 Standard Materials.</summary>
  12. [Serializable]
  13. [CreateAssetMenu(menuName = "TriLib/Mappers/Material/Standard Material Mapper", fileName = "StandardMaterialMapper")]
  14. #if UNITY_EDITOR
  15. [InitializeOnLoad]
  16. #endif
  17. public class StandardMaterialMapper : MaterialMapper
  18. {
  19. static StandardMaterialMapper()
  20. {
  21. AddToRegisteredMappers();
  22. }
  23. [RuntimeInitializeOnLoadMethod]
  24. private static void AddToRegisteredMappers()
  25. {
  26. if (RegisteredMappers.Contains("StandardMaterialMapper"))
  27. {
  28. return;
  29. }
  30. RegisteredMappers.Add("StandardMaterialMapper");
  31. }
  32. public override Material MaterialPreset => Resources.Load<Material>("Materials/Standard/TriLibStandard");
  33. public override Material AlphaMaterialPreset => Resources.Load<Material>("Materials/Standard/TriLibStandardAlphaCutout");
  34. public override Material AlphaMaterialPreset2 => Resources.Load<Material>("Materials/Standard/TriLibStandardAlpha");
  35. public override Material SpecularMaterialPreset => Resources.Load<Material>("Materials/Standard/TriLibStandardSpecular");
  36. public override Material SpecularAlphaMaterialPreset => Resources.Load<Material>("Materials/Standard/TriLibStandardSpecularAlphaCutout");
  37. public override Material SpecularAlphaMaterialPreset2 => Resources.Load<Material>("Materials/Standard/TriLibStandardSpecularAlpha");
  38. public override Material LoadingMaterial => Resources.Load<Material>("Materials/Standard/TriLibStandardLoading");
  39. ///<inheritdoc />
  40. public override bool IsCompatible(MaterialMapperContext materialMapperContext)
  41. {
  42. return GraphicsSettingsUtils.IsUsingStandardPipeline;
  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("_MainTex", 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. }
  125. else
  126. {
  127. materialMapperContext.VirtualMaterial.DisableKeyword("_NORMALMAP");
  128. }
  129. CheckSpecularTexture(materialMapperContext);
  130. }
  131. private void CheckSpecularTexture(MaterialMapperContext materialMapperContext)
  132. {
  133. var specularTexturePropertyName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.SpecularTexture);
  134. if (materialMapperContext.Material.HasProperty(specularTexturePropertyName))
  135. {
  136. LoadTexture(materialMapperContext, TextureType.Specular, materialMapperContext.Material.GetTextureValue(specularTexturePropertyName), ApplySpecGlossMapTexture);
  137. }
  138. else
  139. {
  140. ApplySpecGlossMapTexture(materialMapperContext, TextureType.Specular, null);
  141. }
  142. }
  143. private void ApplySpecGlossMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  144. {
  145. materialMapperContext.VirtualMaterial.SetProperty("_SpecGlossMap", texture);
  146. if (texture != null)
  147. {
  148. materialMapperContext.VirtualMaterial.EnableKeyword("_SPECGLOSSMAP");
  149. }
  150. else
  151. {
  152. materialMapperContext.VirtualMaterial.DisableKeyword("_SPECGLOSSMAP");
  153. }
  154. CheckOcclusionMapTexture(materialMapperContext);
  155. }
  156. private void CheckOcclusionMapTexture(MaterialMapperContext materialMapperContext)
  157. {
  158. var occlusionMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.OcclusionTexture);
  159. if (materialMapperContext.Material.HasProperty(occlusionMapTextureName))
  160. {
  161. LoadTexture(materialMapperContext, TextureType.Occlusion, materialMapperContext.Material.GetTextureValue(occlusionMapTextureName), ApplyOcclusionMapTexture);
  162. }
  163. else
  164. {
  165. ApplyOcclusionMapTexture(materialMapperContext, TextureType.Occlusion, null);
  166. }
  167. }
  168. private void ApplyOcclusionMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  169. {
  170. materialMapperContext.VirtualMaterial.SetProperty("_OcclusionMap", texture);
  171. if (texture != null)
  172. {
  173. materialMapperContext.VirtualMaterial.EnableKeyword("_OCCLUSIONMAP");
  174. }
  175. else
  176. {
  177. materialMapperContext.VirtualMaterial.DisableKeyword("_OCCLUSIONMAP");
  178. }
  179. CheckParallaxMapTexture(materialMapperContext);
  180. }
  181. private void CheckParallaxMapTexture(MaterialMapperContext materialMapperContext)
  182. {
  183. var parallaxMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.ParallaxMap);
  184. if (materialMapperContext.Material.HasProperty(parallaxMapTextureName))
  185. {
  186. LoadTexture(materialMapperContext, TextureType.Parallax, materialMapperContext.Material.GetTextureValue(parallaxMapTextureName), ApplyParallaxMapTexture);
  187. }
  188. else
  189. {
  190. ApplyParallaxMapTexture(materialMapperContext, TextureType.Parallax, null);
  191. }
  192. }
  193. private void ApplyParallaxMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  194. {
  195. materialMapperContext.VirtualMaterial.SetProperty("_ParallaxMap", texture);
  196. if (texture)
  197. {
  198. materialMapperContext.VirtualMaterial.EnableKeyword("_PARALLAXMAP");
  199. }
  200. else
  201. {
  202. materialMapperContext.VirtualMaterial.DisableKeyword("_PARALLAXMAP");
  203. }
  204. CheckMetallicGlossMapTexture(materialMapperContext);
  205. }
  206. private void CheckMetallicGlossMapTexture(MaterialMapperContext materialMapperContext)
  207. {
  208. var metallicGlossMapTextureName = materialMapperContext.Material.GetGenericPropertyName(GenericMaterialProperty.MetallicGlossMap);
  209. if (materialMapperContext.Material.HasProperty(metallicGlossMapTextureName))
  210. {
  211. LoadTexture(materialMapperContext, TextureType.Metalness, materialMapperContext.Material.GetTextureValue(metallicGlossMapTextureName), ApplyMetallicGlossMapTexture);
  212. }
  213. else
  214. {
  215. ApplyMetallicGlossMapTexture(materialMapperContext, TextureType.Metalness, null);
  216. }
  217. }
  218. private void ApplyMetallicGlossMapTexture(MaterialMapperContext materialMapperContext, TextureType textureType, Texture texture)
  219. {
  220. materialMapperContext.VirtualMaterial.SetProperty("_MetallicGlossMap", texture);
  221. if (texture != null)
  222. {
  223. materialMapperContext.VirtualMaterial.EnableKeyword("_METALLICGLOSSMAP");
  224. }
  225. else
  226. {
  227. materialMapperContext.VirtualMaterial.DisableKeyword("_METALLICGLOSSMAP");
  228. }
  229. CheckEmissionColor(materialMapperContext);
  230. }
  231. private void CheckEmissionColor(MaterialMapperContext materialMapperContext)
  232. {
  233. var value = materialMapperContext.Material.GetGenericColorValue(GenericMaterialProperty.EmissionColor) * materialMapperContext.Material.GetGenericPropertyValueMultiplied(GenericMaterialProperty.EmissionColor, 1f);
  234. materialMapperContext.VirtualMaterial.SetProperty("_EmissionColor", value);
  235. if (value != Color.black)
  236. {
  237. materialMapperContext.VirtualMaterial.EnableKeyword("_EMISSION");
  238. materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
  239. }
  240. else
  241. {
  242. materialMapperContext.VirtualMaterial.DisableKeyword("_EMISSION");
  243. materialMapperContext.VirtualMaterial.GlobalIlluminationFlags = MaterialGlobalIlluminationFlags.EmissiveIsBlack;
  244. }
  245. CheckDiffuseColor(materialMapperContext);
  246. }
  247. private void CheckDiffuseColor(MaterialMapperContext materialMapperContext)
  248. {
  249. var value = materialMapperContext.Material.GetGenericColorValue(GenericMaterialProperty.DiffuseColor) * materialMapperContext.Material.GetGenericPropertyValueMultiplied(GenericMaterialProperty.DiffuseColor, 1f);
  250. value.a *= materialMapperContext.Material.GetGenericFloatValue(GenericMaterialProperty.AlphaValue);
  251. if (!materialMapperContext.VirtualMaterial.HasAlpha && value.a < 1f)
  252. {
  253. materialMapperContext.VirtualMaterial.HasAlpha = true;
  254. }
  255. materialMapperContext.VirtualMaterial.SetProperty("_Color", value);
  256. BuildMaterial(materialMapperContext);
  257. }
  258. }
  259. }