Skip to content

Commit

Permalink
Merge pull request #184 from dwango/use_unijson_parser
Browse files Browse the repository at this point in the history
Use unijson parser
  • Loading branch information
ousttrue authored Feb 27, 2019
2 parents bedf655 + 839847e commit 5654604
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 20 deletions.
82 changes: 69 additions & 13 deletions Assets/VRM/UniGLTF/Editor/Tests/UniGLTFTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 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 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);
}
}
}
5 changes: 5 additions & 0 deletions Assets/VRM/UniGLTF/Scripts/Format/glTFBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public void OpenStorage(IStorage storage)
*/
}

public glTFBuffer()
{

}

public glTFBuffer(IBytesBuffer storage)
{
Storage = storage;
Expand Down
7 changes: 5 additions & 2 deletions Assets/VRM/UniGLTF/Scripts/Format/glTFMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public class glTFMesh : JsonSerializableBase
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;
Expand All @@ -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<glTFPrimitives>();
}

protected override void SerializeMembers(GLTFJsonFormatter f)
Expand Down
12 changes: 11 additions & 1 deletion Assets/VRM/UniGLTF/Scripts/IO/ImporterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text;
using System.Collections;
using DepthFirstScheduler;
using UniJSON;
#if UNITY_EDITOR
using UnityEditor;
#endif
Expand Down Expand Up @@ -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<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);
Expand Down
11 changes: 7 additions & 4 deletions Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5654604

Please sign in to comment.