Exemplo n.º 1
0
        public void Add(BlendShapeClip clip, Transform transform, List <Mesh> meshes)
        {
            var list = new List <glTF_VRM_BlendShapeBind>();

            if (clip.Values != null)
            {
                list.AddRange(clip.Values.Select(y => glTF_VRM_BlendShapeBind.Cerate(transform, meshes.ToList(), y)));
            }

            var materialList = new List <glTF_VRM_MaterialValueBind>();

            if (clip.MaterialValues != null)
            {
                materialList.AddRange(clip.MaterialValues.Select(y => new glTF_VRM_MaterialValueBind
                {
                    materialName = y.MaterialName,
                    propertyName = y.ValueName,
                    targetValue  = y.TargetValue.ToArray(),
                }));
            }

            var group = new glTF_VRM_BlendShapeGroup
            {
                name           = clip.BlendShapeName,
                presetName     = clip.Preset.ToString().ToLower(),
                binds          = list,
                materialValues = materialList,
            };

            blendShapeGroups.Add(group);
        }
        public static string ToJson(this glTF_VRM_BlendShapeGroup self)
        {
            var f = new JsonFormatter();

            VRMSerializer.Serialize_vrm_blendShapeMaster_blendShapeGroups_ITEM(f, self);
            return(f.ToString());
        }
 public void BlendShapeGroupTestError()
 {
     var model = new glTF_VRM_BlendShapeGroup()
     {
         presetName = "aaaaaaaaaaaa_not_exists_",
     };
 }
        public void BlendShapeGroupTest()
        {
            var model = new glTF_VRM_BlendShapeGroup()
            {
                presetName = "neutral",
            };

            var json = model.ToJson();

            Assert.AreEqual(@"{""presetName"":""neutral"",""binds"":[],""materialValues"":[],""isBinary"":false}", json);
            Debug.Log(json);
        }
Exemplo n.º 5
0
        public void BlendShapeGroupTestError()
        {
            var model = new glTF_VRM_BlendShapeGroup()
            {
                presetName = "aaaaaaaaaaaa_not_exists_",
            };

            var c = new JsonSchemaValidationContext("")
            {
                EnableDiagnosisForNotRequiredFields = true,
            };
            var ex = Assert.Throws <JsonSchemaValidationException>(
                () => JsonSchema.FromType <glTF_VRM_BlendShapeGroup>().Serialize(model, c)
                );

            Assert.AreEqual("[presetName.String] aaaaaaaaaaaa_not_exists_ is not valid enum", ex.Message);
        }
Exemplo n.º 6
0
        public static void Add(this glTF_VRM_BlendShapeMaster master,
                               BlendShapeClip clip, Transform transform, List <Mesh> meshes)
        {
            var list = new List <glTF_VRM_BlendShapeBind>();

            if (clip.Values != null)
            {
                foreach (var value in clip.Values)
                {
                    var bind = Create(transform, meshes, value);
                    if (bind == null)
                    {
                        // Debug.LogFormat("{0}: skip blendshapebind", clip.name);
                        continue;
                    }
                    list.Add(bind);
                }
            }

            var materialList = new List <glTF_VRM_MaterialValueBind>();

            if (clip.MaterialValues != null)
            {
                materialList.AddRange(clip.MaterialValues.Select(y => new glTF_VRM_MaterialValueBind
                {
                    materialName = y.MaterialName,
                    propertyName = y.ValueName,
                    targetValue  = y.TargetValue.ToArray(),
                }));
            }

            var group = new glTF_VRM_BlendShapeGroup
            {
                name           = clip.BlendShapeName,
                presetName     = clip.Preset.ToString().ToLower(),
                isBinary       = clip.IsBinary,
                binds          = list,
                materialValues = materialList,
            };

            master.blendShapeGroups.Add(group);
        }
Exemplo n.º 7
0
        public void BlendShapeGroupTest()
        {
            var model = new glTF_VRM_BlendShapeGroup()
            {
                presetName = "neutral",
            };

            var json = model.ToJson();

            Assert.AreEqual(@"{""presetName"":""neutral"",""isBinary"":false,""binds"":[],""materialValues"":[]}", json);
            Debug.Log(json);

            var c = new JsonSchemaValidationContext("")
            {
                EnableDiagnosisForNotRequiredFields = true,
            };
            var json2 = JsonSchema.FromType <glTF_VRM_BlendShapeGroup>().Serialize(model, c);

            Assert.AreEqual(@"{""presetName"":""neutral"",""binds"":[],""materialValues"":[],""isBinary"":false}", json2);
        }
Exemplo n.º 8
0
        public static glTF_VRM_BlendShapeGroup Deserialize_vrm_blendShapeMaster_blendShapeGroups_LIST(ListTreeNode <JsonValue> parsed)
        {
            var value = new glTF_VRM_BlendShapeGroup();

            foreach (var kv in parsed.ObjectItems())
            {
                var key = kv.Key.GetString();

                if (key == "name")
                {
                    value.name = kv.Value.GetString();
                    continue;
                }

                if (key == "presetName")
                {
                    value.presetName = kv.Value.GetString();
                    continue;
                }

                if (key == "binds")
                {
                    value.binds = Deserialize_vrm_blendShapeMaster_blendShapeGroups__binds(kv.Value);
                    continue;
                }

                if (key == "materialValues")
                {
                    value.materialValues = Deserialize_vrm_blendShapeMaster_blendShapeGroups__materialValues(kv.Value);
                    continue;
                }

                if (key == "isBinary")
                {
                    value.isBinary = kv.Value.GetBoolean();
                    continue;
                }
            }
            return(value);
        }
Exemplo n.º 9
0
        private static BlendShapeClip LoadBlendShapeBind(VRMImporterContext context,
                                                         glTF_VRM_BlendShapeGroup group)
        {
            var asset     = ScriptableObject.CreateInstance <BlendShapeClip>();
            var groupName = group.name;
            var prefix    = "BlendShape.";

            while (groupName.StartsWith(prefix))
            {
                groupName = groupName.Substring(prefix.Length);
            }
            asset.name = "BlendShape." + groupName;

            if (group != null)
            {
                asset.BlendShapeName = groupName;
                asset.Preset         = EnumUtil.TryParseOrDefault <BlendShapePreset>(group.presetName);
                if (asset.Preset == BlendShapePreset.Unknown)
                {
                    // fallback
                    asset.Preset = EnumUtil.TryParseOrDefault <BlendShapePreset>(group.name);
                }
                asset.Values = group.binds.Select(x =>
                {
                    var mesh         = context.Meshes[x.mesh].Mesh;
                    var node         = context.Root.transform.Traverse().First(y => y.GetSharedMesh() == mesh);
                    var relativePath = UniGLTF.UnityExtensions.RelativePathFrom(node, context.Root.transform);
                    return(new BlendShapeBinding
                    {
                        RelativePath = relativePath,
                        Index = x.index,
                        Weight = x.weight,
                    });
                })
                               .ToArray();
                asset.MaterialValues = group.materialValues.Select(x =>
                {
                    var value = new Vector4();
                    for (int i = 0; i < x.targetValue.Length; ++i)
                    {
                        switch (i)
                        {
                        case 0: value.x = x.targetValue[0]; break;

                        case 1: value.y = x.targetValue[1]; break;

                        case 2: value.z = x.targetValue[2]; break;

                        case 3: value.w = x.targetValue[3]; break;
                        }
                    }
                    return(new MaterialValueBinding
                    {
                        MaterialName = x.materialName,
                        ValueName = x.propertyName,
                        TargetValue = value,
                        BaseValue = context.Materials.First(y => y.name == x.materialName).GetColor(x.propertyName),
                    });
                })
                                       .ToArray();
            }

            return(asset);
        }
Exemplo n.º 10
0
        public static void Serialize_vrm_blendShapeMaster_blendShapeGroups_ITEM(JsonFormatter f, glTF_VRM_BlendShapeGroup value)
        {
            f.BeginMap();


            if (!string.IsNullOrEmpty(value.name))
            {
                f.Key("name");
                f.Value(value.name);
            }

            if (!string.IsNullOrEmpty(value.presetName))
            {
                f.Key("presetName");
                f.Value(value.presetName);
            }

            if (value.binds != null && value.binds.Count >= 0)
            {
                f.Key("binds");
                Serialize_vrm_blendShapeMaster_blendShapeGroups__binds(f, value.binds);
            }

            if (value.materialValues != null && value.materialValues.Count >= 0)
            {
                f.Key("materialValues");
                Serialize_vrm_blendShapeMaster_blendShapeGroups__materialValues(f, value.materialValues);
            }

            if (true)
            {
                f.Key("isBinary");
                f.Value(value.isBinary);
            }

            f.EndMap();
        }
Exemplo n.º 11
0
        BlendShapeClip LoadBlendShapeBind(glTF_VRM_BlendShapeGroup group, Dictionary <Mesh, Transform> transformMeshTable)
        {
            var asset     = ScriptableObject.CreateInstance <BlendShapeClip>();
            var groupName = group.name;
            var prefix    = "BlendShape.";

            while (groupName.StartsWith(prefix))
            {
                groupName = groupName.Substring(prefix.Length);
            }
            asset.name = "BlendShape." + groupName;

            if (group != null)
            {
                asset.BlendShapeName = groupName;
                asset.Preset         = CacheEnum.TryParseOrDefault <BlendShapePreset>(group.presetName, true);
                asset.IsBinary       = group.isBinary;
                if (asset.Preset == BlendShapePreset.Unknown)
                {
                    // fallback
                    asset.Preset = CacheEnum.TryParseOrDefault <BlendShapePreset>(group.name, true);
                }
                asset.Values = group.binds.Select(x =>
                {
                    var mesh         = Meshes[x.mesh].Mesh;
                    var node         = transformMeshTable[mesh];
                    var relativePath = UniGLTF.UnityExtensions.RelativePathFrom(node, Root.transform);
                    return(new BlendShapeBinding
                    {
                        RelativePath = relativePath,
                        Index = x.index,
                        Weight = x.weight,
                    });
                })
                               .ToArray();
                asset.MaterialValues = group.materialValues.Select(x =>
                {
                    var value = new Vector4();
                    for (int i = 0; i < x.targetValue.Length; ++i)
                    {
                        switch (i)
                        {
                        case 0: value.x = x.targetValue[0]; break;

                        case 1: value.y = x.targetValue[1]; break;

                        case 2: value.z = x.targetValue[2]; break;

                        case 3: value.w = x.targetValue[3]; break;
                        }
                    }

                    var material     = GetMaterials().FirstOrDefault(y => y.name == x.materialName);
                    var propertyName = x.propertyName;
                    if (x.propertyName.EndsWith("_ST_S") ||
                        x.propertyName.EndsWith("_ST_T"))
                    {
                        propertyName = x.propertyName.Substring(0, x.propertyName.Length - 2);
                    }

                    var binding = default(MaterialValueBinding?);

                    if (material != null)
                    {
                        try
                        {
                            binding = new MaterialValueBinding
                            {
                                MaterialName = x.materialName,
                                ValueName    = x.propertyName,
                                TargetValue  = value,
                                BaseValue    = material.GetColor(propertyName),
                            };
                        }
                        catch (Exception)
                        {
                            // do nothing
                        }
                    }

                    return(binding);
                })
                                       .Where(x => x.HasValue)
                                       .Select(x => x.Value)
                                       .ToArray();
            }

            return(asset);
        }