FBXMaterialDescriptionPreprocessor.cs 12 KB

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