Пример #1
0
        /// <summary>
        /// JSON 파일로 간편하게 모델을 불러옵니다.
        /// </summary>
        /// <param name="path">모델 구성을 포함한 JSON 파일입니다.</param>
        public static L2DModel LoadModel(string path)
        {
            // JSON 분석
            string modelPath;

            string[] texturePath;
            string   physicsPath = null;
            string   posePath    = null;
            string   parentPath  = Directory.GetParent(path).FullName;
            Dictionary <string, L2DMotion[]>   motionDictionary     = new Dictionary <string, L2DMotion[]>();
            Dictionary <string, L2DExpression> expressionDictionary = new Dictionary <string, L2DExpression>();
            JObject jsonObject = JObject.Parse(File.ReadAllText(path));

            // - model
            modelPath = FixPath(path, jsonObject.GetValue("model").Value <string>());

            // - textures
            texturePath = jsonObject.GetValue("textures").Values <string>().ToArray();
            for (int i = 0; i < texturePath.Length; i++)
            {
                texturePath[i] = FixPath(path, texturePath[i]);
            }

            // - physics
            JToken resultPhysics;

            jsonObject.TryGetValue("physics", out resultPhysics);
            if (resultPhysics != null)
            {
                physicsPath = FixPath(path, resultPhysics.Value <string>());
            }

            // - pose
            JToken resultPose;

            jsonObject.TryGetValue("pose", out resultPose);
            if (resultPose != null)
            {
                posePath = FixPath(path, resultPose.Value <string>());
            }

            // - motions
            foreach (JProperty jsonMotion in jsonObject["motions"].Children())
            {
                List <L2DMotion> motionList = new List <L2DMotion>();
                foreach (JArray jsonChildren in jsonMotion.Children().ToList())
                {
                    foreach (JObject result in jsonChildren)
                    {
                        L2DMotion motion     = null;
                        string    motionFile = FixPath(path, result.GetValue("file").Value <string>());
                        JToken    resultSound;
                        result.TryGetValue("sound", out resultSound);

                        if (resultSound == null)
                        {
                            motion = new L2DMotion(motionFile);
                        }
                        else
                        {
                            string soundFile = FixPath(path, resultSound.Value <string>());
                            motion = new L2DMotion(motionFile, soundFile);
                        }

                        JToken resultFadeIn;
                        result.TryGetValue("fade_in", out resultFadeIn);

                        JToken resultFadeOut;
                        result.TryGetValue("fade_out", out resultFadeOut);

                        if (resultFadeIn != null)
                        {
                            motion.SetFadeIn(resultFadeIn.Value <int>());
                        }

                        if (resultFadeOut != null)
                        {
                            motion.SetFadeOut(resultFadeOut.Value <int>());
                        }

                        if (motion != null)
                        {
                            motionList.Add(motion);
                        }
                    }
                }

                motionDictionary.Add(jsonMotion.Name, motionList.ToArray());
            }

            // - expression
            JToken resultExpression;

            jsonObject.TryGetValue("expressions", out resultExpression);
            if (resultExpression != null)
            {
                foreach (JObject json in resultExpression)
                {
                    string        name       = json["name"].Value <string>();
                    L2DExpression expression = LoadExpression(FixPath(path, json["file"].Value <string>()));
                    expressionDictionary.Add(name, expression);
                }
            }

            // L2DModel 생성
            L2DModel model = new L2DModel(modelPath);

            model.SetTexture(texturePath);
            model.Motion = motionDictionary;
            if (resultExpression != null)
            {
                model.Expression = expressionDictionary;
            }
            if (physicsPath != null)
            {
                model.Physics = LoadPhysics(physicsPath);
            }
            if (posePath != null)
            {
                model.Pose = LoadPose(posePath);
            }
            model.SaveParam();

            return(model);
        }