FBXMaterialDescriptionPreprocessor.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using System;
  2. using System.IO;
  3. using UnityEditor.AssetImporters;
  4. using UnityEngine;
  5. namespace UnityEditor.Rendering.Universal
  6. {
  7. [Obsolete("FBXMaterialDescriptionPreprocessor is deprecated, consider creating a new AsserPostProcessor rather than overriding it.")]
  8. public class FBXMaterialDescriptionPreprocessor : AssetPostprocessor
  9. {
  10. static readonly uint k_Version = 1;
  11. static readonly int k_Order = 2;
  12. public override uint GetVersion()
  13. {
  14. return k_Version;
  15. }
  16. public override int GetPostprocessOrder()
  17. {
  18. return k_Order;
  19. }
  20. public void OnPreprocessMaterialDescription(MaterialDescription description, Material material, AnimationClip[] clips)
  21. {
  22. var lowerCaseExtension = Path.GetExtension(assetPath).ToLower();
  23. if (lowerCaseExtension != ".fbx" && lowerCaseExtension != ".obj" && lowerCaseExtension != ".blend" && lowerCaseExtension != ".mb" && lowerCaseExtension != ".ma" && lowerCaseExtension != ".max")
  24. return;
  25. var shader = Shader.Find("Universal Render Pipeline/Lit");
  26. if (shader == null)
  27. return;
  28. material.shader = shader;
  29. Vector4 vectorProperty;
  30. float floatProperty;
  31. TexturePropertyDescription textureProperty;
  32. bool isTransparent = false;
  33. float opacity;
  34. float transparencyFactor;
  35. if (!description.TryGetProperty("Opacity", out opacity))
  36. {
  37. if (description.TryGetProperty("TransparencyFactor", out transparencyFactor))
  38. {
  39. opacity = transparencyFactor == 1.0f ? 1.0f : 1.0f - transparencyFactor;
  40. }
  41. if (opacity == 1.0f && description.TryGetProperty("TransparentColor", out vectorProperty))
  42. {
  43. opacity = vectorProperty.x == 1.0f ? 1.0f : 1.0f - vectorProperty.x;
  44. }
  45. }
  46. if (opacity < 1.0f || (opacity == 1.0f && description.TryGetProperty("TransparentColor", out textureProperty)))
  47. {
  48. isTransparent = true;
  49. }
  50. else if (description.HasAnimationCurve("TransparencyFactor") || description.HasAnimationCurve("TransparentColor"))
  51. {
  52. isTransparent = true;
  53. }
  54. if (isTransparent)
  55. {
  56. material.SetInt("_Mode", 3);
  57. material.SetOverrideTag("RenderType", "Transparent");
  58. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  59. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  60. material.SetInt("_ZWrite", 0);
  61. material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
  62. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  63. material.SetInt("_Surface", 1);
  64. }
  65. else
  66. {
  67. material.SetInt("_Mode", 0);
  68. material.SetOverrideTag("RenderType", "");
  69. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  70. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  71. material.SetInt("_ZWrite", 1);
  72. material.DisableKeyword("_ALPHATEST_ON");
  73. material.DisableKeyword("_ALPHABLEND_ON");
  74. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  75. material.renderQueue = -1;
  76. material.SetInt("_Surface", 0);
  77. }
  78. if (description.TryGetProperty("DiffuseColor", out textureProperty) && textureProperty.texture!=null)
  79. {
  80. Color diffuseColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
  81. if (description.TryGetProperty("DiffuseFactor", out floatProperty))
  82. diffuseColor *= floatProperty;
  83. diffuseColor.a = opacity;
  84. SetMaterialTextureProperty("_BaseMap", material, textureProperty);
  85. material.SetColor("_BaseColor", diffuseColor);
  86. }
  87. else if (description.TryGetProperty("DiffuseColor", out vectorProperty))
  88. {
  89. Color diffuseColor = vectorProperty;
  90. diffuseColor.a = opacity;
  91. material.SetColor("_BaseColor", PlayerSettings.colorSpace == ColorSpace.Linear ? diffuseColor.gamma : diffuseColor);
  92. }
  93. if (description.TryGetProperty("Bump", out textureProperty))
  94. {
  95. SetMaterialTextureProperty("_BumpMap", material, textureProperty);
  96. material.EnableKeyword("_NORMALMAP");
  97. if (description.TryGetProperty("BumpFactor", out floatProperty))
  98. material.SetFloat("_BumpScale", floatProperty);
  99. }
  100. else if (description.TryGetProperty("NormalMap", out textureProperty))
  101. {
  102. SetMaterialTextureProperty("_BumpMap", material, textureProperty);
  103. material.EnableKeyword("_NORMALMAP");
  104. if (description.TryGetProperty("BumpFactor", out floatProperty))
  105. material.SetFloat("_BumpScale", floatProperty);
  106. }
  107. if (description.TryGetProperty("EmissiveColor", out textureProperty))
  108. {
  109. Color emissiveColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
  110. material.SetColor("_EmissionColor", emissiveColor);
  111. SetMaterialTextureProperty("_EmissionMap", material, textureProperty);
  112. if (description.TryGetProperty("EmissiveFactor", out floatProperty) && floatProperty > 0.0f)
  113. {
  114. material.EnableKeyword("_EMISSION");
  115. material.globalIlluminationFlags |= MaterialGlobalIlluminationFlags.RealtimeEmissive;
  116. }
  117. }
  118. else if (
  119. description.TryGetProperty("EmissiveColor", out vectorProperty) && vectorProperty.magnitude > vectorProperty.w
  120. || description.HasAnimationCurve("EmissiveColor.x"))
  121. {
  122. if (description.TryGetProperty("EmissiveFactor", out floatProperty))
  123. vectorProperty *= floatProperty;
  124. material.SetColor("_EmissionColor", vectorProperty);
  125. if (floatProperty > 0.0f)
  126. {
  127. material.EnableKeyword("_EMISSION");
  128. material.globalIlluminationFlags |= MaterialGlobalIlluminationFlags.RealtimeEmissive;
  129. }
  130. }
  131. material.SetFloat("_Glossiness", 0.0f);
  132. if (PlayerSettings.colorSpace == ColorSpace.Linear)
  133. RemapAndTransformColorCurves(description, clips, "DiffuseColor", "_BaseColor", ConvertFloatLinearToGamma);
  134. else
  135. RemapColorCurves(description, clips, "DiffuseColor", "_BaseColor");
  136. RemapTransparencyCurves(description, clips);
  137. RemapColorCurves(description, clips, "EmissiveColor", "_EmissionColor");
  138. }
  139. static void RemapTransparencyCurves(MaterialDescription description, AnimationClip[] clips)
  140. {
  141. // For some reason, Opacity is never animated, we have to use TransparencyFactor and TransparentColor
  142. for (int i = 0; i < clips.Length; i++)
  143. {
  144. bool foundTransparencyCurve = false;
  145. AnimationCurve curve;
  146. if (description.TryGetAnimationCurve(clips[i].name, "TransparencyFactor", out curve))
  147. {
  148. ConvertKeys(curve, ConvertFloatOneMinus);
  149. clips[i].SetCurve("", typeof(Material), "_BaseColor.a", curve);
  150. foundTransparencyCurve = true;
  151. }
  152. else if (description.TryGetAnimationCurve(clips[i].name, "TransparentColor.x", out curve))
  153. {
  154. ConvertKeys(curve, ConvertFloatOneMinus);
  155. clips[i].SetCurve("", typeof(Material), "_BaseColor.a", curve);
  156. foundTransparencyCurve = true;
  157. }
  158. if (foundTransparencyCurve && !description.HasAnimationCurveInClip(clips[i].name, "DiffuseColor"))
  159. {
  160. Vector4 diffuseColor;
  161. description.TryGetProperty("DiffuseColor", out diffuseColor);
  162. clips[i].SetCurve("", typeof(Material), "_BaseColor.r", AnimationCurve.Constant(0.0f, 1.0f, diffuseColor.x));
  163. clips[i].SetCurve("", typeof(Material), "_BaseColor.g", AnimationCurve.Constant(0.0f, 1.0f, diffuseColor.y));
  164. clips[i].SetCurve("", typeof(Material), "_BaseColor.b", AnimationCurve.Constant(0.0f, 1.0f, diffuseColor.z));
  165. }
  166. }
  167. }
  168. static void RemapColorCurves(MaterialDescription description, AnimationClip[] clips, string originalPropertyName, string newPropertyName)
  169. {
  170. AnimationCurve curve;
  171. for (int i = 0; i < clips.Length; i++)
  172. {
  173. if (description.TryGetAnimationCurve(clips[i].name, originalPropertyName + ".x", out curve))
  174. {
  175. clips[i].SetCurve("", typeof(Material), newPropertyName + ".r", curve);
  176. }
  177. if (description.TryGetAnimationCurve(clips[i].name, originalPropertyName + ".y", out curve))
  178. {
  179. clips[i].SetCurve("", typeof(Material), newPropertyName + ".g", curve);
  180. }
  181. if (description.TryGetAnimationCurve(clips[i].name, originalPropertyName + ".z", out curve))
  182. {
  183. clips[i].SetCurve("", typeof(Material), newPropertyName + ".b", curve);
  184. }
  185. }
  186. }
  187. static void RemapAndTransformColorCurves(MaterialDescription description, AnimationClip[] clips, string originalPropertyName, string newPropertyName, System.Func<float, float> converter)
  188. {
  189. AnimationCurve curve;
  190. for (int i = 0; i < clips.Length; i++)
  191. {
  192. if (description.TryGetAnimationCurve(clips[i].name, originalPropertyName + ".x", out curve))
  193. {
  194. ConvertKeys(curve, converter);
  195. clips[i].SetCurve("", typeof(Material), newPropertyName + ".r", curve);
  196. }
  197. if (description.TryGetAnimationCurve(clips[i].name, originalPropertyName + ".y", out curve))
  198. {
  199. ConvertKeys(curve, converter);
  200. clips[i].SetCurve("", typeof(Material), newPropertyName + ".g", curve);
  201. }
  202. if (description.TryGetAnimationCurve(clips[i].name, originalPropertyName + ".z", out curve))
  203. {
  204. ConvertKeys(curve, converter);
  205. clips[i].SetCurve("", typeof(Material), newPropertyName + ".b", curve);
  206. }
  207. }
  208. }
  209. static float ConvertFloatLinearToGamma(float value)
  210. {
  211. return Mathf.LinearToGammaSpace(value);
  212. }
  213. static float ConvertFloatOneMinus(float value)
  214. {
  215. return 1.0f - value;
  216. }
  217. static void ConvertKeys(AnimationCurve curve, System.Func<float, float> convertionDelegate)
  218. {
  219. Keyframe[] keyframes = curve.keys;
  220. for (int i = 0; i < keyframes.Length; i++)
  221. {
  222. keyframes[i].value = convertionDelegate(keyframes[i].value);
  223. }
  224. curve.keys = keyframes;
  225. }
  226. static void SetMaterialTextureProperty(string propertyName, Material material, TexturePropertyDescription textureProperty)
  227. {
  228. material.SetTexture(propertyName, textureProperty.texture);
  229. material.SetTextureOffset(propertyName, textureProperty.offset);
  230. material.SetTextureScale(propertyName, textureProperty.scale);
  231. }
  232. }
  233. }