From aa7a974b8f15e31272d95fac25b348b0236f513f Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 6 Feb 2019 16:48:21 +0900 Subject: [PATCH 1/4] Add JsonUtilityTest --- .../VRM/UniGLTF/Editor/Tests/UniGLTFTests.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Assets/VRM/UniGLTF/Editor/Tests/UniGLTFTests.cs b/Assets/VRM/UniGLTF/Editor/Tests/UniGLTFTests.cs index b9d567742..d46ed821f 100644 --- a/Assets/VRM/UniGLTF/Editor/Tests/UniGLTFTests.cs +++ b/Assets/VRM/UniGLTF/Editor/Tests/UniGLTFTests.cs @@ -667,5 +667,31 @@ namespace UniGLTF 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); + } } } From a0f23d3739b6f54c72b78ac2517055e6f9825259 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 6 Feb 2019 17:07:30 +0900 Subject: [PATCH 2/4] Add default constructor --- Assets/VRM/UniGLTF/Scripts/Format/glTFBuffer.cs | 5 +++++ Assets/VRM/UniGLTF/Scripts/Format/glTFMesh.cs | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Assets/VRM/UniGLTF/Scripts/Format/glTFBuffer.cs b/Assets/VRM/UniGLTF/Scripts/Format/glTFBuffer.cs index c47975e4f..14f2cd7ad 100644 --- a/Assets/VRM/UniGLTF/Scripts/Format/glTFBuffer.cs +++ b/Assets/VRM/UniGLTF/Scripts/Format/glTFBuffer.cs @@ -24,6 +24,11 @@ namespace UniGLTF */ } + 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 315041653..072084d3d 100644 --- a/Assets/VRM/UniGLTF/Scripts/Format/glTFMesh.cs +++ b/Assets/VRM/UniGLTF/Scripts/Format/glTFMesh.cs @@ -141,7 +141,7 @@ namespace UniGLTF 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 @@ namespace UniGLTF public object extensions; public object extras; + public glTFMesh() + { + } + public glTFMesh(string _name) { name = _name; - primitives = new List(); } protected override void SerializeMembers(GLTFJsonFormatter f) From 0ae38fadb20d76c0f036030ac4c5ba97afc413f8 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 6 Feb 2019 17:08:29 +0900 Subject: [PATCH 3/4] Add ImporterContext.UseUniJSONParser. Add Test. Add nullcheck. --- .../VRM/UniGLTF/Editor/Tests/UniGLTFTests.cs | 56 ++++++++++++++----- .../VRM/UniGLTF/Scripts/IO/ImporterContext.cs | 12 +++- .../UniGLTF/Scripts/IO/MaterialImporter.cs | 9 ++- 3 files changed, 60 insertions(+), 17 deletions(-) diff --git a/Assets/VRM/UniGLTF/Editor/Tests/UniGLTFTests.cs b/Assets/VRM/UniGLTF/Editor/Tests/UniGLTFTests.cs index d46ed821f..ce5d0ebee 100644 --- a/Assets/VRM/UniGLTF/Editor/Tests/UniGLTFTests.cs +++ b/Assets/VRM/UniGLTF/Editor/Tests/UniGLTFTests.cs @@ -647,20 +647,43 @@ namespace UniGLTF 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 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 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 { @@ -685,7 +708,6 @@ namespace UniGLTF public CantConstruct Value; } - [Test] public void JsonUtilityTest() { @@ -693,5 +715,13 @@ namespace UniGLTF 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/IO/ImporterContext.cs b/Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs index 82c78e51e..096699a06 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs @@ -6,6 +6,7 @@ using System.IO; using System.Text; using System.Collections; using DepthFirstScheduler; +using UniJSON; #if UNITY_EDITOR using UnityEditor; #endif @@ -259,12 +260,21 @@ namespace UniGLTF 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 8e9cd4b03..26311424e 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs @@ -89,10 +89,13 @@ namespace UniGLTF 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 From 839847e19c15877d8ea23e333c7e9738b7ff7985 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 6 Feb 2019 20:32:26 +0900 Subject: [PATCH 4/4] Fix null check --- Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs b/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs index 26311424e..8453792cc 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs @@ -211,7 +211,7 @@ namespace UniGLTF 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)