Ejemplo n.º 1
0
        public static BlendShapeManager FromGltf(this VrmBlendShapeMaster master, List <MeshGroup> meshes, List <Material> materials, List <Node> nodes)
        {
            var manager = new BlendShapeManager();

            manager.BlendShapeList.AddRange(master.blendShapeGroups.Select(x =>
            {
                var expression = new BlendShape(x.presetName.FromGltf(), x.name, x.isBinary);
                expression.BlendShapeValues.AddRange(
                    x.binds.Select(y =>
                {
                    var group          = meshes[y.mesh];
                    var node           = nodes.First(z => z.MeshGroup == group);
                    var blendShapeName = group.Meshes[0].MorphTargets[y.index].Name;
                    var value          = new BlendShapeBindValue(node, blendShapeName, y.weight);
                    return(value);
                }));
                expression.MaterialValues.AddRange(
                    x.materialValues.Select(y =>
                {
                    var material = materials.First(z => z.Name == y.materialName);
                    var target   = new Vector4(y.targetValue[0], y.targetValue[1], y.targetValue[2], y.targetValue[3]);
                    var value    = new MaterialBindValue(material, y.propertyName, target);
                    return(value);
                }));
                return(expression);
            }));
            return(manager);
        }
Ejemplo n.º 2
0
        public static BlendShapeManager FromGltf(this VrmBlendShapeMaster master, List <MeshGroup> meshes, List <Material> materials, List <Node> nodes)
        {
            var manager = new BlendShapeManager();

            manager.BlendShapeList.AddRange(master.blendShapeGroups.Select(x =>
            {
                var expression = new BlendShape(x.presetName.FromGltf(), x.name, x.isBinary);
                expression.BlendShapeValues.AddRange(
                    x.binds.Select(y =>
                {
                    var group          = meshes[y.mesh];
                    var node           = nodes.First(z => z.MeshGroup == group);
                    var blendShapeName = group.Meshes[0].MorphTargets[y.index].Name;
                    var value          = new BlendShapeBindValue(node, blendShapeName, y.weight);
                    return(value);
                }));

                /// VRM-0.X の MaterialBindValue を VRM-1.0 仕様に変換する
                foreach (var y in x.materialValues)
                {
                    var material = materials.First(z => z.Name == y.materialName);

                    if (y.propertyName.EndsWith("_ST"))
                    {
                        expression.UVScaleOffsetValues.Add(new UVScaleOffsetValue(material,
                                                                                  new Vector2(y.targetValue[0], y.targetValue[1]),
                                                                                  new Vector2(y.targetValue[2], y.targetValue[3])));
                    }
                    else if (y.propertyName.EndsWith("_ST_S"))
                    {
                        expression.UVScaleOffsetValues.Add(new UVScaleOffsetValue(material,
                                                                                  new Vector2(y.targetValue[0], 1),
                                                                                  new Vector2(y.targetValue[2], 0)));
                    }
                    else if (y.propertyName.EndsWith("_ST_T"))
                    {
                        expression.UVScaleOffsetValues.Add(new UVScaleOffsetValue(material,
                                                                                  new Vector2(1, y.targetValue[1]),
                                                                                  new Vector2(0, y.targetValue[3])));
                    }
                    else
                    {
                        var bindType = material.GetBindType(y.propertyName);
                        var target   = new Vector4(y.targetValue[0], y.targetValue[1], y.targetValue[2], y.targetValue[3]);
                        expression.MaterialValues.Add(new MaterialBindValue(material, bindType, target));
                    }
                }
                return(expression);
            }));
            return(manager);
        }
Ejemplo n.º 3
0
        public static BlendShapeManager FromGltf(this VrmProtobuf.BlendShape master, List <MeshGroup> meshes, List <Material> materials)
        {
            var manager = new BlendShapeManager();

            manager.BlendShapeList.AddRange(master.BlendShapeGroups.Select(x =>
            {
                var expression = new BlendShape((VrmLib.BlendShapePreset)x.Preset,
                                                x.Name,
                                                x.IsBinary.HasValue && x.IsBinary.Value);
                expression.BlendShapeValues.AddRange(
                    x.Binds.Select(y =>
                {
                    var group          = meshes[y.Mesh];
                    var blendShapeName = group.Meshes[0].MorphTargets[y.Index].Name;
                    var value          = new BlendShapeBindValue(group, blendShapeName, y.Weight);
                    return(value);
                }));
                expression.MaterialValues.AddRange(
                    x.MaterialValues.Select(y =>
                {
                    var material   = materials[y.Material];
                    Vector4 target = default;
                    if (y.TargetValue.Count > 0)
                    {
                        target.X = y.TargetValue[0];
                    }
                    if (y.TargetValue.Count > 1)
                    {
                        target.Y = y.TargetValue[1];
                    }
                    if (y.TargetValue.Count > 2)
                    {
                        target.Z = y.TargetValue[2];
                    }
                    if (y.TargetValue.Count > 3)
                    {
                        target.W = y.TargetValue[3];
                    }
                    var value = new MaterialBindValue(material, EnumUtil.Parse <MaterialBindType>(y.Type.ToString()), target);
                    return(value);
                }));
                return(expression);
            }));
            return(manager);
        }
Ejemplo n.º 4
0
        public static VrmProtobuf.BlendShapeBind ToGltf(this BlendShapeBindValue self, List <VrmLib.Node> nodes)
        {
            var name  = self.Name;
            var value = self.Value;
            var index = self.Node.Mesh.MorphTargets.FindIndex(x => x.Name == name);

            if (index < 0)
            {
                throw new IndexOutOfRangeException(string.Format("MorphTargetName {0} is not found", name));
            }

            return(new VrmProtobuf.BlendShapeBind
            {
                Node = nodes.IndexOfThrow(self.Node),
                Index = self.Node.Mesh.MorphTargets.FindIndex(x => x.Name == name),
                Weight = value,
            });
        }
Ejemplo n.º 5
0
        public static VrmLib.BlendShape FromGltf(BlendShapeGroup x, List <VrmLib.Node> nodes, List <VrmLib.Material> materials)
        {
            var expression = new VrmLib.BlendShape((VrmLib.BlendShapePreset)x.Preset,
                                                   x.Name,
                                                   x.IsBinary.HasValue && x.IsBinary.Value)
            {
                IgnoreBlink  = x.IgnoreBlink.GetValueOrDefault(),
                IgnoreLookAt = x.IgnoreLookAt.GetValueOrDefault(),
                IgnoreMouth  = x.IgnoreMouth.GetValueOrDefault(),
            };

            foreach (var y in x.Binds)
            {
                var node           = nodes[y.Node.Value];
                var blendShapeName = node.Mesh.MorphTargets[y.Index.Value].Name;
                var blendShapeBind = new BlendShapeBindValue(node, blendShapeName, y.Weight.Value);
                expression.BlendShapeValues.Add(blendShapeBind);
            }

            foreach (var y in x.MaterialValues)
            {
                var     material = materials[y.Material.Value];
                Vector4 target   = default;
                if (y.TargetValue.Count > 0)
                {
                    target.X = y.TargetValue[0];
                }
                if (y.TargetValue.Count > 1)
                {
                    target.Y = y.TargetValue[1];
                }
                if (y.TargetValue.Count > 2)
                {
                    target.Z = y.TargetValue[2];
                }
                if (y.TargetValue.Count > 3)
                {
                    target.W = y.TargetValue[3];
                }
                var materialColorBind = new MaterialBindValue(material, EnumUtil.Cast <MaterialBindType>(y.Type), target);
                expression.MaterialValues.Add(materialColorBind);
            }

            foreach (var y in x.MaterialUVBinds)
            {
                var material = materials[y.Material.Value];
                var scaling  = Vector2.One;
                if (y.Scaling.Count > 0)
                {
                    scaling.X = y.Scaling[0];
                }
                if (y.Scaling.Count > 1)
                {
                    scaling.Y = y.Scaling[1];
                }
                var offset = Vector2.Zero;
                if (y.Offset.Count > 0)
                {
                    offset.X = y.Offset[0];
                }
                if (y.Offset.Count > 1)
                {
                    offset.Y = y.Offset[1];
                }
                var materialUVBind = new UVScaleOffsetValue(material, scaling, offset);
                expression.UVScaleOffsetValues.Add(materialUVBind);
            }

            return(expression);
        }