Esempio n. 1
0
        public void Awake(ModelType type, string name)
        {
            try
            {
                Type = type;
                Name = name;
                URL  = Live2dPathHelper.GetModelConfigPath(type, name);
                TextAsset config = ResourcesHelper.GetAsset <TextAsset>(URL);
                ModelConfig           = JsonMapper.ToObject <Live2dModelConfig>(config.text);
                ModelConfig.AssetPath = Live2dPathHelper.GetAssetPath(type, name);
                string modelPath  = ModelConfig.GetModelPath();
                var    model3Json = CubismModel3Json.LoadAtPath(modelPath, BuiltinLoadAssetAtPath);
                Model = model3Json.ToModel();
                EntityRef.GameObject       = Model.gameObject;
                EntityRef.Transform        = Model.gameObject.transform;
                EntityRef.GameObject.layer = LayerHelper.LayerNameToLayer("Unit");
                GameObjectHelper.Traversal(EntityRef.Transform, (arg) =>
                {
                    arg.gameObject.layer = LayerHelper.LayerNameToLayer("Unit");
                }, true);

                Animations     = new Dictionary <string, AnimationClip>();
                fadeController = EntityRef.GameObject.AddComponent <CubismFadeController>();
                fadeController.CubismFadeMotionList = ScriptableObject.CreateInstance <CubismFadeMotionList>();
                fadeController.CubismFadeMotionList.MotionInstanceIds       = new int[ModelConfig.Motions.Length];
                fadeController.CubismFadeMotionList.CubismFadeMotionObjects = new CubismFadeMotionData[ModelConfig.Motions.Length];

                for (int i = 0; i < ModelConfig.Motions.Length; ++i)
                {
                    string motion      = ModelConfig.Motions[i];
                    string motionPath  = ModelConfig.GetMotionPath(motion);
                    var    motion3Json = CubismMotion3Json.LoadFrom(ResourcesHelper.GetAsset <TextAsset>(motionPath));
                    var    clip        = ResourcesHelper.GetAsset <AnimationClip>(ModelConfig.GetAnimationClipPath(motion));
                    clip.name = motion;

                    int instanceId = clip.GetInstanceID();
                    var events     = clip.events;
                    for (var j = 0; j < events.Length; ++j)
                    {
                        if (events[j].functionName != "InstanceId")
                        {
                            continue;
                        }

                        instanceId = events[j].intParameter;
                    }

                    fadeController.CubismFadeMotionList.MotionInstanceIds[i]       = instanceId;
                    fadeController.CubismFadeMotionList.CubismFadeMotionObjects[i] = CubismFadeMotionData.CreateInstance(motion3Json, clip.name, clip.length);

                    Animations.Add(motion, clip);
                }

                motionController = EntityRef.GameObject.AddComponent <CubismMotionController>();
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
Esempio n. 2
0
    // Use this for initialization
    void Start()
    {
        jsondata = CubismModel3Json.LoadAtPath(loadingModelJsonPath, normalFileLoad);
        cmodel   = jsondata.ToModel();

        var renderController = cmodel.gameObject.GetComponent <CubismRenderController>();

        renderController.SortingMode = CubismSortingMode.BackToFrontOrder;

        var AutoBlinkController = cmodel.gameObject.AddComponent <CubismAutoEyeBlinkInput>();

        AutoBlinkController.enabled = true;

        var BlinkController = cmodel.gameObject.GetComponent <CubismEyeBlinkController>();

        BlinkController.BlendMode = CubismParameterBlendMode.Override;


        var animeController = cmodel.gameObject.AddComponent <Animation>();

        animeController.playAutomatically = false;

        var nowPlayAudio = cmodel.gameObject.AddComponent <AudioSource>();

        nowPlayAudio.playOnAwake = false;

        var breathController = cmodel.gameObject.AddComponent <CubismBreathController>();
        var breathParam      = cmodel.Parameters.FindById("PARAM_BREATH");

        breathController.enabled = true;
        breathParam.gameObject.AddComponent <CubismBreathParameter>();
    }
Esempio n. 3
0
        private bool IsParameterInGroup(CubismModel3Json modelJson, CubismParameter parameter,
                                        string groupName)
        {
            // Return early if groups aren't available...
            if (modelJson.Groups == null || modelJson.Groups.Length == 0)
            {
                return(false);
            }

            for (var i = 0; i < modelJson.Groups.Length; ++i)
            {
                if (modelJson.Groups[i].Name != groupName)
                {
                    continue;
                }

                if (modelJson.Groups[i].Ids != null)
                {
                    for (var j = 0; j < modelJson.Groups[i].Ids.Length; ++j)
                    {
                        if (modelJson.Groups[i].Ids[j] == parameter.name)
                        {
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Esempio n. 4
0
        /// <summary>
        /// モデルロード
        /// </summary>
        /// <param name="modelPath"></param>
        /// <param name="model3Json"></param>
        /// <param name="targetPosition"></param>
        public void LoadModel(string modelPath, string targetSettingFileName, Vector3 targetPosition)
        {
            //Load model.
            string path = modelPath + ModelPathDefine.MODELS + "/" + targetSettingFileName;

            //モデル3json
            CubismModel3Json model3Json = CubismModel3Json.LoadAtPath(path, AssetLoader.LoadAsset);

            //モデル生成
            this.model = model3Json.ToModel();

            //追加のコンポーネントをアタッチ
            ToModelAttacheEx(model3Json.Groups);

            //モデルゲームオブジェクトを取得する
            this.ModelObject = this.model.gameObject;

            //アニメーションを取得する
            this.Animator = model.gameObject.GetComponent <Animation>();

            //オーディオソース取得
            this.Audio = model.GetComponent <AudioSource>();

            //目パチ制御
            this.EyeBlink = model.gameObject.GetComponent <CubismAutoEyeBlinkInput>();

            //口パク制御
            this.MouthController = model.gameObject.GetComponent <CubismMouthController>();
            this.LipSync         = model.gameObject.GetComponent <CubismAutoMouthInput>();

            //レンダラーコントローラの取得
            this.RenderController = model.gameObject.GetComponent <CubismRenderController>();

            //当たり判定クラス取得
            this.RayCaster = model.gameObject.GetComponent <CubismRaycaster>();

            //当たり判定の設定
            SetHitArea();

            //レンダリング階層に移動
            this.ModelObject.transform.SetParent(CanvasRendering.transform);

            //サイズの設定
            SetScale();

            //位置の設定
            this.SetPosition(targetPosition);

            //モーションのロード
            LoadMotion(this.model, modelPath);

            //方向パラメータの取得
            SetDirectionParam();

            //口パクを初期化
            StopTalking();

            //ドラッグオブジェクトのセット
            SetDragObject();
        }
Esempio n. 5
0
        public void LoadModel(string absolutePath)
        {
            if (!m_LoadedModels.ContainsKey(absolutePath))
            {
                // Load model from path
                var model3Json = CubismModel3Json.LoadAtPath(absolutePath, BuiltinLoadAssetAtPath);
                if (model3Json == null)
                {
                    return;                     // Fail to load model3json
                }
                var model = model3Json.ToModel();
                if (model == null)
                {
                    return;                // Fail to load model
                }
                // init transform
                var mt = model.transform;
                mt.position   = Vector3.zero;
                mt.rotation   = Quaternion.identity;
                mt.localScale = Vector3.one;
                mt.SetParent(ModelParent, false);
                // make it an XLander
                var xLander = model.gameObject.AddComponent <XLander>();
                xLander.Init(absolutePath);
                // add to loaded model dict
                m_LoadedModels.Add(absolutePath, model);
                EvaluateModelDepth();
            }

            CurrentActiveModelPath = absolutePath; // Make the newly loaded model active
        }
Esempio n. 6
0
    void Start()
    {
        //Load model.
        var path       = Application.streamingAssetsPath + "/hiyori_free_t06.model3.json";
        var model3Json = CubismModel3Json.LoadAtPath(path, BuiltinLoadAssetAtPath);

        var model = model3Json.ToModel();
    }
Esempio n. 7
0
    void Start()
    {
        //Load model.
        var path       = Application.streamingAssetsPath + "/18-8-18_Live2d_MeidoChan.model3.json";
        var model3Json = CubismModel3Json.LoadAtPath(path, BuiltinLoadAssetAtPath);

        model = model3Json.ToModel();


        model.transform.localScale = new Vector3(1.3F, 1.3F, 1);
        model.transform.position   = new Vector3(-0.2F, -0.5F, 0);
    }
Esempio n. 8
0
        public CubismModel GetModelFromName(string name, GameObject parent)
        {
            try
            {
                string ModelPath = Application.streamingAssetsPath + "../../../Data/live2d/" + name + ".model3.json";
                //Debug.Log(ModelPath);
                if (System.IO.File.Exists(ModelPath))
                {
                    var ModelFullName = ModelPath;
                    var model3Json    = CubismModel3Json.LoadAtPath(ModelFullName, BuiltinLoadAssetAtPath);
                    var motions       = model3Json.FileReferences.Motions.Motions;
                    var model         = model3Json.ToModel(true);
                    var Scale         = 40f;
                    model.gameObject.transform.localScale += new Vector3(Scale, Scale);
                    CubismRenderController cubisumRenderController = model.GetComponent <CubismRenderController>();
                    cubisumRenderController.SortingMode = CubismSortingMode.BackToFrontOrder;

                    //处理动画(Motion)
                    Animation animation      = model.gameObject.AddComponent <Animation>();
                    var       animationClips = new ArrayList();
                    var       modelD         = Path.GetDirectoryName(ModelPath);
                    if (motions != null)
                    {
                        for (var i = 0; i < motions.Length; ++i)
                        {
                            for (var j = 0; j < motions[i].Length; ++j)
                            {
                                string            MotionPath        = modelD + "/" + motions[i][j].File;
                                CubismMotion3Json cubismMotion3Json = CubismMotion3Json.LoadFrom(File.ReadAllText(MotionPath));
                                AnimationClip     animationClip     = cubismMotion3Json.ToAnimationClip(new AnimationClip
                                {
                                    legacy = true
                                }, false, false, false, null);
                                var motionName = motions[i][j].File.Substring(0, motions[i][j].File.Length - 13);
                                animationClip.name = motionName;
                                animation.AddClip(animationClip, animationClip.name);
                                animationClips.Add(animationClip.name);
                                //animation.Blend(animationClip.name);
                            }
                        }
                    }

                    //处理姿势
                    var pose3Json = model3Json.Pose3Json;
                    if (pose3Json != null)
                    {
                        var groups = pose3Json.Groups;
                        var parts  = model.Parts;
                        for (var groupIndex = 0; groupIndex < groups.Length; ++groupIndex)
                        {
                            var group = groups[groupIndex];
                            if (group == null)
                            {
                                continue;
                            }

                            for (var partIndex = 0; partIndex < group.Length; ++partIndex)
                            {
                                var part = parts.FindById(group[partIndex].Id);

                                if (part == null)
                                {
                                    continue;
                                }

                                var posePart = part.gameObject.GetComponent <CubismPosePart>();

                                if (posePart == null)
                                {
                                    posePart = part.gameObject.AddComponent <CubismPosePart>();
                                }

                                posePart.GroupIndex = groupIndex;
                                posePart.PartIndex  = partIndex;
                                posePart.Link       = group[partIndex].Link;
                            }
                        }
                        model.GetComponent <CubismPoseController>().Refresh();
                    }

                    //+1
                    model.name = model.name + "(" + Globle.ModelNum.ToString() + ")";
                    Globle.ModelNum++;
                    Globle.ModelList.Add(model.gameObject);

                    var modelController = model.gameObject.AddComponent <Live2DModelController>();
                    modelController.DisplayName    = model.name;
                    modelController.ModelPath      = modelD;
                    modelController.Animation      = animation;
                    modelController.animationClips = animationClips;

                    model.transform.SetParent(parent.transform);
                    model.gameObject.transform.localPosition = new Vector3(0, 0, 0);
                    return(model);
                }
                else
                {
                    throw new Exception(Globle.LangController.GetLang("LOG.FileUnisset"));
                }
            }
            catch (Exception err)
            {
                Globle.AddDataLog("Main", Globle.LangController.GetLang("LOG.ModelAddationException", err.Message));
            }
            return(null);
        }
Esempio n. 9
0
        /// <summary>
        /// Shows dialog for opening files.
        /// </summary>
        public void ShowFileDialog()
        {
            // HACK Deselect active UI element.
            EventSystem.current.SetSelectedGameObject(null);


            // Pre-filter for motions in case a model is loaded.
            if (Model != null)
            {
                FileDialog.FilterIndex = 2;
            }


            // Return unless a file was selected.
            if (FileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }


            var absoluteFilePath = FileDialog.FileName;


            // Store path.
            Config.LastFileDialogPath = absoluteFilePath;


            // Handle model JSON files.
            if (absoluteFilePath.EndsWith(".model3.json"))
            {
                var lastModel = Model;


                // Load model.
                ModelJson = CubismModel3Json.LoadAtPath(absoluteFilePath, CubismViewerIo.LoadAsset);
                Model     = ModelJson.ToModel();


                // Trigger event.
                if (OnNewModel != null)
                {
                    OnNewModel(this, Model);
                }


                // Remove previous model.
                if (lastModel != null)
                {
                    Destroy(lastModel.gameObject);
                }


                // Guess best zoom and position.
                // TODO Improve if necessary.
                var modelBounds = Model
                                  .GetComponent <CubismRenderController>()
                                  .Renderers
                                  .GetMeshRendererBounds();


                Camera.transform.position = new Vector3(0f, 0f, -10f);
                Camera.orthographicSize   = modelBounds.extents.y * 1.1f;


                return;
            }


            // Trigger event for other files.
            if (OnFileDrop != null)
            {
                OnFileDrop(this, absoluteFilePath);
            }
        }