MegascansMatertialUtils.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System;
  5. namespace Quixel
  6. {
  7. public class MegascansMaterialUtils : MonoBehaviour
  8. {
  9. public static Material CreateMaterial(int shaderType, string matPath, bool isAlembic, int dispType, int texPack)
  10. {
  11. if ((shaderType == 0 || shaderType == 1) && isAlembic)
  12. {
  13. Debug.Log("Alembic files are not supported in LWRP/HDRP. Please change your export file format in Bridge or change your SRP in Unity.");
  14. return null;
  15. }
  16. try
  17. {
  18. string rp = matPath + ".mat";
  19. Material mat = (Material)AssetDatabase.LoadAssetAtPath(rp, typeof(Material));
  20. if (!mat)
  21. {
  22. mat = new Material(Shader.Find("Standard"));
  23. AssetDatabase.CreateAsset(mat, rp);
  24. AssetDatabase.Refresh();
  25. if (shaderType < 1)
  26. {
  27. mat.shader = Shader.Find("HDRP/Lit");
  28. #if UNITY_2018_1 || UNITY_2018_2
  29. mat.shader = Shader.Find("HDRenderPipeline/Lit");
  30. #endif
  31. AddHDRPValues(mat);
  32. mat.SetInt("_DisplacementMode", dispType);
  33. }
  34. if (shaderType > 0)
  35. {
  36. #if UNITY_2019_3 || UNITY_2019_4 || UNITY_2020 || UNITY_2021
  37. mat.shader = Shader.Find("Universal Render Pipeline/Lit");
  38. #else
  39. if (MegascansUtilities.isLegacy())
  40. mat.shader = Shader.Find("LightweightPipeline/Standard (Physically Based)");
  41. else
  42. mat.shader = Shader.Find("Lightweight Render Pipeline/Lit");
  43. #endif
  44. }
  45. if (shaderType > 1)
  46. {
  47. if (isAlembic)
  48. {
  49. mat.shader = Shader.Find("Alembic/Standard");
  50. if (texPack > 0)
  51. mat.shader = Shader.Find("Alembic/Standard (Specular setup)");
  52. }
  53. else
  54. {
  55. mat.shader = Shader.Find("Standard");
  56. if (texPack > 0)
  57. mat.shader = Shader.Find("Standard (Specular setup)");
  58. }
  59. }
  60. }
  61. return mat;
  62. }
  63. catch (Exception ex)
  64. {
  65. Debug.Log("MegascansMaterialUtils::CreateMaterial::Exception: " + ex.ToString());
  66. MegascansUtilities.HideProgressBar();
  67. return null;
  68. }
  69. }
  70. public static void AddHDRPValues(Material mat)
  71. {
  72. mat.renderQueue = 2225;
  73. mat.EnableKeyword("_DISABLE_SSR_TRANSPARENT");
  74. mat.SetShaderPassEnabled("DistortionVectors", false);
  75. mat.SetShaderPassEnabled("MOTIONVECTORS", false);
  76. mat.SetShaderPassEnabled("TransparentDepthPrepass", false);
  77. mat.SetShaderPassEnabled("TransparentDepthPostpass", false);
  78. mat.SetShaderPassEnabled("TransparentBackface", false);
  79. mat.SetShaderPassEnabled("RayTracingPrepass", false);
  80. mat.SetColor("_EmissionColor", Color.white);
  81. mat.SetFloat("_AlphaDstBlend", 0.0f);
  82. #if UNITY_2020
  83. mat.SetFloat("_DistortionBlurDstBlend", 1f);
  84. mat.SetFloat("_DistortionBlurSrcBlend", 1f);
  85. mat.SetFloat("_DistortionDstBlend", 1f);
  86. mat.SetFloat("_DistortionSrcBlend", 1f);
  87. mat.SetFloat("_ZTestModeDistortion", 4f);
  88. #endif
  89. mat.SetFloat("_StencilRefDepth", 8f);
  90. mat.SetFloat("_StencilWriteMask", 6f);
  91. mat.SetFloat("_StencilWriteMaskGBuffer", 14f);
  92. mat.SetFloat("_StencilWriteMaskMV", 40f);
  93. mat.SetFloat("_StencilRefMV", 40f);
  94. mat.SetFloat("_ZTestDepthEqualForOpaque", 3f);
  95. mat.SetFloat("_ZWrite", 1.0f);
  96. }
  97. public static void AddSSSSettings(Material mat, int shaderType)
  98. {
  99. mat.SetOverrideTag("RenderType", "Transparent");
  100. mat.SetInt("_MaterialID", 0);
  101. //mat.EnableKeyword("_MATERIAL_FEATURE_SUBSURFACE_SCATTERING");
  102. //mat.EnableKeyword("_MATERIAL_FEATURE_TRANSMISSION");
  103. mat.SetFloat("_SurfaceType", 1.0f);
  104. #if UNITY_2020
  105. mat.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
  106. mat.EnableKeyword("_ENABLE_FOG_ON_TRANSPARENT");
  107. #endif
  108. if (shaderType == 0)
  109. {
  110. mat.SetFloat("_StencilRef", 4f);
  111. mat.SetFloat("_ReceivesSSR", 1f);
  112. mat.SetFloat("_ReceivesSSRTransparent", 1f);
  113. mat.SetFloat("_StencilRefGBuffer", 14f); // Check with plants
  114. }
  115. }
  116. }
  117. }
  118. #endif