SketchupMaterialDescriptionPostprocessor.cs 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.IO;
  2. using UnityEngine;
  3. using UnityEditor.AssetImporters;
  4. using UnityEngine.Rendering;
  5. using UnityEngine.Rendering.Universal;
  6. namespace UnityEditor.Rendering.Universal
  7. {
  8. class SketchupMaterialDescriptionPreprocessor : 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 lowerCasePath = Path.GetExtension(assetPath).ToLower();
  26. if (lowerCasePath != ".skp")
  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. float floatProperty;
  34. Vector4 vectorProperty;
  35. TexturePropertyDescription textureProperty;
  36. if (description.TryGetProperty("DiffuseMap", out textureProperty) && textureProperty.texture!=null)
  37. {
  38. SetMaterialTextureProperty("_BaseMap", material, textureProperty);
  39. SetMaterialTextureProperty("_MainTex", material, textureProperty);
  40. var color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
  41. material.SetColor("_BaseColor", color);
  42. material.SetColor("_Color", color);
  43. }
  44. else if (description.TryGetProperty("DiffuseColor", out vectorProperty))
  45. {
  46. Color diffuseColor = vectorProperty;
  47. diffuseColor = PlayerSettings.colorSpace == ColorSpace.Linear ? diffuseColor.gamma : diffuseColor;
  48. material.SetColor("_BaseColor", diffuseColor);
  49. material.SetColor("_Color", diffuseColor);
  50. }
  51. if (description.TryGetProperty("IsTransparent", out floatProperty) && floatProperty == 1.0f)
  52. {
  53. material.SetFloat("_Mode", (float)3.0); // From C# enum BlendMode
  54. material.SetOverrideTag("RenderType", "Transparent");
  55. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  56. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  57. material.SetInt("_ZWrite", 0);
  58. material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
  59. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  60. material.SetInt("_Surface", 1);
  61. }
  62. else
  63. {
  64. material.SetFloat("_Mode", (float)0.0); // From C# enum BlendMode
  65. material.SetOverrideTag("RenderType", "");
  66. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  67. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  68. material.SetInt("_ZWrite", 1);
  69. material.DisableKeyword("_ALPHATEST_ON");
  70. material.DisableKeyword("_ALPHABLEND_ON");
  71. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  72. material.renderQueue = -1;
  73. material.SetInt("_Surface", 0);
  74. }
  75. }
  76. static void SetMaterialTextureProperty(string propertyName, Material material, TexturePropertyDescription textureProperty)
  77. {
  78. material.SetTexture(propertyName, textureProperty.texture);
  79. material.SetTextureOffset(propertyName, textureProperty.offset);
  80. material.SetTextureScale(propertyName, textureProperty.scale);
  81. }
  82. }
  83. }