FBXArnoldSurfaceMaterialDescriptionPreprocessor.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using System.IO;
  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 FBXArnoldSurfaceMaterialDescriptionPreprocessor : AssetPostprocessor
  13. {
  14. static readonly uint k_Version = 2;
  15. static readonly int k_Order = 4;
  16. static readonly string k_ShaderPath = "Packages/com.unity.render-pipelines.universal/Runtime/Materials/ArnoldStandardSurface/ArnoldStandardSurface.shadergraph";
  17. static readonly string k_ShaderTransparentPath = "Packages/com.unity.render-pipelines.universal/Runtime/Materials/ArnoldStandardSurface/ArnoldStandardSurfaceTransparent.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 IsMayaArnoldStandardSurfaceMaterial(MaterialDescription description)
  27. {
  28. float typeId;
  29. description.TryGetProperty("TypeId", out typeId);
  30. return typeId == 1138001;
  31. }
  32. static bool Is3DsMaxArnoldStandardSurfaceMaterial(MaterialDescription description)
  33. {
  34. float classIdA;
  35. float classIdB;
  36. string originalMtl;
  37. description.TryGetProperty("ClassIDa", out classIdA);
  38. description.TryGetProperty("ClassIDb", out classIdB);
  39. description.TryGetProperty("ORIGINAL_MTL", out originalMtl);
  40. return classIdA == 2121471519 && classIdB == 1660373836 && originalMtl != "PHYSICAL_MTL";
  41. }
  42. public void OnPreprocessMaterialDescription(MaterialDescription description, Material material,
  43. AnimationClip[] clips)
  44. {
  45. var pipelineAsset = GraphicsSettings.currentRenderPipeline;
  46. if (!pipelineAsset || pipelineAsset.GetType() != typeof(UniversalRenderPipelineAsset))
  47. return;
  48. var lowerCasePath = Path.GetExtension(assetPath).ToLower();
  49. if (lowerCasePath == ".fbx")
  50. {
  51. if (IsMayaArnoldStandardSurfaceMaterial(description))
  52. CreateFromMayaArnoldStandardSurfaceMaterial(description, material, clips);
  53. else if (Is3DsMaxArnoldStandardSurfaceMaterial(description))
  54. CreateFrom3DsMaxArnoldStandardSurfaceMaterial(description, material, clips);
  55. }
  56. }
  57. void CreateFromMayaArnoldStandardSurfaceMaterial(MaterialDescription description, Material material,
  58. AnimationClip[] clips)
  59. {
  60. float floatProperty;
  61. Vector4 vectorProperty;
  62. TexturePropertyDescription textureProperty;
  63. Shader shader;
  64. float opacity = 1.0f;
  65. Vector4 opacityColor;
  66. TexturePropertyDescription opacityMap;
  67. description.TryGetProperty("opacity", out opacityColor);
  68. bool hasOpacityMap = description.TryGetProperty("opacity", out opacityMap);
  69. opacity = Mathf.Min(Mathf.Min(opacityColor.x, opacityColor.y), opacityColor.z);
  70. float transmission;
  71. description.TryGetProperty("transmission", out transmission);
  72. if (opacity == 1.0f && !hasOpacityMap)
  73. {
  74. opacity = 1.0f - transmission;
  75. }
  76. if (opacity < 1.0f || hasOpacityMap)
  77. {
  78. shader = AssetDatabase.LoadAssetAtPath<Shader>(k_ShaderTransparentPath);
  79. if (shader == null)
  80. return;
  81. material.shader = shader;
  82. if (hasOpacityMap)
  83. {
  84. material.SetTexture("_OPACITY_MAP", opacityMap.texture);
  85. material.SetFloat("_OPACITY", 1.0f);
  86. }
  87. else
  88. {
  89. material.SetFloat("_OPACITY", opacity);
  90. }
  91. }
  92. else
  93. {
  94. shader = AssetDatabase.LoadAssetAtPath<Shader>(k_ShaderPath);
  95. if (shader == null)
  96. return;
  97. material.shader = shader;
  98. }
  99. foreach (var clip in clips)
  100. {
  101. clip.ClearCurves();
  102. }
  103. description.TryGetProperty("base", out floatProperty);
  104. if (description.TryGetProperty("baseColor", out textureProperty))
  105. {
  106. SetMaterialTextureProperty("_BASE_COLOR_MAP", material, textureProperty);
  107. material.SetColor("_BASE_COLOR", Color.white * floatProperty);
  108. }
  109. else if (description.TryGetProperty("baseColor", out vectorProperty))
  110. {
  111. if (QualitySettings.activeColorSpace == ColorSpace.Gamma)
  112. {
  113. vectorProperty.x = Mathf.LinearToGammaSpace(vectorProperty.x);
  114. vectorProperty.y = Mathf.LinearToGammaSpace(vectorProperty.y);
  115. vectorProperty.z = Mathf.LinearToGammaSpace(vectorProperty.z);
  116. vectorProperty *= floatProperty;
  117. }
  118. material.SetColor("_BASE_COLOR", vectorProperty * floatProperty);
  119. }
  120. if (description.TryGetProperty("emission", out floatProperty) && floatProperty > 0.0f)
  121. {
  122. remapPropertyColorOrTexture(description, material, "emissionColor", "_EMISSION_COLOR", floatProperty);
  123. }
  124. remapPropertyFloatOrTexture(description, material, "metalness", "_METALNESS");
  125. description.TryGetProperty("specular", out floatProperty);
  126. remapPropertyColorOrTexture(description, material, "specularColor", "_SPECULAR_COLOR", floatProperty);
  127. remapPropertyFloatOrTexture(description, material, "specularRoughness", "_SPECULAR_ROUGHNESS");
  128. remapPropertyFloatOrTexture(description, material, "specularIOR", "_SPECULAR_IOR");
  129. remapPropertyTexture(description, material, "normalCamera", "_NORMAL_MAP");
  130. }
  131. void CreateFrom3DsMaxArnoldStandardSurfaceMaterial(MaterialDescription description, Material material,
  132. AnimationClip[] clips)
  133. {
  134. float floatProperty;
  135. Vector4 vectorProperty;
  136. TexturePropertyDescription textureProperty;
  137. var shader = AssetDatabase.LoadAssetAtPath<Shader>(k_ShaderPath);
  138. if (shader == null)
  139. return;
  140. material.shader = shader;
  141. foreach (var clip in clips)
  142. {
  143. clip.ClearCurves();
  144. }
  145. float opacity = 1.0f;
  146. Vector4 opacityColor;
  147. TexturePropertyDescription opacityMap;
  148. description.TryGetProperty("opacity", out opacityColor);
  149. bool hasOpacityMap = description.TryGetProperty("opacity", out opacityMap);
  150. opacity = Mathf.Min(Mathf.Min(opacityColor.x, opacityColor.y), opacityColor.z);
  151. if (opacity < 1.0f || hasOpacityMap)
  152. {
  153. if (hasOpacityMap)
  154. {
  155. material.SetTexture("_OPACITY_MAP", opacityMap.texture);
  156. material.SetFloat("_OPACITY", 1.0f);
  157. }
  158. else
  159. {
  160. material.SetFloat("_OPACITY", opacity);
  161. }
  162. }
  163. description.TryGetProperty("base", out floatProperty);
  164. if (description.TryGetProperty("base_color.shader", out textureProperty))
  165. {
  166. SetMaterialTextureProperty("_BASE_COLOR_MAP", material, textureProperty);
  167. material.SetColor("_BASE_COLOR", Color.white * floatProperty);
  168. }
  169. else if (description.TryGetProperty("base_color", out vectorProperty))
  170. {
  171. if (QualitySettings.activeColorSpace == ColorSpace.Gamma)
  172. {
  173. vectorProperty.x = Mathf.LinearToGammaSpace(vectorProperty.x);
  174. vectorProperty.y = Mathf.LinearToGammaSpace(vectorProperty.y);
  175. vectorProperty.z = Mathf.LinearToGammaSpace(vectorProperty.z);
  176. }
  177. material.SetColor("_BASE_COLOR", vectorProperty * floatProperty);
  178. }
  179. if (description.TryGetProperty("emission", out floatProperty) && floatProperty > 0.0f)
  180. {
  181. remapPropertyColorOrTexture3DsMax(description, material, "emission_color", "_EMISSION_COLOR",
  182. floatProperty);
  183. }
  184. remapPropertyFloatOrTexture3DsMax(description, material, "metalness", "_METALNESS");
  185. description.TryGetProperty("specular", out float specularFactor);
  186. remapPropertyColorOrTexture3DsMax(description, material, "specular_color", "_SPECULAR_COLOR",
  187. specularFactor);
  188. remapPropertyFloatOrTexture3DsMax(description, material, "specular_roughness", "_SPECULAR_ROUGHNESS");
  189. remapPropertyFloatOrTexture3DsMax(description, material, "specular_IOR", "_SPECULAR_IOR");
  190. remapPropertyTexture(description, material, "normal_camera", "_NORMAL_MAP");
  191. }
  192. static void SetMaterialTextureProperty(string propertyName, Material material,
  193. TexturePropertyDescription textureProperty)
  194. {
  195. material.SetTexture(propertyName, textureProperty.texture);
  196. material.SetTextureOffset(propertyName, textureProperty.offset);
  197. material.SetTextureScale(propertyName, textureProperty.scale);
  198. }
  199. static void remapPropertyFloat(MaterialDescription description, Material material, string inPropName,
  200. string outPropName)
  201. {
  202. if (description.TryGetProperty(inPropName, out float floatProperty))
  203. {
  204. material.SetFloat(outPropName, floatProperty);
  205. }
  206. }
  207. static void remapPropertyTexture(MaterialDescription description, Material material, string inPropName,
  208. string outPropName)
  209. {
  210. if (description.TryGetProperty(inPropName, out TexturePropertyDescription textureProperty))
  211. {
  212. material.SetTexture(outPropName, textureProperty.texture);
  213. }
  214. }
  215. static void remapPropertyColorOrTexture3DsMax(MaterialDescription description, Material material,
  216. string inPropName, string outPropName, float multiplier = 1.0f)
  217. {
  218. if (description.TryGetProperty(inPropName + ".shader", out TexturePropertyDescription textureProperty))
  219. {
  220. material.SetTexture(outPropName + "_MAP", textureProperty.texture);
  221. material.SetColor(outPropName, Color.white * multiplier);
  222. }
  223. else
  224. {
  225. description.TryGetProperty(inPropName, out Vector4 vectorProperty);
  226. material.SetColor(outPropName, vectorProperty * multiplier);
  227. }
  228. }
  229. static void remapPropertyFloatOrTexture3DsMax(MaterialDescription description, Material material,
  230. string inPropName, string outPropName)
  231. {
  232. if (description.TryGetProperty(inPropName, out TexturePropertyDescription textureProperty))
  233. {
  234. material.SetTexture(outPropName + "_MAP", textureProperty.texture);
  235. material.SetFloat(outPropName, 1.0f);
  236. }
  237. else
  238. {
  239. description.TryGetProperty(inPropName, out float floatProperty);
  240. material.SetFloat(outPropName, floatProperty);
  241. }
  242. }
  243. static void remapPropertyColorOrTexture(MaterialDescription description, Material material, string inPropName,
  244. string outPropName, float multiplier = 1.0f)
  245. {
  246. if (description.TryGetProperty(inPropName, out TexturePropertyDescription textureProperty))
  247. {
  248. material.SetTexture(outPropName + "_MAP", textureProperty.texture);
  249. material.SetColor(outPropName, Color.white * multiplier);
  250. }
  251. else
  252. {
  253. description.TryGetProperty(inPropName, out Vector4 vectorProperty);
  254. material.SetColor(outPropName, vectorProperty * multiplier);
  255. }
  256. }
  257. static void remapPropertyFloatOrTexture(MaterialDescription description, Material material, string inPropName,
  258. string outPropName)
  259. {
  260. if (description.TryGetProperty(inPropName, out TexturePropertyDescription textureProperty))
  261. {
  262. material.SetTexture(outPropName + "_MAP", textureProperty.texture);
  263. material.SetFloat(outPropName, 1.0f);
  264. }
  265. else
  266. {
  267. description.TryGetProperty(inPropName, out float floatProperty);
  268. material.SetFloat(outPropName, floatProperty);
  269. }
  270. }
  271. }
  272. }