Make the mesh name unique #470

This commit is contained in:
ousttrue 2020-08-31 12:26:18 +09:00
parent 26936c1e04
commit 1b4ef3ed92

View File

@ -20,7 +20,7 @@ namespace UniGLTF
/// <summary>
/// GLTF importer
/// </summary>
public class ImporterContext: IDisposable
public class ImporterContext : IDisposable
{
#region MeasureTime
bool m_showSpeedLog
@ -261,7 +261,7 @@ namespace UniGLTF
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);
}
@ -301,6 +301,8 @@ namespace UniGLTF
// Version Compatibility
RestoreOlderVersionValues();
FixUnique();
// parepare byte buffer
//GLTF.baseDir = System.IO.Path.GetDirectoryName(Path);
foreach (var buffer in GLTF.buffers)
@ -309,6 +311,39 @@ namespace UniGLTF
}
}
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);
@ -374,7 +409,7 @@ namespace UniGLTF
#region Load. Build unity objects
public bool EnableLoadBalancing;
/// <summary>
/// ReadAllBytes, Parse, Create GameObject
/// </summary>
@ -636,7 +671,7 @@ namespace UniGLTF
}
yield return null;
}
IEnumerator BuildMesh(MeshImporter.MeshContext x, int i)
{
using (MeasureTime("BuildMesh"))
@ -644,7 +679,7 @@ namespace UniGLTF
MeshWithMaterials meshWithMaterials;
if (EnableLoadBalancing)
{
var buildMesh = MeshImporter.BuildMeshCoroutine(this, x);
var buildMesh = MeshImporter.BuildMeshCoroutine(this, x);
yield return buildMesh;
meshWithMaterials = buildMesh.Current as MeshWithMaterials;
}
@ -733,9 +768,9 @@ namespace UniGLTF
yield return null;
}
#endregion
#endregion
#region Imported
#region Imported
public GameObject Root;
public List<Transform> Nodes = new List<Transform>();
@ -784,7 +819,7 @@ namespace UniGLTF
{
foreach (var x in Meshes)
{
foreach(var y in x.Renderers)
foreach (var y in x.Renderers)
{
y.enabled = true;
}
@ -862,11 +897,11 @@ namespace UniGLTF
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))
{