IKCtrlData.cs 25 KB

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