Exemplo n.º 1
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.º 2
0
 public static VRMBone FromHumanBodyBone(this HumanBodyBones human)
 {
     return(EnumUtil.TryParseOrDefault <VRMBone>(human.ToString(), VRMBone.unknown));
 }
Exemplo n.º 3
0
        BlendShapeClip LoadBlendShapeBind(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         = Meshes[x.mesh].Mesh;
                    var node         = Root.transform.Traverse().First(y => y.GetSharedMesh() == 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);
        }