Skip to content

Commit

Permalink
add texture compression option
Browse files Browse the repository at this point in the history
  • Loading branch information
hybridherbst committed Sep 11, 2023
1 parent 877b73b commit c72f3ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
36 changes: 25 additions & 11 deletions Editor/Scripts/GLTFImporter.cs
Expand Up @@ -84,7 +84,8 @@ private static void EnsureShadersAreLoaded()
[SerializeField] internal bool _importMaterials = true;
[Tooltip("Enable this to get the same main asset identifiers as glTFast uses. This is recommended for new asset imports. Note that changing this for already imported assets will break their scene references and require manually re-adding the affected assets.")]
[SerializeField] internal bool _useSceneNameIdentifier = false;

[SerializeField] internal GLTFImporterTextureCompressionQuality _textureCompression = GLTFImporterTextureCompressionQuality.Best;

// for humanoid importer
[SerializeField] internal bool m_OptimizeGameObjects = false;
[SerializeField] internal HumanDescription m_HumanDescription = new HumanDescription();
Expand Down Expand Up @@ -114,6 +115,15 @@ internal class ExtensionInfo
public bool used;
public bool required;
}

// Matches TextureCompressionQuality and adds "None" as option
internal enum GLTFImporterTextureCompressionQuality
{
None = -50,
Fast = 0,
Normal = 50,
Best = 100
}

[Serializable]
public class TextureInfo
Expand Down Expand Up @@ -585,20 +595,24 @@ string GetUniqueName(string desiredName)
}
else
{
ctx.AddObjectToAsset(GetUniqueName(tex.name), tex);
if (invalidTextures.Contains(tex))
tex.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
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)
if (_textureCompression != GLTFImporterTextureCompressionQuality.None)
{
// 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
// 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) (int) _textureCompression);
// Debug.Log("Mips now: " + tex.mipmapCount); // TODO figure out why mipmaps disappear here
}
}

ctx.AddObjectToAsset(GetUniqueName(tex.name), tex);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions Editor/Scripts/GLTFImporterInspector.cs
Expand Up @@ -59,8 +59,6 @@ private void TextureWarningsGUI(GLTFImporter t)
private void ModelInspectorGUI()
{
var t = target as GLTFImporter;
if (!t) EditorGUILayout.LabelField("NOOOO");

if (!t) return;

// serializedObject.Update();
Expand All @@ -75,10 +73,12 @@ private void ModelInspectorGUI()
EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(GLTFImporter._scaleFactor)));
// EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(GLTFImporter._maximumLod)), new GUIContent("Maximum Shader LOD"));
EditorGUILayout.Separator();

EditorGUILayout.LabelField("Meshes", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(GLTFImporter._readWriteEnabled)), new GUIContent("Read/Write"));
EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(GLTFImporter._generateColliders)));
EditorGUILayout.Separator();

EditorGUILayout.LabelField("Geometry", EditorStyles.boldLabel);
EditorGUI.BeginChangeCheck();
var importNormalsProp = serializedObject.FindProperty(nameof(GLTFImporter._importNormals));
Expand All @@ -98,6 +98,8 @@ private void ModelInspectorGUI()
EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(GLTFImporter._generateLightmapUVs)), new GUIContent("Generate Lightmap UVs"));
EditorGUILayout.Separator();

EditorGUILayout.LabelField("Compression", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(GLTFImporter._textureCompression)));
TextureWarningsGUI(t);
}

Expand Down

0 comments on commit c72f3ac

Please sign in to comment.