PhysicalMaterial3DsMaxPreprocessor.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using UnityEditor.AssetImporters;
  2. using UnityEditor.Experimental;
  3. using UnityEngine;
  4. using UnityEditor.Experimental.AssetImporters;
  5. namespace UnityEditor.Rendering.Universal
  6. {
  7. class PhysicalMaterial3DsMaxPreprocessor : AssetPostprocessor
  8. {
  9. static readonly uint k_Version = 1;
  10. static readonly int k_Order = 4;
  11. static readonly string k_ShaderPath = "Packages/com.unity.render-pipelines.universal/Runtime/Materials/PhysicalMaterial3DsMax/PhysicalMaterial3DsMax.ShaderGraph";
  12. static readonly string k_ShaderTransparentPath = "Packages/com.unity.render-pipelines.universal/Runtime/Materials/PhysicalMaterial3DsMax/PhysicalMaterial3DsMaxTransparent.ShaderGraph";
  13. public override uint GetVersion()
  14. {
  15. return k_Version;
  16. }
  17. public override int GetPostprocessOrder()
  18. {
  19. return k_Order;
  20. }
  21. static bool Is3DsMaxPhysicalMaterial(MaterialDescription description)
  22. {
  23. float classIdA;
  24. float classIdB;
  25. description.TryGetProperty("ClassIDa", out classIdA);
  26. description.TryGetProperty("ClassIDb", out classIdB);
  27. return classIdA == 1030429932 && classIdB == -559038463;
  28. }
  29. public void OnPreprocessMaterialDescription(MaterialDescription description, Material material, AnimationClip[] clips)
  30. {
  31. if (Is3DsMaxPhysicalMaterial(description))
  32. {
  33. CreateFrom3DsPhysicalMaterial(description, material, clips);
  34. }
  35. }
  36. void CreateFrom3DsPhysicalMaterial(MaterialDescription description, Material material, AnimationClip[] clips)
  37. {
  38. float floatProperty;
  39. Vector4 vectorProperty;
  40. TexturePropertyDescription textureProperty;
  41. Shader shader;
  42. description.TryGetProperty("transparency", out floatProperty);
  43. bool hasTransparencyMap =
  44. description.TryGetProperty("transparency_map", out textureProperty);
  45. if (floatProperty > 0.0f || hasTransparencyMap)
  46. {
  47. shader = AssetDatabase.LoadAssetAtPath<Shader>(k_ShaderTransparentPath);
  48. if (shader == null)
  49. return;
  50. material.shader = shader;
  51. if (hasTransparencyMap)
  52. {
  53. material.SetTexture("_TRANSPARENCY_MAP", textureProperty.texture);
  54. material.SetFloat("_TRANSPARENCY", 1.0f);
  55. }
  56. else
  57. {
  58. material.SetFloat("_TRANSPARENCY", floatProperty);
  59. }
  60. }
  61. else
  62. {
  63. shader = AssetDatabase.LoadAssetAtPath<Shader>(k_ShaderPath);
  64. if (shader == null)
  65. return;
  66. material.shader = shader;
  67. }
  68. foreach (var clip in clips)
  69. {
  70. clip.ClearCurves();
  71. }
  72. RemapPropertyFloat(description, material, "base_weight", "_BASE_COLOR_WEIGHT");
  73. if (description.TryGetProperty("base_color_map", out textureProperty))
  74. {
  75. SetMaterialTextureProperty("_BASE_COLOR_MAP", material, textureProperty);
  76. }
  77. else if (description.TryGetProperty("base_color", out vectorProperty))
  78. {
  79. if (QualitySettings.activeColorSpace == ColorSpace.Gamma)
  80. {
  81. vectorProperty.x = Mathf.LinearToGammaSpace(vectorProperty.x);
  82. vectorProperty.y = Mathf.LinearToGammaSpace(vectorProperty.y);
  83. vectorProperty.z = Mathf.LinearToGammaSpace(vectorProperty.z);
  84. vectorProperty.w = Mathf.LinearToGammaSpace(vectorProperty.w);
  85. }
  86. material.SetColor("_BASE_COLOR", vectorProperty);
  87. }
  88. RemapPropertyFloat(description, material, "reflectivity", "_REFLECTIONS_WEIGHT");
  89. RemapPropertyTextureOrColor(description, material, "refl_color", "_REFLECTIONS_COLOR");
  90. RemapPropertyTextureOrFloat(description, material, "metalness", "_METALNESS");
  91. RemapPropertyTextureOrFloat(description, material, "roughness", "_REFLECTIONS_ROUGHNESS");
  92. RemapPropertyTextureOrFloat(description, material, "trans_ior", "_REFLECTIONS_IOR");
  93. RemapPropertyFloat(description, material, "emission", "_EMISSION_WEIGHT");
  94. RemapPropertyTextureOrColor(description, material, "emit_color", "_EMISSION_COLOR");
  95. RemapPropertyFloat(description, material, "bump_map_amt", "_BUMP_MAP_STRENGTH");
  96. RemapPropertyTexture(description, material, "bump_map", "_BUMP_MAP");
  97. }
  98. static void SetMaterialTextureProperty(string propertyName, Material material,
  99. TexturePropertyDescription textureProperty)
  100. {
  101. material.SetTexture(propertyName, textureProperty.texture);
  102. material.SetTextureOffset(propertyName, textureProperty.offset);
  103. material.SetTextureScale(propertyName, textureProperty.scale);
  104. }
  105. static void RemapPropertyFloat(MaterialDescription description, Material material, string inPropName,
  106. string outPropName)
  107. {
  108. if (description.TryGetProperty(inPropName, out float floatProperty))
  109. {
  110. material.SetFloat(outPropName, floatProperty);
  111. }
  112. }
  113. static void RemapPropertyTexture(MaterialDescription description, Material material, string inPropName,
  114. string outPropName)
  115. {
  116. if (description.TryGetProperty(inPropName, out TexturePropertyDescription textureProperty))
  117. {
  118. material.SetTexture(outPropName, textureProperty.texture);
  119. }
  120. }
  121. static void RemapPropertyTextureOrColor(MaterialDescription description, Material material,
  122. string inPropName, string outPropName)
  123. {
  124. if (description.TryGetProperty(inPropName + "_map", out TexturePropertyDescription textureProperty))
  125. {
  126. material.SetTexture(outPropName + "_MAP", textureProperty.texture);
  127. material.SetColor(outPropName, Color.white);
  128. }
  129. else if(description.TryGetProperty(inPropName, out Vector4 color))
  130. {
  131. material.SetColor(outPropName, color);
  132. }
  133. }
  134. static void RemapPropertyTextureOrFloat(MaterialDescription description, Material material,
  135. string inPropName, string outPropName)
  136. {
  137. if (description.TryGetProperty(inPropName + "_map", out TexturePropertyDescription textureProperty))
  138. {
  139. material.SetTexture(outPropName + "_MAP", textureProperty.texture);
  140. material.SetFloat(outPropName, 1.0f);
  141. }
  142. else if(description.TryGetProperty(inPropName, out float floatProperty))
  143. {
  144. material.SetFloat(outPropName, floatProperty);
  145. }
  146. }
  147. }
  148. }