Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the mesh name unique #533

Merged
merged 1 commit into from
Aug 31, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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