Skip to content

Commit

Permalink
Merge pull request #1083 from Santarh/fixDuplicatedSufix
Browse files Browse the repository at this point in the history
Add unique suffix to same name assets.
  • Loading branch information
ousttrue authored Jun 30, 2021
2 parents 164b34f + e1c897b commit 8b645b0
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void ExtractMaterialsAndTextures(ScriptedImporter self, GltfData data, ITextureD
.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,
Expand Down
10 changes: 5 additions & 5 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,28 @@ public sealed class GltfData
/// 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>
Expand Down
92 changes: 32 additions & 60 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/GlbLowLevelParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,7 +45,7 @@ public GltfData Parse()
throw;
}
}

public static List<GlbChunk> ParseGlbChunks(byte[] data)
{
var chunks = glbImporter.ParseGlbChunks(data);
Expand Down Expand Up @@ -97,7 +98,7 @@ public static GltfData ParseGltf(string path, string json, IReadOnlyList<GlbChun

return new GltfData(path, json, GLTF, chunks, storage, migrationFlags);
}

private static void FixMeshNameUnique(glTF GLTF)
{
var used = new HashSet<string>();
Expand Down Expand Up @@ -171,9 +172,9 @@ public static string PrepareUri(string uri)
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:"))
{
Expand All @@ -187,61 +188,26 @@ private static void FixTextureNameUnique(glTF GLTF)
}
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);
}
}

Expand All @@ -266,25 +232,13 @@ private static void FixAnimationNameUnique(glTF GLTF)
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);
}
}

Expand Down Expand Up @@ -319,5 +273,23 @@ public static void AppendImageExtension(glTFImage texture, string extension)
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;
}
}
}
}
}
32 changes: 32 additions & 0 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine;
using VRMShaders;

Expand Down Expand Up @@ -372,6 +373,37 @@ public virtual void Export(MeshExportSettings meshExportSettings, ITextureSerial
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
}
Expand Down
3 changes: 3 additions & 0 deletions Assets/VRM10/Runtime/IO/Vrm10Exporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ public void Export(GameObject root, Model model, ModelExporter converter, Export
{
UniGLTF.Extensions.VRMC_springBone.GltfSerializer.SerializeTo(ref Storage.Gltf.extensions, vrmSpringBone);
}

// Fix Duplicated name
gltfExporter.FixName(Storage.Gltf);
}

/// <summary>
Expand Down

0 comments on commit 8b645b0

Please sign in to comment.