Skip to content

Commit

Permalink
Don't compress normal DDS by default, only if packed normals enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
gleblebedev committed Aug 15, 2024
1 parent cc81958 commit 25a53bc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Editor/ExportSettingsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public override void OnInspectorGUI()
EditorUtility.RevealInFinder(script.path);
}

if (!string.IsNullOrEmpty(script.path) && !script.path.EndsWith("Data"))
if (!string.IsNullOrEmpty(script.path) && !script.path.EndsWith("Data") && !script.path.EndsWith("Data/") && !script.path.EndsWith("Data\\"))
{
EditorGUILayout.HelpBox("The path doesn't end with Data. Are you sure you've picked a correct path to a Data folder?", MessageType.Warning);
}
Expand Down
4 changes: 2 additions & 2 deletions Editor/TextureExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private void ExportDefaultMap(UnityEngine.Texture asset, TextureImporter texture
{
new TextureProcessor().ProcessAndSaveTexture(asset,
"Hidden/UnityToRebelFork/Copy", Settings.ResolveResourceFileName(EvaluateResourcePath(asset)),
textureImporter?.GetAutomaticFormat("Standalone") != TextureImporterFormat.DXT1, new Dictionary<string, float>
hasAlpha: textureImporter?.GetAutomaticFormat("Standalone") != TextureImporterFormat.DXT1, shaderArgs: new Dictionary<string, float>
{
{"_GammaInput",( PlayerSettings.colorSpace == UnityEngine.ColorSpace.Linear)?0.0f:1.0f},
{"_GammaOutput",1.0f},
Expand All @@ -48,7 +48,7 @@ private void ExportNormalMap(UnityEngine.Texture asset)
Settings.PackNormals
? "Hidden/UnityToRebelFork/DecodeNormalMapPackedNormal"
: "Hidden/UnityToRebelFork/DecodeNormalMap",
Settings.ResolveResourceFileName(EvaluateResourcePath(asset)));
Settings.ResolveResourceFileName(EvaluateResourcePath(asset)), compress: Settings.PackNormals);
}
}
}
25 changes: 13 additions & 12 deletions Editor/TextureProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ namespace UnityToRebelFork.Editor
public class TextureProcessor
{
public void ProcessAndSaveTexture(UnityEngine.Texture sourceTexture, string shaderName, string fullOutputPath,
bool hasAlpha = true, Dictionary<string, float> shaderArgs = null)
bool hasAlpha = true, bool compress = true, Dictionary<string, float> shaderArgs = null)
{
ProcessAndSaveTexture(sourceTexture, UnityEngine.Shader.Find(shaderName), fullOutputPath, hasAlpha, shaderArgs);
ProcessAndSaveTexture(sourceTexture, UnityEngine.Shader.Find(shaderName), fullOutputPath, hasAlpha, compress, shaderArgs);
}
public void ProcessTexture(UnityEngine.Texture sourceTexture, string shaderName, Action<UnityEngine.Texture2D> callback)
{
ProcessTexture(sourceTexture, UnityEngine.Shader.Find(shaderName), callback);
}
public void ProcessAndSaveTexture(UnityEngine.Texture sourceTexture, UnityEngine.Shader shader, string fullOutputPath,
bool hasAlpha = true, Dictionary<string, float> shaderArgs = null)
bool hasAlpha = true, bool compress = true, Dictionary<string, float> shaderArgs = null)
{
UnityEngine.Material material = null;

Expand All @@ -40,7 +40,7 @@ public void ProcessAndSaveTexture(UnityEngine.Texture sourceTexture, UnityEngine
}
}
}
ProcessAndSaveTexture(sourceTexture, material, fullOutputPath, hasAlpha);
ProcessAndSaveTexture(sourceTexture, material, fullOutputPath, hasAlpha: hasAlpha, compress: compress);
}
finally
{
Expand All @@ -64,10 +64,10 @@ public void ProcessTexture(UnityEngine.Texture sourceTexture, UnityEngine.Shader
}

public void ProcessAndSaveTexture(UnityEngine.Texture sourceTexture, UnityEngine.Material material, string fullOutputPath,
bool hasAlpha = true)
bool hasAlpha = true, bool compress = true)
{
ProcessAndSaveTexture(sourceTexture, sourceTexture.width, sourceTexture.height, material, fullOutputPath,
hasAlpha);
hasAlpha: hasAlpha, compress: compress);
}

public void ProcessTexture(UnityEngine.Texture sourceTexture, UnityEngine.Material material, Action<UnityEngine.Texture2D> callback)
Expand Down Expand Up @@ -164,13 +164,13 @@ private RenderTextureFormat PickRenderTextureFormat(UnityEngine.Texture sourceTe
}

public void ProcessAndSaveTexture(UnityEngine.Texture sourceTexture, int width, int height, UnityEngine.Material material,
string fullOutputPath, bool hasAlpha = true)
string fullOutputPath, bool hasAlpha = true, bool compress = true)
{
ProcessTexture(sourceTexture, width, height, material,
texture => SaveTexture(texture, fullOutputPath, hasAlpha));
texture => SaveTexture(texture, fullOutputPath, hasAlpha:hasAlpha, compress:compress));
}

public void SaveTexture(UnityEngine.Texture2D texture, string fullOutputPath, bool hasAlpha = true)
public void SaveTexture(UnityEngine.Texture2D texture, string fullOutputPath, bool hasAlpha = true, bool compress = true)
{
if (string.IsNullOrWhiteSpace(fullOutputPath))
return;
Expand All @@ -197,7 +197,7 @@ public void SaveTexture(UnityEngine.Texture2D texture, string fullOutputPath, bo
WriteAllBytes(fullOutputPath, exr);
break;
case ".dds":
DDS.SaveAsRgbaDds(texture, fullOutputPath, hasAlpha);
DDS.SaveAsRgbaDds(texture, fullOutputPath, hasAlpha: hasAlpha, compress: compress);
break;
default:
throw new NotImplementedException("Not implemented texture file type " + ext);
Expand All @@ -212,15 +212,16 @@ private void WriteAllBytes(string fullOutputPath, byte[] buffer)
}
}
}

public class DDS
{
public static void SaveAsRgbaDds(UnityEngine.Texture2D texture, string fileName, bool hasAlpha = true, bool dither = false)
public static void SaveAsRgbaDds(UnityEngine.Texture2D texture, string fileName, bool hasAlpha = true, bool compress = true, bool dither = false)
{
using (var fileStream = System.IO.File.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.Read))
{
using (var binaryWriter = new BinaryWriter(fileStream))
{
var compress = (0 == (texture.width % 4)) && (0 == (texture.height % 4));
compress = compress && (0 == (texture.width % 4)) && (0 == (texture.height % 4));
if (compress)
{
WriteCompressedHeader(binaryWriter, texture.width, texture.height, texture.mipmapCount, false,
Expand Down

0 comments on commit 25a53bc

Please sign in to comment.