public static byte[] Migrate(byte[] src) { var glb = UniGLTF.Glb.Parse(src); var json = glb.Json.Bytes.ParseAsJson(); var gltf = UniGLTF.GltfDeserializer.Deserialize(json); // attach glb bin to buffer foreach (var buffer in gltf.buffers) { buffer.OpenStorage(new UniGLTF.SimpleStorage(glb.Binary.Bytes)); } // https://github.com/vrm-c/vrm-specification/issues/205 RotateY180.Rotate(gltf); var extensions = new UniGLTF.glTFExtensionExport(); { var vrm0 = json["extensions"]["VRM"]; { // vrm var vrm1 = new UniGLTF.Extensions.VRMC_vrm.VRMC_vrm(); vrm1.Meta = MigrationVrmMeta.Migrate(vrm0["meta"]); vrm1.Humanoid = MigrationVrmHumanoid.Migrate(vrm0["humanoid"]); vrm1.Expressions = MigrationVrmExpression.Migrate(gltf, vrm0["blendShapeMaster"]).ToList(); // lookat // firstperson var f = new JsonFormatter(); UniGLTF.Extensions.VRMC_vrm.GltfSerializer.Serialize(f, vrm1); extensions.Add(UniGLTF.Extensions.VRMC_vrm.VRMC_vrm.ExtensionName, f.GetStoreBytes()); } { // springBone & collider var vrm1 = MigrationVrmSpringBone.Migrate(gltf, json["extensions"]["VRM"]["secondaryAnimation"]); var f = new JsonFormatter(); UniGLTF.Extensions.VRMC_springBone.GltfSerializer.Serialize(f, vrm1); extensions.Add(UniGLTF.Extensions.VRMC_springBone.VRMC_springBone.ExtensionName, f.GetStoreBytes()); } { // MToon } { // constraint } } ArraySegment <byte> vrm1Json = default; { gltf.extensions = extensions; var f = new JsonFormatter(); UniGLTF.GltfSerializer.Serialize(f, gltf); vrm1Json = f.GetStoreBytes(); } return(UniGLTF.Glb.Create(vrm1Json, glb.Binary.Bytes).ToBytes()); }
public static void Check(JsonNode vrm0, UniGLTF.Extensions.VRMC_vrm.Meta vrm1) { if (vrm0["title"].GetString() != vrm1.Name) { throw new MigrationException("meta.title", vrm1.Name); } if (vrm0["version"].GetString() != vrm1.Version) { throw new MigrationException("meta.version", vrm1.Version); } if (!IsSingleList("meta.author", vrm0["author"].GetString(), vrm1.Authors)) { throw new MigrationException("meta.author", $"{vrm1.Authors}"); } if (vrm0["contactInformation"].GetString() != vrm1.ContactInformation) { throw new MigrationException("meta.contactInformation", vrm1.ContactInformation); } if (!IsSingleList("meta.reference", vrm0["reference"].GetString(), vrm1.References)) { throw new MigrationException("meta.reference", $"{vrm1.References}"); } if (vrm0["texture"].GetInt32() != vrm1.ThumbnailImage) { throw new MigrationException("meta.texture", $"{vrm1.ThumbnailImage}"); } if (vrm0["allowedUserName"].GetString() != AvatarPermission("meta.allowedUserName", vrm1.AvatarPermission)) { throw new MigrationException("meta.allowedUserName", $"{vrm1.AvatarPermission}"); } if (vrm0["violentUssageName"].GetString() == "Allow" != vrm1.AllowExcessivelyViolentUsage) { throw new MigrationException("meta.violentUssageName", $"{vrm1.AllowExcessivelyViolentUsage}"); } if (vrm0["sexualUssageName"].GetString() == "Allow" != vrm1.AllowExcessivelySexualUsage) { throw new MigrationException("meta.sexualUssageName", $"{vrm1.AllowExcessivelyViolentUsage}"); } if (vrm0["commercialUssageName"].GetString() == "Allow") { if (vrm1.CommercialUsage == UniGLTF.Extensions.VRMC_vrm.CommercialUsageType.personalNonProfit) { throw new MigrationException("meta.commercialUssageName", $"{vrm1.CommercialUsage}"); } } else { if (vrm1.CommercialUsage == UniGLTF.Extensions.VRMC_vrm.CommercialUsageType.corporation || vrm1.CommercialUsage == UniGLTF.Extensions.VRMC_vrm.CommercialUsageType.personalProfit) { throw new MigrationException("meta.commercialUssageName", $"{vrm1.CommercialUsage}"); } } if (MigrationVrmMeta.GetLicenseUrl(vrm0) != vrm1.OtherLicenseUrl) { throw new MigrationException("meta.otherLicenseUrl", vrm1.OtherLicenseUrl); } switch (vrm0["licenseName"].GetString()) { case "Other": { if (vrm1.Modification != UniGLTF.Extensions.VRMC_vrm.ModificationType.prohibited) { throw new MigrationException("meta.licenceName", $"{vrm1.Modification}"); } if (vrm1.AllowRedistribution.Value) { throw new MigrationException("meta.liceneName", $"{vrm1.Modification}"); } break; } default: throw new NotImplementedException(); } }
public static void Check(JsonNode vrm0, UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm1) { MigrationVrmMeta.Check(vrm0["meta"], vrm1.Meta); MigrationVrmHumanoid.Check(vrm0["humanoid"], vrm1.Humanoid); }
public static byte[] Migrate(JsonNode json, ArraySegment <byte> bin) { var gltf = UniGLTF.GltfDeserializer.Deserialize(json); // attach glb bin to buffer foreach (var buffer in gltf.buffers) { buffer.OpenStorage(new UniGLTF.SimpleStorage(bin)); } // https://github.com/vrm-c/vrm-specification/issues/205 RotateY180.Rotate(gltf); var vrm0 = json["extensions"]["VRM"]; gltf.extensions = null; { // vrm var vrm1 = new UniGLTF.Extensions.VRMC_vrm.VRMC_vrm(); // meta (required) vrm1.Meta = MigrationVrmMeta.Migrate(vrm0["meta"]); // humanoid (required) vrm1.Humanoid = MigrationVrmHumanoid.Migrate(vrm0["humanoid"]); // blendshape (optional) if (vrm0.TryGet("blendShapeMaster", out JsonNode vrm0BlendShape)) { vrm1.Expressions = MigrationVrmExpression.Migrate(gltf, vrm0BlendShape).ToList(); } // lookat & firstperson (optional) if (vrm0.TryGet("firstPerson", out JsonNode vrm0FirstPerson)) { (vrm1.LookAt, vrm1.FirstPerson) = MigrationVrmLookAtAndFirstPerson.Migrate(gltf, vrm0FirstPerson); } UniGLTF.Extensions.VRMC_vrm.GltfSerializer.SerializeTo(ref gltf.extensions, vrm1); } // springBone & collider (optional) if (vrm0.TryGet("secondaryAnimation", out JsonNode vrm0SpringBone)) { var springBone = MigrationVrmSpringBone.Migrate(gltf, vrm0SpringBone); UniGLTF.Extensions.VRMC_springBone.GltfSerializer.SerializeTo(ref gltf.extensions, springBone); } // MToon { MigrationMToon.Migrate(gltf, json); } // Serialize whole glTF ArraySegment <byte> vrm1Json = default; { var f = new JsonFormatter(); UniGLTF.GltfSerializer.Serialize(f, gltf); vrm1Json = f.GetStoreBytes(); } // JSON 部分だけが改変されて、BIN はそのまま return(UniGLTF.Glb.Create(vrm1Json, bin).ToBytes()); }
static byte[] MigrateVrm(glTF gltf, ArraySegment <byte> bin, JsonNode vrm0) { var meshToNode = CreateMeshToNode(gltf); { // vrm var vrm1 = new UniGLTF.Extensions.VRMC_vrm.VRMC_vrm(); // meta (required) vrm1.Meta = MigrationVrmMeta.Migrate(gltf, vrm0["meta"]); // humanoid (required) vrm1.Humanoid = MigrationVrmHumanoid.Migrate(vrm0["humanoid"]); // blendshape (optional) if (vrm0.TryGet("blendShapeMaster", out JsonNode vrm0BlendShape)) { vrm1.Expressions = new UniGLTF.Extensions.VRMC_vrm.Expressions { Preset = new UniGLTF.Extensions.VRMC_vrm.Preset(), Custom = new Dictionary <string, UniGLTF.Extensions.VRMC_vrm.Expression>(), }; foreach (var(preset, customName, expression) in MigrationVrmExpression.Migrate(gltf, vrm0BlendShape, meshToNode)) { switch (preset) { case ExpressionPreset.happy: SetIfNull(ref vrm1.Expressions.Preset.Happy, expression); break; case ExpressionPreset.angry: SetIfNull(ref vrm1.Expressions.Preset.Angry, expression); break; case ExpressionPreset.sad: SetIfNull(ref vrm1.Expressions.Preset.Sad, expression); break; case ExpressionPreset.relaxed: SetIfNull(ref vrm1.Expressions.Preset.Relaxed, expression); break; case ExpressionPreset.surprised: SetIfNull(ref vrm1.Expressions.Preset.Surprised, expression); break; case ExpressionPreset.aa: SetIfNull(ref vrm1.Expressions.Preset.Aa, expression); break; case ExpressionPreset.ih: SetIfNull(ref vrm1.Expressions.Preset.Ih, expression); break; case ExpressionPreset.ou: SetIfNull(ref vrm1.Expressions.Preset.Ou, expression); break; case ExpressionPreset.ee: SetIfNull(ref vrm1.Expressions.Preset.Ee, expression); break; case ExpressionPreset.oh: SetIfNull(ref vrm1.Expressions.Preset.Oh, expression); break; case ExpressionPreset.blink: SetIfNull(ref vrm1.Expressions.Preset.Blink, expression); break; case ExpressionPreset.blinkLeft: SetIfNull(ref vrm1.Expressions.Preset.BlinkLeft, expression); break; case ExpressionPreset.blinkRight: SetIfNull(ref vrm1.Expressions.Preset.BlinkRight, expression); break; case ExpressionPreset.lookUp: SetIfNull(ref vrm1.Expressions.Preset.LookUp, expression); break; case ExpressionPreset.lookDown: SetIfNull(ref vrm1.Expressions.Preset.LookDown, expression); break; case ExpressionPreset.lookLeft: SetIfNull(ref vrm1.Expressions.Preset.LookLeft, expression); break; case ExpressionPreset.lookRight: SetIfNull(ref vrm1.Expressions.Preset.LookRight, expression); break; case ExpressionPreset.neutral: SetIfNull(ref vrm1.Expressions.Preset.Neutral, expression); break; case ExpressionPreset.custom: if (vrm1.Expressions.Custom.ContainsKey(customName)) { // 同名が既存。先着を有効とする } else { vrm1.Expressions.Custom[customName] = expression; } break; default: throw new NotImplementedException(); } } } // lookat & firstperson (optional) if (vrm0.TryGet("firstPerson", out JsonNode vrm0FirstPerson)) { (vrm1.LookAt, vrm1.FirstPerson) = MigrationVrmLookAtAndFirstPerson.Migrate(gltf, vrm0FirstPerson); } UniGLTF.Extensions.VRMC_vrm.GltfSerializer.SerializeTo(ref gltf.extensions, vrm1); } // springBone & collider (optional) if (vrm0.TryGet("secondaryAnimation", out JsonNode vrm0SpringBone)) { var springBone = MigrationVrmSpringBone.Migrate(gltf, vrm0SpringBone); UniGLTF.Extensions.VRMC_springBone.GltfSerializer.SerializeTo(ref gltf.extensions, springBone); } // Material { MigrationMaterials.Migrate(gltf, vrm0); } // Serialize whole glTF ArraySegment <byte> vrm1Json = default; { var f = new JsonFormatter(); GltfSerializer.Serialize(f, gltf); vrm1Json = f.GetStoreBytes(); } return(Glb.Create(vrm1Json, bin).ToBytes()); }
public static void Check(JsonNode vrm0, UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm1, MeshIndexToNodeIndexFunc meshToNode) { MigrationVrmMeta.Check(vrm0["meta"], vrm1.Meta); MigrationVrmHumanoid.Check(vrm0["humanoid"], vrm1.Humanoid); MigrationVrmExpression.Check(vrm0["blendShapeMaster"], vrm1.Expressions, meshToNode); }
public static byte[] Migrate(JsonNode json, ArraySegment <byte> bin) { var gltf = UniGLTF.GltfDeserializer.Deserialize(json); // attach glb bin to buffer foreach (var buffer in gltf.buffers) { buffer.OpenStorage(new UniGLTF.SimpleStorage(bin)); } // https://github.com/vrm-c/vrm-specification/issues/205 RotateY180.Rotate(gltf); var vrm0 = json["extensions"]["VRM"]; gltf.extensions = null; { // vrm var vrm1 = new UniGLTF.Extensions.VRMC_vrm.VRMC_vrm(); // meta (required) vrm1.Meta = MigrationVrmMeta.Migrate(vrm0["meta"]); // humanoid (required) vrm1.Humanoid = MigrationVrmHumanoid.Migrate(vrm0["humanoid"]); // blendshape (optional) if (vrm0.TryGet("blendShapeMaster", out JsonNode vrm0BlendShape)) { vrm1.Expressions = new UniGLTF.Extensions.VRMC_vrm.Expressions { Preset = new UniGLTF.Extensions.VRMC_vrm.Preset(), Custom = new Dictionary <string, UniGLTF.Extensions.VRMC_vrm.Expression>(), }; foreach (var(preset, customName, expression) in MigrationVrmExpression.Migrate(gltf, vrm0BlendShape)) { switch (preset) { case ExpressionPreset.happy: vrm1.Expressions.Preset.Happy = expression; break; case ExpressionPreset.angry: vrm1.Expressions.Preset.Angry = expression; break; case ExpressionPreset.sad: vrm1.Expressions.Preset.Sad = expression; break; case ExpressionPreset.relaxed: vrm1.Expressions.Preset.Relaxed = expression; break; case ExpressionPreset.surprised: vrm1.Expressions.Preset.Surprised = expression; break; case ExpressionPreset.aa: vrm1.Expressions.Preset.Aa = expression; break; case ExpressionPreset.ih: vrm1.Expressions.Preset.Ih = expression; break; case ExpressionPreset.ou: vrm1.Expressions.Preset.Ou = expression; break; case ExpressionPreset.ee: vrm1.Expressions.Preset.Ee = expression; break; case ExpressionPreset.oh: vrm1.Expressions.Preset.Oh = expression; break; case ExpressionPreset.blink: vrm1.Expressions.Preset.Blink = expression; break; case ExpressionPreset.blinkLeft: vrm1.Expressions.Preset.BlinkLeft = expression; break; case ExpressionPreset.blinkRight: vrm1.Expressions.Preset.BlinkRight = expression; break; case ExpressionPreset.lookUp: vrm1.Expressions.Preset.LookUp = expression; break; case ExpressionPreset.lookDown: vrm1.Expressions.Preset.LookDown = expression; break; case ExpressionPreset.lookLeft: vrm1.Expressions.Preset.LookLeft = expression; break; case ExpressionPreset.lookRight: vrm1.Expressions.Preset.LookRight = expression; break; case ExpressionPreset.neutral: vrm1.Expressions.Custom[customName] = expression; break; case ExpressionPreset.custom: vrm1.Expressions.Custom[customName] = expression; break; default: throw new NotImplementedException(); } } } // lookat & firstperson (optional) if (vrm0.TryGet("firstPerson", out JsonNode vrm0FirstPerson)) { (vrm1.LookAt, vrm1.FirstPerson) = MigrationVrmLookAtAndFirstPerson.Migrate(gltf, vrm0FirstPerson); } UniGLTF.Extensions.VRMC_vrm.GltfSerializer.SerializeTo(ref gltf.extensions, vrm1); } // springBone & collider (optional) if (vrm0.TryGet("secondaryAnimation", out JsonNode vrm0SpringBone)) { var springBone = MigrationVrmSpringBone.Migrate(gltf, vrm0SpringBone); UniGLTF.Extensions.VRMC_springBone.GltfSerializer.SerializeTo(ref gltf.extensions, springBone); } // MToon { MigrationMToon.Migrate(gltf, json); } // Serialize whole glTF ArraySegment <byte> vrm1Json = default; { var f = new JsonFormatter(); UniGLTF.GltfSerializer.Serialize(f, gltf); vrm1Json = f.GetStoreBytes(); } // JSON 部分だけが改変されて、BIN はそのまま return(UniGLTF.Glb.Create(vrm1Json, bin).ToBytes()); }