IKCtrlData.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. using System;
  2. using UnityEngine;
  3. public abstract class IKCtrlData
  4. {
  5. public IKCtrlData(FullBodyIKCtrl ik_ctrl, Transform tgt_bone, bool use_old = false, bool attach_ik = false)
  6. {
  7. this.MyIKCtrl = ik_ctrl;
  8. this.UseOldIK = use_old;
  9. this.m_TargetBone = tgt_bone;
  10. this.m_AttachPointIK = attach_ik;
  11. string text = (!this.MyIKCtrl.TgtMaid.boMAN) ? "Bip01" : "ManBip";
  12. string name = this.m_TargetBone.name;
  13. Transform transform = this.MyIKCtrl.IKTgtRoot.Find(name);
  14. if (!transform)
  15. {
  16. transform = new GameObject(name).transform;
  17. transform.transform.SetParent(this.MyIKCtrl.IKTgtRoot, false);
  18. GameObject gameObject = new GameObject("IKTarget");
  19. gameObject.transform.SetParent(transform.transform, false);
  20. this.m_IKTarget = gameObject.transform;
  21. }
  22. else
  23. {
  24. this.m_IKTarget = transform.Find("IKTarget");
  25. }
  26. this.m_BoneTgtPair = new IKCtrlData.BoneTgtPair(this.TargetBone, this.m_IKTarget);
  27. this.IkCmoInit();
  28. }
  29. public bool ForceIK
  30. {
  31. get
  32. {
  33. return this.m_ForceIK;
  34. }
  35. set
  36. {
  37. this.m_ForceIK = (this.m_ForceIKEnable && value);
  38. }
  39. }
  40. public IKCtrlData.PosRotPair BlendPosRot
  41. {
  42. get
  43. {
  44. return this.m_BlendPosRot;
  45. }
  46. }
  47. public IKCtrlData.IKParam PointIK
  48. {
  49. get
  50. {
  51. return (!this.m_PointFlagData.IsFlagOn) ? this.m_PointIK : this.m_NextPointIK;
  52. }
  53. }
  54. public IKCtrlData.IKParam RotateIK
  55. {
  56. get
  57. {
  58. return (!this.m_RotateFlagData.IsFlagOn) ? this.m_RotateIK : this.m_NextRotateIK;
  59. }
  60. }
  61. public TBody.IKCMO IKCmo
  62. {
  63. get
  64. {
  65. return this.m_IKCmo;
  66. }
  67. }
  68. public IKCtrlData.Vec3Enable PosEnable
  69. {
  70. get
  71. {
  72. return this.m_PosEnable;
  73. }
  74. }
  75. public IKCtrlData.Vec3Enable RotEnable
  76. {
  77. get
  78. {
  79. return this.m_RotEnable;
  80. }
  81. }
  82. public bool IsIKExec
  83. {
  84. get
  85. {
  86. return this.PointIK.IsIKExec || this.RotateIK.IsIKExec || this.ForceIK;
  87. }
  88. }
  89. public bool OldIkExec
  90. {
  91. get
  92. {
  93. return this.PointIK.IsIKExec && this.PointIK.IsOldIK;
  94. }
  95. }
  96. public bool AttachPointIK
  97. {
  98. get
  99. {
  100. return this.m_AttachPointIK;
  101. }
  102. }
  103. public Transform IKTarget
  104. {
  105. get
  106. {
  107. return this.m_IKTarget;
  108. }
  109. }
  110. public IKCtrlData.Vec3Enable OffsetEnable
  111. {
  112. get
  113. {
  114. return this.m_OffsetEnable;
  115. }
  116. }
  117. public IKCtrlData.ChangeFlagData PointFlagData
  118. {
  119. get
  120. {
  121. return this.m_PointFlagData;
  122. }
  123. }
  124. public IKCtrlData.ChangeFlagData RotateFlagData
  125. {
  126. get
  127. {
  128. return this.m_RotateFlagData;
  129. }
  130. }
  131. public virtual Transform TargetBone
  132. {
  133. get
  134. {
  135. return this.m_TargetBone;
  136. }
  137. }
  138. public abstract Transform[] ChainBones { get; }
  139. public abstract float PositionWeight { get; set; }
  140. public abstract float RotationWeight { get; set; }
  141. public static bool operator true(IKCtrlData data)
  142. {
  143. return data != null;
  144. }
  145. public static bool operator false(IKCtrlData data)
  146. {
  147. return data == null;
  148. }
  149. public static bool operator !(IKCtrlData data)
  150. {
  151. return data == null;
  152. }
  153. private void IkCmoInit()
  154. {
  155. if (!this.UseOldIK)
  156. {
  157. return;
  158. }
  159. this.IKCmo.Init(this.TargetBone.parent.parent, this.TargetBone.parent, this.TargetBone, this.MyIKCtrl.TgtBody);
  160. }
  161. private void IkCmoPorc(Vector3 tgt, Vector3 vechand_offset)
  162. {
  163. if (!this.UseOldIK)
  164. {
  165. return;
  166. }
  167. for (int i = 0; i < 3; i++)
  168. {
  169. this.IKCmo.Porc(this.TargetBone.parent.parent, this.TargetBone.parent, this.TargetBone, tgt, vechand_offset, this);
  170. }
  171. }
  172. private void SetIKPosRot(IKCtrlData.IKParam data, ref Vector3 pos, ref Quaternion rot, bool include_offset = false)
  173. {
  174. if (data.Target != null)
  175. {
  176. pos = data.Target.position;
  177. rot = data.Target.rotation;
  178. }
  179. else if (data.Tgt_AttachName != string.Empty)
  180. {
  181. if (this.MyIKCtrl.TgtBody.goSlot[data.Tgt_AttachSlot].morph != null)
  182. {
  183. if (data.TgtMaid != null && data.TgtMaid.body0 != null)
  184. {
  185. Vector3 vector;
  186. data.TgtMaid.body0.goSlot[data.Tgt_AttachSlot].morph.GetAttachPoint(data.Tgt_AttachName, out pos, out rot, out vector, false);
  187. if (data.MyType == IKCtrlData.IKAttachType.NewPoint && data.AxisTgt)
  188. {
  189. rot = data.AxisTgt.rotation;
  190. }
  191. }
  192. else
  193. {
  194. data.IsIKExec = false;
  195. data.Tgt_AttachName = string.Empty;
  196. }
  197. }
  198. }
  199. else if (this.ForceIK)
  200. {
  201. pos = this.m_IKTarget.position;
  202. rot = this.m_IKTarget.rotation;
  203. }
  204. else if (data.BlendWeight != 1f && data.BlendType == IKCtrlData.IKBlendType.IK_To_Detach)
  205. {
  206. pos = this.BlendPosRot.pos;
  207. rot = this.BlendPosRot.rot;
  208. }
  209. else
  210. {
  211. data.IsIKExec = false;
  212. }
  213. if (data.IsIKExec)
  214. {
  215. pos = this.GetIKEnable(data.MyType).GetEnable(this.TargetBone.position, pos, false);
  216. if (include_offset && !data.DoSetOffset)
  217. {
  218. data.DoSetOffset = true;
  219. if (data.IsPointAttach)
  220. {
  221. Vector3 start = pos;
  222. if (this.OffsetWorld)
  223. {
  224. pos += data.TgtOffset;
  225. }
  226. else if (this.OffsetBone)
  227. {
  228. pos -= this.TargetBone.rotation * data.TgtOffset;
  229. }
  230. else
  231. {
  232. pos += rot * data.TgtOffset;
  233. }
  234. Debug.DrawLine(start, pos, Color.white);
  235. }
  236. else
  237. {
  238. rot *= Quaternion.Euler(this.GetIKEnable(data.MyType).GetEnable(data.TgtOffset, false));
  239. }
  240. }
  241. }
  242. }
  243. private void ApplyIKSetting(IKCtrlData.IKParam data)
  244. {
  245. if (this.MyIKCtrl.IsUpdateLate)
  246. {
  247. return;
  248. }
  249. if (data.TgtMaid && data.TgtMaid != this.MyIKCtrl.TgtMaid)
  250. {
  251. IKCtrlData ikdata = data.TgtMaid.IKCtrl.GetIKData(data.Tgt_AttachName, false);
  252. if (ikdata != null && ikdata.IsIKExec)
  253. {
  254. IKCtrlData.IKParam ikparam = ikdata.GetIKParam(data.MyType, false, false);
  255. this.m_EachOtherIK = (ikparam.IsIKExec && ikparam.Target == this.TargetBone);
  256. }
  257. if (!data.TgtMaid.body0.LateUpdateEnd && !this.MyIKCtrl.TgtBody.LateUpdateEnd)
  258. {
  259. this.MyIKCtrl.IsUpdateLate = true;
  260. if (data.TgtMaid.IKCtrl.IsIKExec)
  261. {
  262. FullBodyIKCtrl ikctrl = data.TgtMaid.IKCtrl;
  263. ikctrl.PostSolverUpdate = (Action)Delegate.Combine(ikctrl.PostSolverUpdate, new Action(this.MyIKCtrl.LateIKUpdate));
  264. }
  265. else
  266. {
  267. TBody body = data.TgtMaid.body0;
  268. body.OnLateUpdateEnd = (Action)Delegate.Combine(body.OnLateUpdateEnd, new Action(this.MyIKCtrl.LateIKUpdate));
  269. }
  270. return;
  271. }
  272. }
  273. data.DoSetOffset = false;
  274. data.IsIKExec = true;
  275. Vector3 vector = this.TargetBone.position;
  276. Quaternion quaternion = this.TargetBone.rotation;
  277. if (this.GetFlagData(data.MyType).IsEnable)
  278. {
  279. if (!this.GetFlagData(data.MyType).BlendCtrlSelf)
  280. {
  281. IKCtrlData.IKParam ikparam2 = this.GetIKParam(data.MyType, true, false);
  282. ikparam2.DoSetOffset = false;
  283. Vector3 a = vector;
  284. Quaternion a2 = quaternion;
  285. this.SetIKPosRot(ikparam2, ref a, ref a2, true);
  286. IKCtrlData.IKParam ikparam3 = this.GetIKParam(data.MyType, true, true);
  287. ikparam3.DoSetOffset = false;
  288. Vector3 b = vector;
  289. Quaternion b2 = quaternion;
  290. this.SetIKPosRot(ikparam3, ref b, ref b2, true);
  291. float num = this.GetFlagData(data.MyType).Value01();
  292. data.IsIKExec = (num != 0f);
  293. if (data.IsPointAttach)
  294. {
  295. vector = Vector3.Lerp(a, b, num);
  296. }
  297. else
  298. {
  299. quaternion = Quaternion.Lerp(a2, b2, num);
  300. }
  301. }
  302. else if (this.GetFlagData(data.MyType).IsFlagChange())
  303. {
  304. bool isFlagOn = this.GetFlagData(data.MyType).IsFlagOn;
  305. IKCtrlData.IKParam ikparam4 = this.GetIKParam(data.MyType, true, !isFlagOn);
  306. IKCtrlData.IKParam ikparam5 = this.GetIKParam(data.MyType, true, isFlagOn);
  307. ikparam4.BlendRecet(true);
  308. ikparam5.BlendRecet(true);
  309. ikparam5.SetLastTarget(ikparam4.Tgt_AttachName, ikparam4.TgtMaid, ikparam4.Target);
  310. ikparam5.CheckBlendType(false);
  311. if (ikparam5.MyType == IKCtrlData.IKAttachType.Rotate)
  312. {
  313. this.BlendPosRot.rot = this.m_lastIKPosRot.rot;
  314. }
  315. else
  316. {
  317. this.BlendPosRot.pos = this.m_lastIKPosRot.pos;
  318. }
  319. if (ikparam5.BlendType == IKCtrlData.IKBlendType.IK_To_Detach && ikparam5.BlendTime <= 0f)
  320. {
  321. ikparam5.BlendTime = ikparam4.BlendTime;
  322. }
  323. this.SetIKPosRot(data, ref vector, ref quaternion, false);
  324. }
  325. else
  326. {
  327. this.SetIKPosRot(data, ref vector, ref quaternion, false);
  328. }
  329. }
  330. else
  331. {
  332. this.SetIKPosRot(data, ref vector, ref quaternion, false);
  333. }
  334. if (data.IsIKExec)
  335. {
  336. if (this.m_EachOtherIK && !data.TgtMaid.IKCtrl.IsUpdateEnd)
  337. {
  338. if (data.IsPointAttach)
  339. {
  340. vector = Vector3.Lerp(this.TargetBone.position, vector, 0.5f);
  341. }
  342. else
  343. {
  344. quaternion = Quaternion.Lerp(this.TargetBone.rotation, quaternion, 0.5f);
  345. }
  346. }
  347. if (data.FirstFrame && !data.BlendNow)
  348. {
  349. if (data.IsPointAttach)
  350. {
  351. this.m_FirstTgtPosRot.pos = vector;
  352. }
  353. else
  354. {
  355. this.m_FirstTgtPosRot.rot = quaternion;
  356. }
  357. }
  358. if (data.DoAnimation && !data.BlendNow)
  359. {
  360. if (!data.DoSetPosRot)
  361. {
  362. Transform targetBone = this.TargetBone;
  363. if (data.IsPointAttach)
  364. {
  365. if (!data.FirstFrame)
  366. {
  367. this.m_PosRotOffset.pos = targetBone.position - vector - this.m_FirstPosRotOffset.pos;
  368. }
  369. else
  370. {
  371. this.m_FirstPosRotOffset.pos = targetBone.position - vector;
  372. }
  373. }
  374. else if (!data.FirstFrame)
  375. {
  376. this.m_PosRotOffset.rot = targetBone.rotation * Quaternion.Inverse(quaternion) * Quaternion.Inverse(this.m_FirstPosRotOffset.rot);
  377. }
  378. else
  379. {
  380. this.m_FirstPosRotOffset.rot = targetBone.rotation * Quaternion.Inverse(quaternion);
  381. }
  382. }
  383. }
  384. else
  385. {
  386. this.m_PosRotOffset.Recet();
  387. }
  388. this.SetTargetTransform(data, vector, quaternion);
  389. if (!data.BlendNow)
  390. {
  391. data.FirstFrame = false;
  392. }
  393. if (data.IsTgtAxis)
  394. {
  395. KasaiUtility.DrawAxis(vector, quaternion, 0.125f);
  396. }
  397. if (data.BlendNow && !data.DoSetPosRot)
  398. {
  399. data.ElapsedTime += Time.deltaTime;
  400. }
  401. data.DoSetPosRot = true;
  402. }
  403. }
  404. protected bool IsEachBoneIK(IKCtrlData.IKParam param)
  405. {
  406. return param.IsIKExec && param.Target == this.TargetBone;
  407. }
  408. protected virtual void SetTargetTransform(IKCtrlData.IKParam data, Vector3 pos, Quaternion rot)
  409. {
  410. switch (data.MyType)
  411. {
  412. case IKCtrlData.IKAttachType.Point:
  413. pos += this.m_PosRotOffset.pos;
  414. Debug.DrawLine(pos, pos + rot * Vector3.forward * 0.2f, Color.cyan);
  415. if (!data.DoSetOffset)
  416. {
  417. this.IkCmoPorc(pos, data.TgtOffset);
  418. }
  419. else
  420. {
  421. this.IkCmoPorc(pos, Vector3.zero);
  422. }
  423. break;
  424. case IKCtrlData.IKAttachType.Rotate:
  425. rot *= this.m_PosRotOffset.rot;
  426. if (!data.DoSetOffset)
  427. {
  428. rot *= Quaternion.Euler(this.GetIKEnable(data.MyType).GetEnable(data.TgtOffset, false));
  429. }
  430. this.m_IKTarget.rotation = rot;
  431. if (data.BlendType == IKCtrlData.IKBlendType.IK_To_IK)
  432. {
  433. this.m_IKTarget.rotation = Quaternion.Lerp(this.BlendPosRot.rot, this.m_IKTarget.rotation, data.BlendWeight);
  434. this.RotationWeight = this.m_IKWeight;
  435. }
  436. else if (data.BlendType == IKCtrlData.IKBlendType.IK_To_Detach)
  437. {
  438. this.RotationWeight = this.m_IKWeight * (1f - data.BlendWeight);
  439. }
  440. else
  441. {
  442. this.RotationWeight = this.m_IKWeight * data.BlendWeight;
  443. }
  444. break;
  445. case IKCtrlData.IKAttachType.NewPoint:
  446. pos += this.m_PosRotOffset.pos;
  447. if (!data.DoSetOffset)
  448. {
  449. Vector3 start = pos;
  450. if (this.OffsetWorld)
  451. {
  452. pos += data.TgtOffset;
  453. }
  454. else if (this.OffsetBone)
  455. {
  456. pos -= this.TargetBone.rotation * data.TgtOffset;
  457. }
  458. else
  459. {
  460. pos += rot * data.TgtOffset;
  461. }
  462. Debug.DrawLine(start, pos, Color.white);
  463. }
  464. this.m_IKTarget.position = pos;
  465. if (data.BlendType == IKCtrlData.IKBlendType.IK_To_IK)
  466. {
  467. this.m_IKTarget.position = Vector3.Lerp(this.BlendPosRot.pos, this.m_IKTarget.position, data.BlendWeight);
  468. this.PositionWeight = this.m_IKWeight;
  469. }
  470. else if (data.BlendType == IKCtrlData.IKBlendType.IK_To_Detach)
  471. {
  472. this.PositionWeight = this.m_IKWeight * (1f - data.BlendWeight);
  473. }
  474. else
  475. {
  476. this.PositionWeight = this.m_IKWeight * data.BlendWeight;
  477. }
  478. break;
  479. }
  480. data.DoSetOffset = true;
  481. }
  482. public IKCtrlData.IKParam GetIKParam(IKCtrlData.IKAttachType attach_type, bool ignore_flag = false, bool is_next = false)
  483. {
  484. if (ignore_flag)
  485. {
  486. if (attach_type == IKCtrlData.IKAttachType.Rotate)
  487. {
  488. return (!is_next) ? this.m_RotateIK : this.m_NextRotateIK;
  489. }
  490. return (!is_next) ? this.m_PointIK : this.m_NextPointIK;
  491. }
  492. else
  493. {
  494. if (attach_type == IKCtrlData.IKAttachType.Rotate)
  495. {
  496. return this.RotateIK;
  497. }
  498. return this.PointIK;
  499. }
  500. }
  501. public IKCtrlData.IKParam GetNextIKParam(IKCtrlData.IKAttachType attach_type)
  502. {
  503. return this.GetIKParam(attach_type, true, true);
  504. }
  505. public IKCtrlData.ChangeFlagData GetFlagData(IKCtrlData.IKAttachType attach_type)
  506. {
  507. if (attach_type == IKCtrlData.IKAttachType.Rotate)
  508. {
  509. return this.m_RotateFlagData;
  510. }
  511. return this.m_PointFlagData;
  512. }
  513. public IKCtrlData.Vec3Enable GetIKEnable(IKCtrlData.IKAttachType attach_type)
  514. {
  515. if (attach_type == IKCtrlData.IKAttachType.Rotate)
  516. {
  517. return this.RotEnable;
  518. }
  519. return this.PosEnable;
  520. }
  521. public void SetFlagTarget(IKCtrlData.IKAttachType attach_type, string name)
  522. {
  523. if (this.MyIKCtrl.TgtMaid.boMAN)
  524. {
  525. this.GetFlagData(attach_type).Target = this.MyIKCtrl.TgtBody.GetBone(name);
  526. }
  527. else
  528. {
  529. this.GetFlagData(attach_type).Target = this.MyIKCtrl.GetSTFlagObj(name);
  530. }
  531. }
  532. public void SetIKSetting(IKCtrlData.IKAttachType attachType, bool is_another, Maid tgt_maid, int slot_no, string attach_name, Transform axis_bone, Vector3 f_vecOffset, bool do_animation = false, float blend_time = 0f)
  533. {
  534. this.SetIKSetting(attachType, is_another, tgt_maid, slot_no, attach_name, axis_bone, null, f_vecOffset, do_animation, blend_time);
  535. }
  536. public void SetIKSetting(IKCtrlData.IKAttachType attachType, bool is_another, Maid tgt_maid, Transform target, Vector3 f_vecOffset, bool do_animation = false, float blend_time = 0f)
  537. {
  538. this.SetIKSetting(attachType, is_another, tgt_maid, -1, string.Empty, null, target, f_vecOffset, do_animation, blend_time);
  539. }
  540. public void SetIKSetting(IKCtrlData.IKAttachType attachType, bool is_another, Transform target, Vector3 f_vecOffset, bool do_animation = false, float blend_time = 0f)
  541. {
  542. this.SetIKSetting(attachType, is_another, null, -1, string.Empty, null, target, f_vecOffset, do_animation, blend_time);
  543. }
  544. public void SetIKSetting(IKCtrlData.IKAttachType attachType, bool is_another, Maid tgt_maid, string bone_name, Transform target, Vector3 f_vecOffset, bool do_animation = false, float blend_time = 0f)
  545. {
  546. this.SetIKSetting(attachType, is_another, tgt_maid, -1, bone_name, null, target, f_vecOffset, do_animation, blend_time);
  547. }
  548. public virtual void Update()
  549. {
  550. }
  551. public virtual void ApplyIKSetting()
  552. {
  553. if (this.ForceIK)
  554. {
  555. this.PointIK.ChangePointType(IKCtrlData.IKAttachType.NewPoint);
  556. }
  557. if (!this.MyIKCtrl.IsUpdateEnd)
  558. {
  559. float num = 0f;
  560. this.RotationWeight = num;
  561. this.PositionWeight = num;
  562. this.m_BoneTgtPair.Cppy();
  563. this.m_EachOtherIK = false;
  564. this.ForceIK |= this.MyIKCtrl.AllForceIK;
  565. }
  566. this.ApplyIKSetting(this.PointIK);
  567. this.ApplyIKSetting(this.RotateIK);
  568. }
  569. public virtual void SetIKSetting(IKCtrlData.IKAttachType attachType, bool is_next, Maid tgt_maid, int slot_no, string attach_name, Transform axis_bone, Transform target, Vector3 f_vecOffset, bool do_animation = false, float blend_time = 0f)
  570. {
  571. this.GetIKParam(attachType, true, is_next).ChangePointType(attachType);
  572. if (this.GetIKParam(attachType, true, is_next).MyType != attachType)
  573. {
  574. return;
  575. }
  576. this.MyIKCtrl.IKActive = true;
  577. this.BlendPosRot.Copy(this.m_lastIKPosRot);
  578. this.m_PosRotOffset.Recet();
  579. this.m_FirstPosRotOffset.Recet();
  580. this.m_FirstTgtPosRot.Recet();
  581. this.GetIKParam(attachType, true, is_next).SetIKSetting(tgt_maid, slot_no, attach_name, axis_bone, target, f_vecOffset, do_animation, blend_time);
  582. this.GetFlagData(attachType).IsFlagChange();
  583. }
  584. public virtual void Detach(IKCtrlData.IKAttachType attachType, float blend_time = 0f)
  585. {
  586. this.OffsetBone = false;
  587. this.OffsetWorld = false;
  588. this.ForceIK = false;
  589. this.m_BoneTgtPair.PosOffset = Vector3.zero;
  590. this.BlendPosRot.Copy(this.m_lastIKPosRot);
  591. this.GetFlagData(attachType).Reset();
  592. this.GetIKParam(attachType, true, false).Detach(blend_time);
  593. this.GetIKParam(attachType, true, true).Detach(blend_time);
  594. }
  595. public virtual void LateUpdate()
  596. {
  597. if (this.IsIKExec && this.m_DrawLine && !this.MyIKCtrl.IsUpdateLate)
  598. {
  599. KasaiUtility.DrawObjAxis(this.TargetBone, 0.0625f);
  600. if (!this.OldIkExec)
  601. {
  602. Color color = (!this.PointIK.IsIKExec) ? Color.cyan : Color.yellow;
  603. for (int i = 0; i < this.ChainBones.Length - 1; i++)
  604. {
  605. Debug.DrawLine(this.ChainBones[i].position, this.ChainBones[i + 1].position, color);
  606. }
  607. }
  608. }
  609. this.m_lastIKPosRot.Copy(this.m_TargetBone);
  610. this.PointIK.DoSetPosRot = (this.RotateIK.DoSetPosRot = false);
  611. }
  612. public virtual void SetTargetOffset(Vector3 offset, bool inverse = false)
  613. {
  614. if (this.IsIKExec)
  615. {
  616. return;
  617. }
  618. this.m_BoneTgtPair.PosOffset = this.m_OffsetEnable.GetEnable(offset, inverse);
  619. }
  620. [SerializeField]
  621. [Header("位置IK")]
  622. private IKCtrlData.IKParam m_PointIK = new IKCtrlData.IKParam(IKCtrlData.IKAttachType.NewPoint, false);
  623. [SerializeField]
  624. private IKCtrlData.IKParam m_NextPointIK = new IKCtrlData.IKParam(IKCtrlData.IKAttachType.NewPoint, true);
  625. [SerializeField]
  626. private IKCtrlData.Vec3Enable m_PosEnable = new IKCtrlData.Vec3Enable();
  627. [SerializeField]
  628. [Header("回転IK")]
  629. private IKCtrlData.IKParam m_RotateIK = new IKCtrlData.IKParam(IKCtrlData.IKAttachType.Rotate, false);
  630. [SerializeField]
  631. private IKCtrlData.IKParam m_NextRotateIK = new IKCtrlData.IKParam(IKCtrlData.IKAttachType.Rotate, true);
  632. [SerializeField]
  633. private IKCtrlData.Vec3Enable m_RotEnable = new IKCtrlData.Vec3Enable();
  634. private TBody.IKCMO m_IKCmo = new TBody.IKCMO();
  635. private IKCtrlData.PosRotPair m_BlendPosRot = new IKCtrlData.PosRotPair();
  636. [SerializeField]
  637. [Space]
  638. private IKCtrlData.PosRotPair m_PosRotOffset = new IKCtrlData.PosRotPair();
  639. private IKCtrlData.PosRotPair m_FirstPosRotOffset = new IKCtrlData.PosRotPair();
  640. [SerializeField]
  641. private bool m_AttachPointIK;
  642. [SerializeField]
  643. private Transform m_TargetBone;
  644. [SerializeField]
  645. private Transform m_IKTarget;
  646. [SerializeField]
  647. private bool m_DrawLine = true;
  648. private IKCtrlData.BoneTgtPair m_BoneTgtPair;
  649. protected IKCtrlData.PosRotPair m_FirstTgtPosRot = new IKCtrlData.PosRotPair();
  650. protected IKCtrlData.PosRotPair m_lastIKPosRot = new IKCtrlData.PosRotPair();
  651. [SerializeField]
  652. [Range(0f, 1f)]
  653. protected float m_IKWeight = 1f;
  654. [SerializeField]
  655. protected bool m_ForceIK;
  656. protected bool m_ForceIKEnable;
  657. protected bool m_EachOtherIK;
  658. protected IKCtrlData.Vec3Enable m_OffsetEnable = new IKCtrlData.Vec3Enable();
  659. [SerializeField]
  660. [Header("フラグ情報")]
  661. protected IKCtrlData.ChangeFlagData m_PointFlagData = new IKCtrlData.ChangeFlagData();
  662. [SerializeField]
  663. protected IKCtrlData.ChangeFlagData m_RotateFlagData = new IKCtrlData.ChangeFlagData();
  664. [Space]
  665. public bool OffsetWorld;
  666. public bool OffsetBone;
  667. public readonly FullBodyIKCtrl MyIKCtrl;
  668. public readonly bool UseOldIK;
  669. public enum IKAttachType
  670. {
  671. Point,
  672. Rotate,
  673. NewPoint
  674. }
  675. public enum IKBlendType
  676. {
  677. Detach_To_IK,
  678. IK_To_IK,
  679. IK_To_Detach,
  680. Not_Blend
  681. }
  682. [Serializable]
  683. public class Vec3Enable
  684. {
  685. public bool AnyDisable
  686. {
  687. get
  688. {
  689. return !this.EnableX || !this.EnableY || !this.EnableZ;
  690. }
  691. }
  692. public Vector3 GetEnable(Vector3 pos, bool inverse = false)
  693. {
  694. if (this.EnableX == inverse)
  695. {
  696. pos.x = 0f;
  697. }
  698. if (this.EnableY == inverse)
  699. {
  700. pos.y = 0f;
  701. }
  702. if (this.EnableZ == inverse)
  703. {
  704. pos.z = 0f;
  705. }
  706. return pos;
  707. }
  708. public Vector3 GetEnable(Vector3 from, Vector3 to, bool inverse = false)
  709. {
  710. if (this.EnableX == inverse)
  711. {
  712. to.x = from.x;
  713. }
  714. if (this.EnableY == inverse)
  715. {
  716. to.y = from.y;
  717. }
  718. if (this.EnableZ == inverse)
  719. {
  720. to.z = from.z;
  721. }
  722. return to;
  723. }
  724. public void Recet()
  725. {
  726. this.EnableX = (this.EnableY = (this.EnableZ = true));
  727. }
  728. public void Copy(IKCtrlData.Vec3Enable cpy)
  729. {
  730. this.EnableX = cpy.EnableX;
  731. this.EnableY = cpy.EnableY;
  732. this.EnableZ = cpy.EnableZ;
  733. }
  734. public bool EnableX = true;
  735. public bool EnableY = true;
  736. public bool EnableZ = true;
  737. }
  738. [Serializable]
  739. public class PosRotPair
  740. {
  741. public void Recet()
  742. {
  743. this.pos = Vector3.zero;
  744. this.rot = new Quaternion(0f, 0f, 0f, 1f);
  745. }
  746. public void Copy(IKCtrlData.PosRotPair pair)
  747. {
  748. this.pos = pair.pos;
  749. this.rot = pair.rot;
  750. }
  751. public void Copy(Transform transform)
  752. {
  753. this.pos = transform.position;
  754. this.rot = transform.rotation;
  755. }
  756. public void DrawAxis()
  757. {
  758. KasaiUtility.DrawAxis(this.pos, this.rot, 0.0625f);
  759. }
  760. public Vector3 pos = Vector3.zero;
  761. public Quaternion rot = new Quaternion(0f, 0f, 0f, 1f);
  762. }
  763. public class BoneTgtPair
  764. {
  765. public BoneTgtPair(Transform bone, Transform target)
  766. {
  767. this.Bone = bone;
  768. this.Target = target;
  769. }
  770. public void Cppy()
  771. {
  772. this.Target.position = this.Bone.position + this.PosOffset;
  773. this.Target.rotation = this.Bone.rotation;
  774. }
  775. public readonly Transform Bone;
  776. public readonly Transform Target;
  777. public Vector3 PosOffset = Vector3.zero;
  778. }
  779. [Serializable]
  780. public class ChangeFlagData
  781. {
  782. public bool IsEnable
  783. {
  784. get
  785. {
  786. return this.Target != null;
  787. }
  788. }
  789. public bool IsFlagOn
  790. {
  791. get
  792. {
  793. return this.IsEnable && this.Value() >= this.OnValue;
  794. }
  795. }
  796. public float Value()
  797. {
  798. if (!this.Target)
  799. {
  800. return 0f;
  801. }
  802. Vector3 vector = KasaiUtility.Vec3Multiply(this.Target.localPosition, this.Axis);
  803. return vector.magnitude * Vector3.Dot(this.Axis, vector.normalized);
  804. }
  805. public float Value01()
  806. {
  807. return Mathf.Clamp01(this.Value() / this.OnValue);
  808. }
  809. public bool IsFlagChange()
  810. {
  811. if (!this.Target)
  812. {
  813. return false;
  814. }
  815. bool cunnretFlag = this.m_CunnretFlag;
  816. this.m_CunnretFlag = this.IsFlagOn;
  817. return cunnretFlag != this.m_CunnretFlag;
  818. }
  819. public void Reset()
  820. {
  821. this.Target = null;
  822. this.m_CunnretFlag = false;
  823. this.BlendCtrlSelf = false;
  824. }
  825. private bool m_CunnretFlag;
  826. public Transform Target;
  827. public Vector3 Axis = Vector3.forward;
  828. public float OnValue = 0.1f;
  829. [HideInInspector]
  830. public bool BlendCtrlSelf;
  831. }
  832. [Serializable]
  833. public class IKParam
  834. {
  835. public IKParam(IKCtrlData.IKAttachType type, bool is_next = false)
  836. {
  837. this.MyType = type;
  838. this.IsNext = is_next;
  839. this.BlendType = IKCtrlData.IKBlendType.Not_Blend;
  840. }
  841. public IKCtrlData.IKAttachType MyType { get; private set; }
  842. public IKCtrlData.IKBlendType BlendType { get; private set; }
  843. public bool DoIKBlend { get; private set; }
  844. public bool IsPointAttach
  845. {
  846. get
  847. {
  848. return this.MyType != IKCtrlData.IKAttachType.Rotate;
  849. }
  850. }
  851. public bool NeedBlend
  852. {
  853. get
  854. {
  855. return this.BlendTime > 0f;
  856. }
  857. }
  858. public float BlendWeight
  859. {
  860. get
  861. {
  862. return (!this.NeedBlend || !this.DoIKBlend) ? 1f : Mathf.Clamp01(this.ElapsedTime / this.BlendTime);
  863. }
  864. }
  865. public bool BlendNow
  866. {
  867. get
  868. {
  869. return this.NeedBlend && this.ElapsedTime <= this.BlendTime;
  870. }
  871. }
  872. public bool IsOldIK
  873. {
  874. get
  875. {
  876. return this.MyType == IKCtrlData.IKAttachType.Point;
  877. }
  878. }
  879. public bool ExistTgt
  880. {
  881. get
  882. {
  883. return this.Target != null || !string.IsNullOrEmpty(this.Tgt_AttachName);
  884. }
  885. }
  886. public void ChangePointType(IKCtrlData.IKAttachType type)
  887. {
  888. if (this.MyType == IKCtrlData.IKAttachType.Rotate)
  889. {
  890. return;
  891. }
  892. this.MyType = type;
  893. }
  894. public bool IsEachOtherIK(bool check_update = false)
  895. {
  896. return this.TgtMaid && this.TgtMaid.IKCtrl.IsIKExec && this.TgtMaid.IKCtrl.IsUpdateEnd == check_update;
  897. }
  898. public void CheckBlendType(bool check_time = true)
  899. {
  900. this.BlendType = IKCtrlData.IKBlendType.IK_To_IK;
  901. this.DoIKBlend = true;
  902. if (this.m_lastTarget)
  903. {
  904. if (!this.Target && string.IsNullOrEmpty(this.Tgt_AttachName))
  905. {
  906. this.BlendType = IKCtrlData.IKBlendType.IK_To_Detach;
  907. }
  908. else if (this.Target)
  909. {
  910. this.DoIKBlend = (this.m_lastTarget != this.Target);
  911. }
  912. }
  913. else if (!string.IsNullOrEmpty(this.m_lastAttachName))
  914. {
  915. if (string.IsNullOrEmpty(this.Tgt_AttachName) && !this.Target)
  916. {
  917. this.BlendType = IKCtrlData.IKBlendType.IK_To_Detach;
  918. }
  919. else if (!this.Target)
  920. {
  921. this.DoIKBlend = (this.m_lastAttachName != this.Tgt_AttachName || this.m_lastTgtMaid != this.TgtMaid);
  922. }
  923. }
  924. else
  925. {
  926. this.BlendType = IKCtrlData.IKBlendType.Detach_To_IK;
  927. if (!this.Target && string.IsNullOrEmpty(this.Tgt_AttachName))
  928. {
  929. this.BlendType = IKCtrlData.IKBlendType.Not_Blend;
  930. this.DoIKBlend = false;
  931. }
  932. }
  933. if (check_time)
  934. {
  935. this.DoIKBlend &= (this.BlendTime > 0f);
  936. }
  937. }
  938. public void SetLastTarget(string attach_name, Maid maid, Transform target)
  939. {
  940. this.m_lastAttachName = attach_name;
  941. this.m_lastTgtMaid = maid;
  942. this.m_lastTarget = target;
  943. }
  944. public void BlendRecet(bool elpsed_only = false)
  945. {
  946. this.ElapsedTime = 0f;
  947. if (!elpsed_only)
  948. {
  949. this.DoIKBlend = false;
  950. this.BlendTime = -1f;
  951. this.BlendType = IKCtrlData.IKBlendType.Not_Blend;
  952. }
  953. }
  954. public void SetIKSetting(Maid tgt_maid, int slot_no, string attach_name, Transform axis_bone, Transform target, Vector3 f_vecOffset, bool do_animation, float blend_time)
  955. {
  956. this.SetLastTarget(this.Tgt_AttachName, this.TgtMaid, this.Target);
  957. this.TgtMaid = tgt_maid;
  958. this.Tgt_AttachSlot = slot_no;
  959. this.Tgt_AttachName = attach_name;
  960. this.Target = target;
  961. this.AxisTgt = axis_bone;
  962. if (this.IsPointAttach)
  963. {
  964. this.TgtOffset = f_vecOffset;
  965. }
  966. else
  967. {
  968. this.TgtOffset = KasaiUtility.AngleRimmit360(f_vecOffset);
  969. }
  970. this.BlendRecet(false);
  971. this.BlendTime = blend_time;
  972. this.CheckBlendType(true);
  973. this.DoAnimation = do_animation;
  974. this.IsIKExec = true;
  975. this.FirstFrame = true;
  976. }
  977. public void Detach(float blend_time = 0f)
  978. {
  979. this.BlendRecet(false);
  980. this.BlendTime = blend_time;
  981. this.SetLastTarget(this.Tgt_AttachName, this.TgtMaid, this.Target);
  982. this.TgtMaid = null;
  983. this.Tgt_AttachSlot = -1;
  984. this.Tgt_AttachName = string.Empty;
  985. this.Target = null;
  986. this.AxisTgt = null;
  987. this.TgtOffset = Vector3.zero;
  988. this.CheckBlendType(true);
  989. this.DoSetOffset = false;
  990. this.DoSetPosRot = false;
  991. this.DoAnimation = false;
  992. this.IsIKExec = false;
  993. this.FirstFrame = false;
  994. }
  995. private string m_lastAttachName = string.Empty;
  996. private Transform m_lastTarget;
  997. private Maid m_lastTgtMaid;
  998. public Transform Target;
  999. public Vector3 TgtOffset;
  1000. public int Tgt_AttachSlot = -1;
  1001. public string Tgt_AttachName = string.Empty;
  1002. public Maid TgtMaid;
  1003. public bool IsTgtAxis;
  1004. public bool IsIKExec;
  1005. public Transform AxisTgt;
  1006. public float BlendTime = -1f;
  1007. public float ElapsedTime;
  1008. [HideInInspector]
  1009. public bool FirstFrame;
  1010. public bool DoAnimation;
  1011. [HideInInspector]
  1012. public bool DoSetPosRot;
  1013. [HideInInspector]
  1014. public bool DoSetOffset;
  1015. public readonly bool IsNext;
  1016. }
  1017. }