/// <summary>
        /// ストリームから表情モーションを生成する
        /// </summary>
        /// <param name="stream">ストリーム</param>
        /// <param name="model">対象のモデル</param>
        public CubismExpressionMotion(Stream stream, CubismModel model)
        {
            // 表情モーションデータを読み込む
            var json = CubismExpressionJson.Create(stream);

            GlobalFadeInSeconds  = Math.Min(Math.Max(json.FadeInTime, 0.0), 1.0);
            GlobalFadeOutSeconds = Math.Min(Math.Max(json.FadeOutTime, 0.0), 1.0);

            // パラメータを読み取る
            Parameters = new ExpressionParameter[json.Parameters.Length];
            for (int index = 0; index < json.Parameters.Length; index++)
            {
                var parameter      = new ExpressionParameter();
                var parameter_item = json.Parameters[index];
                parameter.Parameter = model.GetParameter(parameter_item.Id);
                switch (parameter_item.Blend)
                {
                case "Add":
                default:
                    parameter.Blend = BlendType.Add;
                    break;

                case "Multiply":
                    parameter.Blend = BlendType.Multiply;
                    break;

                case "Overwrite":
                    parameter.Blend = BlendType.Overwrite;
                    break;
                }
                parameter.Value   = parameter_item.Value;
                Parameters[index] = parameter;
            }
        }
Ejemplo n.º 2
0
        private static CubismMotionCurve ParseCurve(CubismMotionJson.CurveItem item, CubismModel model)
        {
            CubismMotionCurve curve = new CubismMotionCurve();

            if (item.Target == "Model")
            {
                curve.Target = CubismMotionCurve.TargetType.Model;
                curve.Effect = new CubismId(item.Id);
            }
            else if (item.Target == "Parameter")
            {
                curve.Target    = CubismMotionCurve.TargetType.Parameter;
                curve.Parameter = model.GetParameter(item.Id);
                if (curve.Parameter == null)
                {
                    return(null);
                }
            }
            else if (item.Target == "PartOpacity")
            {
                curve.Target = CubismMotionCurve.TargetType.PartOpacity;
                curve.Part   = model.GetPart(item.Id);
                if (curve.Part == null)
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
            curve.FadeInTime  = item.FadeInTime;
            curve.FadeOutTime = item.FadeOutTime;

            // 制御点を読み込む
            // 初めの2アイテムは最初の制御点を示す
            var segment_item             = item.Segments;
            int segment_count            = item.Segments.Length;
            CubismMotionPoint last_point = new CubismMotionPoint(segment_item[0], segment_item[1]);

            // 以降の制御点を読み込む
            var segments = new List <ICubismMotionSegment>();

            for (int segment_index = 2; segment_index < segment_count;)
            {
                switch ((CubismMotionSegmentType)segment_item[segment_index])
                {
                case CubismMotionSegmentType.Linear:
                {
                    var segment = new CubismMotionLinearSegment();
                    segment.Points[0] = last_point;
                    segment.Points[1] = new CubismMotionPoint(segment_item[segment_index + 1], segment_item[segment_index + 2]);
                    segments.Add(segment);
                    last_point     = segment.Points[1];
                    segment_index += 3;
                    break;
                }

                case CubismMotionSegmentType.Bezier:
                {
                    var segment = new CubismMotionBezierSegment();
                    segment.Points[0] = last_point;
                    segment.Points[1] = new CubismMotionPoint(segment_item[segment_index + 1], segment_item[segment_index + 2]);
                    segment.Points[2] = new CubismMotionPoint(segment_item[segment_index + 3], segment_item[segment_index + 4]);
                    segment.Points[3] = new CubismMotionPoint(segment_item[segment_index + 5], segment_item[segment_index + 6]);
                    segments.Add(segment);
                    last_point     = segment.Points[3];
                    segment_index += 7;
                    break;
                }

                case CubismMotionSegmentType.Stepped:
                {
                    var segment = new CubismMotionSteppedSegment();
                    segment.Points[0] = last_point;
                    segment.Points[1] = new CubismMotionPoint(segment_item[segment_index + 1], segment_item[segment_index + 2]);
                    segments.Add(segment);
                    last_point     = segment.Points[1];
                    segment_index += 3;
                    break;
                }

                case CubismMotionSegmentType.InverseStepped:
                {
                    var segment = new CubismMotionInverseSteppedSegment();
                    segment.Points[0] = last_point;
                    segment.Points[1] = new CubismMotionPoint(segment_item[segment_index + 1], segment_item[segment_index + 2]);
                    segments.Add(segment);
                    last_point     = segment.Points[1];
                    segment_index += 3;
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            curve.Segments = segments.ToArray();

            return(curve);
        }