IKMgrData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [Serializable]
  5. public class IKMgrData
  6. {
  7. public TBody Body { get; private set; }
  8. public Transform KinematicTgt
  9. {
  10. get
  11. {
  12. return this.m_KinematicTgt;
  13. }
  14. }
  15. public IKMgrData.IKParam PointIK
  16. {
  17. get
  18. {
  19. return this.m_PointIK;
  20. }
  21. }
  22. public IKMgrData.IKParam RotateIK
  23. {
  24. get
  25. {
  26. return this.m_RotateIK;
  27. }
  28. }
  29. public TBody.IKCMO IKCmo
  30. {
  31. get
  32. {
  33. return this.m_IKCmo;
  34. }
  35. }
  36. public bool IsIkExec
  37. {
  38. get
  39. {
  40. return this.PointIK.IsIKExec || this.RotateIK.IsIKExec;
  41. }
  42. }
  43. private void IkCmoPorc(Vector3 tgt, Vector3 vechand_offset)
  44. {
  45. if (this.m_KinematicList.Count < 3)
  46. {
  47. return;
  48. }
  49. for (int i = 0; i < 3; i++)
  50. {
  51. this.IKCmo.Porc(this.m_KinematicList[2], this.m_KinematicList[1], this.m_KinematicList[0], tgt, vechand_offset);
  52. }
  53. }
  54. private void TransratePoint(Vector3 tgt_pos, Quaternion tgt_rot, Vector3 tgt_offset)
  55. {
  56. Vector3 vector = tgt_pos + tgt_rot * tgt_offset;
  57. Debug.DrawLine(tgt_pos, vector, Color.white);
  58. for (int i = 0; i < 5; i++)
  59. {
  60. Vector3 a = vector;
  61. if (this.m_AttachPointData != null)
  62. {
  63. Vector3 normalized = (this.KinematicTgt.position - this.KinematicTgt.parent.position).normalized;
  64. float d = Vector3.Dot(this.m_AttachPointData.AttachTrans.position - this.KinematicTgt.position, normalized);
  65. a += this.KinematicTgt.position + normalized * d - this.m_AttachPointData.AttachTrans.position;
  66. }
  67. for (int j = 1; j < this.m_KinematicList.Count; j++)
  68. {
  69. bool flag = j + 1 < this.m_KinematicList.Count;
  70. Vector3 vector2 = Vector3.zero;
  71. if (flag)
  72. {
  73. Quaternion rotation = Quaternion.FromToRotation(this.m_KinematicList[j - 1].position - this.m_KinematicList[j].position, a - this.m_KinematicList[j].position) * this.m_KinematicList[j].rotation;
  74. Vector3 point = this.m_KinematicList[j].InverseTransformPoint(this.m_KinematicList[j - 1].position);
  75. Vector3 b = this.m_KinematicList[j].position + rotation * point;
  76. vector2 = this.m_KinematicList[j].position + (a - b);
  77. this.m_KinematicList[j + 1].rotation = Quaternion.FromToRotation(this.m_KinematicList[j].position - this.m_KinematicList[j + 1].position, vector2 - this.m_KinematicList[j + 1].position) * this.m_KinematicList[j + 1].rotation;
  78. }
  79. this.m_KinematicList[j].rotation = Quaternion.FromToRotation(this.m_KinematicList[j - 1].position - this.m_KinematicList[j].position, a - this.m_KinematicList[j].position) * this.m_KinematicList[j].rotation;
  80. if (flag)
  81. {
  82. a = vector2;
  83. }
  84. }
  85. for (int k = this.m_KinematicList.Count - 1; k > 0; k--)
  86. {
  87. Debug.DrawLine(this.m_KinematicList[k].position, this.m_KinematicList[k - 1].position, Color.Lerp(Color.red, Color.yellow, (float)i / 4f));
  88. }
  89. if (this.m_AttachPointData != null)
  90. {
  91. Debug.DrawLine(this.KinematicTgt.position, this.m_AttachPointData.AttachTrans.position, Color.Lerp(Color.red, Color.yellow, (float)i / 4f));
  92. }
  93. }
  94. }
  95. private void TransratePoint(Transform tgt, Vector3 tgt_offset)
  96. {
  97. this.TransratePoint(tgt.position, tgt.rotation, tgt_offset);
  98. }
  99. private void ConstraintRot(Quaternion tgt_rot, Vector3 tgt_offset)
  100. {
  101. this.KinematicTgt.rotation = tgt_rot * Quaternion.Euler(tgt_offset);
  102. }
  103. private void ApplyIKSetting(IKMgrData.IKParam data)
  104. {
  105. if (this.IsExecLate)
  106. {
  107. return;
  108. }
  109. if (data.TgtMaid && data.TgtMaid != this.Body.maid && !data.TgtMaid.body0.LateUpdateEnd)
  110. {
  111. TBody body = data.TgtMaid.body0;
  112. body.OnLateUpdateEnd = (Action)Delegate.Combine(body.OnLateUpdateEnd, new Action(this.AfterExec));
  113. data.IsIKExec = false;
  114. this.IsExecLate = true;
  115. return;
  116. }
  117. data.IsIKExec = true;
  118. if (data.Target != null)
  119. {
  120. Vector3 position = data.Target.position;
  121. Quaternion rotation = data.Target.rotation;
  122. switch (data.MyType)
  123. {
  124. case IKMgrData.IKAttachType.Point:
  125. Debug.DrawLine(position, position + rotation * Vector3.forward * 0.2f, Color.cyan);
  126. this.IkCmoPorc(position, data.TgtOffset);
  127. break;
  128. case IKMgrData.IKAttachType.Rotate:
  129. this.ConstraintRot(rotation, data.TgtOffset);
  130. break;
  131. case IKMgrData.IKAttachType.NewPoint:
  132. this.TransratePoint(data.Target, data.TgtOffset);
  133. break;
  134. }
  135. if (data.IsTgtAxis)
  136. {
  137. KasaiUtility.DrawAxis(position, rotation, 0.125f);
  138. }
  139. }
  140. else if (data.Tgt_AttachName != string.Empty)
  141. {
  142. if (this.Body.goSlot[data.Tgt_AttachSlot].morph != null)
  143. {
  144. if (data.TgtMaid != null && data.TgtMaid.body0 != null)
  145. {
  146. Vector3 vector;
  147. Quaternion rotation2;
  148. Vector3 vector2;
  149. data.TgtMaid.body0.goSlot[data.Tgt_AttachSlot].morph.GetAttachPoint(data.Tgt_AttachName, out vector, out rotation2, out vector2, false);
  150. switch (data.MyType)
  151. {
  152. case IKMgrData.IKAttachType.Point:
  153. Debug.DrawLine(vector, vector + rotation2 * Vector3.forward * 0.2f, Color.blue);
  154. this.IkCmoPorc(vector, data.TgtOffset);
  155. break;
  156. case IKMgrData.IKAttachType.Rotate:
  157. this.ConstraintRot(rotation2, data.TgtOffset);
  158. break;
  159. case IKMgrData.IKAttachType.NewPoint:
  160. if (data.AxisTgt)
  161. {
  162. rotation2 = data.AxisTgt.rotation;
  163. }
  164. this.TransratePoint(vector, rotation2, data.TgtOffset);
  165. break;
  166. }
  167. if (data.IsTgtAxis)
  168. {
  169. KasaiUtility.DrawAxis(vector, rotation2, 0.125f);
  170. }
  171. }
  172. else
  173. {
  174. data.IsIKExec = false;
  175. data.Tgt_AttachName = string.Empty;
  176. }
  177. }
  178. }
  179. else
  180. {
  181. data.IsIKExec = false;
  182. }
  183. }
  184. private void AfterExec()
  185. {
  186. this.IKExec();
  187. if (!this.Body.IKExecLate)
  188. {
  189. this.Body.AutoTwist();
  190. for (int i = 0; i < this.Body.goSlot.Count; i++)
  191. {
  192. if (this.Body.goSlot[i].obj != null)
  193. {
  194. this.Body.goSlot[i].CopyTrans();
  195. }
  196. this.Body.goSlot[i].Update();
  197. }
  198. }
  199. }
  200. public void SetKinematicTgt(Transform tgt, TBody body, int weight = 3)
  201. {
  202. this.m_KinematicTgt = tgt;
  203. this.Body = body;
  204. this.m_KinematicList.Clear();
  205. int num = 0;
  206. Transform transform = tgt;
  207. while (num != weight)
  208. {
  209. this.m_KinematicList.Add(transform);
  210. Transform parent = transform.parent;
  211. if (!parent)
  212. {
  213. Debug.LogWarningFormat("IKのウェイトが大きすぎます:{0}", new object[]
  214. {
  215. weight
  216. });
  217. break;
  218. }
  219. transform = parent;
  220. num++;
  221. }
  222. }
  223. public void IkCmoInit(TBody body)
  224. {
  225. if (this.m_KinematicList.Count < 3)
  226. {
  227. return;
  228. }
  229. this.IKCmo.Init(this.m_KinematicList[2], this.m_KinematicList[1], this.m_KinematicList[0], body);
  230. }
  231. public IKMgrData.IKParam GetIKParam(IKMgrData.IKAttachType attach_type)
  232. {
  233. if (attach_type == IKMgrData.IKAttachType.Rotate)
  234. {
  235. return this.RotateIK;
  236. }
  237. return this.PointIK;
  238. }
  239. public void IKExec()
  240. {
  241. this.IsExecLate = false;
  242. this.ApplyIKSetting(this.PointIK);
  243. this.ApplyIKSetting(this.RotateIK);
  244. if (this.IsIkExec && !this.IsExecLate)
  245. {
  246. if (this.m_AttachPointData != null)
  247. {
  248. KasaiUtility.DrawObjAxis(this.m_AttachPointData.AttachTrans, 0.0625f);
  249. }
  250. else
  251. {
  252. KasaiUtility.DrawObjAxis(this.KinematicTgt, 0.0625f);
  253. }
  254. if (!this.PointIK.IsIKExec)
  255. {
  256. for (int i = this.m_KinematicList.Count - 1; i > 0; i--)
  257. {
  258. Debug.DrawLine(this.m_KinematicList[i].position, this.m_KinematicList[i - 1].position, Color.cyan);
  259. }
  260. if (this.m_AttachPointData != null)
  261. {
  262. Debug.DrawLine(this.KinematicTgt.position, this.m_AttachPointData.AttachTrans.position, Color.cyan);
  263. }
  264. }
  265. }
  266. }
  267. public Transform CreateAttachPointObj(string obj_name, Transform parent, TBody body, string slot_name, string attach_name)
  268. {
  269. if (this.m_AttachPointData != null && this.m_AttachPointData.AttachTrans)
  270. {
  271. return this.m_AttachPointData.AttachTrans;
  272. }
  273. GameObject gameObject = new GameObject(obj_name);
  274. gameObject.transform.SetParent(parent, false);
  275. this.m_AttachPointData = new IKMgrData.AttachPointIKData();
  276. this.m_AttachPointData.AttachTrans = gameObject.transform;
  277. this.m_AttachPointData.Tbody = body;
  278. this.m_AttachPointData.AttachName = attach_name;
  279. this.m_AttachPointData.SlotName = slot_name;
  280. return gameObject.transform;
  281. }
  282. public void SyncAttachPoint()
  283. {
  284. if (this.m_AttachPointData == null || !this.m_AttachPointData.AttachTrans)
  285. {
  286. return;
  287. }
  288. int slotNo = this.m_AttachPointData.Tbody.GetSlotNo(this.m_AttachPointData.SlotName);
  289. if (slotNo < 0 || slotNo >= this.m_AttachPointData.Tbody.goSlot.Count)
  290. {
  291. Debug.LogErrorFormat("スロット{0}は存在しません", new object[]
  292. {
  293. this.m_AttachPointData.SlotName
  294. });
  295. return;
  296. }
  297. Vector3 position;
  298. Quaternion rotation;
  299. Vector3 localScale;
  300. this.m_AttachPointData.Tbody.goSlot[slotNo].morph.GetAttachPoint(this.m_AttachPointData.AttachName, out position, out rotation, out localScale, false);
  301. this.m_AttachPointData.AttachTrans.position = position;
  302. this.m_AttachPointData.AttachTrans.rotation = rotation;
  303. this.m_AttachPointData.AttachTrans.localScale = localScale;
  304. }
  305. private const int m_CorrectNum = 5;
  306. [SerializeField]
  307. private List<Transform> m_KinematicList = new List<Transform>();
  308. [SerializeField]
  309. private Transform m_KinematicTgt;
  310. [SerializeField]
  311. private IKMgrData.IKParam m_PointIK = new IKMgrData.IKParam(IKMgrData.IKAttachType.NewPoint);
  312. [SerializeField]
  313. private IKMgrData.IKParam m_RotateIK = new IKMgrData.IKParam(IKMgrData.IKAttachType.Rotate);
  314. private TBody.IKCMO m_IKCmo = new TBody.IKCMO();
  315. private IKMgrData.AttachPointIKData m_AttachPointData;
  316. public bool IsExecLate;
  317. public enum IKAttachType
  318. {
  319. Point,
  320. Rotate,
  321. NewPoint
  322. }
  323. [Serializable]
  324. public class IKParam
  325. {
  326. public IKParam(IKMgrData.IKAttachType type)
  327. {
  328. this.MyType = type;
  329. }
  330. public IKMgrData.IKAttachType MyType { get; private set; }
  331. public void ChangePointType(IKMgrData.IKAttachType type)
  332. {
  333. if (this.MyType == IKMgrData.IKAttachType.Rotate)
  334. {
  335. return;
  336. }
  337. this.MyType = type;
  338. }
  339. public bool IsPointAttach
  340. {
  341. get
  342. {
  343. return this.MyType != IKMgrData.IKAttachType.Rotate;
  344. }
  345. }
  346. public Transform Target;
  347. public Vector3 TgtOffset;
  348. public int Tgt_AttachSlot = -1;
  349. public string Tgt_AttachName = string.Empty;
  350. public Maid TgtMaid;
  351. public bool IsTgtAxis;
  352. public bool IsIKExec;
  353. public Transform AxisTgt;
  354. }
  355. private class AttachPointIKData
  356. {
  357. public Transform AttachTrans;
  358. public TBody Tbody;
  359. public string SlotName = string.Empty;
  360. public string AttachName = string.Empty;
  361. }
  362. }