ThreeDSMaterialDescriptionPostprocessor.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. using UnityEditor.AssetImporters;
  5. using UnityEngine.Rendering;
  6. using UnityEngine.Rendering.Universal;
  7. namespace UnityEditor.Rendering.Universal
  8. {
  9. class ThreeDSMaterialDescriptionPreprocessor : AssetPostprocessor
  10. {
  11. static readonly uint k_Version = 1;
  12. static readonly int k_Order = 2;
  13. public override uint GetVersion()
  14. {
  15. return k_Version;
  16. }
  17. public override int GetPostprocessOrder()
  18. {
  19. return k_Order;
  20. }
  21. public void OnPreprocessMaterialDescription(MaterialDescription description, Material material, AnimationClip[] clips)
  22. {
  23. var pipelineAsset = GraphicsSettings.currentRenderPipeline;
  24. if (!pipelineAsset || pipelineAsset.GetType() != typeof(UniversalRenderPipelineAsset))
  25. return;
  26. var lowerCasePath = Path.GetExtension(assetPath).ToLower();
  27. if (lowerCasePath != ".3ds")
  28. return;
  29. string path = AssetDatabase.GUIDToAssetPath(ShaderUtils.GetShaderGUID(ShaderPathID.Lit));
  30. var shader = AssetDatabase.LoadAssetAtPath<Shader>(path);
  31. if (shader == null)
  32. return;
  33. material.shader = shader;
  34. TexturePropertyDescription textureProperty;
  35. float floatProperty;
  36. Vector4 vectorProperty;
  37. description.TryGetProperty("diffuse", out vectorProperty);
  38. vectorProperty.x /= 255.0f;
  39. vectorProperty.y /= 255.0f;
  40. vectorProperty.z /= 255.0f;
  41. vectorProperty.w /= 255.0f;
  42. description.TryGetProperty("transparency", out floatProperty);
  43. bool isTransparent = vectorProperty.w <= 0.99f || floatProperty > .0f;
  44. if (isTransparent)
  45. {
  46. material.SetFloat("_Mode", (float)3.0); // From C# enum BlendMode
  47. material.SetOverrideTag("RenderType", "Transparent");
  48. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  49. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  50. material.SetInt("_ZWrite", 0);
  51. material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
  52. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  53. material.SetInt("_Surface", 1);
  54. }
  55. else
  56. {
  57. material.SetFloat("_Mode", (float)0.0); // From C# enum BlendMode
  58. material.SetOverrideTag("RenderType", "");
  59. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  60. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  61. material.SetInt("_ZWrite", 1);
  62. material.DisableKeyword("_ALPHATEST_ON");
  63. material.DisableKeyword("_ALPHABLEND_ON");
  64. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  65. material.renderQueue = -1;
  66. material.SetInt("_Surface", 0);
  67. }
  68. if (floatProperty > .0f)
  69. vectorProperty.w = 1.0f - floatProperty;
  70. Color diffuseColor = vectorProperty;
  71. material.SetColor("_Color", PlayerSettings.colorSpace == ColorSpace.Linear ? diffuseColor.gamma : diffuseColor);
  72. material.SetColor("_BaseColor", PlayerSettings.colorSpace == ColorSpace.Linear ? diffuseColor.gamma : diffuseColor);
  73. if (description.TryGetProperty("EmissiveColor", out vectorProperty))
  74. {
  75. material.SetColor("_EmissionColor", vectorProperty);
  76. material.globalIlluminationFlags |= MaterialGlobalIlluminationFlags.RealtimeEmissive;
  77. material.EnableKeyword("_EMISSION");
  78. }
  79. if (description.TryGetProperty("texturemap1", out textureProperty))
  80. {
  81. SetMaterialTextureProperty("_BaseMap", material, textureProperty);
  82. }
  83. if (description.TryGetProperty("bumpmap", out textureProperty))
  84. {
  85. if (material.HasProperty("_BumpMap"))
  86. {
  87. SetMaterialTextureProperty("_BumpMap", material, textureProperty);
  88. material.EnableKeyword("_NORMALMAP");
  89. }
  90. }
  91. }
  92. static void SetMaterialTextureProperty(string propertyName, Material material, TexturePropertyDescription textureProperty)
  93. {
  94. material.SetTexture(propertyName, textureProperty.texture);
  95. material.SetTextureOffset(propertyName, textureProperty.offset);
  96. material.SetTextureScale(propertyName, textureProperty.scale);
  97. }
  98. }
  99. }