Merge pull request #184 from dwango/use_unijson_parser

Use unijson parser
This commit is contained in:
ousttrue
2019-02-28 03:12:55 +09:00
committed by GitHub
5 changed files with 96 additions and 19 deletions

View File

@@ -647,25 +647,81 @@ namespace UniGLTF
Assert.AreNotEqual(gltf.nodes[0].mesh, gltf.nodes[1].mesh);
// import
var context = new ImporterContext();
context.ParseJson(json, new SimpleStorage(new ArraySegment<byte>(new byte[1024 * 1024])));
//Debug.LogFormat("{0}", context.Json);
context.Load();
{
var context = new ImporterContext();
context.ParseJson(json, new SimpleStorage(new ArraySegment<byte>(new byte[1024 * 1024])));
//Debug.LogFormat("{0}", context.Json);
context.Load();
var importedRed = context.Root.transform.GetChild(0);
var importedRedMaterial = importedRed.GetComponent<Renderer>().sharedMaterial;
Assert.AreEqual("red", importedRedMaterial.name);
Assert.AreEqual(Color.red, importedRedMaterial.color);
var importedRed = context.Root.transform.GetChild(0);
var importedRedMaterial = importedRed.GetComponent<Renderer>().sharedMaterial;
Assert.AreEqual("red", importedRedMaterial.name);
Assert.AreEqual(Color.red, importedRedMaterial.color);
var importedBlue = context.Root.transform.GetChild(1);
var importedBlueMaterial = importedBlue.GetComponent<Renderer>().sharedMaterial;
Assert.AreEqual("blue", importedBlueMaterial.name);
Assert.AreEqual(Color.blue, importedBlueMaterial.color);
var importedBlue = context.Root.transform.GetChild(1);
var importedBlueMaterial = importedBlue.GetComponent<Renderer>().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<byte>(new byte[1024 * 1024])));
//Debug.LogFormat("{0}", context.Json);
context.Load();
var importedRed = context.Root.transform.GetChild(0);
var importedRedMaterial = importedRed.GetComponent<Renderer>().sharedMaterial;
Assert.AreEqual("red", importedRedMaterial.name);
Assert.AreEqual(Color.red, importedRedMaterial.color);
var importedBlue = context.Root.transform.GetChild(1);
var importedBlueMaterial = importedBlue.GetComponent<Renderer>().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<Dummy>("{}");
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);
}
}
}

View File

@@ -24,6 +24,11 @@ namespace UniGLTF
*/
}
public glTFBuffer()
{
}
public glTFBuffer(IBytesBuffer storage)
{
Storage = storage;

View File

@@ -141,7 +141,7 @@ namespace UniGLTF
public string name;
[JsonSchema(Required = true, MinItems = 1)]
public List<glTFPrimitives> primitives;
public List<glTFPrimitives> primitives = new List<glTFPrimitives>();
[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<glTFPrimitives>();
}
protected override void SerializeMembers(GLTFJsonFormatter f)

View File

@@ -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<glTF>(Json);
if (UseUniJSONParser)
{
Json.ParseAsJson().Deserialize(ref GLTF);
}
else
{
GLTF = JsonUtility.FromJson<glTF>(Json);
}
if (GLTF.asset.version != "2.0")
{
throw new UniGLTFException("unknown gltf version {0}", GLTF.asset.version);

View File

@@ -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
@@ -208,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)