コード例 #1
0
ファイル: VrmExpressionAdapter.cs プロジェクト: seibe/UniVRM
        public static UniGLTF.Extensions.VRMC_vrm.Expression ToGltf(this VrmLib.Expression x, List <VrmLib.Node> nodes, List <VrmLib.Material> materials)
        {
            var g = new UniGLTF.Extensions.VRMC_vrm.Expression
            {
                Preset       = (UniGLTF.Extensions.VRMC_vrm.ExpressionPreset)x.Preset,
                Name         = x.Name,
                IsBinary     = x.IsBinary,
                IgnoreBlink  = x.IgnoreBlink,
                IgnoreLookAt = x.IgnoreLookAt,
                IgnoreMouth  = x.IgnoreMouth,
            };

            foreach (var blendShapeBind in x.MorphTargetBinds)
            {
                g.MorphTargetBinds.Add(blendShapeBind.ToGltf(nodes));
            }
            foreach (var materialColorBind in x.MaterialColorBinds)
            {
                g.MaterialColorBinds.Add(materialColorBind.ToGltf(materials));
            }
            foreach (var materialUVBind in x.TextureTransformBinds)
            {
                g.TextureTransformBinds.Add(materialUVBind.ToGltf(materials));
            }
            return(g);
        }
コード例 #2
0
        public static IEnumerable <(ExpressionPreset, string, UniGLTF.Extensions.VRMC_vrm.Expression)> Migrate(UniGLTF.glTF gltf, JsonNode json,
                                                                                                               MigrationVrm.MeshIndexToNodeIndexFunc meshToNode)
        {
            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 preset     = ToPreset(blendShapeClip["presetName"], name);
                var expression = new UniGLTF.Extensions.VRMC_vrm.Expression
                {
                    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(blendShapeClip["binds"], meshToNode).ToList();

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

                yield return(preset, name, expression);
            }
        }
コード例 #3
0
ファイル: SerializationTests.cs プロジェクト: seibe/UniVRM
        public void ExpressionTest()
        {
            var q = "\"";

            {
                var data = new UniGLTF.Extensions.VRMC_vrm.Expression();
                data.IgnoreBlink = true;

                var json = Serialize(data, UniGLTF.Extensions.VRMC_vrm.GltfSerializer.Serialize_Expressions_ITEM);
                Assert.AreEqual($"{{{q}preset{q}:{q}custom{q},{q}ignoreBlink{q}:true}}", json);
            }

            {
                var expression = new VrmLib.Expression(VrmLib.ExpressionPreset.Blink, "blink", true)
                {
                    IgnoreBlink  = true,
                    IgnoreLookAt = true,
                    IgnoreMouth  = true,
                };

                // export
                var gltf = UniVRM10.ExpressionAdapter.ToGltf(expression, new List <VrmLib.Node>(), new List <VrmLib.Material>());
                Assert.AreEqual(true, gltf.IgnoreBlink);
                Assert.AreEqual(true, gltf.IgnoreLookAt);
                Assert.AreEqual(true, gltf.IgnoreMouth);

                // import
                var imported = UniVRM10.ExpressionAdapter.FromGltf(gltf, new List <VrmLib.Node>(), new List <VrmLib.Material>());
                Assert.AreEqual(true, imported.IgnoreBlink);
                Assert.AreEqual(true, imported.IgnoreLookAt);
                Assert.AreEqual(true, imported.IgnoreMouth);
            }
        }
コード例 #4
0
 /// <summary>
 /// dst が null の場合だけ代入する。
 /// 先に来た方を有効にしたい。
 /// </summary>
 /// <param name="dst"></param>
 /// <param name="src"></param>
 static void SetIfNull(ref UniGLTF.Extensions.VRMC_vrm.Expression dst, UniGLTF.Extensions.VRMC_vrm.Expression src)
 {
     if (dst == null)
     {
         dst = src;
     }
 }
コード例 #5
0
ファイル: VrmExpressionAdapter.cs プロジェクト: pdkyll/UniVRM
        public static UniGLTF.Extensions.VRMC_vrm.Expression ToGltf(this VrmLib.Expression x, List <VrmLib.Node> nodes, List <VrmLib.Material> materials)
        {
            var g = new UniGLTF.Extensions.VRMC_vrm.Expression
            {
                Preset         = x.Preset.ToGltfFormat(),
                Name           = x.Name,
                IsBinary       = x.IsBinary,
                OverrideBlink  = EnumUtil.Cast <UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType>(x.OverrideBlink),
                OverrideLookAt = EnumUtil.Cast <UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType>(x.OverrideLookAt),
                OverrideMouth  = EnumUtil.Cast <UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType>(x.OverrideMouth),
            };

            g.MorphTargetBinds = new List <UniGLTF.Extensions.VRMC_vrm.MorphTargetBind>();
            foreach (var blendShapeBind in x.MorphTargetBinds)
            {
                g.MorphTargetBinds.Add(blendShapeBind.ToGltf(nodes));
            }

            g.MaterialColorBinds = new List <UniGLTF.Extensions.VRMC_vrm.MaterialColorBind>();
            foreach (var materialColorBind in x.MaterialColorBinds)
            {
                g.MaterialColorBinds.Add(materialColorBind.ToGltf(materials));
            }

            g.TextureTransformBinds = new List <UniGLTF.Extensions.VRMC_vrm.TextureTransformBind>();
            foreach (var materialUVBind in x.TextureTransformBinds)
            {
                g.TextureTransformBinds.Add(materialUVBind.ToGltf(materials));
            }

            return(g);
        }
コード例 #6
0
 static ExpressionKey CreateKey(UniGLTF.Extensions.VRMC_vrm.Expression expression)
 {
     if (expression.Preset == UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.custom)
     {
         return(ExpressionKey.CreateCustom(expression.Name));
     }
     else
     {
         return(ExpressionKey.CreateFromPreset(expression.Preset));
     }
 }
コード例 #7
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,
             });
         }
     }
 }
コード例 #8
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);
     }
 }
コード例 #9
0
ファイル: VrmExpressionAdapter.cs プロジェクト: seibe/UniVRM
        public static VrmLib.Expression FromGltf(this UniGLTF.Extensions.VRMC_vrm.Expression x, List <VrmLib.Node> nodes, List <VrmLib.Material> materials)
        {
            var expression = new VrmLib.Expression((VrmLib.ExpressionPreset)x.Preset,
                                                   x.Name,
                                                   x.IsBinary.HasValue && x.IsBinary.Value)
            {
                IgnoreBlink  = x.IgnoreBlink.GetValueOrDefault(),
                IgnoreLookAt = x.IgnoreLookAt.GetValueOrDefault(),
                IgnoreMouth  = x.IgnoreMouth.GetValueOrDefault(),
            };

            if (x.MorphTargetBinds != null)
            {
                foreach (var y in x.MorphTargetBinds)
                {
                    var node           = nodes[y.Node.Value];
                    var blendShapeName = node.Mesh.MorphTargets[y.Index.Value].Name;
                    var blendShapeBind = new MorphTargetBind(node, blendShapeName, y.Weight.Value);
                    expression.MorphTargetBinds.Add(blendShapeBind);
                }
            }

            if (x.MaterialColorBinds != null)
            {
                foreach (var y in x.MaterialColorBinds)
                {
                    var material          = materials[y.Material.Value];
                    var materialColorBind = new MaterialColorBind(material, EnumUtil.Cast <MaterialBindType>(y.Type), y.TargetValue.ToVector4(Vector4.Zero));
                    expression.MaterialColorBinds.Add(materialColorBind);
                }
            }

            if (x.TextureTransformBinds != null)
            {
                foreach (var y in x.TextureTransformBinds)
                {
                    var material       = materials[y.Material.Value];
                    var materialUVBind = new TextureTransformBind(material,
                                                                  y.Scaling.ToVector2(Vector2.One),
                                                                  y.Offset.ToVector2(Vector2.Zero));
                    expression.TextureTransformBinds.Add(materialUVBind);
                }
            }

            return(expression);
        }
コード例 #10
0
        static void Check(string name, JsonNode vrm0, UniGLTF.Extensions.VRMC_vrm.Expression vrm1,
                          MigrationVrm.MeshIndexToNodeIndexFunc meshToNode)
        {
            if (vrm0["binds"].GetArrayCount() == 0)
            {
                if (vrm1.MorphTargetBinds == null)
                {
                    // OK
                    return;
                }
                else
                {
                    throw new MigrationException($"expression.{name}.binds", "different count");
                }
            }

            foreach (var(l, r) in Enumerable.Zip(vrm0["binds"].ArrayItems(), vrm1.MorphTargetBinds, (x, y) => (x, y)))
            {
                var mesh = l["mesh"].GetInt32();
                var node = meshToNode(mesh);
                if (node != r.Node)
                {
                    throw new MigrationException($"expression.{name}.binds.node", $"{node} != {r.Node}");
                }

                var index = l["index"].GetInt32();
                if (index != r.Index)
                {
                    throw new MigrationException($"expression.{name}.binds.index", $"{index} != {r.Index}");
                }

                var weight = l["weight"].GetSingle() * 0.01f; // [0, 100] to [0, 1.0f]
                if (weight != r.Weight)
                {
                    throw new MigrationException($"expression.{name}.binds.weight", $"{weight} != {r.Weight}");
                }
            }
        }
コード例 #11
0
        public void ExpressionTest()
        {
            var q = "\"";

            {
                var data = new UniGLTF.Extensions.VRMC_vrm.Expression();
                data.OverrideBlink = UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType.block;

                var json = Serialize(data, UniGLTF.Extensions.VRMC_vrm.GltfSerializer.__expressions_Serialize_Custom_ITEM);
                Assert.AreEqual($"{{{q}preset{q}:{q}custom{q},{q}overrideBlink{q}:{q}block{q},{q}overrideLookAt{q}:{q}none{q},{q}overrideMouth{q}:{q}none{q}}}", json);
            }

            {
                // var expression = new UniGLTF.Extensions.VRMC_vrm.Expression(UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.Blink, "blink", true)
                // {
                //     OverrideBlink = UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType.None,
                //     OverrideLookAt = UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType.Block,
                //     OverrideMouth = UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType.Blend,
                // };

                // // export
                // var gltf = UniVRM10.ExpressionAdapter.ToGltf(expression, new List<UniGLTF.Extensions.VRMC_vrm.Node>(), new List<UniGLTF.Extensions.VRMC_vrm.Material>());
                // Assert.AreEqual(UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType.none, gltf.OverrideBlink);
                // Assert.AreEqual(UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType.block, gltf.OverrideLookAt);
                // Assert.AreEqual(UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType.blend, gltf.OverrideMouth);

                // // import
                // var imported = UniVRM10.ExpressionAdapter.FromGltf(gltf, new List<UniGLTF.Extensions.VRMC_vrm.Node>(), new List<UniGLTF.Extensions.VRMC_vrm.Material>());
                // Assert.AreEqual(UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType.None, imported.OverrideBlink);
                // Assert.AreEqual(UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType.Block, imported.OverrideLookAt);
                // Assert.AreEqual(UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType.Blend, imported.OverrideMouth);
            }

            {
                // // export
                // foreach (var preset in Enum.GetValues(typeof(UniGLTF.Extensions.VRMC_vrm.ExpressionPreset)) as UniGLTF.Extensions.VRMC_vrm.ExpressionPreset[])
                // {
                //     var expression = new UniGLTF.Extensions.VRMC_vrm.Expression(preset, "", false);

                //     // expect no exception
                //     var gltf = ExpressionAdapter.ToGltf(
                //         expression,
                //         new List<UniGLTF.Extensions.VRMC_vrm.Node>(),
                //         new List<UniGLTF.Extensions.VRMC_vrm.Material>());
                // }

                // // import
                // foreach (var preset in Enum.GetValues(typeof(UniGLTF.Extensions.VRMC_vrm.ExpressionPreset)) as UniGLTF.Extensions.VRMC_vrm.ExpressionPreset[])
                // {
                //     var gltf = new UniGLTF.Extensions.VRMC_vrm.Expression
                //     {
                //         Preset = preset,
                //     };

                //     // expect no exception
                //     ExpressionAdapter.FromGltf(
                //         gltf,
                //         new List<UniGLTF.Extensions.VRMC_vrm.Node>(),
                //         new List<UniGLTF.Extensions.VRMC_vrm.Material>());
                // }
            }
        }
コード例 #12
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,
                    });
                }
            }
        }
コード例 #13
0
ファイル: SerializationTests.cs プロジェクト: pdkyll/UniVRM
        public void ExpressionTest()
        {
            var q = "\"";

            {
                var data = new UniGLTF.Extensions.VRMC_vrm.Expression();
                data.OverrideBlink = UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType.block;

                var json = Serialize(data, UniGLTF.Extensions.VRMC_vrm.GltfSerializer.Serialize_Expressions_ITEM);
                Assert.AreEqual($"{{{q}preset{q}:{q}custom{q},{q}overrideBlink{q}:{q}block{q},{q}overrideLookAt{q}:{q}none{q},{q}overrideMouth{q}:{q}none{q}}}", json);
            }

            {
                var expression = new VrmLib.Expression(VrmLib.ExpressionPreset.Blink, "blink", true)
                {
                    OverrideBlink  = VrmLib.ExpressionOverrideType.None,
                    OverrideLookAt = VrmLib.ExpressionOverrideType.Block,
                    OverrideMouth  = VrmLib.ExpressionOverrideType.Blend,
                };

                // export
                var gltf = UniVRM10.ExpressionAdapter.ToGltf(expression, new List <VrmLib.Node>(), new List <VrmLib.Material>());
                Assert.AreEqual(UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType.none, gltf.OverrideBlink);
                Assert.AreEqual(UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType.block, gltf.OverrideLookAt);
                Assert.AreEqual(UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType.blend, gltf.OverrideMouth);

                // import
                var imported = UniVRM10.ExpressionAdapter.FromGltf(gltf, new List <VrmLib.Node>(), new List <VrmLib.Material>());
                Assert.AreEqual(VrmLib.ExpressionOverrideType.None, imported.OverrideBlink);
                Assert.AreEqual(VrmLib.ExpressionOverrideType.Block, imported.OverrideLookAt);
                Assert.AreEqual(VrmLib.ExpressionOverrideType.Blend, imported.OverrideMouth);
            }

            {
                // export
                foreach (var preset in Enum.GetValues(typeof(VrmLib.ExpressionPreset)) as VrmLib.ExpressionPreset[])
                {
                    var expression = new VrmLib.Expression(preset, "", false);

                    // expect no exception
                    var gltf = ExpressionAdapter.ToGltf(
                        expression,
                        new List <VrmLib.Node>(),
                        new List <VrmLib.Material>());
                }

                // import
                foreach (var preset in Enum.GetValues(typeof(UniGLTF.Extensions.VRMC_vrm.ExpressionPreset)) as UniGLTF.Extensions.VRMC_vrm.ExpressionPreset[])
                {
                    var gltf = new UniGLTF.Extensions.VRMC_vrm.Expression
                    {
                        Preset = preset,
                    };

                    // expect no exception
                    ExpressionAdapter.FromGltf(
                        gltf,
                        new List <VrmLib.Node>(),
                        new List <VrmLib.Material>());
                }
            }
        }