public static (LookAt, FirstPerson) Migrate(glTF gltf, JsonNode vrm0) { // VRM1 // firstPerson に同居していた LookAt は独立します LookAt lookAt = default; lookAt = new LookAt { RangeMapHorizontalInner = MigrateLookAtRangeMap(vrm0["lookAtHorizontalInner"]), RangeMapHorizontalOuter = MigrateLookAtRangeMap(vrm0["lookAtHorizontalOuter"]), RangeMapVerticalDown = MigrateLookAtRangeMap(vrm0["lookAtVerticalDown"]), RangeMapVerticalUp = MigrateLookAtRangeMap(vrm0["lookAtVerticalUp"]), Type = MigrateLookAtType(vrm0["lookAtTypeName"]), OffsetFromHeadBone = MigrateVector3.Migrate(vrm0, "firstPersonBoneOffset"), }; var firstPerson = new FirstPerson { // VRM1 // firstPersonBoneOffset は廃止されます。LookAt.OffsetFromHeadBone を使ってください。 // firstPersonBone は廃止されます。Head 固定です。 MeshAnnotations = new System.Collections.Generic.List <MeshAnnotation>(), }; if (vrm0.TryGet("meshAnnotations", out JsonNode meshAnnotations)) { Func <int, int> meshIndexToRenderNodeIndex = meshIndex => { for (int i = 0; i < gltf.nodes.Count; ++i) { var node = gltf.nodes[i]; if (node.mesh == meshIndex) { return(i); } } throw new NotImplementedException("mesh is not used"); }; foreach (var x in meshAnnotations.ArrayItems()) { var a = new MeshAnnotation { Node = meshIndexToRenderNodeIndex(x["mesh"].GetInt32()), Type = MigrateFirstPersonType(x["firstPersonFlag"]), }; firstPerson.MeshAnnotations.Add(a); } } ; return(lookAt, firstPerson); }
public SpringBoneGroupMigrator(UniGLTF.glTF gltf, JsonNode vrm0BoneGroup) { _gltf = gltf; _comment = vrm0BoneGroup.GetObjectValueOrDefault("comment", ""); _dragForce = vrm0BoneGroup["dragForce"].GetSingle(); _gravityDir = MigrateVector3.Migrate(vrm0BoneGroup["gravityDir"]); _gravityPower = vrm0BoneGroup["gravityPower"].GetSingle(); _hitRadius = vrm0BoneGroup["hitRadius"].GetSingle(); _stiffness = vrm0BoneGroup["stiffiness"].GetSingle(); _colliderGroups = vrm0BoneGroup["colliderGroups"].ArrayItems().Select(z => z.GetInt32()).ToArray(); if (vrm0BoneGroup.ContainsKey("bones")) { foreach (var vrm0Bone in vrm0BoneGroup["bones"].ArrayItems()) { MigrateRootBone(vrm0Bone.GetInt32()); } } }
/// <summary> /// { /// "colliderGroups": [ /// ], /// "boneGroups": [ /// ], /// } /// </summary> /// <param name="gltf"></param> /// <param name="vrm0"></param> /// <returns></returns> public static VRMC_springBone Migrate(UniGLTF.glTF gltf, JsonNode vrm0) { var springBone = new VRMC_springBone { Colliders = new List <Collider>(), ColliderGroups = new List <ColliderGroup>(), Springs = new List <Spring>(), }; // NOTE: ColliderGroups をマイグレーションする. // ColliderGroup は Spring から index で参照されているため、順序を入れ替えたり増減させてはいけない. foreach (var vrm0ColliderGroup in vrm0["colliderGroups"].ArrayItems()) { // { // "node": 14, // "colliders": [ // { // "offset": { // "x": 0.025884293, // "y": -0.120000005, // "z": 0 // }, // "radius": 0.05 // }, // { // "offset": { // "x": -0.02588429, // "y": -0.120000005, // "z": 0 // }, // "radius": 0.05 // }, // { // "offset": { // "x": 0, // "y": -0.0220816135, // "z": 0 // }, // "radius": 0.08 // } // ] // }, // NOTE: 1.0 では ColliderGroup は Collider の実体ではなく index を参照する. var colliderIndices = new List <int>(); if (vrm0ColliderGroup.ContainsKey("node") && vrm0ColliderGroup.ContainsKey("colliders")) { var nodeIndex = vrm0ColliderGroup["node"].GetInt32(); // NOTE: ColliderGroup に含まれる Collider をマイグレーションする. foreach (var vrm0Collider in vrm0ColliderGroup["colliders"].ArrayItems()) { if (!vrm0Collider.ContainsKey("offset")) { continue; } if (!vrm0Collider.ContainsKey("radius")) { continue; } colliderIndices.Add(springBone.Colliders.Count); springBone.Colliders.Add(new Collider { Node = nodeIndex, Shape = new ColliderShape { Sphere = new ColliderShapeSphere { Offset = MigrateVector3.Migrate(vrm0Collider["offset"]), Radius = vrm0Collider["radius"].GetSingle() } } }); } } var colliderGroup = new ColliderGroup() { Colliders = colliderIndices.ToArray(), }; springBone.ColliderGroups.Add(colliderGroup); } foreach (var vrm0BoneGroup in vrm0["boneGroups"].ArrayItems()) { // { // "comment": "", // "stiffiness": 2, // "gravityPower": 0, // "gravityDir": { // "x": 0, // "y": -1, // "z": 0 // }, // "dragForce": 0.7, // "center": -1, // "hitRadius": 0.02, // "bones": [ // 97, // 99, // 101, // 113, // 114 // ], // "colliderGroups": [ // 3, // 4, // 5 // ] // }, var migrator = new SpringBoneGroupMigrator(gltf, vrm0BoneGroup); springBone.Springs.AddRange(migrator.Springs); } return(springBone); }
public static UniGLTF.Extensions.VRMC_springBone.VRMC_springBone Migrate(UniGLTF.glTF gltf, JsonNode sa) { var colliderNodes = new List <int>(); foreach (var x in sa["colliderGroups"].ArrayItems()) { var node = x["node"].GetInt32(); colliderNodes.Add(node); var gltfNode = gltf.nodes[node]; var collider = new UniGLTF.Extensions.VRMC_node_collider.VRMC_node_collider() { Shapes = new List <UniGLTF.Extensions.VRMC_node_collider.ColliderShape>(), }; // { // "node": 14, // "colliders": [ // { // "offset": { // "x": 0.025884293, // "y": -0.120000005, // "z": 0 // }, // "radius": 0.05 // }, // { // "offset": { // "x": -0.02588429, // "y": -0.120000005, // "z": 0 // }, // "radius": 0.05 // }, // { // "offset": { // "x": 0, // "y": -0.0220816135, // "z": 0 // }, // "radius": 0.08 // } // ] // }, foreach (var y in x["colliders"].ArrayItems()) { collider.Shapes.Add(new UniGLTF.Extensions.VRMC_node_collider.ColliderShape { Sphere = new UniGLTF.Extensions.VRMC_node_collider.ColliderShapeSphere { Offset = MigrateVector3.Migrate(y["offset"]), Radius = y["radius"].GetSingle() } }); } if (!(gltfNode.extensions is UniGLTF.glTFExtensionExport extensions)) { extensions = new UniGLTF.glTFExtensionExport(); gltfNode.extensions = extensions; } var f = new JsonFormatter(); UniGLTF.Extensions.VRMC_node_collider.GltfSerializer.Serialize(f, collider); extensions.Add(UniGLTF.Extensions.VRMC_node_collider.VRMC_node_collider.ExtensionName, f.GetStoreBytes()); } var springBone = new UniGLTF.Extensions.VRMC_springBone.VRMC_springBone { Springs = new List <UniGLTF.Extensions.VRMC_springBone.Spring>(), }; foreach (var x in sa["boneGroups"].ArrayItems()) { // { // "comment": "", // "stiffiness": 2, // "gravityPower": 0, // "gravityDir": { // "x": 0, // "y": -1, // "z": 0 // }, // "dragForce": 0.7, // "center": -1, // "hitRadius": 0.02, // "bones": [ // 97, // 99, // 101, // 113, // 114 // ], // "colliderGroups": [ // 3, // 4, // 5 // ] // }, foreach (var y in x["bones"].ArrayItems()) { var comment = x.GetObjectValueOrDefault("comment", ""); var spring = new UniGLTF.Extensions.VRMC_springBone.Spring { Name = comment, Colliders = x["colliderGroups"].ArrayItems().Select(z => colliderNodes[z.GetInt32()]).ToArray(), Joints = new List <UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint>(), }; foreach (var z in EnumJoint(gltf.nodes, gltf.nodes[y.GetInt32()])) { spring.Joints.Add(new UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint { Node = gltf.nodes.IndexOf(z), DragForce = x["dragForce"].GetSingle(), GravityDir = MigrateVector3.Migrate(x["gravityDir"]), GravityPower = x["gravityPower"].GetSingle(), HitRadius = x["hitRadius"].GetSingle(), Stiffness = x["stiffiness"].GetSingle(), }); } springBone.Springs.Add(spring); } } return(springBone); }
public static UniGLTF.Extensions.VRMC_springBone.VRMC_springBone Migrate(UniGLTF.glTF gltf, JsonNode sa) { var colliderNodes = new List <int>(); var springBone = new UniGLTF.Extensions.VRMC_springBone.VRMC_springBone { ColliderGroups = new List <UniGLTF.Extensions.VRMC_springBone.ColliderGroup>(), Springs = new List <UniGLTF.Extensions.VRMC_springBone.Spring>(), }; foreach (var x in sa["colliderGroups"].ArrayItems()) { var node = x["node"].GetInt32(); colliderNodes.Add(node); var colliderGroup = new UniGLTF.Extensions.VRMC_springBone.ColliderGroup() { Colliders = new List <UniGLTF.Extensions.VRMC_springBone.Collider>(), }; springBone.ColliderGroups.Add(colliderGroup); // { // "node": 14, // "colliders": [ // { // "offset": { // "x": 0.025884293, // "y": -0.120000005, // "z": 0 // }, // "radius": 0.05 // }, // { // "offset": { // "x": -0.02588429, // "y": -0.120000005, // "z": 0 // }, // "radius": 0.05 // }, // { // "offset": { // "x": 0, // "y": -0.0220816135, // "z": 0 // }, // "radius": 0.08 // } // ] // }, foreach (var y in x["colliders"].ArrayItems()) { colliderGroup.Colliders.Add(new UniGLTF.Extensions.VRMC_springBone.Collider { Node = x["node"].GetInt32(), Shape = new UniGLTF.Extensions.VRMC_springBone.ColliderShape { Sphere = new UniGLTF.Extensions.VRMC_springBone.ColliderShapeSphere { Offset = MigrateVector3.Migrate(y["offset"]), Radius = y["radius"].GetSingle() } } }); } } foreach (var x in sa["boneGroups"].ArrayItems()) { // { // "comment": "", // "stiffiness": 2, // "gravityPower": 0, // "gravityDir": { // "x": 0, // "y": -1, // "z": 0 // }, // "dragForce": 0.7, // "center": -1, // "hitRadius": 0.02, // "bones": [ // 97, // 99, // 101, // 113, // 114 // ], // "colliderGroups": [ // 3, // 4, // 5 // ] // }, foreach (var y in x["bones"].ArrayItems()) { var comment = x.GetObjectValueOrDefault("comment", ""); var spring = new UniGLTF.Extensions.VRMC_springBone.Spring { Name = comment, ColliderGroups = x["colliderGroups"].ArrayItems().Select(z => z.GetInt32()).ToArray(), Joints = new List <UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint>(), }; foreach (var z in EnumJoint(gltf.nodes, gltf.nodes[y.GetInt32()])) { spring.Joints.Add(new UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint { Node = gltf.nodes.IndexOf(z), DragForce = x["dragForce"].GetSingle(), GravityDir = MigrateVector3.Migrate(x["gravityDir"]), GravityPower = x["gravityPower"].GetSingle(), HitRadius = x["hitRadius"].GetSingle(), Stiffness = x["stiffiness"].GetSingle(), }); } springBone.Springs.Add(spring); } } return(springBone); }
/// <summary> /// { /// "colliderGroups": [ /// ], /// "boneGroups": [ /// ], /// } /// </summary> /// <param name="gltf"></param> /// <param name="vrm0"></param> /// <returns></returns> public static VRMC_springBone Migrate(UniGLTF.glTF gltf, JsonNode vrm0) { var springBone = new VRMC_springBone { Colliders = new List <Collider>(), ColliderGroups = new List <ColliderGroup>(), Springs = new List <Spring>(), }; foreach (var vrm0ColliderGroup in vrm0["colliderGroups"].ArrayItems()) { // { // "node": 14, // "colliders": [ // { // "offset": { // "x": 0.025884293, // "y": -0.120000005, // "z": 0 // }, // "radius": 0.05 // }, // { // "offset": { // "x": -0.02588429, // "y": -0.120000005, // "z": 0 // }, // "radius": 0.05 // }, // { // "offset": { // "x": 0, // "y": -0.0220816135, // "z": 0 // }, // "radius": 0.08 // } // ] // }, var colliders = new List <int>(); foreach (var vrm0Collider in vrm0ColliderGroup["colliders"].ArrayItems()) { colliders.Add(springBone.Colliders.Count); springBone.Colliders.Add(new Collider { Node = vrm0ColliderGroup["node"].GetInt32(), Shape = new ColliderShape { Sphere = new ColliderShapeSphere { Offset = MigrateVector3.Migrate(vrm0Collider["offset"]), Radius = vrm0Collider["radius"].GetSingle() } } }); } var colliderGroup = new ColliderGroup() { Colliders = colliders.ToArray(), }; springBone.ColliderGroups.Add(colliderGroup); } foreach (var vrm0BoneGroup in vrm0["boneGroups"].ArrayItems()) { // { // "comment": "", // "stiffiness": 2, // "gravityPower": 0, // "gravityDir": { // "x": 0, // "y": -1, // "z": 0 // }, // "dragForce": 0.7, // "center": -1, // "hitRadius": 0.02, // "bones": [ // 97, // 99, // 101, // 113, // 114 // ], // "colliderGroups": [ // 3, // 4, // 5 // ] // }, var migrator = new SpringBoneGroupMigrator(gltf, vrm0BoneGroup); springBone.Springs.AddRange(migrator.Springs); } return(springBone); }
/// <summary> /// { /// "colliderGroups": [ /// ], /// "boneGroups": [ /// ], /// } /// </summary> /// <param name="gltf"></param> /// <param name="sa"></param> /// <returns></returns> public static UniGLTF.Extensions.VRMC_springBone.VRMC_springBone Migrate(UniGLTF.glTF gltf, JsonNode sa) { var springBone = new UniGLTF.Extensions.VRMC_springBone.VRMC_springBone { Colliders = new List <UniGLTF.Extensions.VRMC_springBone.Collider>(), ColliderGroups = new List <UniGLTF.Extensions.VRMC_springBone.ColliderGroup>(), Springs = new List <UniGLTF.Extensions.VRMC_springBone.Spring>(), }; foreach (var x in sa["colliderGroups"].ArrayItems()) { // { // "node": 14, // "colliders": [ // { // "offset": { // "x": 0.025884293, // "y": -0.120000005, // "z": 0 // }, // "radius": 0.05 // }, // { // "offset": { // "x": -0.02588429, // "y": -0.120000005, // "z": 0 // }, // "radius": 0.05 // }, // { // "offset": { // "x": 0, // "y": -0.0220816135, // "z": 0 // }, // "radius": 0.08 // } // ] // }, var colliders = new List <int>(); foreach (var y in x["colliders"].ArrayItems()) { colliders.Add(springBone.Colliders.Count); springBone.Colliders.Add(new UniGLTF.Extensions.VRMC_springBone.Collider { Node = x["node"].GetInt32(), Shape = new UniGLTF.Extensions.VRMC_springBone.ColliderShape { Sphere = new UniGLTF.Extensions.VRMC_springBone.ColliderShapeSphere { Offset = MigrateVector3.Migrate(y["offset"]), Radius = y["radius"].GetSingle() } } }); } var colliderGroup = new UniGLTF.Extensions.VRMC_springBone.ColliderGroup() { Colliders = colliders.ToArray(), }; springBone.ColliderGroups.Add(colliderGroup); } // https://github.com/vrm-c/vrm-specification/pull/255 // 1.0 では末端に7cmの遠さに joint を追加する動作をしなくなった。 // その差異に対応して、7cmの遠さに node を追加する。 foreach (var x in sa["boneGroups"].ArrayItems()) { foreach (var y in x["bones"].ArrayItems()) { var joints = TraverseFirstChild(gltf.nodes, gltf.nodes[y.GetInt32()]).ToArray(); AddTail7cm(gltf, joints); } } foreach (var x in sa["boneGroups"].ArrayItems()) { // { // "comment": "", // "stiffiness": 2, // "gravityPower": 0, // "gravityDir": { // "x": 0, // "y": -1, // "z": 0 // }, // "dragForce": 0.7, // "center": -1, // "hitRadius": 0.02, // "bones": [ // 97, // 99, // 101, // 113, // 114 // ], // "colliderGroups": [ // 3, // 4, // 5 // ] // }, foreach (var y in x["bones"].ArrayItems()) { var comment = x.GetObjectValueOrDefault("comment", ""); var spring = new UniGLTF.Extensions.VRMC_springBone.Spring { Name = comment, ColliderGroups = x["colliderGroups"].ArrayItems().Select(z => z.GetInt32()).ToArray(), Joints = new List <UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint>(), }; foreach (var z in TraverseFirstChild(gltf.nodes, gltf.nodes[y.GetInt32()])) { spring.Joints.Add(new UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint { Node = gltf.nodes.IndexOf(z), DragForce = x["dragForce"].GetSingle(), GravityDir = MigrateVector3.Migrate(x["gravityDir"]), GravityPower = x["gravityPower"].GetSingle(), HitRadius = x["hitRadius"].GetSingle(), Stiffness = x["stiffiness"].GetSingle(), }); } springBone.Springs.Add(spring); } } return(springBone); }