diff --git a/Editor/Config/Config.cs b/Editor/Config/Config.cs index 9c0adfe..4ab1ab2 100644 --- a/Editor/Config/Config.cs +++ b/Editor/Config/Config.cs @@ -113,7 +113,7 @@ public override void OnGUI() public class DefaultPMDImportConfig : ConfigBase { public PMDConverter.ShaderType shader_type = PMDConverter.ShaderType.MMDShader; - public bool use_mecanim = false; + public PMXConverter.AnimationType animation_type = PMXConverter.AnimationType.LegacyAnimation; public bool rigidFlag = true; public bool use_ik = true; public float scale = 0.085f; @@ -130,7 +130,7 @@ public override void OnGUI() { shader_type = (PMDConverter.ShaderType)EditorGUILayout.EnumPopup("Shader Type", shader_type); rigidFlag = EditorGUILayout.Toggle("Rigidbody", rigidFlag); - use_mecanim = EditorGUILayout.Toggle("Use Mecanim", use_mecanim); // ここfalseになってたけど理由があるのかわからない + animation_type = (PMXConverter.AnimationType)EditorGUILayout.EnumPopup("Animation Type", animation_type); use_ik = EditorGUILayout.Toggle("Use IK", use_ik); is_pmx_base_import = EditorGUILayout.Toggle("Use PMX Base Import", is_pmx_base_import); } diff --git a/Editor/Inspector/PMDInspector.cs b/Editor/Inspector/PMDInspector.cs index 2cd409a..6c6e375 100644 --- a/Editor/Inspector/PMDInspector.cs +++ b/Editor/Inspector/PMDInspector.cs @@ -12,7 +12,7 @@ public class PMDInspector : Editor // PMD Load option public PMDConverter.ShaderType shader_type; public bool rigidFlag; - public bool use_mecanim; + public PMXConverter.AnimationType animation_type; public bool use_ik; public float scale; public bool is_pmx_base_import; @@ -30,7 +30,7 @@ private void setup() var config = MMD.Config.LoadAndCreate(); shader_type = config.pmd_config.shader_type; rigidFlag = config.pmd_config.rigidFlag; - use_mecanim = config.pmd_config.use_mecanim; + animation_type = config.pmd_config.animation_type; use_ik = config.pmd_config.use_ik; scale = config.pmd_config.scale; is_pmx_base_import = config.pmd_config.is_pmx_base_import; @@ -64,7 +64,7 @@ public override void OnInspectorGUI() rigidFlag = EditorGUILayout.Toggle("Rigidbody", rigidFlag); // Mecanimを使うかどうか - use_mecanim = EditorGUILayout.Toggle("Use Mecanim", use_mecanim); + animation_type = (PMXConverter.AnimationType)EditorGUILayout.EnumPopup("Animation Type", animation_type); // IKを使うかどうか use_ik = EditorGUILayout.Toggle("Use IK", use_ik); @@ -100,7 +100,7 @@ public override void OnInspectorGUI() var obj = (PMDScriptableObject)target; model_agent = new ModelAgent(obj.assetPath); } - model_agent.CreatePrefab(shader_type, rigidFlag, use_mecanim, use_ik, scale, is_pmx_base_import); + model_agent.CreatePrefab(shader_type, rigidFlag, animation_type, use_ik, scale, is_pmx_base_import); message = "Loading done."; } } diff --git a/Editor/MMDLoader/PMDLoaderWindow.cs b/Editor/MMDLoader/PMDLoaderWindow.cs index 35862a9..61ade98 100644 --- a/Editor/MMDLoader/PMDLoaderWindow.cs +++ b/Editor/MMDLoader/PMDLoaderWindow.cs @@ -1,11 +1,11 @@ -using UnityEngine; +using UnityEngine; using System.Collections; using UnityEditor; public class PMDLoaderWindow : EditorWindow { Object pmdFile = null; bool rigidFlag = true; - bool use_mecanim = false; + MMD.PMXConverter.AnimationType animation_type = MMD.PMXConverter.AnimationType.LegacyAnimation; MMD.PMDConverter.ShaderType shader_type = MMD.PMDConverter.ShaderType.MMDShader; bool use_ik = true; @@ -13,33 +13,33 @@ public class PMDLoaderWindow : EditorWindow { bool is_pmx_base_import = false; [MenuItem("MMD for Unity/PMD Loader")] - static void Init() { - var window = (PMDLoaderWindow)EditorWindow.GetWindow(true, "PMDLoader"); + static void Init() { + var window = (PMDLoaderWindow)EditorWindow.GetWindow(true, "PMDLoader"); window.Show(); } - public PMDLoaderWindow() - { - // デフォルトコンフィグ + public PMDLoaderWindow() + { + // デフォルトコンフィグ var config = MMD.Config.LoadAndCreate(); shader_type = config.pmd_config.shader_type; rigidFlag = config.pmd_config.rigidFlag; - use_mecanim = config.pmd_config.use_mecanim; + animation_type = config.pmd_config.animation_type; use_ik = config.pmd_config.use_ik; is_pmx_base_import = config.pmd_config.is_pmx_base_import; - } + } void OnGUI() { pmdFile = EditorGUILayout.ObjectField("PMD File" , pmdFile, typeof(Object), false); // シェーダの種類 - shader_type = (PMDConverter.ShaderType)EditorGUILayout.EnumPopup("Shader Type", shader_type); + shader_type = (MMD.PMDConverter.ShaderType)EditorGUILayout.EnumPopup("Shader Type", shader_type); // 剛体を入れるかどうか rigidFlag = EditorGUILayout.Toggle("Rigidbody", rigidFlag); - // Mecanimを使うかどうか - use_mecanim = EditorGUILayout.Toggle("Use Mecanim", use_mecanim); + // アニメーションタイプ + animation_type = (MMD.PMXConverter.AnimationType)EditorGUILayout.EnumPopup("Animation Type", animation_type); // IKを使うかどうか use_ik = EditorGUILayout.Toggle("Use IK", use_ik); @@ -74,7 +74,7 @@ void OnGUI() { void LoadModel() { string file_path = AssetDatabase.GetAssetPath(pmdFile); MMD.ModelAgent model_agent = new MMD.ModelAgent(file_path); - model_agent.CreatePrefab(shader_type, rigidFlag, use_mecanim, use_ik, scale, is_pmx_base_import); + model_agent.CreatePrefab(shader_type, rigidFlag, animation_type, use_ik, scale, is_pmx_base_import); // 読み込み完了メッセージ var window = LoadedWindow.Init(); diff --git a/Editor/MMDLoader/Private/AvatarSettingScript.cs b/Editor/MMDLoader/Private/AvatarSettingScript.cs index 41b6729..3245dc1 100644 --- a/Editor/MMDLoader/Private/AvatarSettingScript.cs +++ b/Editor/MMDLoader/Private/AvatarSettingScript.cs @@ -18,15 +18,37 @@ public AvatarSettingScript(GameObject root_game_object, GameObject[] bones) { } /// - /// アバダーを設定する + /// 汎用アバダーを設定する + /// + /// アニメーター + public Animator SettingGenericAvatar() { + //アバタールートトランスフォームの取得 + Transform avatar_root_transform = root_game_object_.transform.FindChild("Model"); + if (null == avatar_root_transform) { + //ルートゲームオブジェクト直下にモデルルートオブジェクトが無い(PMDConverter)なら + //ルートゲームオブジェクトをアバタールートオブジェクトとする + avatar_root_transform = root_game_object_.transform; + } + + //ジェネリックアバター作成 + string root_name = ((HasBone("全ての親"))? "全ての親": ""); + avatar_ = AvatarBuilder.BuildGenericAvatar(avatar_root_transform.gameObject, root_name); + + //アバターをアニメーターに設定 + animator_ = root_game_object_.AddComponent(); + animator_.avatar = avatar_; + + return animator_; + } + + /// + /// 人型アバダーを設定する /// /// アニメーター - /// ルートゲームオブジェクト - /// ボーンのゲームオブジェクト /// /// モデルに依ってボーン構成に差が有るが、MMD標準モデルとの一致を優先する /// - public Animator SettingAvatar() { + public Animator SettingHumanAvatar() { //生成済みのボーンをUnity推奨ポーズに設定 SetRequirePose(); diff --git a/Editor/MMDLoader/Private/ModelAgent.cs b/Editor/MMDLoader/Private/ModelAgent.cs index 2e1be8b..cd721b4 100644 --- a/Editor/MMDLoader/Private/ModelAgent.cs +++ b/Editor/MMDLoader/Private/ModelAgent.cs @@ -32,11 +32,11 @@ public ModelAgent(string file_path) { /// /// シェーダーの種類 /// 剛体を使用するか - /// Mecanimを使用するか + /// アニメーションタイプ /// IKを使用するか /// スケール /// PMX Baseでインポートするか - public void CreatePrefab(PMDConverter.ShaderType shader_type, bool use_rigidbody, bool use_mecanim, bool use_ik, float scale, bool is_pmx_base_import) { + public void CreatePrefab(PMDConverter.ShaderType shader_type, bool use_rigidbody, PMXConverter.AnimationType animation_type, bool use_ik, float scale, bool is_pmx_base_import) { GameObject game_object; string prefab_path; if (is_pmx_base_import) { @@ -54,7 +54,7 @@ public void CreatePrefab(PMDConverter.ShaderType shader_type, bool use_rigidbody } header_ = pmx_format.header; //ゲームオブジェクトの作成 - game_object = PMXConverter.CreateGameObject(pmx_format, use_rigidbody, use_mecanim, use_ik, scale); + game_object = PMXConverter.CreateGameObject(pmx_format, use_rigidbody, animation_type, use_ik, scale); // プレファブパスの設定 prefab_path = pmx_format.meta_header.folder + "/" + pmx_format.meta_header.name + ".prefab"; @@ -74,6 +74,7 @@ public void CreatePrefab(PMDConverter.ShaderType shader_type, bool use_rigidbody header_ = PMXLoaderScript.PMD2PMX(pmd_format.head); //ゲームオブジェクトの作成 + bool use_mecanim = PMXConverter.AnimationType.LegacyAnimation == animation_type; game_object = PMDConverter.CreateGameObject(pmd_format, shader_type, use_rigidbody, use_mecanim, use_ik, scale); // プレファブパスの設定 diff --git a/Editor/MMDLoader/Private/PMDConverter.cs b/Editor/MMDLoader/Private/PMDConverter.cs index 2e27b90..58e46f5 100644 --- a/Editor/MMDLoader/Private/PMDConverter.cs +++ b/Editor/MMDLoader/Private/PMDConverter.cs @@ -89,7 +89,7 @@ private GameObject CreateGameObject_(PMDFormat format, ShaderType shader_type, b // Mecanim設定 if (use_mecanim_) { AvatarSettingScript avatar_setting = new AvatarSettingScript(root_game_object_, bones); - avatar_setting.SettingAvatar(); + avatar_setting.SettingHumanAvatar(); string path = format_.folder + "/"; string name = GetFilePathString(format_.name); diff --git a/Editor/MMDLoader/Private/PMXConverter.cs b/Editor/MMDLoader/Private/PMXConverter.cs index 77461e2..00c627f 100644 --- a/Editor/MMDLoader/Private/PMXConverter.cs +++ b/Editor/MMDLoader/Private/PMXConverter.cs @@ -8,18 +8,27 @@ namespace MMD { public class PMXConverter : System.IDisposable { + /// + /// アニメーションタイプ + /// + public enum AnimationType { + GenericMecanim, //汎用アバターでのMecanim + HumanMecanim, //人型アバターでのMecanim + LegacyAnimation, //旧式アニメーション + } + /// /// GameObjectを作成する /// /// 内部形式データ /// 剛体を使用するか - /// Mecanimを使用するか + /// アニメーションタイプ /// IKを使用するか /// スケール - public static GameObject CreateGameObject(PMXFormat format, bool use_rigidbody, bool use_mecanim, bool use_ik, float scale) { + public static GameObject CreateGameObject(PMXFormat format, bool use_rigidbody, AnimationType animation_type, bool use_ik, float scale) { GameObject result; using (PMXConverter converter = new PMXConverter()) { - result = converter.CreateGameObject_(format, use_rigidbody, use_mecanim, use_ik, scale); + result = converter.CreateGameObject_(format, use_rigidbody, animation_type, use_ik, scale); } return result; } @@ -47,13 +56,11 @@ public void Dispose() /// /// 内部形式データ /// 剛体を使用するか - /// Mecanimを使用するか + /// アニメーションタイプ /// IKを使用するか /// スケール - private GameObject CreateGameObject_(PMXFormat format, bool use_rigidbody, bool use_mecanim, bool use_ik, float scale) { + private GameObject CreateGameObject_(PMXFormat format, bool use_rigidbody, AnimationType animation_type, bool use_ik, float scale) { format_ = format; - use_rigidbody_ = use_rigidbody; - use_mecanim_ = use_mecanim; use_ik_ = use_ik; scale_ = scale; root_game_object_ = new GameObject(format_.meta_header.name); @@ -83,7 +90,7 @@ private GameObject CreateGameObject_(PMXFormat format, bool use_rigidbody, bool } // 剛体関連 - if (use_rigidbody_) { + if (use_rigidbody) { GameObject[] rigids = CreateRigids(); AssignRigidbodyToBone(bones, rigids); SetRigidsSettings(bones, rigids); @@ -98,10 +105,19 @@ private GameObject CreateGameObject_(PMXFormat format, bool use_rigidbody, bool } // Mecanim設定 - if (use_mecanim_) { + if (AnimationType.LegacyAnimation != animation_type) { //アニメーター追加 AvatarSettingScript avatar_setting = new AvatarSettingScript(root_game_object_, bones); - avatar_setting.SettingAvatar(); + switch (animation_type) { + case AnimationType.GenericMecanim: //汎用アバターでのMecanim + avatar_setting.SettingGenericAvatar(); + break; + case AnimationType.HumanMecanim: //人型アバターでのMecanim + avatar_setting.SettingHumanAvatar(); + break; + default: + throw new System.ArgumentException(); + } string path = format_.meta_header.folder + "/"; string name = GetFilePathString(format_.meta_header.name); @@ -2020,8 +2036,6 @@ private static string GetFilePathString(string src) { GameObject root_game_object_; PMXFormat format_; - bool use_rigidbody_; - bool use_mecanim_; bool use_ik_; float scale_; AlphaReadableTexture alpha_readable_texture_ = null; diff --git a/Editor/MMDLoader/Private/VMDConverter.cs b/Editor/MMDLoader/Private/VMDConverter.cs index 53141cc..fbe78bc 100644 --- a/Editor/MMDLoader/Private/VMDConverter.cs +++ b/Editor/MMDLoader/Private/VMDConverter.cs @@ -53,6 +53,8 @@ private AnimationClip CreateAnimationClip_(MMD.VMD.VMDFormat format, GameObject CreateKeysForSkin(format, clip); // 表情の追加 + SetAnimationType(clip, assign_pmd); //アニメーションタイプの設定 + return clip; } @@ -535,6 +537,25 @@ void GetGameObjects(Dictionary obj, GameObject assign_pmd) } } + /// + /// アニメーションタイプの設定 + /// + /// 設定するアニメーションクリップ. + /// 設定の為に参照するAnimatorを持つゲームオブジェクト + static void SetAnimationType(AnimationClip clip, GameObject game_object) + { + ModelImporterAnimationType animation_type; + Animator animator = game_object.GetComponent(); + if (null == animator) { + animation_type = ModelImporterAnimationType.Legacy; + } else if ((null == animator.avatar) && animator.avatar.isHuman) { + animation_type = ModelImporterAnimationType.Human; + } else { + animation_type = ModelImporterAnimationType.Generic; + } + AnimationUtility.SetAnimationType(clip, animation_type); + } + private float scale_ = 1.0f; } }