ThreeDSMaterialDescriptionPostprocessor.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. using UnityEditor.AssetImporters;
  5. namespace UnityEditor.Rendering.Universal
  6. {
  7. [Obsolete("ThreeDSMaterialDescriptionPreprocessor is deprecated, consider creating a new AsserPostProcessor rather than overriding it.")]
  8. public class ThreeDSMaterialDescriptionPreprocessor : 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 lowerCasePath = Path.GetExtension(assetPath).ToLower();
  23. if (lowerCasePath != ".3ds")
  24. return;
  25. var shader = Shader.Find("Universal Render Pipeline/Lit");
  26. if (shader == null)
  27. return;
  28. material.shader = shader;
  29. TexturePropertyDescription textureProperty;
  30. float floatProperty;
  31. Vector4 vectorProperty;
  32. description.TryGetProperty("diffuse", out vectorProperty);
  33. vectorProperty.x /= 255.0f;
  34. vectorProperty.y /= 255.0f;
  35. vectorProperty.z /= 255.0f;
  36. vectorProperty.w /= 255.0f;
  37. description.TryGetProperty("transparency", out floatProperty);
  38. bool isTransparent = vectorProperty.w <= 0.99f || floatProperty > .0f;
  39. if (isTransparent)
  40. {
  41. material.SetFloat("_Mode", (float)3.0); // From C# enum BlendMode
  42. material.SetOverrideTag("RenderType", "Transparent");
  43. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  44. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  45. material.SetInt("_ZWrite", 0);
  46. material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
  47. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  48. material.SetInt("_Surface", 1);
  49. }
  50. else
  51. {
  52. material.SetFloat("_Mode", (float)0.0); // From C# enum BlendMode
  53. material.SetOverrideTag("RenderType", "");
  54. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  55. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  56. material.SetInt("_ZWrite", 1);
  57. material.DisableKeyword("_ALPHATEST_ON");
  58. material.DisableKeyword("_ALPHABLEND_ON");
  59. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  60. material.renderQueue = -1;
  61. material.SetInt("_Surface", 0);
  62. }
  63. if (floatProperty > .0f)
  64. vectorProperty.w = 1.0f - floatProperty;
  65. Color diffuseColor = vectorProperty;
  66. material.SetColor("_Color", PlayerSettings.colorSpace == ColorSpace.Linear ? diffuseColor.gamma : diffuseColor);
  67. material.SetColor("_BaseColor", PlayerSettings.colorSpace == ColorSpace.Linear ? diffuseColor.gamma : diffuseColor);
  68. if (description.TryGetProperty("EmissiveColor", out vectorProperty))
  69. {
  70. material.SetColor("_EmissionColor", vectorProperty);
  71. material.globalIlluminationFlags |= MaterialGlobalIlluminationFlags.RealtimeEmissive;
  72. material.EnableKeyword("_EMISSION");
  73. }
  74. if (description.TryGetProperty("texturemap1", out textureProperty))
  75. {
  76. SetMaterialTextureProperty("_BaseMap", material, textureProperty);
  77. }
  78. if (description.TryGetProperty("bumpmap", out textureProperty))
  79. {
  80. if (material.HasProperty("_BumpMap"))
  81. {
  82. SetMaterialTextureProperty("_BumpMap", material, textureProperty);
  83. material.EnableKeyword("_NORMALMAP");
  84. }
  85. }
  86. }
  87. static void SetMaterialTextureProperty(string propertyName, Material material, TexturePropertyDescription textureProperty)
  88. {
  89. material.SetTexture(propertyName, textureProperty.texture);
  90. material.SetTextureOffset(propertyName, textureProperty.offset);
  91. material.SetTextureScale(propertyName, textureProperty.scale);
  92. }
  93. }
  94. }