Skip to content

Commit

Permalink
Merge pull request #533 from ousttrue/fix/fix_mesh_dupname
Browse files Browse the repository at this point in the history
Make the mesh name unique
  • Loading branch information
hiroj authored Aug 31, 2020
2 parents 049ed52 + 1b4ef3e commit 4ea6601
Showing 1 changed file with 46 additions and 11 deletions.
57 changes: 46 additions & 11 deletions Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace UniGLTF
/// <summary>
/// GLTF importer
/// </summary>
public class ImporterContext: IDisposable
public class ImporterContext : IDisposable
{
#region MeasureTime
bool m_showSpeedLog
Expand Down Expand Up @@ -261,7 +261,7 @@ public void ParseGlb(Byte[] bytes)
ParseJson(Encoding.UTF8.GetString(jsonBytes.Array, jsonBytes.Offset, jsonBytes.Count),
new SimpleStorage(chunks[1].Bytes));
}
catch(StackOverflowException ex)
catch (StackOverflowException ex)
{
throw new Exception("[UniVRM Import Error] json parsing failed, nesting is too deep.\n" + ex);
}
Expand Down Expand Up @@ -301,6 +301,8 @@ public virtual void ParseJson(string json, IStorage storage)
// Version Compatibility
RestoreOlderVersionValues();

FixUnique();

// parepare byte buffer
//GLTF.baseDir = System.IO.Path.GetDirectoryName(Path);
foreach (var buffer in GLTF.buffers)
Expand All @@ -309,6 +311,39 @@ public virtual void ParseJson(string json, IStorage storage)
}
}

static string MakeUniqueName(string name, HashSet<string> used)
{
for (var i = 0; i < 100; ++i)
{
name = $"{name}_{i}";
if (!used.Contains(name))
{
return name;
}
}

throw new Exception("hobo arienai");
}

void FixUnique()
{
var used = new HashSet<string>();
foreach (var mesh in GLTF.meshes)
{
var lname = mesh.name.ToLower();
if (used.Contains(lname))
{
// rename
var uname = MakeUniqueName(lname, used);
Debug.LogWarning($"same name: {lname} => {uname}");
mesh.name = uname;
lname = uname;
}

used.Add(lname);
}
}

void RestoreOlderVersionValues()
{
var parsed = UniJSON.JsonParser.Parse(Json);
Expand Down Expand Up @@ -374,7 +409,7 @@ void RestoreOlderVersionValues()
#region Load. Build unity objects

public bool EnableLoadBalancing;

/// <summary>
/// ReadAllBytes, Parse, Create GameObject
/// </summary>
Expand Down Expand Up @@ -636,15 +671,15 @@ IEnumerator LoadMaterials()
}
yield return null;
}

IEnumerator BuildMesh(MeshImporter.MeshContext x, int i)
{
using (MeasureTime("BuildMesh"))
{
MeshWithMaterials meshWithMaterials;
if (EnableLoadBalancing)
{
var buildMesh = MeshImporter.BuildMeshCoroutine(this, x);
var buildMesh = MeshImporter.BuildMeshCoroutine(this, x);
yield return buildMesh;
meshWithMaterials = buildMesh.Current as MeshWithMaterials;
}
Expand Down Expand Up @@ -733,9 +768,9 @@ IEnumerator BuildHierarchy()

yield return null;
}
#endregion
#endregion

#region Imported
#region Imported
public GameObject Root;
public List<Transform> Nodes = new List<Transform>();

Expand Down Expand Up @@ -784,7 +819,7 @@ public void ShowMeshes()
{
foreach (var x in Meshes)
{
foreach(var y in x.Renderers)
foreach (var y in x.Renderers)
{
y.enabled = true;
}
Expand Down Expand Up @@ -862,11 +897,11 @@ public virtual bool AvoidOverwriteAndLoad(UnityPath assetPath, UnityEngine.Objec
var loaded = assetPath.LoadAsset<Material>();

// replace component reference
foreach(var mesh in Meshes)
foreach (var mesh in Meshes)
{
foreach(var r in mesh.Renderers)
foreach (var r in mesh.Renderers)
{
for(int i=0; i<r.sharedMaterials.Length; ++i)
for (int i = 0; i < r.sharedMaterials.Length; ++i)
{
if (r.sharedMaterials.Contains(o))
{
Expand Down

0 comments on commit 4ea6601

Please sign in to comment.