Exemplo n.º 1
0
        public void CreateTextureItems(UniGLTF.glTF gltf, UnityPath imageBaseDir)
        {
            if (m_textures.Any())
            {
                return;
            }

            for (int i = 0; i < gltf.textures.Count; ++i)
            {
                TextureItem item = null;
#if UNITY_EDITOR
                var image = gltf.GetImageFromTextureIndex(i);
                if (imageBaseDir.IsUnderAssetsFolder &&
                    !string.IsNullOrEmpty(image.uri) &&
                    !image.uri.FastStartsWith("data:")
                    )
                {
                    ///
                    /// required SaveTexturesAsPng or SetTextureBaseDir
                    ///
                    var assetPath   = imageBaseDir.Child(image.uri);
                    var textureName = !string.IsNullOrEmpty(image.name) ? image.name : Path.GetFileNameWithoutExtension(image.uri);
                    item = new TextureItem(i, assetPath, textureName);
                }
                else
#endif
                {
                    item = new TextureItem(i, CreateTextureLoader(i));
                }

                AddTexture(item);
            }
        }
Exemplo n.º 2
0
        public static IEnumerable <UniGLTF.Extensions.VRMC_vrm.Expression> Migrate(UniGLTF.glTF gltf, JsonNode json)
        {
            foreach (var blendShapeClip in json["blendShapeGroups"].ArrayItems())
            {
                var name     = blendShapeClip["name"].GetString();
                var isBinary = false;
                if (blendShapeClip.TryGet("isBinary", out JsonNode isBinaryNode))
                {
                    isBinary = isBinaryNode.GetBoolean();
                }
                var expression = new UniGLTF.Extensions.VRMC_vrm.Expression
                {
                    Name                  = name,
                    Preset                = ToPreset(blendShapeClip["presetName"]),
                    IsBinary              = isBinary,
                    MorphTargetBinds      = new List <UniGLTF.Extensions.VRMC_vrm.MorphTargetBind>(),
                    MaterialColorBinds    = new List <UniGLTF.Extensions.VRMC_vrm.MaterialColorBind>(),
                    TextureTransformBinds = new List <UniGLTF.Extensions.VRMC_vrm.TextureTransformBind>(),
                };
                expression.MorphTargetBinds = ToMorphTargetBinds(gltf, blendShapeClip["binds"]).ToList();

                if (blendShapeClip.TryGet("materialValues", out JsonNode materialValues))
                {
                    ToMaterialColorBinds(gltf, materialValues, expression);
                }

                yield return(expression);
            }
        }
Exemplo n.º 3
0
        static void AddTail7cm(UniGLTF.glTF gltf, UniGLTF.glTFNode[] joints)
        {
            if (joints.Length < 2)
            {
                return;
            }
            var last = joints.Last();
            var name = last.name ?? "";
            var v1   = new UnityEngine.Vector3(last.translation[0], last.translation[1], last.translation[2]);
            // var last2 = joints[joints.Length - 2];
            // var v2 = new UnityEngine.Vector3(last2.translation[0], last2.translation[1], last2.translation[2]);
            var delta = v1.normalized * 0.07f; // 7cm
            var tail  = new UniGLTF.glTFNode
            {
                name        = name + "_end",
                translation = new float[] {
                    delta.x,
                    delta.y,
                    delta.z
                },
            };
            var tail_index = gltf.nodes.Count;

            gltf.nodes.Add(tail);
            if (last.children != null && last.children.Length > 0)
            {
                throw new System.Exception();
            }
            last.children = new[] { tail_index };
        }
Exemplo n.º 4
0
 /// <summary>
 /// for export
 /// </summary>
 public Vrm10Storage()
 {
     Gltf = new UniGLTF.glTF()
     {
         extensionsUsed = new List <string>(),
     };
     Buffers = new List <ArrayByteBuffer10>()
     {
         new ArrayByteBuffer10()
     };
 }
Exemplo n.º 5
0
 /// <summary>
 /// MaterialValue の仕様変更
 ///
 /// * MaterialColorBind
 /// * TextureTransformBind
 ///
 /// の2種類になった。
 ///
 /// </summary>
 /// <param name="gltf"></param>
 /// <param name="json"></param>
 /// <param name="expression"></param>
 static void ToMaterialColorBinds(UniGLTF.glTF gltf, JsonNode json, UniGLTF.Extensions.VRMC_vrm.Expression expression)
 {
     foreach (var x in json.ArrayItems())
     {
         var materialName  = x["materialName"].GetString();
         var materialIndex = gltf.materials.IndexOf(gltf.materials.First(y => y.name == materialName));
         var propertyName  = x["propertyName"].GetString();
         var targetValue   = x["targetValue"].ArrayItems().Select(y => y.GetSingle()).ToArray();
         if (propertyName.EndsWith("_ST"))
         {
             var scaling = new float[] { targetValue[0], targetValue[1] };
             expression.TextureTransformBinds.Add(new UniGLTF.Extensions.VRMC_vrm.TextureTransformBind
             {
                 Material = materialIndex,
                 Scaling  = new float[] { targetValue[0], targetValue[1] },
                 Offset   = new float[] { targetValue[2], targetValue[3] }
             });
         }
         else if (propertyName.EndsWith("_ST_S"))
         {
             expression.TextureTransformBinds.Add(new UniGLTF.Extensions.VRMC_vrm.TextureTransformBind
             {
                 Material = materialIndex,
                 Scaling  = new float[] { targetValue[0], 1 },
                 Offset   = new float[] { targetValue[2], 0 }
             });
         }
         else if (propertyName.EndsWith("_ST_T"))
         {
             expression.TextureTransformBinds.Add(new UniGLTF.Extensions.VRMC_vrm.TextureTransformBind
             {
                 Material = materialIndex,
                 Scaling  = new float[] { 1, targetValue[1] },
                 Offset   = new float[] { 0, targetValue[3] }
             });
         }
         else
         {
             // color
             expression.MaterialColorBinds.Add(new UniGLTF.Extensions.VRMC_vrm.MaterialColorBind
             {
                 Material    = materialIndex,
                 Type        = ToMaterialType(propertyName),
                 TargetValue = targetValue,
             });
         }
     }
 }
Exemplo n.º 6
0
 public static IEnumerable <UniGLTF.Extensions.VRMC_vrm.Expression> Migrate(UniGLTF.glTF gltf, JsonNode json)
 {
     foreach (var blendShapeClip in json["blendShapeGroups"].ArrayItems())
     {
         var name       = blendShapeClip["name"].GetString();
         var expression = new UniGLTF.Extensions.VRMC_vrm.Expression
         {
             Name     = name,
             Preset   = ToPreset(blendShapeClip["presetName"]),
             IsBinary = blendShapeClip["isBinary"].GetBoolean(),
         };
         expression.MorphTargetBinds = ToMorphTargetBinds(gltf, blendShapeClip["binds"]).ToList();
         ToMaterialColorBinds(gltf, blendShapeClip["materialValues"], expression);
         yield return(expression);
     }
 }
Exemplo n.º 7
0
 public void Prepare(UniGLTF.glTF gltf, UnityPath imageBaseDir = default(UnityPath))
 {
     if (m_textures.Count == 0)
     {
         //
         // runtime
         //
         CreateTextureItems(gltf, imageBaseDir);
     }
     else
     {
         //
         // already CreateTextures(by assetPostProcessor or editor menu)
         //
     }
 }
Exemplo n.º 8
0
            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());
                    }
                }
            }
Exemplo n.º 9
0
        /// <summary>
        /// for import
        /// </summary>
        /// <param name="json"></param>
        /// <param name="bin"></param>
        public Vrm10Storage(ArraySegment <byte> json, ArraySegment <byte> bin)
        {
            OriginalJson = json;
            Gltf         = UniGLTF.GltfDeserializer.Deserialize(json.ParseAsJson());

            if (UniGLTF.Extensions.VRMC_vrm.GltfDeserializer.TryGet(Gltf.extensions,
                                                                    out UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm))
            {
                gltfVrm = vrm;
            }

            if (UniGLTF.Extensions.VRMC_springBone.GltfDeserializer.TryGet(Gltf.extensions,
                                                                           out UniGLTF.Extensions.VRMC_springBone.VRMC_springBone springBone))
            {
                gltfVrmSpringBone = springBone;
            }

            var array = bin.ToArray();

            Buffers = new List <ArrayByteBuffer10>()
            {
                new ArrayByteBuffer10(array, bin.Count)
            };
        }
 void OnExported(UniGLTF.glTF vrm)
 {
     Debug.LogFormat("exported");
 }
Exemplo n.º 11
0
        /// <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);
        }
Exemplo n.º 12
0
        public static UniGLTF.Extensions.VRMC_vrm.Meta Migrate(UniGLTF.glTF gltf, JsonNode vrm0)
        {
            var meta = new UniGLTF.Extensions.VRMC_vrm.Meta
            {
                AllowPoliticalOrReligiousUsage = false,
                AllowExcessivelySexualUsage    = false,
                AllowExcessivelyViolentUsage   = false,
                AllowRedistribution            = false,
                AvatarPermission = UniGLTF.Extensions.VRMC_vrm.AvatarPermissionType.onlyAuthor,
                CommercialUsage  = UniGLTF.Extensions.VRMC_vrm.CommercialUsageType.personalNonProfit,
                CreditNotation   = UniGLTF.Extensions.VRMC_vrm.CreditNotationType.required,
                Modification     = UniGLTF.Extensions.VRMC_vrm.ModificationType.prohibited,
            };

            string otherLicenseUrl    = default;
            string otherPermissionUrl = default;

            foreach (var kv in vrm0.ObjectItems())
            {
                var key = kv.Key.GetString();
                switch (key)
                {
                case "title": meta.Name = kv.Value.GetString(); break;

                case "version": meta.Version = kv.Value.GetString(); break;

                case "author": meta.Authors = new List <string>()
                {
                        kv.Value.GetString()
                }; break;

                case "contactInformation": meta.ContactInformation = kv.Value.GetString(); break;

                case "reference": meta.References = new List <string>()
                {
                        kv.Value.GetString()
                }; break;

                case "texture":
                {
                    // vrm0x use texture. vrm10 use image
                    var textureIndex = kv.Value.GetInt32();
                    if (textureIndex == -1)
                    {
                        meta.ThumbnailImage = -1;
                    }
                    else
                    {
                        var gltfTexture = gltf.textures[textureIndex];
                        meta.ThumbnailImage = gltfTexture.source;
                    }
                    break;
                }

                case "allowedUserName":
                {
                    var allowedUserName = kv.Value.GetString();
                    switch (allowedUserName)
                    {
                    case "OnlyAuthor":
                        meta.AvatarPermission = UniGLTF.Extensions.VRMC_vrm.AvatarPermissionType.onlyAuthor;
                        break;

                    case "ExplicitlyLicensedPerson":
                        meta.AvatarPermission = UniGLTF.Extensions.VRMC_vrm.AvatarPermissionType.onlySeparatelyLicensedPerson;
                        break;

                    case "Everyone":
                        meta.AvatarPermission = UniGLTF.Extensions.VRMC_vrm.AvatarPermissionType.everyone;
                        break;

                    default:
                        throw new NotImplementedException($"{key}: {allowedUserName}");
                    }
                }
                break;

                case "violentUssageName":     // Typo "Ussage" is VRM 0.x spec.
                {
                    var violentUsageName = kv.Value.GetString();
                    switch (violentUsageName)
                    {
                    case "Allow":
                        meta.AllowExcessivelyViolentUsage = true;
                        break;

                    case "Disallow":
                        meta.AllowExcessivelyViolentUsage = false;
                        break;

                    default:
                        throw new NotImplementedException($"{key}: {violentUsageName}");
                    }
                }
                break;

                case "sexualUssageName":     // Typo "Ussage" is VRM 0.x spec.
                {
                    var sexualUsageName = kv.Value.GetString();
                    switch (sexualUsageName)
                    {
                    case "Allow":
                        meta.AllowExcessivelySexualUsage = true;
                        break;

                    case "Disallow":
                        meta.AllowExcessivelySexualUsage = false;
                        break;

                    default:
                        throw new NotImplementedException($"{key}: {sexualUsageName}");
                    }
                }
                break;

                case "commercialUssageName":     // Typo "Ussage" is VRM 0.x spec.
                {
                    var commercialUsageName = kv.Value.GetString();
                    switch (commercialUsageName)
                    {
                    case "Allow":
                        meta.CommercialUsage = UniGLTF.Extensions.VRMC_vrm.CommercialUsageType.personalProfit;
                        break;

                    case "Disallow":
                        meta.CommercialUsage = UniGLTF.Extensions.VRMC_vrm.CommercialUsageType.personalNonProfit;
                        break;

                    default:
                        throw new NotImplementedException($"{key}: {commercialUsageName}");
                    }
                }
                break;

                case "otherPermissionUrl": otherPermissionUrl = kv.Value.GetString(); break;

                case "otherLicenseUrl": otherLicenseUrl = kv.Value.GetString(); break;

                case "licenseName":
                {
                    // TODO
                    // CreditNotation = CreditNotationType.required,
                }
                break;

                default:
                    Debug.LogWarning($"[meta migration] unknown key: {key}");
                    break;
                } // switch
            }     // foreach

            //
            // OtherLicenseUrl migrate
            // OtherPermissionURL removed
            //
            if (!string.IsNullOrEmpty(otherLicenseUrl) && !string.IsNullOrEmpty(otherPermissionUrl))
            {
                if (otherLicenseUrl == otherPermissionUrl)
                {
                    // OK
                    meta.OtherLicenseUrl = otherLicenseUrl;
                }
                else
                {
                    // https://github.com/vrm-c/UniVRM/issues/1611
                    // 両方を記述しエラーとしない
                    meta.OtherLicenseUrl = $"'{otherLicenseUrl}', '{otherPermissionUrl}'";
                }
            }
            else if (!string.IsNullOrEmpty(otherLicenseUrl))
            {
                meta.OtherLicenseUrl = otherLicenseUrl;
            }
            else if (!string.IsNullOrEmpty(otherPermissionUrl))
            {
                // otherPermissionUrl => otherLicenseUrl
                meta.OtherLicenseUrl = otherPermissionUrl;
            }
            else
            {
                // null
            }

            return(meta);
        }
Exemplo n.º 13
0
        static IEnumerable <UniGLTF.Extensions.VRMC_vrm.MorphTargetBind> ToMorphTargetBinds(UniGLTF.glTF gltf, JsonNode json)
        {
            foreach (var x in json.ArrayItems())
            {
                var meshIndex        = x["mesh"].GetInt32();
                var morphTargetIndex = x["index"].GetInt32();
                var weight           = x["weight"].GetSingle();

                var bind = new UniGLTF.Extensions.VRMC_vrm.MorphTargetBind();

                // https://github.com/vrm-c/vrm-specification/pull/106
                // https://github.com/vrm-c/vrm-specification/pull/153
                bind.Node  = gltf.nodes.IndexOf(gltf.nodes.First(y => y.mesh == meshIndex));
                bind.Index = morphTargetIndex;
                // https://github.com/vrm-c/vrm-specification/issues/209
                bind.Weight = weight * 0.01f;

                yield return(bind);
            }
        }
        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);
        }
Exemplo n.º 15
0
        /// <summary>
        /// MaterialValue の仕様変更
        ///
        /// * MaterialColorBind
        /// * TextureTransformBind
        ///
        /// の2種類になった。
        ///
        /// </summary>
        /// <param name="gltf"></param>
        /// <param name="json"></param>
        /// <param name="expression"></param>
        static void ToMaterialColorBinds(UniGLTF.glTF gltf, JsonNode json, UniGLTF.Extensions.VRMC_vrm.Expression expression)
        {
            foreach (var x in json.ArrayItems())
            {
                var materialName = x["materialName"].GetString();
                var material     = gltf.materials.FirstOrDefault(y => y.name == materialName);
                if (material == null)
                {
                    // invalid data. skip
                    Debug.LogWarning($"[MigrationVrmExpression] material.name == {materialName} is not found");
                    continue;
                }
                var materialIndex = gltf.materials.IndexOf(material);
                if (materialIndex == -1)
                {
                    // invalid data. skip
                    Debug.LogWarning($"[MigrationVrmExpression] material.name == {materialName} index");
                    continue;
                }
                var propertyName = x["propertyName"].GetString();
                var targetValue  = x["targetValue"].ArrayItems().Select(y => y.GetSingle()).ToArray();
                if (propertyName.EndsWith("_ST"))
                {
                    // VRM-0 は無変換
                    var(scale, offset) = UniGLTF.TextureTransform.VerticalFlipScaleOffset(
                        new UnityEngine.Vector2(targetValue[0], targetValue[1]),
                        new UnityEngine.Vector2(targetValue[2], targetValue[3]));

                    expression.TextureTransformBinds.Add(new UniGLTF.Extensions.VRMC_vrm.TextureTransformBind
                    {
                        Material = materialIndex,
                        Scale    = new float[] { scale.x, scale.y },
                        Offset   = new float[] { offset.x, offset.y }
                    });
                }
                else if (propertyName.EndsWith("_ST_S"))
                {
                    // VRM-0 は無変換
                    var(scale, offset) = UniGLTF.TextureTransform.VerticalFlipScaleOffset(
                        new UnityEngine.Vector2(targetValue[0], 1),
                        new UnityEngine.Vector2(targetValue[2], 0));

                    expression.TextureTransformBinds.Add(new UniGLTF.Extensions.VRMC_vrm.TextureTransformBind
                    {
                        Material = materialIndex,
                        Scale    = new float[] { scale.x, scale.y },
                        Offset   = new float[] { offset.x, offset.y }
                    });
                }
                else if (propertyName.EndsWith("_ST_T"))
                {
                    // VRM-0 は無変換
                    var(scale, offset) = UniGLTF.TextureTransform.VerticalFlipScaleOffset(
                        new UnityEngine.Vector2(1, targetValue[1]),
                        new UnityEngine.Vector2(0, targetValue[3]));

                    expression.TextureTransformBinds.Add(new UniGLTF.Extensions.VRMC_vrm.TextureTransformBind
                    {
                        Material = materialIndex,
                        Scale    = new float[] { scale.x, scale.y },
                        Offset   = new float[] { offset.x, offset.y }
                    });
                }
                else
                {
                    // color
                    expression.MaterialColorBinds.Add(new UniGLTF.Extensions.VRMC_vrm.MaterialColorBind
                    {
                        Material    = materialIndex,
                        Type        = ToMaterialType(propertyName),
                        TargetValue = targetValue,
                    });
                }
            }
        }
Exemplo n.º 16
0
        /// <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);
        }
Exemplo n.º 17
0
        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);
        }
Exemplo n.º 18
0
        /// <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);
        }