MagicaMeshClothInspector.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Magica Cloth.
  2. // Copyright (c) MagicaSoft, 2020-2022.
  3. // https://magicasoft.jp
  4. using System.Collections.Generic;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace MagicaCloth
  8. {
  9. /// <summary>
  10. /// メッシュクロスのエディタ拡張
  11. /// </summary>
  12. [CustomEditor(typeof(MagicaMeshCloth))]
  13. public class MagicaMeshClothInspector : ClothEditor
  14. {
  15. protected override void OnEnable()
  16. {
  17. base.OnEnable();
  18. }
  19. public override void OnInspectorGUI()
  20. {
  21. MagicaMeshCloth scr = target as MagicaMeshCloth;
  22. // データ状態
  23. EditorInspectorUtility.DispVersionStatus(scr);
  24. EditorInspectorUtility.DispDataStatus(scr);
  25. serializedObject.Update();
  26. // データ検証
  27. if (EditorApplication.isPlaying == false)
  28. VerifyData();
  29. // モニターボタン
  30. EditorInspectorUtility.MonitorButtonInspector();
  31. // メイン
  32. MainInspector();
  33. // コライダー
  34. ColliderInspector();
  35. // スキニング
  36. SkinningInspector();
  37. // パラメータ
  38. EditorGUILayout.Space();
  39. EditorGUILayout.Space();
  40. EditorPresetUtility.DrawPresetButton(scr, scr.Params);
  41. {
  42. var cparam = serializedObject.FindProperty("clothParams");
  43. if (EditorInspectorUtility.AlgorithmInspector(cparam, scr.HasChangedParam(ClothParams.ParamType.Algorithm), ConvertToLatestAlgorithmParameters))
  44. scr.Params.SetChangeParam(ClothParams.ParamType.Algorithm);
  45. if (EditorInspectorUtility.RadiusInspector(cparam))
  46. scr.Params.SetChangeParam(ClothParams.ParamType.Radius);
  47. if (EditorInspectorUtility.MassInspector(cparam))
  48. scr.Params.SetChangeParam(ClothParams.ParamType.Mass);
  49. if (EditorInspectorUtility.GravityInspector(cparam))
  50. scr.Params.SetChangeParam(ClothParams.ParamType.Gravity);
  51. if (EditorInspectorUtility.ExternalForceInspector(cparam))
  52. scr.Params.SetChangeParam(ClothParams.ParamType.ExternalForce);
  53. if (EditorInspectorUtility.DragInspector(cparam))
  54. scr.Params.SetChangeParam(ClothParams.ParamType.Drag);
  55. if (EditorInspectorUtility.MaxVelocityInspector(cparam))
  56. scr.Params.SetChangeParam(ClothParams.ParamType.MaxVelocity);
  57. if (EditorInspectorUtility.WorldInfluenceInspector(cparam, scr.HasChangedParam(ClothParams.ParamType.WorldInfluence)))
  58. scr.Params.SetChangeParam(ClothParams.ParamType.WorldInfluence);
  59. if (EditorInspectorUtility.DistanceDisableInspector(cparam, scr.HasChangedParam(ClothParams.ParamType.DistanceDisable)))
  60. scr.Params.SetChangeParam(ClothParams.ParamType.DistanceDisable);
  61. if (EditorInspectorUtility.ClampDistanceInspector(cparam, scr.HasChangedParam(ClothParams.ParamType.ClampDistance)))
  62. scr.Params.SetChangeParam(ClothParams.ParamType.ClampDistance);
  63. if (EditorInspectorUtility.ClampPositionInspector(cparam, false, scr.HasChangedParam(ClothParams.ParamType.ClampPosition)))
  64. scr.Params.SetChangeParam(ClothParams.ParamType.ClampPosition);
  65. if (EditorInspectorUtility.ClampRotationInspector(cparam, scr.HasChangedParam(ClothParams.ParamType.ClampRotation), scr.ClothData))
  66. scr.Params.SetChangeParam(ClothParams.ParamType.ClampRotation);
  67. if (EditorInspectorUtility.RestoreDistanceInspector(cparam, scr.HasChangedParam(ClothParams.ParamType.RestoreDistance)))
  68. scr.Params.SetChangeParam(ClothParams.ParamType.RestoreDistance);
  69. if (EditorInspectorUtility.RestoreRotationInspector(cparam, scr.HasChangedParam(ClothParams.ParamType.RestoreRotation), scr.ClothData))
  70. scr.Params.SetChangeParam(ClothParams.ParamType.RestoreRotation);
  71. if (EditorInspectorUtility.TriangleBendInspector(cparam, scr.HasChangedParam(ClothParams.ParamType.TriangleBend), scr.ClothData))
  72. scr.Params.SetChangeParam(ClothParams.ParamType.TriangleBend);
  73. //if (EditorInspectorUtility.VolumeInspector(cparam))
  74. // scr.Params.SetChangeParam(ClothParams.ParamType.Volume);
  75. if (EditorInspectorUtility.CollisionInspector(cparam, scr.HasChangedParam(ClothParams.ParamType.ColliderCollision)))
  76. scr.Params.SetChangeParam(ClothParams.ParamType.ColliderCollision);
  77. if (EditorInspectorUtility.PenetrationInspector(serializedObject, cparam, scr.HasChangedParam(ClothParams.ParamType.Penetration)))
  78. scr.Params.SetChangeParam(ClothParams.ParamType.Penetration);
  79. //if (EditorInspectorUtility.BaseSkinningInspector(serializedObject, cparam))
  80. // scr.Params.SetChangeParam(ClothParams.ParamType.BaseSkinning);
  81. if (EditorInspectorUtility.RotationInterpolationInspector(cparam, scr.HasChangedParam(ClothParams.ParamType.RotationInterpolation)))
  82. scr.Params.SetChangeParam(ClothParams.ParamType.RotationInterpolation);
  83. }
  84. serializedObject.ApplyModifiedProperties();
  85. // データ作成
  86. if (EditorApplication.isPlaying == false)
  87. {
  88. EditorGUI.BeginDisabledGroup(CheckCreate() == false);
  89. EditorGUILayout.Space();
  90. EditorGUILayout.Space();
  91. GUI.backgroundColor = Color.red;
  92. if (GUILayout.Button("Create"))
  93. {
  94. Undo.RecordObject(scr, "CreateMeshCloth");
  95. // 共有選択データが存在しない場合は作成する
  96. if (scr.ClothSelection == null)
  97. InitSelectorData();
  98. BuildManager.CreateComponent(scr);
  99. }
  100. GUI.backgroundColor = Color.white;
  101. EditorGUI.EndDisabledGroup();
  102. }
  103. else
  104. {
  105. EditorGUILayout.Space();
  106. EditorGUILayout.Space();
  107. GUI.backgroundColor = Color.blue;
  108. if (GUILayout.Button("Reset Position"))
  109. {
  110. scr.ResetCloth();
  111. }
  112. GUI.backgroundColor = Color.white;
  113. }
  114. EditorGUILayout.Space();
  115. }
  116. //=========================================================================================
  117. /// <summary>
  118. /// 作成を実行できるか判定する
  119. /// </summary>
  120. /// <returns></returns>
  121. protected override bool CheckCreate()
  122. {
  123. MagicaMeshCloth scr = target as MagicaMeshCloth;
  124. if (PointSelector.EditEnable)
  125. return false;
  126. if (scr.Deformer == null)
  127. return false;
  128. if (scr.Deformer.VerifyData() != Define.Error.None)
  129. return false;
  130. if (scr.IsValidPointSelect() == false)
  131. return false;
  132. return true;
  133. }
  134. /// <summary>
  135. /// データ検証
  136. /// </summary>
  137. private void VerifyData()
  138. {
  139. MagicaMeshCloth scr = target as MagicaMeshCloth;
  140. if (scr.VerifyData() != Define.Error.None)
  141. {
  142. // 検証エラー
  143. serializedObject.ApplyModifiedProperties();
  144. }
  145. }
  146. //=========================================================================================
  147. void MainInspector()
  148. {
  149. MagicaMeshCloth scr = target as MagicaMeshCloth;
  150. EditorGUILayout.LabelField("Main Setup", EditorStyles.boldLabel);
  151. // 仮想メッシュ
  152. EditorGUILayout.PropertyField(serializedObject.FindProperty("virtualDeformer"));
  153. EditorGUILayout.Space();
  154. // チーム項目
  155. TeamBasicInspector();
  156. // ポイント選択
  157. if (scr.Deformer != null)
  158. {
  159. EditorGUI.BeginDisabledGroup(scr.Deformer.VerifyData() != Define.Error.None);
  160. DrawInspectorGUI(scr.Deformer);
  161. EditorGUI.EndDisabledGroup();
  162. }
  163. EditorGUILayout.Space();
  164. // カリング
  165. CullingInspector();
  166. }
  167. //=============================================================================================
  168. /// <summary>
  169. /// 選択データの初期化
  170. /// 配列はすでに頂点数分が確保されゼロクリアされています。
  171. /// </summary>
  172. /// <param name="selectorData"></param>
  173. protected override void OnResetSelector(List<int> selectorData)
  174. {
  175. MagicaMeshCloth scr = target as MagicaMeshCloth;
  176. // すでに選択クラスがある場合は内容をコピーする
  177. if (scr.ClothSelection != null)
  178. {
  179. var sel = scr.ClothSelection.GetSelectionData(scr.Deformer.MeshData, scr.Deformer.GetRenderDeformerMeshList());
  180. for (int i = 0; i < selectorData.Count; i++)
  181. selectorData[i] = sel[i];
  182. }
  183. }
  184. /// <summary>
  185. /// 選択データの決定
  186. /// </summary>
  187. /// <param name="selectorData"></param>
  188. protected override void OnFinishSelector(List<int> selectorData)
  189. {
  190. MagicaMeshCloth scr = target as MagicaMeshCloth;
  191. // 必ず新規データを作成する(ヒエラルキーでのコピー対策)
  192. var sel = CreateSelection(scr, "clothSelection");
  193. // 選択データコピー
  194. sel.SetSelectionData(scr.Deformer.MeshData, selectorData, scr.Deformer.GetRenderDeformerMeshList());
  195. // 現在のデータと比較し差異がない場合は抜ける
  196. if (scr.ClothSelection != null && scr.ClothSelection.Compare(sel))
  197. return;
  198. //if (scr.ClothSelection != null)
  199. // Undo.RecordObject(scr.ClothSelection, "Set Selector");
  200. // 保存
  201. var cdata = serializedObject.FindProperty("clothSelection");
  202. cdata.objectReferenceValue = sel;
  203. serializedObject.ApplyModifiedProperties();
  204. }
  205. }
  206. }