Skip to content

Commit

Permalink
compress textures on import to platform-specific default format
Browse files Browse the repository at this point in the history
  • Loading branch information
hybridherbst committed Sep 11, 2023
1 parent 7f859e8 commit 877b73b
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Editor/Scripts/GLTFImporter.cs
Expand Up @@ -588,6 +588,17 @@ string GetUniqueName(string desiredName)
ctx.AddObjectToAsset(GetUniqueName(tex.name), tex);
if (invalidTextures.Contains(tex))
tex.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;

// platform-dependant texture compression
var buildTargetName = BuildPipeline.GetBuildTargetName(ctx.selectedBuildTarget);
var format = TextureImporterHelper.GetAutomaticFormat(tex, buildTargetName);
var convertedFormat = (TextureFormat) (int) format;
if ((int) convertedFormat > -1)
{
// Debug.Log("Compressing texture " + tex.name + "(format: " + tex.format + ", mips: " + tex.mipmapCount + ") to: " + convertedFormat);
EditorUtility.CompressTexture(tex, convertedFormat, TextureCompressionQuality.Best);
// Debug.Log("Mips now: " + tex.mipmapCount); // TODO figure out why mipmaps disappear here
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions Editor/Scripts/Internal/DefaultImportSettings.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
135 changes: 135 additions & 0 deletions Editor/Scripts/Internal/DefaultImportSettings/DefaultTexture.png.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions Editor/Scripts/Internal/TextureImporterHelper.cs
@@ -0,0 +1,51 @@
using System;
using System.Reflection;
using UnityEditor;
using UnityEditor.Build;
using UnityEngine;

namespace UnityGLTF
{
public class TextureImporterHelper
{
private static MethodInfo GetFixedPlatformName;
private const string DefaultTextureAssetGuid = "7b7ff3cec11c24c599d6f12443877d5e";

public static TextureImporterFormat GetAutomaticFormat(Texture2D texture, string platform)
{
var defaultTextureImporter = AssetImporter.GetAtPath(AssetDatabase.GUIDToAssetPath(DefaultTextureAssetGuid)) as TextureImporter;
// defaultTextureImporter = new TextureImporter();

if (GetFixedPlatformName == null)
GetFixedPlatformName = typeof(TextureImporter)
.GetMethod("GetFixedPlatformName", BindingFlags.Static | BindingFlags.NonPublic);

if (GetFixedPlatformName == null)
throw new MissingMethodException("TextureImporter.GetFixedPlatformName");

platform = GetFixedPlatformName.Invoke(null, new object[] { platform }) as string;
TextureImporterSettings importerSettings = new TextureImporterSettings();

importerSettings.aniso = 5;
importerSettings.wrapMode = texture.wrapMode;
importerSettings.filterMode = texture.filterMode;
importerSettings.mipmapEnabled = texture.mipmapCount > 1;
importerSettings.alphaSource = TextureImporterAlphaSource.FromInput;

var hasAlpha = TextureUtil.HasAlphaTextureFormat(texture.format);
var isHDR = TextureUtil.IsHDRFormat(texture.format);

// var platformSettings = new TextureImporterPlatformSettings();

foreach (BuildPlatform validPlatform in BuildPlatforms.instance.GetValidPlatforms())
{
// TextureImporter.RecommendedFormatsFromTextureTypeAndPlatform
if (validPlatform.name == platform)
return TextureImporter.DefaultFormatFromTextureParameters(importerSettings, defaultTextureImporter.GetPlatformTextureSettings(platform), hasAlpha, isHDR, validPlatform.defaultTarget);
}

// This should never happen
return TextureImporterFormat.Automatic;
}
}
}
3 changes: 3 additions & 0 deletions Editor/Scripts/Internal/TextureImporterHelper.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 877b73b

Please sign in to comment.