暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SketchupMaterialDescriptionPostprocessor.cs 4.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 = 2;
  11. static readonly int k_Order = -980;
  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", 3.0f); // From C# enum BlendMode
  54. material.SetOverrideTag("RenderType", "Transparent");
  55. material.SetFloat("_SrcBlend", (float)UnityEngine.Rendering.BlendMode.One);
  56. material.SetFloat("_DstBlend", (float)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  57. material.SetFloat("_ZWrite", 0.0f);
  58. material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
  59. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  60. material.SetFloat("_Surface", 1.0f);
  61. material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
  62. }
  63. else
  64. {
  65. material.SetFloat("_Mode", 0.0f); // From C# enum BlendMode
  66. material.SetOverrideTag("RenderType", "");
  67. material.SetFloat("_SrcBlend", (float)UnityEngine.Rendering.BlendMode.One);
  68. material.SetFloat("_DstBlend", (float)UnityEngine.Rendering.BlendMode.Zero);
  69. material.SetFloat("_ZWrite", 1.0f);
  70. material.DisableKeyword("_ALPHATEST_ON");
  71. material.DisableKeyword("_ALPHABLEND_ON");
  72. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  73. material.renderQueue = -1;
  74. material.SetFloat("_Surface", 0.0f);
  75. material.DisableKeyword("_SURFACE_TYPE_TRANSPARENT");
  76. }
  77. }
  78. static void SetMaterialTextureProperty(string propertyName, Material material, TexturePropertyDescription textureProperty)
  79. {
  80. material.SetTexture(propertyName, textureProperty.texture);
  81. material.SetTextureOffset(propertyName, textureProperty.offset);
  82. material.SetTextureScale(propertyName, textureProperty.scale);
  83. }
  84. }
  85. }