Skip to content

Commit

Permalink
fixes for non-readable normal textures when converting to dxt5nm format
Browse files Browse the repository at this point in the history
  • Loading branch information
pfcDorn committed Jan 17, 2024
1 parent b9b987c commit 20b1c5f
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions Runtime/Scripts/SceneImporter/NormalMapEncodingConverter.cs
Expand Up @@ -8,9 +8,9 @@ public static class NormalMapEncodingConverter
public static async Task<Texture2D> ConvertToDxt5nmAndCheckTextureFormatAsync(Texture2D source)
{
Texture2D dest = source;
var pixels = source.GetPixels();
if (source.format != TextureFormat.RGBA32)
Color[] pixels;

void CreateDestinationTexture()
{
dest = new Texture2D(source.width, source.height, TextureFormat.RGBA32, source.mipmapCount > 0, true);
dest.wrapMode = source.wrapMode;
Expand All @@ -20,14 +20,47 @@ public static async Task<Texture2D> ConvertToDxt5nmAndCheckTextureFormatAsync(Te
dest.filterMode = source.filterMode;
dest.anisoLevel = source.anisoLevel;
dest.mipMapBias = source.mipMapBias;


}

void DestroySourceTexture()
{
#if UNITY_EDITOR
Texture.DestroyImmediate(source);
#else
Texture.Destroy(source);
#endif
}

if (!source.isReadable)
{
CreateDestinationTexture();

var destRenderTexture = RenderTexture.GetTemporary(source.width, source.height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear);
var previousSRGBState = GL.sRGBWrite;
GL.sRGBWrite = false;

Graphics.Blit(source, destRenderTexture);

dest.ReadPixels(new Rect(0, 0, destRenderTexture.width, destRenderTexture.height), 0, 0);

GL.sRGBWrite = previousSRGBState;

RenderTexture.ReleaseTemporary(destRenderTexture);

pixels = dest.GetPixels();

DestroySourceTexture();
}
else
{
pixels = source.GetPixels();

if (source.format != TextureFormat.RGBA32)
{
CreateDestinationTexture();
DestroySourceTexture();
}
}

await Task.Run(() =>
{
Expand Down

0 comments on commit 20b1c5f

Please sign in to comment.