BoneClothTarget.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Magica Cloth.
  2. // Copyright (c) MagicaSoft, 2020-2022.
  3. // https://magicasoft.jp
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace MagicaCloth
  7. {
  8. /// <summary>
  9. /// ボーンクロスのターゲットトランスフォーム
  10. /// </summary>
  11. [System.Serializable]
  12. public class BoneClothTarget : IDataHash, IBoneReplace
  13. {
  14. /// <summary>
  15. /// ルートトランスフォーム
  16. /// </summary>
  17. [SerializeField]
  18. private List<Transform> rootList = new List<Transform>();
  19. /// <summary>
  20. /// 接続モード
  21. /// </summary>
  22. public enum ConnectionMode
  23. {
  24. Line = 0,
  25. MeshAutomatic = 1,
  26. MeshSequentialLoop = 2,
  27. MeshSequentialNoLoop = 3,
  28. }
  29. [SerializeField]
  30. private ConnectionMode connection = ConnectionMode.Line;
  31. /// <summary>
  32. /// メッシュ構築時に同一とみなす面角度(面法線方向に影響)
  33. /// </summary>
  34. [SerializeField]
  35. [Range(10.0f, 90.0f)]
  36. private float sameSurfaceAngle = 80.0f;
  37. //=========================================================================================
  38. /// <summary>
  39. /// ルートの親トランスフォームの登録インデックス
  40. /// </summary>
  41. private int[] parentIndexList = null;
  42. //=========================================================================================
  43. /// <summary>
  44. /// データを識別するハッシュコードを作成して返す
  45. /// </summary>
  46. /// <returns></returns>
  47. public int GetDataHash()
  48. {
  49. int hash = 0;
  50. hash += rootList.GetDataHash();
  51. return hash;
  52. }
  53. //=========================================================================================
  54. /// <summary>
  55. /// ルートトランスフォームの数
  56. /// </summary>
  57. public int RootCount
  58. {
  59. get
  60. {
  61. return rootList.Count;
  62. }
  63. }
  64. /// <summary>
  65. /// ルートトランスフォーム取得
  66. /// </summary>
  67. /// <param name="index"></param>
  68. /// <returns></returns>
  69. public Transform GetRoot(int index)
  70. {
  71. if (index < rootList.Count)
  72. return rootList[index];
  73. return null;
  74. }
  75. /// <summary>
  76. /// ルートトランスフォームのインデックスを返す。無い場合は(-1)
  77. /// </summary>
  78. /// <param name="root"></param>
  79. /// <returns></returns>
  80. public int GetRootIndex(Transform root)
  81. {
  82. return rootList.IndexOf(root);
  83. }
  84. /// <summary>
  85. /// ルートの親トランスフォームをすべて登録する
  86. /// </summary>
  87. public void AddParentTransform()
  88. {
  89. if (rootList.Count > 0)
  90. {
  91. HashSet<Transform> parentSet = new HashSet<Transform>();
  92. foreach (var t in rootList)
  93. {
  94. if (t && t.parent)
  95. parentSet.Add(t.parent);
  96. }
  97. parentIndexList = new int[parentSet.Count];
  98. int i = 0;
  99. foreach (var parent in parentSet)
  100. {
  101. int index = -1;
  102. if (parent)
  103. {
  104. index = MagicaPhysicsManager.Instance.Bone.AddBone(parent);
  105. }
  106. parentIndexList[i] = index;
  107. i++;
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// ルートの親トランスフォームをすべて解除する
  113. /// </summary>
  114. public void RemoveParentTransform()
  115. {
  116. if (MagicaPhysicsManager.IsInstance())
  117. {
  118. if (parentIndexList != null && parentIndexList.Length > 0)
  119. {
  120. for (int i = 0; i < parentIndexList.Length; i++)
  121. {
  122. var index = parentIndexList[i];
  123. if (index >= 0)
  124. {
  125. MagicaPhysicsManager.Instance.Bone.RemoveBone(index);
  126. }
  127. }
  128. }
  129. }
  130. parentIndexList = null;
  131. }
  132. /// <summary>
  133. /// ルートの親トランスフォームの未来予測をリセットする
  134. /// </summary>
  135. public void ResetFuturePredictionParentTransform()
  136. {
  137. if (parentIndexList != null && parentIndexList.Length > 0)
  138. {
  139. for (int i = 0; i < parentIndexList.Length; i++)
  140. {
  141. var index = parentIndexList[i];
  142. if (index >= 0)
  143. {
  144. MagicaPhysicsManager.Instance.Bone.ResetFuturePrediction(index);
  145. }
  146. }
  147. }
  148. }
  149. /// <summary>
  150. /// ボーンのUnityPhysics利用カウンタを増減させる
  151. /// </summary>
  152. /// <param name="sw"></param>
  153. public void ChangeUnityPhysicsCount(bool sw)
  154. {
  155. if (parentIndexList != null && parentIndexList.Length > 0)
  156. {
  157. for (int i = 0; i < parentIndexList.Length; i++)
  158. {
  159. var index = parentIndexList[i];
  160. if (index >= 0)
  161. {
  162. MagicaPhysicsManager.Instance.Bone.ChangeUnityPhysicsCount(index, sw);
  163. }
  164. }
  165. }
  166. }
  167. /// <summary>
  168. /// 接続モード取得
  169. /// </summary>
  170. public ConnectionMode Connection
  171. {
  172. get
  173. {
  174. return connection;
  175. }
  176. }
  177. public float SameSurfaceAngle
  178. {
  179. get
  180. {
  181. return sameSurfaceAngle;
  182. }
  183. }
  184. public bool IsMeshConnection
  185. {
  186. get
  187. {
  188. return connection == ConnectionMode.MeshAutomatic || connection == ConnectionMode.MeshSequentialLoop || connection == ConnectionMode.MeshSequentialNoLoop;
  189. }
  190. }
  191. //=========================================================================================
  192. /// <summary>
  193. /// ボーン置換
  194. /// </summary>
  195. /// <param name="boneReplaceDict"></param>
  196. public void ReplaceBone<T>(Dictionary<T, Transform> boneReplaceDict) where T : class
  197. {
  198. for (int i = 0; i < rootList.Count; i++)
  199. {
  200. rootList[i] = MeshUtility.GetReplaceBone(rootList[i], boneReplaceDict);
  201. }
  202. }
  203. /// <summary>
  204. /// 現在使用しているボーンを格納して返す
  205. /// </summary>
  206. /// <returns></returns>
  207. public HashSet<Transform> GetUsedBones()
  208. {
  209. return new HashSet<Transform>(rootList);
  210. }
  211. }
  212. }