PhysicalMaterial3DsMaxPreprocessor.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using UnityEditor.Experimental;
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4. using UnityEngine.Rendering.Universal;
  5. #if UNITY_2020_2_OR_NEWER
  6. using UnityEditor.AssetImporters;
  7. #else
  8. using UnityEditor.Experimental.AssetImporters;
  9. #endif
  10. namespace UnityEditor.Rendering.Universal
  11. {
  12. class PhysicalMaterial3DsMaxPreprocessor : AssetPostprocessor
  13. {
  14. static readonly uint k_Version = 1;
  15. static readonly int k_Order = 4;
  16. static readonly string k_ShaderPath = "Packages/com.unity.render-pipelines.universal/Runtime/Materials/PhysicalMaterial3DsMax/PhysicalMaterial3DsMax.shadergraph";
  17. static readonly string k_ShaderTransparentPath = "Packages/com.unity.render-pipelines.universal/Runtime/Materials/PhysicalMaterial3DsMax/PhysicalMaterial3DsMaxTransparent.shadergraph";
  18. public override uint GetVersion()
  19. {
  20. return k_Version;
  21. }
  22. public override int GetPostprocessOrder()
  23. {
  24. return k_Order;
  25. }
  26. static bool Is3DsMaxPhysicalMaterial(MaterialDescription description)
  27. {
  28. float classIdA;
  29. float classIdB;
  30. string originalMtl;
  31. description.TryGetProperty("ClassIDa", out classIdA);
  32. description.TryGetProperty("ClassIDb", out classIdB);
  33. description.TryGetProperty("ORIGINAL_MTL", out originalMtl);
  34. return classIdA == 1030429932 && classIdB == -559038463 || originalMtl == "PHYSICAL_MTL";
  35. }
  36. static bool Is3DsMaxSimplifiedPhysicalMaterial(MaterialDescription description)
  37. {
  38. float classIdA;
  39. float classIdB;
  40. float useGlossiness;
  41. description.TryGetProperty("ClassIDa", out classIdA);
  42. description.TryGetProperty("ClassIDb", out classIdB);
  43. description.TryGetProperty("useGlossiness", out useGlossiness);
  44. return classIdA == -804315648 && classIdB == -1099438848 && useGlossiness == 2.0f;
  45. }
  46. public void OnPreprocessMaterialDescription(MaterialDescription description, Material material, AnimationClip[] clips)
  47. {
  48. var pipelineAsset = GraphicsSettings.currentRenderPipeline;
  49. if (!pipelineAsset || pipelineAsset.GetType() != typeof(UniversalRenderPipelineAsset))
  50. return;
  51. if (Is3DsMaxPhysicalMaterial(description))
  52. {
  53. CreateFrom3DsPhysicalMaterial(description, material, clips);
  54. }
  55. else if (Is3DsMaxSimplifiedPhysicalMaterial(description))
  56. {
  57. CreateFrom3DsSimplifiedPhysicalMaterial(description, material, clips);
  58. }
  59. }
  60. void CreateFrom3DsSimplifiedPhysicalMaterial(MaterialDescription description, Material material, AnimationClip[] clips)
  61. {
  62. float floatProperty;
  63. Vector4 vectorProperty;
  64. TexturePropertyDescription textureProperty;
  65. description.TryGetProperty("basecolor", out vectorProperty);
  66. bool hasTransparencyScalar = vectorProperty.w !=1.0f;
  67. var hasTransparencyMap = description.TryGetProperty("opacity_map", out textureProperty);
  68. bool isTransparent = hasTransparencyMap | hasTransparencyScalar;
  69. Shader shader;
  70. if (isTransparent)
  71. shader = GraphicsSettings.currentRenderPipeline.autodeskInteractiveTransparentShader;
  72. else
  73. shader = GraphicsSettings.currentRenderPipeline.autodeskInteractiveShader;
  74. if (shader == null)
  75. return;
  76. material.shader = shader;
  77. foreach (var clip in clips)
  78. {
  79. clip.ClearCurves();
  80. }
  81. if (hasTransparencyMap)
  82. {
  83. material.SetFloat("_UseOpacityMap", 1.0f);
  84. material.SetTexture("_OpacityMap", textureProperty.texture);
  85. }
  86. else if (hasTransparencyScalar)
  87. {
  88. material.SetFloat("_Opacity", vectorProperty.w);
  89. }
  90. if (description.TryGetProperty("basecolor", out vectorProperty))
  91. material.SetColor("_Color", vectorProperty);
  92. if (description.TryGetProperty("emit_color", out vectorProperty))
  93. material.SetColor("_EmissionColor", vectorProperty);
  94. if (description.TryGetProperty("roughness", out floatProperty))
  95. material.SetFloat("_Glossiness", floatProperty);
  96. if (description.TryGetProperty("metalness", out floatProperty))
  97. material.SetFloat("_Metallic", floatProperty);
  98. if (description.TryGetProperty("base_color_map", out textureProperty))
  99. {
  100. material.SetTexture("_MainTex", textureProperty.texture);
  101. material.SetFloat("_UseColorMap", 1.0f);
  102. material.SetColor("_UvTiling", new Vector4(textureProperty.scale.x, textureProperty.scale.y, 0.0f, 0.0f));
  103. material.SetColor("_UvOffset", new Vector4(textureProperty.offset.x, textureProperty.offset.y, 0.0f, 0.0f));
  104. }
  105. else
  106. {
  107. material.SetFloat("_UseColorMap", 0.0f);
  108. }
  109. if (description.TryGetProperty("norm_map", out textureProperty))
  110. {
  111. material.SetTexture("_BumpMap", textureProperty.texture);
  112. material.SetFloat("_UseNormalMap", 1.0f);
  113. }
  114. else
  115. {
  116. material.SetFloat("_UseNormalMap", 0.0f);
  117. }
  118. if (description.TryGetProperty("roughness_map", out textureProperty))
  119. {
  120. material.SetTexture("_SpecGlossMap", textureProperty.texture);
  121. material.SetFloat("_UseRoughnessMap", 1.0f);
  122. }
  123. else
  124. {
  125. material.SetFloat("_UseRoughnessMap", 0.0f);
  126. }
  127. if (description.TryGetProperty("metalness_map", out textureProperty))
  128. {
  129. material.SetTexture("_MetallicGlossMap", textureProperty.texture);
  130. material.SetFloat("_UseMetallicMap", 1.0f);
  131. }
  132. else
  133. {
  134. material.SetFloat("_UseMetallicMap", 0.0f);
  135. }
  136. if (description.TryGetProperty("emit_color_map", out textureProperty))
  137. {
  138. material.SetTexture("_EmissionMap", textureProperty.texture);
  139. material.SetFloat("_UseEmissiveMap", 1.0f);
  140. }
  141. else
  142. {
  143. material.SetFloat("_UseEmissiveMap", 0.0f);
  144. }
  145. if (description.TryGetProperty("ao_map", out textureProperty))
  146. {
  147. var tex = AssetDatabase.LoadAssetAtPath<Texture>(textureProperty.relativePath);
  148. material.SetTexture("AoMap", tex);
  149. material.SetFloat("UseAoMap", 1.0f);
  150. }
  151. else
  152. {
  153. material.SetFloat("UseAoMap", 0.0f);
  154. }
  155. }
  156. void CreateFrom3DsPhysicalMaterial(MaterialDescription description, Material material, AnimationClip[] clips)
  157. {
  158. float floatProperty;
  159. Vector4 vectorProperty;
  160. TexturePropertyDescription textureProperty;
  161. Shader shader;
  162. description.TryGetProperty("transparency", out floatProperty);
  163. bool hasTransparencyMap =
  164. description.TryGetProperty("transparency_map", out textureProperty);
  165. if (floatProperty > 0.0f || hasTransparencyMap)
  166. {
  167. shader = AssetDatabase.LoadAssetAtPath<Shader>(k_ShaderTransparentPath);
  168. if (shader == null)
  169. return;
  170. material.shader = shader;
  171. if (hasTransparencyMap)
  172. {
  173. material.SetTexture("_TRANSPARENCY_MAP", textureProperty.texture);
  174. material.SetFloat("_TRANSPARENCY", 1.0f);
  175. }
  176. else
  177. {
  178. material.SetFloat("_TRANSPARENCY", floatProperty);
  179. }
  180. }
  181. else
  182. {
  183. shader = AssetDatabase.LoadAssetAtPath<Shader>(k_ShaderPath);
  184. if (shader == null)
  185. return;
  186. material.shader = shader;
  187. }
  188. foreach (var clip in clips)
  189. {
  190. clip.ClearCurves();
  191. }
  192. RemapPropertyFloat(description, material, "base_weight", "_BASE_COLOR_WEIGHT");
  193. if (description.TryGetProperty("base_color_map", out textureProperty))
  194. {
  195. SetMaterialTextureProperty("_BASE_COLOR_MAP", material, textureProperty);
  196. }
  197. else if (description.TryGetProperty("base_color", out vectorProperty))
  198. {
  199. if (QualitySettings.activeColorSpace == ColorSpace.Gamma)
  200. {
  201. vectorProperty.x = Mathf.LinearToGammaSpace(vectorProperty.x);
  202. vectorProperty.y = Mathf.LinearToGammaSpace(vectorProperty.y);
  203. vectorProperty.z = Mathf.LinearToGammaSpace(vectorProperty.z);
  204. vectorProperty.w = Mathf.LinearToGammaSpace(vectorProperty.w);
  205. }
  206. material.SetColor("_BASE_COLOR", vectorProperty);
  207. }
  208. RemapPropertyFloat(description, material, "reflectivity", "_REFLECTIONS_WEIGHT");
  209. RemapPropertyTextureOrColor(description, material, "refl_color", "_REFLECTIONS_COLOR");
  210. RemapPropertyTextureOrFloat(description, material, "metalness", "_METALNESS");
  211. RemapPropertyTextureOrFloat(description, material, "roughness", "_REFLECTIONS_ROUGHNESS");
  212. RemapPropertyTextureOrFloat(description, material, "trans_ior", "_REFLECTIONS_IOR");
  213. RemapPropertyFloat(description, material, "emission", "_EMISSION_WEIGHT");
  214. RemapPropertyTextureOrColor(description, material, "emit_color", "_EMISSION_COLOR");
  215. RemapPropertyFloat(description, material, "bump_map_amt", "_BUMP_MAP_STRENGTH");
  216. RemapPropertyTexture(description, material, "bump_map", "_BUMP_MAP");
  217. }
  218. static void SetMaterialTextureProperty(string propertyName, Material material,
  219. TexturePropertyDescription textureProperty)
  220. {
  221. material.SetTexture(propertyName, textureProperty.texture);
  222. material.SetTextureOffset(propertyName, textureProperty.offset);
  223. material.SetTextureScale(propertyName, textureProperty.scale);
  224. }
  225. static void RemapPropertyFloat(MaterialDescription description, Material material, string inPropName,
  226. string outPropName)
  227. {
  228. if (description.TryGetProperty(inPropName, out float floatProperty))
  229. {
  230. material.SetFloat(outPropName, floatProperty);
  231. }
  232. }
  233. static void RemapPropertyTexture(MaterialDescription description, Material material, string inPropName,
  234. string outPropName)
  235. {
  236. if (description.TryGetProperty(inPropName, out TexturePropertyDescription textureProperty))
  237. {
  238. material.SetTexture(outPropName, textureProperty.texture);
  239. }
  240. }
  241. static void RemapPropertyTextureOrColor(MaterialDescription description, Material material,
  242. string inPropName, string outPropName)
  243. {
  244. if (description.TryGetProperty(inPropName + "_map", out TexturePropertyDescription textureProperty))
  245. {
  246. material.SetTexture(outPropName + "_MAP", textureProperty.texture);
  247. material.SetColor(outPropName, Color.white);
  248. }
  249. else if(description.TryGetProperty(inPropName, out Vector4 color))
  250. {
  251. material.SetColor(outPropName, color);
  252. }
  253. }
  254. static void RemapPropertyTextureOrFloat(MaterialDescription description, Material material,
  255. string inPropName, string outPropName)
  256. {
  257. if (description.TryGetProperty(inPropName + "_map", out TexturePropertyDescription textureProperty))
  258. {
  259. material.SetTexture(outPropName + "_MAP", textureProperty.texture);
  260. material.SetFloat(outPropName, 1.0f);
  261. }
  262. else if(description.TryGetProperty(inPropName, out float floatProperty))
  263. {
  264. material.SetFloat(outPropName, floatProperty);
  265. }
  266. }
  267. }
  268. }