Skip to content

Commit

Permalink
Fixed texture output due to wacky isReadable Flag on some versions of…
Browse files Browse the repository at this point in the history
… Unity
  • Loading branch information
Jaimi committed Jun 12, 2024
1 parent d2bda56 commit 33073e7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
8 changes: 0 additions & 8 deletions UMAProject/Assets/Plugins.meta

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.IO;
using UMA.Examples;
using UMA.PoseTools;
using static UMA.UMAData;
using UnityEngine.Experimental.Rendering;

namespace UMA.Editors
{
Expand Down Expand Up @@ -185,7 +187,7 @@ public static void ConvertToNonUMA(GameObject baseObject, UMAAvatarBase avatar,
}


[UnityEditor.MenuItem("GameObject/UMA/Save Atlas Textures (runtime only)")]
[UnityEditor.MenuItem("GameObject/UMA/Save Atlas Textures")]
[MenuItem("CONTEXT/DynamicCharacterAvatar/Save Selected Avatars generated textures to PNG", false, 10)]
[MenuItem("UMA/Runtime/Save Selected Avatar Atlas Textures")]
public static void SaveSelectedAvatarsPNG()
Expand Down Expand Up @@ -217,6 +219,38 @@ public static void SaveSelectedAvatarsPNG()
{
string basename = System.IO.Path.GetFileNameWithoutExtension(path);
string pathname = System.IO.Path.GetDirectoryName(path);

// Get the UMAMaterials for each atlas.

UMAData umaData = avatar.umaData;
GeneratedMaterials gmatContainer = umaData.generatedMaterials;

int i = 0;
foreach (var gm in gmatContainer.materials)
{
UMAMaterial umat = gm.umaMaterial;
Material mat = gm.skinnedMeshRenderer.sharedMaterials[gm.materialIndex];
Material omat = gm.material;
foreach(var tex in umat.GetTexturePropertyNames())
{
Texture texture = mat.GetTexture(tex);
if (texture != null)
{
string tname = $"{pathname}/{basename}_{i}_{umat.name}{tex}.PNG";
try
{
SaveTexture(texture, tname);
}
catch
{
// Not a readable texture. This is actually OK. Wish isReadable wasn't broken.
}
}
}
i++;
}

/*
// save the diffuse texture
for (int i = 0; i < smr.materials.Length; i++)
{
Expand All @@ -234,8 +268,8 @@ public static void SaveSelectedAvatarsPNG()
SaveTexture(texture, texname);
}
}
}
}
}*/
}
}

private static void SaveTexture(Texture texture, string diffuseName, bool isNormal = false)
Expand Down Expand Up @@ -315,6 +349,9 @@ private static Texture2D sconvertNormalMap(Texture tex)

static public Texture2D GetRTPixels(RenderTexture rt)
{
// Remember crrently active render texture
RenderTexture currentActiveRT = RenderTexture.active;

/// Some goofiness ends up with the texture being too dark unless
/// I send it to a new render texture.
RenderTexture outputMap = new RenderTexture(rt.width, rt.height, 32, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
Expand All @@ -325,8 +362,6 @@ static public Texture2D GetRTPixels(RenderTexture rt)
Graphics.Blit(rt, outputMap);


// Remember crrently active render texture
RenderTexture currentActiveRT = RenderTexture.active;

// Set the supplied RenderTexture as the active one
RenderTexture.active = outputMap;
Expand Down Expand Up @@ -376,15 +411,14 @@ public static void SaveRenderTexture(RenderTexture texture, string textureName,

private static void SaveTexture2D(Texture2D texture, string textureName)
{
if (texture.isReadable)
{
byte[] data = texture.EncodeToPNG();
// ?? texture.isReadable seems to always return true, regardless of whether the texture is readable or not
// so we'll just try to encode it and see if it throws an exception
// This didn't use to be the case, and may be fixed in various Unity versions...
//if (texture.isReadable)
{
byte[] data = texture.EncodeToPNG();
System.IO.File.WriteAllBytes(textureName, data);
}
else
{
Debug.LogError("Texture: " + texture.name + " is not readable. Skipping.");
}
}

[UnityEditor.MenuItem("CONTEXT/DynamicCharacterAvatar/Save as UMA Preset")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,21 @@ public static void CreateMaterialAsset()
}
#endif

public List<string> GetTexturePropertyNames()
{
List<string> names = new List<string>();


foreach (MaterialChannel channel in channels)
{
if (channel.channelType == ChannelType.Texture || channel.channelType == ChannelType.TintedTexture || channel.channelType == ChannelType.DiffuseTexture)
{
names.Add(channel.materialPropertyName);
}
}
return names;
}

private bool isGeneratedTextures
{
get
Expand Down

0 comments on commit 33073e7

Please sign in to comment.