FBXArnoldSurfaceMaterialDescriptionPreprocessor.cs 12 KB

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