Skip to content

Commit 2fb1230

Browse files
committed
Merge pull request #22 from mmd-for-unity-proj/horizontal-arm
Horizontal arm
2 parents ccf3323 + 6360298 commit 2fb1230

File tree

3 files changed

+56
-16
lines changed

3 files changed

+56
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
*.csproj
77
*.unityproj
88
*.sln
9+
*.asset

Editor/Config/Config.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public static Config LoadAndCreate()
7777
AssetDatabase.CreateAsset(config, path);
7878
EditorUtility.SetDirty(config);
7979
}
80-
Debug.Log(config);
8180
return config;
8281
}
8382
}
@@ -131,7 +130,7 @@ public override void OnGUI()
131130
{
132131
shader_type = (PMDConverter.ShaderType)EditorGUILayout.EnumPopup("Shader Type", shader_type);
133132
rigidFlag = EditorGUILayout.Toggle("Rigidbody", rigidFlag);
134-
use_mecanim = false;
133+
use_mecanim = EditorGUILayout.Toggle("Use Mecanim", use_mecanim); // ここfalseになってたけど理由があるのかわからない
135134
use_ik = EditorGUILayout.Toggle("Use IK", use_ik);
136135
is_pmx_base_import = EditorGUILayout.Toggle("Use PMX Base Import", is_pmx_base_import);
137136
}

Editor/MMDLoader/Private/AvatarSettingScript.cs

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,28 +85,68 @@ void SetRequirePose()
8585
}
8686
}
8787

88+
/// <summary>
89+
/// 対象のボーンの中からより深く枝分かれする子ボーンを選び出す
90+
/// 剛体用ボーンじゃないきちんとしたボーンを選び出すために使う
91+
/// </summary>
92+
/// <param name="transform">対象のボーン</param>
93+
/// <returns>さらに枝分かれする子ボーン</returns>
94+
Transform SelectBranchedChildWhereManyChildren(Transform transform)
95+
{
96+
Transform[] children = new Transform[transform.childCount];
97+
if (children.Length <= 0) Debug.LogError(transform.name + "の子がないので落ちるのです!");
98+
for (int i = 0; i < transform.childCount; i++)
99+
children[i] = transform.GetChild(i);
100+
int max = children.Max(x => x.childCount);
101+
return children.Where(x => x.childCount == max).First();
102+
}
103+
104+
/// <summary>
105+
/// 親子関係を見てボーンを水平にする
106+
/// </summary>
107+
/// <param name="transform">対象のボーン</param>
108+
/// <returns>Z軸のみを回転させるQuaternion</returns>
109+
Quaternion ResetHorizontalPose(Transform transform, Transform child_transform)
110+
{
111+
// ボーンの向きを取得
112+
var bone_vector = child_transform.position - transform.position;
113+
bone_vector.z = 0f; // 平面化
114+
bone_vector.Normalize();
115+
116+
// 平面化した正規化ベクトルと単位ベクトルを比較して,角度を取得する
117+
Vector3 normalized_vector = bone_vector.x >= 0 ? Vector3.right : Vector3.left;
118+
float cos_value = Vector3.Dot(bone_vector, normalized_vector);
119+
float theta = Mathf.Acos(cos_value) * Mathf.Rad2Deg;
120+
121+
theta = bone_vector.x >= 0 ? -theta : theta; // ボーンの向きによって回転方向が違う
122+
123+
return Quaternion.Euler(0f, 0f, theta);
124+
}
125+
126+
/// <summary>
127+
/// 腕全体を平行にする処理
128+
/// </summary>
129+
/// <param name="shoulder">肩ボーン</param>
130+
void StartResettingHorizontal(Transform shoulder)
131+
{
132+
var arm = SelectBranchedChildWhereManyChildren(shoulder);
133+
var hinge = SelectBranchedChildWhereManyChildren(arm);
134+
var wrist = SelectBranchedChildWhereManyChildren(hinge);
135+
shoulder.transform.localRotation = ResetHorizontalPose(shoulder, arm);
136+
arm.transform.localRotation = ResetHorizontalPose(arm, hinge);
137+
hinge.transform.localRotation = ResetHorizontalPose(hinge, wrist);
138+
}
139+
88140
/// <summary>
89141
/// 生成済みのボーンをUnity推奨ポーズに設定
90142
/// </summary>
91143
/// <param name='transform'>ボーンのトランスフォーム</param>
92144
void SetRequirePose(Transform transform)
93145
{
94146
switch (transform.name) {
95-
case "左肩": //Tポーズにする為に腕を持ち上げる
96-
transform.localRotation = Quaternion.Euler(0.0f, 0.0f, 9.0f);
97-
break;
147+
case "左肩": goto case "右肩";
98148
case "右肩": //Tポーズにする為に腕を持ち上げる
99-
transform.localRotation = Quaternion.Euler(0.0f, 0.0f, -9.0f);
100-
break;
101-
case "左ひじ": //稀に肘が曲がっていることがあるので矯正
102-
break;
103-
case "右ひじ": //上に同様に肘を矯正
104-
break;
105-
case "左腕": //Tポーズにする為に腕を持ち上げる
106-
transform.localRotation = Quaternion.Euler(0.0f, 0.0f, 27.0f);
107-
break;
108-
case "右腕": //Tポーズにする為に腕を持ち上げる
109-
transform.localRotation = Quaternion.Euler(0.0f, 0.0f, -27.0f);
149+
StartResettingHorizontal(transform);
110150
break;
111151
case "腰": goto case "センター";
112152
case "センター":

0 commit comments

Comments
 (0)