ThreeDSMaterialDescriptionPostprocessor.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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", 3.0f); // From C# enum BlendMode
  47. material.SetOverrideTag("RenderType", "Transparent");
  48. material.SetFloat("_SrcBlend", (float)UnityEngine.Rendering.BlendMode.One);
  49. material.SetFloat("_DstBlend", (float)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  50. material.SetFloat("_ZWrite", 0.0f);
  51. material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
  52. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  53. material.SetFloat("_Surface", 1.0f);
  54. material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
  55. }
  56. else
  57. {
  58. material.SetFloat("_Mode", 0.0f); // From C# enum BlendMode
  59. material.SetOverrideTag("RenderType", "");
  60. material.SetFloat("_SrcBlend", (float)UnityEngine.Rendering.BlendMode.One);
  61. material.SetFloat("_DstBlend", (float)UnityEngine.Rendering.BlendMode.Zero);
  62. material.SetFloat("_ZWrite", 1.0f);
  63. material.DisableKeyword("_ALPHATEST_ON");
  64. material.DisableKeyword("_ALPHABLEND_ON");
  65. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  66. material.renderQueue = -1;
  67. material.SetFloat("_Surface", 0.0f);
  68. material.DisableKeyword("_SURFACE_TYPE_TRANSPARENT");
  69. }
  70. if (floatProperty > .0f)
  71. vectorProperty.w = 1.0f - floatProperty;
  72. Color diffuseColor = vectorProperty;
  73. material.SetColor("_Color", PlayerSettings.colorSpace == ColorSpace.Linear ? diffuseColor.gamma : diffuseColor);
  74. material.SetColor("_BaseColor", PlayerSettings.colorSpace == ColorSpace.Linear ? diffuseColor.gamma : diffuseColor);
  75. if (description.TryGetProperty("EmissiveColor", out vectorProperty))
  76. {
  77. material.SetColor("_EmissionColor", vectorProperty);
  78. material.globalIlluminationFlags |= MaterialGlobalIlluminationFlags.RealtimeEmissive;
  79. material.EnableKeyword("_EMISSION");
  80. }
  81. if (description.TryGetProperty("texturemap1", out textureProperty))
  82. {
  83. SetMaterialTextureProperty("_BaseMap", material, textureProperty);
  84. }
  85. if (description.TryGetProperty("bumpmap", out textureProperty))
  86. {
  87. if (material.HasProperty("_BumpMap"))
  88. {
  89. SetMaterialTextureProperty("_BumpMap", material, textureProperty);
  90. material.EnableKeyword("_NORMALMAP");
  91. }
  92. }
  93. }
  94. static void SetMaterialTextureProperty(string propertyName, Material material, TexturePropertyDescription textureProperty)
  95. {
  96. material.SetTexture(propertyName, textureProperty.texture);
  97. material.SetTextureOffset(propertyName, textureProperty.offset);
  98. material.SetTextureScale(propertyName, textureProperty.scale);
  99. }
  100. }
  101. }