IKCtrlData.cs 28 KB

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