MegascansTextureProcessor.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using System.IO;
  4. using UnityEditor;
  5. using System;
  6. namespace Quixel
  7. {
  8. public class MegascansTextureProcessor : MonoBehaviour
  9. {
  10. string sourcePath;
  11. string destPath;
  12. bool normalMap;
  13. bool sRGB;
  14. public MegascansTextureProcessor(string sourcePath, string destPath, bool normalMap = false, bool sRGB = true)
  15. {
  16. this.sourcePath = sourcePath;
  17. this.destPath = destPath;
  18. this.normalMap = normalMap;
  19. this.sRGB = sRGB;
  20. }
  21. public Texture2D ImportTexture()
  22. {
  23. MegascansUtilities.CopyFileToProject(sourcePath, destPath);
  24. TextureImporter tImp = AssetImporter.GetAtPath(destPath) as TextureImporter;
  25. int importResolution = Convert.ToInt32(Math.Pow(2, 9 + EditorPrefs.GetInt("QuixelDefaultImportResolution", 4)));
  26. tImp.maxTextureSize = importResolution;
  27. tImp.sRGBTexture = sRGB;
  28. tImp.textureType = normalMap ? TextureImporterType.NormalMap : TextureImporterType.Default;
  29. AssetDatabase.ImportAsset(destPath);
  30. AssetDatabase.Refresh();
  31. return AssetDatabase.LoadAssetAtPath<Texture2D>(destPath);
  32. }
  33. public void AdjustAlphaCutoff(float alphaCutoff = 0.33f, bool alphaIsTransparency = true, bool mipMapsPreserveCoverage = true)
  34. {
  35. TextureImporter tImp = AssetImporter.GetAtPath(destPath) as TextureImporter;
  36. tImp.mipMapsPreserveCoverage = mipMapsPreserveCoverage;
  37. tImp.alphaIsTransparency = alphaIsTransparency;
  38. tImp.alphaTestReferenceValue = alphaCutoff;
  39. AssetDatabase.ImportAsset(destPath);
  40. AssetDatabase.Refresh();
  41. AssetDatabase.LoadAssetAtPath<Texture2D>(destPath);
  42. }
  43. }
  44. }
  45. #endif