SketchupMaterialDescriptionPostprocessor.cs 3.8 KB

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