diff --git a/Assets/VRM/UniGLTF/Editor/Tests/UniGLTFTests.cs b/Assets/VRM/UniGLTF/Editor/Tests/UniGLTFTests.cs index b9d5677426..ce5d0ebeea 100644 --- a/Assets/VRM/UniGLTF/Editor/Tests/UniGLTFTests.cs +++ b/Assets/VRM/UniGLTF/Editor/Tests/UniGLTFTests.cs @@ -647,25 +647,81 @@ public void SameMeshButDifferentMaterialExport() Assert.AreNotEqual(gltf.nodes[0].mesh, gltf.nodes[1].mesh); // import - var context = new ImporterContext(); - context.ParseJson(json, new SimpleStorage(new ArraySegment(new byte[1024 * 1024]))); - //Debug.LogFormat("{0}", context.Json); - context.Load(); - - var importedRed = context.Root.transform.GetChild(0); - var importedRedMaterial = importedRed.GetComponent().sharedMaterial; - Assert.AreEqual("red", importedRedMaterial.name); - Assert.AreEqual(Color.red, importedRedMaterial.color); + { + var context = new ImporterContext(); + context.ParseJson(json, new SimpleStorage(new ArraySegment(new byte[1024 * 1024]))); + //Debug.LogFormat("{0}", context.Json); + context.Load(); + + var importedRed = context.Root.transform.GetChild(0); + var importedRedMaterial = importedRed.GetComponent().sharedMaterial; + Assert.AreEqual("red", importedRedMaterial.name); + Assert.AreEqual(Color.red, importedRedMaterial.color); + + var importedBlue = context.Root.transform.GetChild(1); + var importedBlueMaterial = importedBlue.GetComponent().sharedMaterial; + Assert.AreEqual("blue", importedBlueMaterial.name); + Assert.AreEqual(Color.blue, importedBlueMaterial.color); + } - var importedBlue = context.Root.transform.GetChild(1); - var importedBlueMaterial = importedBlue.GetComponent().sharedMaterial; - Assert.AreEqual("blue", importedBlueMaterial.name); - Assert.AreEqual(Color.blue, importedBlueMaterial.color); + // import new version + { + var context = new ImporterContext + { + UseUniJSONParser = true + }; + context.ParseJson(json, new SimpleStorage(new ArraySegment(new byte[1024 * 1024]))); + //Debug.LogFormat("{0}", context.Json); + context.Load(); + + var importedRed = context.Root.transform.GetChild(0); + var importedRedMaterial = importedRed.GetComponent().sharedMaterial; + Assert.AreEqual("red", importedRedMaterial.name); + Assert.AreEqual(Color.red, importedRedMaterial.color); + + var importedBlue = context.Root.transform.GetChild(1); + var importedBlueMaterial = importedBlue.GetComponent().sharedMaterial; + Assert.AreEqual("blue", importedBlueMaterial.name); + Assert.AreEqual(Color.blue, importedBlueMaterial.color); + } } finally { GameObject.DestroyImmediate(go); } } + + [Serializable] + class CantConstruct + { + public bool Value = true; + + public CantConstruct(bool value) + { + throw new Exception(); + } + } + + [Serializable] + class Dummy + { + public CantConstruct Value; + } + + [Test] + public void JsonUtilityTest() + { + var dummy = JsonUtility.FromJson("{}"); + Assert.NotNull(dummy.Value); + Assert.False(dummy.Value.Value); + } + + [Test] + public void UniJSONTest() + { + var dummy = default(Dummy); + "{}".ParseAsJson().Deserialize(ref dummy); + Assert.Null(dummy.Value); + } } } diff --git a/Assets/VRM/UniGLTF/Scripts/Format/glTFBuffer.cs b/Assets/VRM/UniGLTF/Scripts/Format/glTFBuffer.cs index c47975e4f0..14f2cd7adb 100644 --- a/Assets/VRM/UniGLTF/Scripts/Format/glTFBuffer.cs +++ b/Assets/VRM/UniGLTF/Scripts/Format/glTFBuffer.cs @@ -24,6 +24,11 @@ public void OpenStorage(IStorage storage) */ } + public glTFBuffer() + { + + } + public glTFBuffer(IBytesBuffer storage) { Storage = storage; diff --git a/Assets/VRM/UniGLTF/Scripts/Format/glTFMesh.cs b/Assets/VRM/UniGLTF/Scripts/Format/glTFMesh.cs index 315041653a..072084d3da 100644 --- a/Assets/VRM/UniGLTF/Scripts/Format/glTFMesh.cs +++ b/Assets/VRM/UniGLTF/Scripts/Format/glTFMesh.cs @@ -141,7 +141,7 @@ public class glTFMesh : JsonSerializableBase public string name; [JsonSchema(Required = true, MinItems = 1)] - public List primitives; + public List primitives = new List(); [JsonSchema(MinItems = 1)] public float[] weights; @@ -150,10 +150,13 @@ public class glTFMesh : JsonSerializableBase public object extensions; public object extras; + public glTFMesh() + { + } + public glTFMesh(string _name) { name = _name; - primitives = new List(); } protected override void SerializeMembers(GLTFJsonFormatter f) diff --git a/Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs b/Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs index 82c78e51ee..096699a06e 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs @@ -6,6 +6,7 @@ using System.Text; using System.Collections; using DepthFirstScheduler; +using UniJSON; #if UNITY_EDITOR using UnityEditor; #endif @@ -259,12 +260,21 @@ public void ParseGlb(Byte[] bytes) new SimpleStorage(chunks[1].Bytes)); } + public bool UseUniJSONParser; public virtual void ParseJson(string json, IStorage storage) { Json = json; Storage = storage; - GLTF = JsonUtility.FromJson(Json); + if (UseUniJSONParser) + { + Json.ParseAsJson().Deserialize(ref GLTF); + } + else + { + GLTF = JsonUtility.FromJson(Json); + } + if (GLTF.asset.version != "2.0") { throw new UniGLTFException("unknown gltf version {0}", GLTF.asset.version); diff --git a/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs b/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs index 8e9cd4b031..8453792ccf 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs @@ -89,10 +89,13 @@ public virtual Material CreateMaterial(int i, glTFMaterial x) if (x.extensions != null && x.extensions.KHR_materials_unlit != null) { // texture - var texture = m_context.GetTexture(x.pbrMetallicRoughness.baseColorTexture.index); - if (texture != null) + if (x.pbrMetallicRoughness.baseColorTexture != null) { - material.mainTexture = texture.Texture; + var texture = m_context.GetTexture(x.pbrMetallicRoughness.baseColorTexture.index); + if (texture != null) + { + material.mainTexture = texture.Texture; + } } // color @@ -208,7 +211,7 @@ public virtual Material CreateMaterial(int i, glTFMaterial x) material.SetColor("_EmissionColor", new Color(x.emissiveFactor[0], x.emissiveFactor[1], x.emissiveFactor[2])); } - if (x.emissiveTexture.index != -1) + if (x.emissiveTexture != null && x.emissiveTexture.index != -1) { var texture = Context.GetTexture(x.emissiveTexture.index); if (texture != null)