Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Editor/Config/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions Editor/Inspector/PMDInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.";
}
}
Expand Down
26 changes: 13 additions & 13 deletions Editor/MMDLoader/PMDLoaderWindow.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
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;
float scale = 0.085f;
bool is_pmx_base_import = false;

[MenuItem("MMD for Unity/PMD Loader")]
static void Init() {
var window = (PMDLoaderWindow)EditorWindow.GetWindow<PMDLoaderWindow>(true, "PMDLoader");
static void Init() {
var window = (PMDLoaderWindow)EditorWindow.GetWindow<PMDLoaderWindow>(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);
Expand Down Expand Up @@ -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();
Expand Down
30 changes: 26 additions & 4 deletions Editor/MMDLoader/Private/AvatarSettingScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,37 @@ public AvatarSettingScript(GameObject root_game_object, GameObject[] bones) {
}

/// <summary>
/// アバダーを設定する
/// 汎用アバダーを設定する
/// </summary>
/// <returns>アニメーター</returns>
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>();
animator_.avatar = avatar_;

return animator_;
}

/// <summary>
/// 人型アバダーを設定する
/// </summary>
/// <returns>アニメーター</returns>
/// <param name='root_game_object'>ルートゲームオブジェクト</param>
/// <param name='bones'>ボーンのゲームオブジェクト</param>
/// <remarks>
/// モデルに依ってボーン構成に差が有るが、MMD標準モデルとの一致を優先する
/// </remarks>
public Animator SettingAvatar() {
public Animator SettingHumanAvatar() {
//生成済みのボーンをUnity推奨ポーズに設定
SetRequirePose();

Expand Down
7 changes: 4 additions & 3 deletions Editor/MMDLoader/Private/ModelAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public ModelAgent(string file_path) {
/// </summary>
/// <param name='shader_type'>シェーダーの種類</param>
/// <param name='use_rigidbody'>剛体を使用するか</param>
/// <param name='use_mecanim'>Mecanimを使用するか</param>
/// <param name='animation_type'>アニメーションタイプ</param>
/// <param name='use_ik'>IKを使用するか</param>
/// <param name='scale'>スケール</param>
/// <param name='is_pmx_base_import'>PMX Baseでインポートするか</param>
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) {
Expand All @@ -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";
Expand All @@ -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);

// プレファブパスの設定
Expand Down
2 changes: 1 addition & 1 deletion Editor/MMDLoader/Private/PMDConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 26 additions & 12 deletions Editor/MMDLoader/Private/PMXConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,27 @@ namespace MMD
{
public class PMXConverter : System.IDisposable
{
/// <summary>
/// アニメーションタイプ
/// </summary>
public enum AnimationType {
GenericMecanim, //汎用アバターでのMecanim
HumanMecanim, //人型アバターでのMecanim
LegacyAnimation, //旧式アニメーション
}

/// <summary>
/// GameObjectを作成する
/// </summary>
/// <param name='format'>内部形式データ</param>
/// <param name='use_rigidbody'>剛体を使用するか</param>
/// <param name='use_mecanim'>Mecanimを使用するか</param>
/// <param name='animation_type'>アニメーションタイプ</param>
/// <param name='use_ik'>IKを使用するか</param>
/// <param name='scale'>スケール</param>
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;
}
Expand Down Expand Up @@ -47,13 +56,11 @@ public void Dispose()
/// </summary>
/// <param name='format'>内部形式データ</param>
/// <param name='use_rigidbody'>剛体を使用するか</param>
/// <param name='use_mecanim'>Mecanimを使用するか</param>
/// <param name='animation_type'>アニメーションタイプ</param>
/// <param name='use_ik'>IKを使用するか</param>
/// <param name='scale'>スケール</param>
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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions Editor/MMDLoader/Private/VMDConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ private AnimationClip CreateAnimationClip_(MMD.VMD.VMDFormat format, GameObject

CreateKeysForSkin(format, clip); // 表情の追加

SetAnimationType(clip, assign_pmd); //アニメーションタイプの設定

return clip;
}

Expand Down Expand Up @@ -535,6 +537,25 @@ void GetGameObjects(Dictionary<string, GameObject> obj, GameObject assign_pmd)
}
}

/// <summary>
/// アニメーションタイプの設定
/// </summary>
/// <param name="clip">設定するアニメーションクリップ.</param>
/// <param name="engine">設定の為に参照するAnimatorを持つゲームオブジェクト</param>
static void SetAnimationType(AnimationClip clip, GameObject game_object)
{
ModelImporterAnimationType animation_type;
Animator animator = game_object.GetComponent<Animator>();
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;
}
}