Merge pull request #798 from ousttrue/fix/gltf_material_name_is_empty

UnitTest追加(GltfSmapleModels, VRM_TEST_MODELS)
This commit is contained in:
ousttrue
2021-03-18 13:40:42 +09:00
committed by GitHub
14 changed files with 268 additions and 26 deletions

View File

@@ -83,7 +83,7 @@ namespace UniGLTF
{
var gltfTexture = parser.GLTF.textures.First(y => y.name == texParam.GltflName);
var gltfImage = parser.GLTF.images[gltfTexture.source];
if (!string.IsNullOrEmpty(gltfImage.uri))
if (!string.IsNullOrEmpty(gltfImage.uri) && !gltfImage.uri.StartsWith("data:"))
{
var child = dir.Child(gltfImage.uri);
var asset = AssetDatabase.LoadAssetAtPath<Texture2D>(child.Value);

View File

@@ -16,7 +16,7 @@ namespace UniGLTF
private static TangentMode GetTangentMode(string interpolation)
{
if (interpolation == glTFAnimationTarget.Interpolations.LINEAR.ToString())
if (string.IsNullOrEmpty(interpolation) || interpolation == glTFAnimationTarget.Interpolations.LINEAR.ToString())
{
return TangentMode.Linear;
}
@@ -30,7 +30,7 @@ namespace UniGLTF
}
else
{
throw new NotImplementedException();
throw new UniGLTFNotSupportedException($"unknown interpolation: {interpolation}");
}
}
@@ -274,7 +274,7 @@ namespace UniGLTF
if (!gltf_mesh_extras_targetNames.TryGet(mesh, out List<string> targetNames))
{
throw new Exception("glTF BlendShape Animation. targetNames invalid.");
throw new UniGLTFNotSupportedException("glTF BlendShape Animation. targetNames invalid.");
}
var keyNames = targetNames

View File

@@ -77,6 +77,8 @@ namespace UniGLTF
Parse(path, File.ReadAllBytes(path));
}
public string TargetPath;
/// <summary>
/// Parse gltf json or Parse json chunk of glb
/// </summary>
@@ -84,6 +86,7 @@ namespace UniGLTF
/// <param name="bytes"></param>
public virtual void Parse(string path, Byte[] bytes)
{
TargetPath = path;
var ext = Path.GetExtension(path).ToLower();
switch (ext)
{
@@ -185,7 +188,7 @@ namespace UniGLTF
{
// empty
mesh.name = "mesh_" + Guid.NewGuid().ToString("N");
Debug.LogWarning($"no name: => {mesh.name}");
// Debug.LogWarning($"mesh.name: => {mesh.name}");
used.Add(mesh.name);
}
else
@@ -195,7 +198,7 @@ namespace UniGLTF
{
// rename
var uname = lower + "_" + Guid.NewGuid().ToString("N");
Debug.LogWarning($"same name: {lower} => {uname}");
// Debug.LogWarning($"mesh.name: {lower} => {uname}");
mesh.name = uname;
lower = uname;
}
@@ -230,9 +233,9 @@ namespace UniGLTF
{
var gltfTexture = GLTF.textures[i];
var gltfImage = GLTF.images[gltfTexture.source];
if (!string.IsNullOrEmpty(gltfImage.uri))
if (!string.IsNullOrEmpty(gltfImage.uri) && !gltfImage.uri.StartsWith("data:"))
{
// from image uri
// from image uri
gltfTexture.name = Path.GetFileNameWithoutExtension(gltfImage.uri);
}
if (string.IsNullOrEmpty(gltfTexture.name))
@@ -261,7 +264,7 @@ namespace UniGLTF
{
// rename
var uname = lower + "_" + Guid.NewGuid().ToString("N");
Debug.LogWarning($"texture.name: {lower} => {uname}");
// Debug.LogWarning($"texture.name: {lower} => {uname}");
gltfTexture.name = uname;
if (!used.Add(uname))
{
@@ -275,11 +278,17 @@ namespace UniGLTF
public void FixMaterialNameUnique()
{
var used = new HashSet<string>();
foreach (var material in GLTF.materials)
for (int i = 0; i < GLTF.materials.Count; ++i)
{
var material = GLTF.materials[i];
var originalName = material.name;
int j = 2;
if (string.IsNullOrEmpty(material.name))
{
material.name = $"material_{i}";
}
while (true)
{
if (used.Add(material.name))

View File

@@ -247,7 +247,7 @@ namespace UniGLTF
foreach (var x in AnimationClips)
{
#if VRM_DEVELOP
Debug.Log($"Destroy {x}");
// Debug.Log($"Destroy {x}");
#endif
destroy(x);
}
@@ -256,7 +256,7 @@ namespace UniGLTF
foreach (var x in Meshes)
{
#if VRM_DEVELOP
Debug.Log($"Destroy {x.Mesh}");
// Debug.Log($"Destroy {x.Mesh}");
#endif
destroy(x.Mesh);
}
@@ -268,7 +268,7 @@ namespace UniGLTF
if (m_ownRoot && Root != null)
{
#if VRM_DEVELOP
Debug.Log($"Destroy {Root}");
// Debug.Log($"Destroy {Root}");
#endif
destroy(Root);
}

View File

@@ -18,18 +18,11 @@ namespace UniGLTF
}
if (task.IsFaulted)
{
if (task.Exception is AggregateException ae && ae.InnerExceptions.Count == 1)
{
throw ae.InnerException;
}
else
{
throw task.Exception;
}
throw new AggregateException(task.Exception);
}
#if VRM_DEVELOP
Debug.Log(meassureTime.GetSpeedLog());
Debug.Log($"{self.Parser.TargetPath}: {meassureTime.GetSpeedLog()}");
#endif
}
}

View File

@@ -93,7 +93,7 @@ namespace UniGLTF
{
// 外部の '.asset' からロードしていない
#if VRM_DEVELOP
Debug.Log($"Destroy {x.Asset}");
// Debug.Log($"Destroy {x.Asset}");
#endif
UnityEngine.Object.DestroyImmediate(x.Asset, false);
}

View File

@@ -69,7 +69,11 @@ namespace UniGLTF
}
else if (occlusionTexture != null)
{
throw new NotImplementedException("occlusion only");
var copyTexture = TextureConverter.CopyTexture(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ImportPixel(default, metallicFactor, roughnessFactor, x)).ToArray());
copyTexture.Apply();
copyTexture.name = occlusionTexture.name;
return copyTexture;
}
else
{

View File

@@ -95,7 +95,7 @@ namespace UniGLTF
if (!kv.Value.IsExternal)
{
#if VRM_DEVELOP
Debug.Log($"Destroy {kv.Value.Texture}");
// Debug.Log($"Destroy {kv.Value.Texture}");
#endif
destroy(kv.Value.Texture);
}
@@ -178,7 +178,11 @@ namespace UniGLTF
case GetTextureParam.TextureTypes.StandardMap:
{
var baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, false);
TextureLoadInfo baseTexture = default;
if (param.Index0.HasValue)
{
baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, false);
}
TextureLoadInfo occlusionBaseTexture = default;
if (param.Index1.HasValue)
{

View File

@@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
using UnityEngine;
namespace UniGLTF
{
public class GltfLoadTests
{
static IEnumerable<FileInfo> EnumerateGltfFiles(DirectoryInfo dir)
{
if (dir.Name == ".git")
{
yield break;
}
foreach (var child in dir.EnumerateDirectories())
{
foreach (var x in EnumerateGltfFiles(child))
{
yield return x;
}
}
foreach (var child in dir.EnumerateFiles())
{
switch (child.Extension.ToLower())
{
case ".gltf":
case ".glb":
yield return child;
break;
}
}
}
static void Message(string path, Exception exception)
{
while (exception.InnerException != null)
{
exception = exception.InnerException;
}
if (exception is UniGLTFNotSupportedException ex)
{
Debug.LogWarning($"LoadError: {path}: {ex}");
}
else
{
Debug.LogError($"LoadError: {path}");
Debug.LogException(exception);
}
}
static void Load(FileInfo gltf, DirectoryInfo root)
{
var parser = new GltfParser();
try
{
parser.ParsePath(gltf.FullName);
}
catch (Exception ex)
{
Debug.LogError($"ParseError: {gltf}");
Debug.LogException(ex);
}
try
{
using (var importer = new ImporterContext(parser))
{
importer.Load();
}
}
catch (Exception ex)
{
Message(gltf.FullName.Substring(root.FullName.Length), ex);
}
}
[Test]
public void GltfSampleModelsTests()
{
var env = System.Environment.GetEnvironmentVariable("GLTF_SAMPLE_MODELS");
if (string.IsNullOrEmpty(env))
{
return;
}
var root = new DirectoryInfo($"{env}/2.0");
if (!root.Exists)
{
return;
}
foreach (var gltf in EnumerateGltfFiles(root))
{
Load(gltf, root);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c0454ecef7a44cf4280a6f0fcacf3666
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bd80effebd71ba445976e6285b714f1c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
using UniGLTF;
using UnityEngine;
namespace VRM
{
public class VRMLoadTest
{
static IEnumerable<FileInfo> EnumerateGltfFiles(DirectoryInfo dir)
{
if (dir.Name == ".git")
{
yield break;
}
foreach (var child in dir.EnumerateDirectories())
{
foreach (var x in EnumerateGltfFiles(child))
{
yield return x;
}
}
foreach (var child in dir.EnumerateFiles())
{
switch (child.Extension.ToLower())
{
case ".vrm":
yield return child;
break;
}
}
}
static void Message(string path, Exception exception)
{
while (exception.InnerException != null)
{
exception = exception.InnerException;
}
{
Debug.LogError($"LoadError: {path}");
Debug.LogException(exception);
}
}
static void Load(FileInfo gltf, DirectoryInfo root)
{
var parser = new GltfParser();
try
{
parser.ParsePath(gltf.FullName);
}
catch (Exception ex)
{
Debug.LogError($"ParseError: {gltf}");
Debug.LogException(ex);
}
try
{
using (var importer = new VRMImporterContext(parser))
{
importer.Load();
}
}
catch (Exception ex)
{
Message(gltf.FullName.Substring(root.FullName.Length), ex);
}
}
[Test]
public void VrmTestModelsTests()
{
var env = System.Environment.GetEnvironmentVariable("VRM_TEST_MODELS");
if (string.IsNullOrEmpty(env))
{
return;
}
var root = new DirectoryInfo(env);
if (!root.Exists)
{
return;
}
foreach (var gltf in EnumerateGltfFiles(root))
{
Load(gltf, root);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f4a4042cfb50bb84495fd90f548683ca
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: