public void SerializerCompare() { // Aliciaを古いデシリアライザでロードする var path = AliciaPath; var parser = new GltfParser(); parser.ParseGlb(File.ReadAllBytes(path)); using (var context = new VRMImporterContext(parser)) { var oldJson = context.GLTF.ToJson().ParseAsJson().ToString(" "); // 生成シリアライザでJSON化する var f = new JsonFormatter(); GltfSerializer.Serialize(f, context.GLTF); var parsed = f.ToString().ParseAsJson(); var newJson = parsed.ToString(" "); // File.WriteAllText("old.json", oldJson); // File.WriteAllText("new.json", newJson); // 比較 Assert.AreEqual(oldJson.ParseAsJson().ToString(), newJson.ParseAsJson().ToString()); // 生成デシリアライザでロードする var ff = new JsonFormatter(); var des = GltfDeserializer.Deserialize(parsed); ff.Clear(); GltfSerializer.Serialize(ff, des); var desJson = ff.ToString().ParseAsJson().ToString(" "); Assert.AreEqual(oldJson.ParseAsJson().ToString(), desJson.ParseAsJson().ToString()); } }
public virtual void ParseJson(string json, IStorage storage) { Json = json; Storage = storage; GLTF = GltfDeserializer.Deserialize(json.ParseAsJson()); if (GLTF.asset.version != "2.0") { throw new UniGLTFException("unknown gltf version {0}", GLTF.asset.version); } // Version Compatibility RestoreOlderVersionValues(); FixMeshNameUnique(); FixTextureNameUnique(); FixMaterialNameUnique(); FixNodeName(); // parepare byte buffer //GLTF.baseDir = System.IO.Path.GetDirectoryName(Path); foreach (var buffer in GLTF.buffers) { buffer.OpenStorage(storage); } }
public virtual void ParseJson(string json, IStorage storage) { Json = json; Storage = storage; if (_serializerType == SerializerTypes.UniJSON) { Json.ParseAsJson().Deserialize(ref GLTF); } else if (_serializerType == SerializerTypes.Generated) { GLTF = GltfDeserializer.Deserialize(json.ParseAsJson()); } else if (_serializerType == SerializerTypes.JsonSerializable) { // Obsolete GLTF = JsonUtility.FromJson <glTF>(Json); } if (GLTF.asset.version != "2.0") { throw new UniGLTFException("unknown gltf version {0}", GLTF.asset.version); } // Version Compatibility RestoreOlderVersionValues(); // parepare byte buffer //GLTF.baseDir = System.IO.Path.GetDirectoryName(Path); foreach (var buffer in GLTF.buffers) { buffer.OpenStorage(storage); } }
public static GltfData ParseGltf(string path, string json, IReadOnlyList <GlbChunk> chunks, IStorage storage, MigrationFlags migrationFlags) { var GLTF = GltfDeserializer.Deserialize(json.ParseAsJson()); if (GLTF.asset.version != "2.0") { throw new UniGLTFException("unknown gltf version {0}", GLTF.asset.version); } // Version Compatibility RestoreOlderVersionValues(json, GLTF); FixMeshNameUnique(GLTF); foreach (var image in GLTF.images) { image.uri = PrepareUri(image.uri); } FixTextureNameUnique(GLTF); FixMaterialNameUnique(GLTF); FixNodeName(GLTF); FixAnimationNameUnique(GLTF); // parepare byte buffer //GLTF.baseDir = System.IO.Path.GetDirectoryName(Path); foreach (var buffer in GLTF.buffers) { buffer.OpenStorage(storage); } return(new GltfData(path, json, GLTF, chunks, storage, migrationFlags)); }
/// <summary> /// Unitテスト向けにバッファを比較する /// </summary> public static void AssertEqualBuffers(this Glb lhs, Glb rhs) { var lJson = lhs.Json.Bytes.ParseAsJson(); var rJson = rhs.Json.Bytes.ParseAsJson(); var lGltf = GltfDeserializer.Deserialize(lJson); var rGltf = GltfDeserializer.Deserialize(rJson); //////////////////////////////////////////////////////////// // compare buffer // Meshは同数で同じ順番で並んでいるであろう //////////////////////////////////////////////////////////// var lBytes = lhs.Binary.Bytes; var rBytes = rhs.Binary.Bytes; if (lGltf.meshes.Count != rGltf.meshes.Count) { throw new Exception($"differenct mesh count"); } for (var x = 0; x < lGltf.meshes.Count; ++x) { var lMesh = lGltf.meshes[x]; var rMesh = rGltf.meshes[x]; if (lMesh.primitives.Count != rMesh.primitives.Count) { throw new Exception($"mesh[{x}]: different primitive count"); } for (var y = 0; y < lMesh.primitives.Count; ++y) { var lPrim = lMesh.primitives[y]; var rPrim = rMesh.primitives[y]; if (!IsAccessorEquals( lGltf, lPrim.indices, lBytes, rGltf, rPrim.indices, rBytes)) { throw new Exception($"mesh[{x}].primitives[{y}].indices"); } if (!IsAccessorEquals( lGltf, lPrim.attributes.POSITION, lBytes, rGltf, rPrim.attributes.POSITION, rBytes)) { throw new Exception($"mesh[{x}].primitives[{y}].attributes.POSITION"); } if (!IsAccessorEquals( lGltf, lPrim.attributes.NORMAL, lBytes, rGltf, rPrim.attributes.NORMAL, rBytes)) { throw new Exception($"mesh[{x}].primitives[{y}].attributes.NORMAL"); } if (!IsAccessorEquals( lGltf, lPrim.attributes.TEXCOORD_0, lBytes, rGltf, rPrim.attributes.TEXCOORD_0, rBytes)) { throw new Exception($"mesh[{x}].primitives[{y}].attributes.TEXCOORD_0"); } if (!IsAccessorEquals( lGltf, lPrim.attributes.TANGENT, lBytes, rGltf, rPrim.attributes.TANGENT, rBytes)) { throw new Exception($"mesh[{x}].primitives[{y}].attributes.TANGENTS"); } if (!IsAccessorEquals( lGltf, lPrim.attributes.COLOR_0, lBytes, rGltf, rPrim.attributes.COLOR_0, rBytes)) { throw new Exception($"mesh[{x}].primitives[{y}].attributes.COLOR_0"); } if (!IsAccessorEquals( lGltf, lPrim.attributes.JOINTS_0, lBytes, rGltf, rPrim.attributes.JOINTS_0, rBytes)) { throw new Exception($"mesh[{x}].primitives[{y}].attributes.JOINTS_0"); } if (!IsAccessorEquals( lGltf, lPrim.attributes.WEIGHTS_0, lBytes, rGltf, rPrim.attributes.WEIGHTS_0, rBytes)) { throw new Exception($"mesh[{x}].primitives[{y}].attributes.WEIGHTS_0"); } // マテリアルで使っているテクスチャを比較する var lMaterial = lGltf.materials[lPrim.material]; var rMaterial = rGltf.materials[rPrim.material]; // baseColor if (!IsTextureEquals( lGltf, lMaterial.pbrMetallicRoughness?.baseColorTexture, lBytes, rGltf, rMaterial.pbrMetallicRoughness?.baseColorTexture, rBytes )) { throw new Exception($"mesh[{x}].primitives[{y}].material.pbrMetallicRoughness.baseColorTexture"); } if (!IsTextureEquals( lGltf, lMaterial.pbrMetallicRoughness?.metallicRoughnessTexture, lBytes, rGltf, rMaterial.pbrMetallicRoughness?.metallicRoughnessTexture, rBytes )) { throw new Exception($"mesh[{x}].primitives[{y}].material.pbrMetallicRoughness.metallicRoughnessTexture"); } if (!IsTextureEquals( lGltf, lMaterial.emissiveTexture, lBytes, rGltf, rMaterial.emissiveTexture, rBytes )) { throw new Exception($"mesh[{x}].primitives[{y}].material.emissiveTexture"); } if (!IsTextureEquals( lGltf, lMaterial.normalTexture, lBytes, rGltf, rMaterial.normalTexture, rBytes )) { throw new Exception($"mesh[{x}].primitives[{y}].material.normalTexture"); } if (!IsTextureEquals( lGltf, lMaterial.occlusionTexture, lBytes, rGltf, rMaterial.occlusionTexture, rBytes )) { throw new Exception($"mesh[{x}].primitives[{y}].material.occlusionTexture"); } } } }