示例#1
0
        /// <summary>
        /// JSON 파일로 간편하게 표정을 불러옵니다.
        /// </summary>
        /// <param name="path">표정 구성을 포함한 표준 JSON 파일입니다.</param>
        public static L2DExpression LoadExpression(string path)
        {
            L2DExpression expression = new L2DExpression();

            JObject jsonObject = JObject.Parse(File.ReadAllText(path));

            expression.SetFadeIn(jsonObject["fade_in"].Value <int>());
            expression.SetFadeIn(jsonObject["fade_out"].Value <int>());

            JToken resultParams;

            jsonObject.TryGetValue("params", out resultParams);

            if (resultParams != null)
            {
                foreach (JObject json in resultParams)
                {
                    string paramID      = json["id"].Value <string>();
                    float  value        = json["val"].Value <float>();
                    string calc         = "add";
                    float  defaultValue = 0;

                    JToken resultCalc;
                    json.TryGetValue("calc", out resultCalc);
                    if (resultCalc != null)
                    {
                        calc = resultCalc.Value <string>();
                    }

                    JToken resultDef;
                    json.TryGetValue("def", out resultDef);
                    if (resultDef != null)
                    {
                        defaultValue = resultDef.Value <float>();
                    }

                    expression.AddParam(paramID, calc, value, defaultValue);
                }
            }

            return(expression);
        }
示例#2
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);
        }