IKCtrlData.cs 27 KB

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