Add unique suffix to same name assets.

This commit is contained in:
Masataka SUMI
2021-06-29 22:23:03 +09:00
parent dfbb953956
commit e1c897b220
5 changed files with 73 additions and 66 deletions

View File

@@ -90,7 +90,7 @@ namespace UniGLTF
.ToDictionary(kv => kv.Item1, kv => kv.Item2)
;
var assetPath = UnityPath.FromFullpath(data.TargetPath);
var assetPath = UnityPath.FromFullpath(self.assetPath);
var dirName = textureDir(assetPath.Value); // $"{assetPath.FileNameWithoutExtension}.Textures";
TextureExtractor.ExtractTextures(
data,

View File

@@ -14,28 +14,28 @@ namespace UniGLTF
/// Maybe empty if source file was on memory.
/// </summary>
public string TargetPath { get; }
/// <summary>
/// JSON source
/// </summary>
public string Json { get; }
/// <summary>
/// GLTF parsed from JSON
/// </summary>
public glTF GLTF { get; }
/// <summary>
/// Chunk Data.
/// Maybe empty if source file was not glb format.
/// </summary>
public IReadOnlyList<GlbChunk> Chunks { get; }
/// <summary>
/// URI access
/// </summary>
public IStorage Storage { get; }
/// <summary>
/// Migration Flags used by ImporterContext
/// </summary>

View File

@@ -12,9 +12,10 @@ namespace UniGLTF
/// </summary>
public sealed class GlbLowLevelParser
{
public static readonly string UniqueFixResourceSuffix = "__UNIGLTF__DUPLICATED__";
private readonly string _path;
private readonly byte[] _binary;
public GlbLowLevelParser(string path, byte[] specifiedBinary)
{
_path = path;
@@ -44,7 +45,7 @@ namespace UniGLTF
throw;
}
}
public static List<GlbChunk> ParseGlbChunks(byte[] data)
{
var chunks = glbImporter.ParseGlbChunks(data);
@@ -97,7 +98,7 @@ namespace UniGLTF
return new GltfData(path, json, GLTF, chunks, storage, migrationFlags);
}
private static void FixMeshNameUnique(glTF GLTF)
{
var used = new HashSet<string>();
@@ -171,9 +172,9 @@ namespace UniGLTF
private static void FixTextureNameUnique(glTF GLTF)
{
var used = new HashSet<string>();
for (int i = 0; i < GLTF.textures.Count; ++i)
for (var textureIdx = 0; textureIdx < GLTF.textures.Count; ++textureIdx)
{
var gltfTexture = GLTF.textures[i];
var gltfTexture = GLTF.textures[textureIdx];
var gltfImage = GLTF.images[gltfTexture.source];
if (!string.IsNullOrEmpty(gltfImage.uri) && !gltfImage.uri.StartsWith("data:"))
{
@@ -187,61 +188,26 @@ namespace UniGLTF
}
if (string.IsNullOrEmpty(gltfTexture.name))
{
// no name
var newName = $"texture_{i}";
if (!used.Add(newName))
{
newName = "texture_" + Guid.NewGuid().ToString("N");
if (!used.Add(newName))
{
throw new Exception();
}
}
gltfTexture.name = newName;
}
else
{
var lower = gltfTexture.name.ToLower();
if (!used.Add(lower))
{
// rename
var uname = lower + "_" + Guid.NewGuid().ToString("N");
// Debug.LogWarning($"texture.name: {lower} => {uname}");
gltfTexture.name = uname;
if (!used.Add(uname))
{
throw new Exception();
}
}
gltfTexture.name = $"texture_{textureIdx}";
}
gltfTexture.name = FixNameUnique(used, gltfTexture.name);
}
}
private static void FixMaterialNameUnique(glTF GLTF)
{
var used = new HashSet<string>();
for (int i = 0; i < GLTF.materials.Count; ++i)
for (var materialIdx = 0; materialIdx < GLTF.materials.Count; ++materialIdx)
{
var material = GLTF.materials[i];
var originalName = material.name;
int j = 2;
var material = GLTF.materials[materialIdx];
if (string.IsNullOrEmpty(material.name))
{
material.name = $"material_{i}";
material.name = $"material_{materialIdx}";
}
while (true)
{
if (used.Add(material.name))
{
#if VRM_DEVELOP
// Debug.Log($"Material: {material.name}");
#endif
break;
}
material.name = string.Format("{0}({1})", originalName, j++);
}
material.name = FixNameUnique(used, material.name);
}
}
@@ -266,25 +232,13 @@ namespace UniGLTF
for (int i = 0; i < GLTF.animations.Count; ++i)
{
var animation = GLTF.animations[i];
var originalName = animation.name;
int j = 2;
if (string.IsNullOrEmpty(animation.name))
{
animation.name = $"animation_{i}";
}
while (true)
{
if (used.Add(animation.name))
{
#if VRM_DEVELOP
// Debug.Log($"Material: {material.name}");
#endif
break;
}
animation.name = string.Format("{0}({1})", originalName, j++);
}
animation.name = FixNameUnique(used, animation.name);
}
}
@@ -319,5 +273,23 @@ namespace UniGLTF
texture.name = texture.name + extension;
}
}
private static string FixNameUnique(HashSet<string> used, string originalName)
{
if (used.Add(originalName))
{
return originalName;
}
var duplicatedIdx = 2;
while (true)
{
var newName = $"{originalName}{UniqueFixResourceSuffix}{duplicatedIdx++}";
if (used.Add(newName))
{
return newName;
}
}
}
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine;
using VRMShaders;
@@ -372,6 +373,37 @@ namespace UniGLTF
var (unityTexture, colorSpace) = exported[exportedTextureIdx];
glTF.PushGltfTexture(bufferIndex, unityTexture, colorSpace, textureSerializer);
}
FixName(glTF);
}
/// <summary>
/// GlbLowPevelParser.FixNameUnique で付与した Suffix を remove
/// </summary>
public static void FixName(glTF gltf)
{
var regex = new Regex($@"{GlbLowLevelParser.UniqueFixResourceSuffix}\d+$");
foreach (var gltfImages in gltf.images)
{
if (regex.IsMatch(gltfImages.name))
{
gltfImages.name = regex.Replace(gltfImages.name, string.Empty);
}
}
foreach (var gltfMaterial in gltf.materials)
{
if (regex.IsMatch(gltfMaterial.name))
{
gltfMaterial.name = regex.Replace(gltfMaterial.name, string.Empty);
}
}
foreach (var gltfAnimation in gltf.animations)
{
if (regex.IsMatch(gltfAnimation.name))
{
gltfAnimation.name = regex.Replace(gltfAnimation.name, string.Empty);
}
}
}
#endregion
}

View File

@@ -203,6 +203,9 @@ namespace UniVRM10
{
UniGLTF.Extensions.VRMC_springBone.GltfSerializer.SerializeTo(ref Storage.Gltf.extensions, vrmSpringBone);
}
// Fix Duplicated name
gltfExporter.FixName(Storage.Gltf);
}
/// <summary>