Skip to content

Commit

Permalink
fixed normal map encoding conversion on asset import > ensure a RGBA …
Browse files Browse the repository at this point in the history
…format
  • Loading branch information
pfcDorn committed Jan 16, 2024
1 parent a7e335a commit 0af4c96
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Runtime/Scripts/SceneImporter/ImporterTextures.cs
Expand Up @@ -290,7 +290,10 @@ protected virtual async Task ConstructUnityTexture(Stream stream, bool markGpuOn

if (texture && convertToDxt5nmFormat)
{
await NormalMapEncodingConverter.ConvertToDxt5nmAsync(texture);
texture = await NormalMapEncodingConverter.ConvertToDxt5nmAndCheckTextureFormatAsync(texture);
if (texture != newTextureObject)
newTextureObject = texture;

texture.Apply();
}

Expand Down
33 changes: 28 additions & 5 deletions Runtime/Scripts/SceneImporter/NormalMapEncodingConverter.cs
Expand Up @@ -5,15 +5,38 @@ namespace UnityGLTF
{
public static class NormalMapEncodingConverter
{
public static Texture2D ConvertToDxt5nm(Texture2D source)
public static async Task<Texture2D> ConvertToDxt5nmAndCheckTextureFormatAsync(Texture2D source)
{
var dest = new Texture2D(source.width, source.height, TextureFormat.DXT5, false);
Texture2D dest = source;
var pixels = source.GetPixels();
for (var i = 0; i < pixels.Length; i++)

if (source.format != TextureFormat.RGBA32)
{
var c = pixels[i];
pixels[i] = new Color(1, c.g, 1, c.r);
dest = new Texture2D(source.width, source.height, TextureFormat.RGBA32, source.mipmapCount > 0, true);
dest.wrapMode = source.wrapMode;
dest.wrapModeU = source.wrapModeU;
dest.wrapModeV = source.wrapModeV;
dest.wrapModeW = source.wrapModeW;
dest.filterMode = source.filterMode;
dest.anisoLevel = source.anisoLevel;
dest.mipMapBias = source.mipMapBias;


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

await Task.Run(() =>
{
for (var i = 0; i < pixels.Length; i++)
{
var c = pixels[i];
pixels[i] = new Color(1, c.g, 1, c.r);
}
});

dest.SetPixels(pixels);
return dest;
Expand Down

0 comments on commit 0af4c96

Please sign in to comment.