FixTextureNameUnique

This commit is contained in:
ousttrue 2021-02-25 17:22:58 +09:00
parent 53e9e81aac
commit 04eebeda0d
3 changed files with 55 additions and 93 deletions

View File

@ -164,7 +164,7 @@ namespace UniGLTF
RestoreOlderVersionValues();
FixMeshNameUnique();
FixTextureAndImageNameUnique();
FixTextureNameUnique();
FixMaterialNameUnique();
FixNodeName();
@ -223,86 +223,42 @@ namespace UniGLTF
/// gltfTexture.name を Unity Asset 名として運用する。
/// ユニークである必要がある。
/// </summary>
void FixTextureAndImageNameUnique()
void FixTextureNameUnique()
{
var used = new HashSet<string>();
for (int i = 0; i < GLTF.textures.Count; ++i)
{
// process images
var used = new HashSet<string>();
for (int i = 0; i < GLTF.images.Count; ++i)
var gltfTexture = GLTF.textures[i];
if (string.IsNullOrEmpty(gltfTexture.name))
{
var image = GLTF.images[i];
if (string.IsNullOrEmpty(image.name))
{
RenameImageFromTexture(i);
}
if (string.IsNullOrEmpty(image.name))
{
var newName = $"image_{i}";
if (!used.Add(newName))
{
newName = "image_" + Guid.NewGuid().ToString("N");
if (!used.Add(newName))
{
throw new Exception();
}
}
image.name = newName;
// Debug.LogWarning($"no name: => {image.name}");
}
else
{
var lower = image.name.ToLower();
if (used.Contains(lower))
{
// rename
var uname = lower + "_" + Guid.NewGuid().ToString("N");
Debug.LogWarning($"same name: {lower} => {uname}");
image.name = uname;
lower = uname;
}
used.Add(lower);
}
// use image name
gltfTexture.name = GLTF.images[gltfTexture.source].name;
}
}
{
// process textures
var used = new HashSet<string>();
for (int i = 0; i < GLTF.textures.Count; ++i)
if (string.IsNullOrEmpty(gltfTexture.name))
{
var gltfTexture = GLTF.textures[i];
if (string.IsNullOrEmpty(gltfTexture.name))
var newName = $"texture_{i}";
if (!used.Add(newName))
{
// use image name
gltfTexture.name = GLTF.images[gltfTexture.source].name;
}
if (string.IsNullOrEmpty(gltfTexture.name))
{
var newName = $"image_{i}";
newName = "texture_" + Guid.NewGuid().ToString("N");
if (!used.Add(newName))
{
newName = "image_" + Guid.NewGuid().ToString("N");
if (!used.Add(newName))
{
throw new Exception();
}
throw new Exception();
}
gltfTexture.name = newName;
}
else
gltfTexture.name = newName;
}
else
{
var lower = gltfTexture.name.ToLower();
if (used.Contains(lower))
{
var lower = gltfTexture.name.ToLower();
if (used.Contains(lower))
{
// rename
var uname = lower + "_" + Guid.NewGuid().ToString("N");
Debug.LogWarning($"same name: {lower} => {uname}");
gltfTexture.name = uname;
lower = uname;
}
used.Add(lower);
// rename
var uname = lower + "_" + Guid.NewGuid().ToString("N");
Debug.LogWarning($"same name: {lower} => {uname}");
gltfTexture.name = uname;
lower = uname;
}
used.Add(lower);
}
}
}
@ -463,12 +419,16 @@ namespace UniGLTF
public IEnumerable<GetTextureParam> EnumerateTextures()
{
var used = new HashSet<string>();
for (int i = 0; i < GLTF.materials.Count; ++i)
{
var m = GLTF.materials[i];
foreach (var x in EnumerateTextures(m))
{
yield return x;
if (used.Add(x.Name))
{
yield return x;
}
}
}
}

View File

@ -8,7 +8,7 @@ namespace UniGLTF
public const string METALLIC_GLOSS_PROP = "_MetallicGlossMap";
public const string OCCLUSION_PROP = "_OcclusionMap";
public string Name;
public readonly string Name;
public readonly string TextureType;
public readonly float MetallicFactor;
public readonly ushort? Index0;
@ -69,13 +69,13 @@ namespace UniGLTF
public static GetTextureParam CreateMetallic(glTF gltf, int textureIndex, float metallicFactor)
{
var name = gltf.textures[textureIndex].name;
return new GetTextureParam(name, METALLIC_GLOSS_PROP, metallicFactor, textureIndex, default, default, default, default, default);
return new GetTextureParam(name + ".metallicRoughness", METALLIC_GLOSS_PROP, metallicFactor, textureIndex, default, default, default, default, default);
}
public static GetTextureParam CreateOcclusion(glTF gltf, int textureIndex)
{
var name = gltf.textures[textureIndex].name;
return new GetTextureParam(name, OCCLUSION_PROP, default, textureIndex, default, default, default, default, default);
return new GetTextureParam(name + ".occlusion", OCCLUSION_PROP, default, textureIndex, default, default, default, default, default);
}
}
}

View File

@ -50,11 +50,10 @@ namespace UniGLTF
{
if (param.Index0.HasValue && m_externalMap != null)
{
var gltfTexture = m_gltf.textures[param.Index0.Value];
if (m_externalMap.TryGetValue(gltfTexture.name, out external))
if (m_externalMap.TryGetValue(param.Name, out external))
{
Debug.Log($"use external: {gltfTexture.name}");
m_textureCache.Add(param, new TextureLoadInfo(external, used, true));
// Debug.Log($"use external: {param.Name}");
m_textureCache.Add(param.Name, new TextureLoadInfo(external, used, true));
return external;
}
}
@ -94,7 +93,7 @@ namespace UniGLTF
}
}
Dictionary<GetTextureParam, TextureLoadInfo> m_textureCache = new Dictionary<GetTextureParam, TextureLoadInfo>();
Dictionary<string, TextureLoadInfo> m_textureCache = new Dictionary<string, TextureLoadInfo>();
public IEnumerable<TextureLoadInfo> Textures => m_textureCache.Values;
@ -108,15 +107,15 @@ namespace UniGLTF
#endif
}
async Awaitable<Texture2D> GetOrCreateBaseTexture(glTF gltf, int index, bool used)
async Awaitable<TextureLoadInfo> GetOrCreateBaseTexture(glTF gltf, int textureIndex, bool used)
{
var defaultParam = GetTextureParam.Create(gltf, index);
if (!m_textureCache.TryGetValue(defaultParam, out TextureLoadInfo cacheInfo))
var name = gltf.textures[textureIndex].name;
if (!m_textureCache.TryGetValue(name, out TextureLoadInfo cacheInfo))
{
cacheInfo = await LoadTextureAsync(index, used);
m_textureCache.Add(defaultParam, cacheInfo);
cacheInfo = await LoadTextureAsync(textureIndex, used);
m_textureCache.Add(name, cacheInfo);
}
return cacheInfo.Texture;
return cacheInfo;
}
/// <summary>
@ -129,7 +128,7 @@ namespace UniGLTF
/// <returns></returns>
public async Awaitable<Texture2D> GetTextureAsync(glTF gltf, GetTextureParam param)
{
if (m_textureCache.TryGetValue(param, out TextureLoadInfo cacheInfo))
if (m_textureCache.TryGetValue(param.Name, out TextureLoadInfo cacheInfo))
{
return cacheInfo.Texture;
}
@ -145,16 +144,17 @@ namespace UniGLTF
if (Application.isPlaying)
{
var baseTexture = await GetOrCreateBaseTexture(gltf, param.Index0.Value, false);
var converted = new NormalConverter().GetImportTexture(baseTexture);
var converted = new NormalConverter().GetImportTexture(baseTexture.Texture);
var info = new TextureLoadInfo(converted, true, false);
m_textureCache.Add(param, info);
m_textureCache.Add(param.Name, info);
return info.Texture;
}
else
{
#if UNITY_EDITOR
var info = await LoadTextureAsync(param.Index0.Value, true);
m_textureCache.Add(GetTextureParam.CreateNormal(gltf, param.Index0.Value), info);
var name = gltf.textures[param.Index0.Value].name;
m_textureCache.Add(name, info);
var textureAssetPath = AssetDatabase.GetAssetPath(info.Texture);
TextureIO.MarkTextureAssetAsNormalMap(textureAssetPath);
@ -167,25 +167,27 @@ namespace UniGLTF
{
// Bake roughnessFactor values into a texture.
var baseTexture = await GetOrCreateBaseTexture(gltf, param.Index0.Value, false);
var converted = new MetallicRoughnessConverter(param.MetallicFactor).GetImportTexture(baseTexture);
var converted = new MetallicRoughnessConverter(param.MetallicFactor).GetImportTexture(baseTexture.Texture);
converted.name = param.Name;
var info = new TextureLoadInfo(converted, true, false);
m_textureCache.Add(param, info);
m_textureCache.Add(param.Name, info);
return info.Texture;
}
case GetTextureParam.OCCLUSION_PROP:
{
var baseTexture = await GetOrCreateBaseTexture(gltf, param.Index0.Value, false);
var converted = new OcclusionConverter().GetImportTexture(baseTexture);
var converted = new OcclusionConverter().GetImportTexture(baseTexture.Texture);
converted.name = param.Name;
var info = new TextureLoadInfo(converted, true, false);
m_textureCache.Add(param, info);
m_textureCache.Add(param.Name, info);
return info.Texture;
}
default:
{
var baseTexture = await GetOrCreateBaseTexture(gltf, param.Index0.Value, true);
return baseTexture;
return baseTexture.Texture;
}
throw new NotImplementedException();