Esempio n. 1
0
        /// <summary>
        /// コアからDrawable情報を読み出す。
        /// </summary>
        private void InitializeDrawables()
        {
            int drawable_count = CubismCore.CsmGetDrawableCount(ModelPtr);

            Drawables = new CubismDrawable[drawable_count];

            // DrawableのID名リストを取得する
            string[] drawable_name_list = IntPtrToStringArray(CubismCore.CsmGetDrawableIds(ModelPtr), drawable_count);

            // テクスチャ番号を読み出す
            int[] texture_indexes = DrawableTextureIndexList;

            // 静的フラグを読み出す
            byte[] constant_flags = DrawableConstantFlagsList;

            // クリッピングマスクを読み出す
            int[]    mask_counts = DrawableClippingMaskCountList;
            IntPtr[] mask_ptrs   = DrawableClippingMaskPointerList;

            // Drawablesを構築する
            for (int index = 0; index < drawable_count; index++)
            {
                string name          = drawable_name_list[index];
                int    texture_index = texture_indexes[index];
                var    flags         = (CubismCore.ConstantDrawableFlags)constant_flags[index];
                var    masks         = IntPtrToIntArray(mask_ptrs[index], mask_counts[index]);
                Drawables[index] = new CubismDrawable(index, name, texture_index, flags, masks);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// アセットを読み込む。
        /// </summary>
        /// <param name="model_file_path">読み込むmodel3.jsonへのファイルパス</param>
        /// <param name="reader">ファイルからデータを読み込むのに使うデリゲート</param>
        /// <returns>trueなら読み込みが成功したことを示す。</returns>
        public CubismAsset(string model_file_path, CubismFileLoader reader)
        {
            string base_dir      = Path.GetDirectoryName(model_file_path);
            var    model_setting = CubismModelSettingJson.Create(reader(model_file_path));

            // mocファイルを読み込み、CubismModelを作成する
            using (Stream stream = reader(Path.Combine(base_dir, model_setting.FileReferences.Moc)))
            {
                byte[] stream_bytes = new byte[stream.Length];
                stream.Read(stream_bytes, 0, (int)stream.Length);
                Moc = new CubismMoc(stream_bytes);
            }
            Model = Moc.CreateModel();

            // パラメータIDグループを読み込む
            // (まばたきやリップシンクに紐づいたパラメータIDのリスト)
            ParameterGroups = new Dictionary <string, CubismParameter[]>();
            if (model_setting.Groups != null)
            {
                foreach (var group in model_setting.Groups)
                {
                    if (group.Target == "Parameter")
                    {
                        // 重複除去のためにHashSetに読み込む
                        var id_names = new HashSet <string>(group.Ids);
                        var ids      = new CubismParameter[id_names.Count];
                        int index    = 0;
                        foreach (var id_name in id_names)
                        {
                            ids[index] = Model.GetParameter(id_name);
                            index++;
                        }
                        ParameterGroups.Add(group.Name, ids);
                    }
                }
            }
            Model.SaveParameters();

            // 呼吸を設定する
            //EyeBlinkController = new CubismEyeBlink(ParameterGroups["EyeBlink"]);
            //BreathController = new CubismBreath();
            //BreathController.SetParameter(new CubismBreathParameter(Model.GetParameter("ParamAngleX"), 0.0, 15.0, 6.5345, 0.5));
            //BreathController.SetParameter(new CubismBreathParameter(Model.GetParameter("ParamAngleY"), 0.0, 8.0, 3.5345, 0.5));
            //BreathController.SetParameter(new CubismBreathParameter(Model.GetParameter("ParamAngleZ"), 0.0, 10.0, 5.5345, 0.5));
            //BreathController.SetParameter(new CubismBreathParameter(Model.GetParameter("ParamBodyAngleX"), 0.0, 4.0, 15.5345, 0.5));
            //BreathController.SetParameter(new CubismBreathParameter(Model.GetParameter("ParamBreath"), 0.5, 0.5, 3.2345, 0.5));

            // モーションを読み込む
            MotionGroups = new Dictionary <string, ICubismMotion[]>();
            if (model_setting.FileReferences.Motions != null)
            {
                foreach (var item in model_setting.FileReferences.Motions)
                {
                    var group_name        = item.Key;
                    var motion_group_item = item.Value;
                    var motion_group      = new List <ICubismMotion>();
                    foreach (var motion_item in motion_group_item)
                    {
                        using (Stream stream = reader(Path.Combine(base_dir, motion_item.File)))
                        {
                            var motion = LoadMotion(stream, motion_item);
                            if (motion != null)
                            {
                                motion_group.Add(motion);
                            }
                        }
                    }
                    MotionGroups.Add(group_name, motion_group.ToArray());
                }
            }

            // 表情モーションを読み込む
            Expressions = new Dictionary <string, ICubismMotion>();
            if (model_setting.FileReferences.Expressions != null)
            {
                foreach (var expression_item in model_setting.FileReferences.Expressions)
                {
                    ICubismMotion expression;
                    using (Stream stream = reader(Path.Combine(base_dir, expression_item.File)))
                    {
                        expression = new CubismExpressionMotion(stream, Model);
                    }
                    if (expression != null)
                    {
                        Expressions.Add(expression_item.Name, expression);
                    }
                }
            }

            // ユーザーデータを読み込む
            UserData = new CubismUserData();
            if (string.IsNullOrEmpty(model_setting.FileReferences.UserData) == false)
            {
                using (Stream stream = reader(Path.Combine(base_dir, model_setting.FileReferences.UserData)))
                {
                    UserData = new CubismUserData(stream);
                }
            }

            // ヒットエリアを読み込む
            HitAreas = new Dictionary <string, CubismDrawable>();
            if (model_setting.HitAreas != null)
            {
                foreach (var hit_area in model_setting.HitAreas)
                {
                    CubismDrawable drawable = Model.GetDrawable(hit_area.Id);
                    if (drawable != null)
                    {
                        HitAreas.Add(hit_area.Name, drawable);
                    }
                }
            }

            // テクスチャを読み込む
            TextureByteArrays = new byte[model_setting.FileReferences.Textures.Length][];
            for (int index = 0; index < TextureByteArrays.Length; index++)
            {
                string path = Path.Combine(base_dir, model_setting.FileReferences.Textures[index]);
                using (Stream stream = reader(path))
                {
                    byte[] buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, buffer.Length);
                    TextureByteArrays[index] = buffer;
                }
            }

            // 物理演算データを読み込む
            // To do.

            // ポーズを読み込む
            if (string.IsNullOrEmpty(model_setting.FileReferences.Pose) == false)
            {
                using (Stream stream = reader(Path.Combine(base_dir, model_setting.FileReferences.Pose)))
                {
                    PoseController = new CubismPose(stream, Model);
                }
            }

            // モデル行列を計算する
            var(canvas_width, canvas_height) = Model.GetCanvasSize();
            ModelMatrix = new CubismModelMatrix(canvas_width, canvas_height);
            ModelMatrix.SetupFromLayout(model_setting.Layout);

            // モーションマネージャーを初期化する
            BaseMotionManager       = new CubismMotionManager();
            ExpressionMotionManager = new CubismMotionManager();
            EffectMotionManager     = new CubismMotionManager();
        }