FBXMaterialDescriptionPreprocessor.cs 12 KB

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