FixTextureAndImageNameUnique

This commit is contained in:
ousttrue 2021-02-25 16:57:13 +09:00
parent 6d690d0892
commit c0719ecbf1
7 changed files with 108 additions and 82 deletions

View File

@ -82,20 +82,6 @@ namespace UniGLTF
}
}
// public void ExtractTextures()
// {
// // extract textures to files
// this.ExtractTextures(TextureDirName);
// // reimport
// AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
// }
// public void ExtractMaterials()
// {
// this.ExtractAssets<UnityEngine.Material>(MaterialDirName, ".mat");
// AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
// }
public void ExtractMaterialsAndTextures()
{
this.ExtractTextures(TextureDirName, () =>
@ -105,11 +91,6 @@ namespace UniGLTF
});
}
// public Dictionary<string, T> GetExternalUnityObjects<T>() where T : UnityEngine.Object
// {
// return this.GetExternalObjectMap().Where(x => x.Key.type == typeof(T)).ToDictionary(x => x.Key.name, x => (T)x.Value);
// }
public void SetExternalUnityObject<T>(UnityEditor.AssetImporter.SourceAssetIdentifier sourceAssetIdentifier, T obj) where T : UnityEngine.Object
{
this.AddRemap(sourceAssetIdentifier, obj);

View File

@ -172,12 +172,5 @@ namespace UniGLTF
&& animations.SequenceEqual(other.animations)
;
}
public string ImageNameFromTextureIndex(int index)
{
var gltfTexture = textures[index];
var glTFImage = images[gltfTexture.source];
return glTFImage.name;
}
}
}

View File

@ -164,7 +164,7 @@ namespace UniGLTF
RestoreOlderVersionValues();
FixMeshNameUnique();
FixImageNameUnique();
FixTextureAndImageNameUnique();
FixMaterialNameUnique();
FixNodeName();
@ -219,49 +219,90 @@ namespace UniGLTF
}
}
void FixImageNameUnique()
/// <summary>
/// gltfTexture.name を Unity Asset 名として運用する。
/// ユニークである必要がある。
/// </summary>
void FixTextureAndImageNameUnique()
{
var used = new HashSet<string>();
for (int i = 0; i < GLTF.images.Count; ++i)
{
var image = GLTF.images[i];
if (string.IsNullOrEmpty(image.name))
// process images
var used = new HashSet<string>();
for (int i = 0; i < GLTF.images.Count; ++i)
{
RenameImageFromTexture(i);
}
if (string.IsNullOrEmpty(image.name))
{
var newName = $"image_{i}";
if (!used.Add(newName))
var image = GLTF.images[i];
if (string.IsNullOrEmpty(image.name))
{
newName = "image_" + Guid.NewGuid().ToString("N");
RenameImageFromTexture(i);
}
if (string.IsNullOrEmpty(image.name))
{
var newName = $"image_{i}";
if (!used.Add(newName))
{
throw new Exception();
newName = "image_" + Guid.NewGuid().ToString("N");
if (!used.Add(newName))
{
throw new Exception();
}
}
image.name = newName;
// Debug.LogWarning($"no name: => {image.name}");
}
image.name = newName;
// Debug.LogWarning($"no name: => {image.name}");
}
else
{
var lower = image.name.ToLower();
if (used.Contains(lower))
else
{
// rename
var uname = lower + "_" + Guid.NewGuid().ToString("N");
Debug.LogWarning($"same name: {lower} => {uname}");
image.name = uname;
lower = uname;
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);
}
used.Add(lower);
}
}
var ext = GetTextureExtension(i);
if (!string.IsNullOrEmpty(ext))
{
// process textures
var used = new HashSet<string>();
for (int i = 0; i < GLTF.textures.Count; ++i)
{
AppendImageExtension(image, ext);
var gltfTexture = GLTF.textures[i];
if (string.IsNullOrEmpty(gltfTexture.name))
{
// use image name
gltfTexture.name = GLTF.images[gltfTexture.source].name;
}
if (string.IsNullOrEmpty(gltfTexture.name))
{
var newName = $"image_{i}";
if (!used.Add(newName))
{
newName = "image_" + Guid.NewGuid().ToString("N");
if (!used.Add(newName))
{
throw new Exception();
}
}
gltfTexture.name = newName;
}
else
{
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);
}
}
}
}

View File

@ -98,16 +98,16 @@ namespace UniGLTF
throw new UniGLTFNotSupportedException("draco is not supported");
}
using (MeasureTime("LoadTextures"))
{
for (int i = 0; i < GLTF.materials.Count; ++i)
{
foreach (var param in MaterialFactory.EnumerateGetTextureparam(i))
{
await m_textureFactory.GetTextureAsync(GLTF, param);
}
}
}
// using (MeasureTime("LoadTextures"))
// {
// for (int i = 0; i < GLTF.materials.Count; ++i)
// {
// foreach (var param in MaterialFactory.EnumerateGetTextureparam(i))
// {
// await m_textureFactory.GetTextureAsync(GLTF, param);
// }
// }
// }
using (MeasureTime("LoadMaterials"))
{

View File

@ -96,6 +96,11 @@ namespace UniGLTF
return m_materials[index].Asset;
}
/// <summary>
/// テクスチャ生成
/// </summary>
/// <param name="getTexture"></param>
/// <returns></returns>
public async Awaitable LoadMaterialsAsync(GetTextureAsyncFunc getTexture)
{
if (m_gltf.materials == null || m_gltf.materials.Count == 0)

View File

@ -1,4 +1,6 @@
namespace UniGLTF
using System;
namespace UniGLTF
{
public struct GetTextureParam
{
@ -18,7 +20,12 @@
public GetTextureParam(string name, string textureType, float metallicFactor, int i0, int i1, int i2, int i3, int i4, int i5)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException();
}
Name = name;
TextureType = textureType;
MetallicFactor = metallicFactor;
Index0 = (ushort)i0;
@ -29,10 +36,10 @@
Index5 = (ushort)i5;
}
public static GetTextureParam Create(glTF gltf, int index)
public static GetTextureParam Create(glTF gltf, int textureIndex)
{
var name = gltf.ImageNameFromTextureIndex(index);
return new GetTextureParam(name, default, default, index, default, default, default, default, default);
var name = gltf.textures[textureIndex].name;
return new GetTextureParam(name, default, default, textureIndex, default, default, default, default, default);
}
public static GetTextureParam Create(glTF gltf, int index, string prop)
@ -53,22 +60,22 @@
}
}
public static GetTextureParam CreateNormal(glTF gltf, int index)
public static GetTextureParam CreateNormal(glTF gltf, int textureIndex)
{
var name = gltf.ImageNameFromTextureIndex(index);
return new GetTextureParam(name, NORMAL_PROP, default, index, default, default, default, default, default);
var name = gltf.textures[textureIndex].name;
return new GetTextureParam(name, NORMAL_PROP, default, textureIndex, default, default, default, default, default);
}
public static GetTextureParam CreateMetallic(glTF gltf, int index, float metallicFactor)
public static GetTextureParam CreateMetallic(glTF gltf, int textureIndex, float metallicFactor)
{
var name = gltf.ImageNameFromTextureIndex(index);
return new GetTextureParam(name, METALLIC_GLOSS_PROP, metallicFactor, index, default, default, default, default, default);
var name = gltf.textures[textureIndex].name;
return new GetTextureParam(name, METALLIC_GLOSS_PROP, metallicFactor, textureIndex, default, default, default, default, default);
}
public static GetTextureParam CreateOcclusion(glTF gltf, int index)
public static GetTextureParam CreateOcclusion(glTF gltf, int textureIndex)
{
var name = gltf.ImageNameFromTextureIndex(index);
return new GetTextureParam(name, OCCLUSION_PROP, default, index, default, default, default, default, default);
var name = gltf.textures[textureIndex].name;
return new GetTextureParam(name, OCCLUSION_PROP, default, textureIndex, default, default, default, default, default);
}
}
}

View File

@ -26,7 +26,6 @@ namespace UniGLTF
public static async Awaitable<Texture2D> LoadTextureAsync(glTF gltf, IStorage storage, int textureIndex)
{
string textureName = default;
var imageBytes = await Awaitable.Run(() =>
{
var imageIndex = gltf.textures[textureIndex].source;
@ -43,7 +42,7 @@ namespace UniGLTF
var sampler = gltf.GetSamplerFromTextureIndex(textureIndex);
var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, isLinear);
texture.name = textureName;
texture.name = gltf.textures[textureIndex].name;
if (imageBytes != null)
{
texture.LoadImage(imageBytes);