COM3D2.XtMasterSlave.IKO132.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. #define IKO132
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using static ExtensionMethods.MyExtensions;
  7. using CM3D2.XtMasterSlave.Plugin;
  8. using UnityEngine;
  9. using System.Reflection;
  10. namespace XtMasterSlave_IK_XDLL
  11. {
  12. #if IKO132
  13. public class IkpInst : IkInst
  14. {
  15. public const string INFO = "Fixed by @ghorsington on Twitter";
  16. public static bool boAnime = false; //?
  17. public static bool IKBend = false; //
  18. public override bool IsNewPointIK(Maid m, string hand = "右手")
  19. {
  20. var ikP = m.body0.IKCtrl.GetIKData(hand, IKBend).GetIKSettingData(IKCtrlData.IKAttachType.Point);
  21. return (ikP.MyType == IKCtrlData.IKAttachType.NewPoint);
  22. }
  23. public override object GetIkPoint(TBody body, string hand = "右手")
  24. {
  25. #if DEBUG
  26. if (Input.GetKey(KeyCode.Space))
  27. {
  28. IKBend = true;
  29. Console.WriteLine("IKBend: ON");
  30. }
  31. else
  32. {
  33. IKBend = false;
  34. }
  35. #endif
  36. var obj = body.IKCtrl.GetIKData(hand, IKBend).GetIKSettingData(IKCtrlData.IKAttachType.Point);
  37. if (obj == null)
  38. obj = body.IKCtrl.GetIKData(hand, IKBend).GetIKSettingData(IKCtrlData.IKAttachType.NewPoint);
  39. return obj;
  40. }
  41. public override object GetIkCtrl(Maid maid)
  42. {
  43. return maid.IKCtrl;
  44. }
  45. public override object GetIkCtrlPoint(TBody body, string hand = "右手")
  46. {
  47. #if DEBUG
  48. if (Input.GetKey(KeyCode.Space))
  49. {
  50. IKBend = true;
  51. Console.WriteLine("IKBend: ON");
  52. }
  53. else
  54. {
  55. IKBend = false;
  56. }
  57. #endif
  58. var obj = body.IKCtrl.GetIKData(hand, IKBend);
  59. return obj;
  60. }
  61. private IKCtrlData.IKAttachType GetDefType(XtMasterSlave.MsLinkConfig mscfg)
  62. {
  63. if (mscfg.doIK159NewPointToDef)
  64. {
  65. return IKCtrlData.IKAttachType.NewPoint;
  66. }
  67. else
  68. {
  69. return IKCtrlData.IKAttachType.Point;
  70. }
  71. }
  72. public override void IkClear(Maid tgt, XtMasterSlave.MsLinkConfig mscfg)
  73. {
  74. List<string> listHand = new List<string> { "右手", "左手" };
  75. IkClear(tgt, listHand, mscfg);
  76. }
  77. //public override void IkClear(Maid tgt, List<string> listHand, XtMasterSlave.MsLinkConfig mscfg, IKCtrlData.IKAttachType IkType = (IKCtrlData.IKAttachType)(-1))
  78. public override void IkClear(Maid tgt, List<string> listHand, XtMasterSlave.MsLinkConfig mscfg, int IkType = (-1))
  79. {
  80. List<IKCtrlData.IKAttachType> listTypes = new List<IKCtrlData.IKAttachType>
  81. { IKCtrlData.IKAttachType.NewPoint, IKCtrlData.IKAttachType.Rotate };
  82. listHand.ToList().ForEach(h =>
  83. {
  84. var ctrl = tgt.body0.IKCtrl.GetIKData(h, IKBend);
  85. listTypes.ForEach(t =>
  86. {
  87. var iks = ctrl.GetIKSettingData(t);
  88. if (IkXT.IsIkCtrlO117)
  89. {
  90. ctrl.SetIKSetting(t, IKCtrlData.IKExecTiming.Normal, null, string.Empty, null, Vector3.zero);
  91. ctrl.Detach(t);
  92. //ctrl.SetIKSetting(t, false, null, -1, string.Empty, null, null, Vector3.zero, false, 0f);
  93. //iks.SetIKSetting(null, -1, string.Empty, null, null, Vector3.zero, false, 0f);
  94. //ctrl.Detach(t, 0f);
  95. }
  96. else
  97. {
  98. //iks.TgtMaid = null;
  99. //iks.Tgt_AttachSlot = -1;
  100. //iks.Tgt_AttachName = string.Empty;
  101. //iks.Target = null;
  102. //iks.AxisTgt = null;
  103. //iks.TgtOffset = Vector3.zero;
  104. //iks.IsTgtAxis
  105. }
  106. if (iks.MyType != IKCtrlData.IKAttachType.Rotate)
  107. {
  108. if (IkType >= 0 && IkType != (int)IKCtrlData.IKAttachType.Rotate
  109. && Enum.IsDefined(typeof(IKCtrlData.IKAttachType), IkType))
  110. {
  111. iks.ChangeIKType((IKCtrlData.IKAttachType)IkType);
  112. }
  113. else
  114. {
  115. if (mscfg != null)
  116. iks.ChangeIKType(GetDefType(mscfg));/*fix v5.0
  117. else
  118. iks.ChangePointType(IKCtrlData.IKAttachType.NewPoint);*/
  119. }
  120. }
  121. });
  122. });
  123. }
  124. public override void CopyHandIK(Maid master, Maid slave, XtMasterSlave.v3Offsets[] v3ofs, int num_)
  125. {
  126. List<string> listHand = new List<string> { "右手", "左手" };
  127. List<IKCtrlData.IKAttachType> listTypes = new List<IKCtrlData.IKAttachType>
  128. { IKCtrlData.IKAttachType.NewPoint, IKCtrlData.IKAttachType.Rotate };
  129. listHand.ToList().ForEach(h =>
  130. {
  131. var ikcm = master.body0.IKCtrl.GetIKData(h, IKBend);
  132. var ikcs = slave.body0.IKCtrl.GetIKData(h, IKBend);
  133. listTypes.ForEach(t =>
  134. {
  135. var ikm = ikcm.GetIKSettingData(t);
  136. var iks = ikcs.GetIKSettingData(t);
  137. if (!(string.IsNullOrEmpty(ikm.curTargetData.Tgt_AttachName) && ikm.curTargetData.Target == null))
  138. {
  139. //Console.WriteLine("{0} {1} -> {2} {3} {4}", h, t, ikm.MyType, ikm.Tgt_AttachName, ikm.Target);
  140. if (iks.MyType != IKCtrlData.IKAttachType.Rotate)
  141. {
  142. if (ikm.MyType != IKCtrlData.IKAttachType.Rotate)
  143. {
  144. iks.ChangeIKType(ikm.MyType);
  145. }
  146. }
  147. float fixAngle(float angle)
  148. {
  149. while (Mathf.Abs(angle) > 360f)
  150. {
  151. angle = ((!(angle < 0f)) ? (angle - 360f) : (angle + 360f));
  152. }
  153. return angle;
  154. }
  155. if (IkXT.IsIkCtrlO117)
  156. {
  157. ikcs.SetIKSetting(t, IKCtrlData.IKExecTiming.Normal, ikm.curTargetData.TgtChara, ikm.curTargetData.Tgt_AttachSlot, ikm.curTargetData.Tgt_AttachName, ikm.curTargetData.AxisTgt, ikm.curTargetData.Target, ikm.curTargetData.TgtOffset, ikm.DoAnimation);
  158. //ikcs.SetIKSetting(t, false, ikm.TgtMaid, ikm.Tgt_AttachSlot, ikm.Tgt_AttachName, ikm.AxisTgt, ikm.Target, ikm.TgtOffset, ikm.DoAnimation, ikm.BlendTime);
  159. //iks.SetIKSetting(ikm.TgtMaid, ikm.Tgt_AttachSlot, ikm.Tgt_AttachName, ikm.AxisTgt, ikm.Target, ikm.TgtOffset, ikm.DoAnimation, ikm.BlendTime);
  160. }
  161. else
  162. {
  163. //iks.TgtMaid = ikm.TgtMaid;
  164. //iks.Tgt_AttachSlot = ikm.Tgt_AttachSlot;
  165. //iks.Tgt_AttachName = ikm.Tgt_AttachName;
  166. //iks.Target = ikm.Target;
  167. //iks.AxisTgt = ikm.AxisTgt;
  168. }
  169. if (iks.IsPointAttach)
  170. {
  171. iks.curTargetData.TgtOffset = ikm.curTargetData.TgtOffset;
  172. if (h == "右手")
  173. iks.curTargetData.TgtOffset += v3ofs[num_].v3HandROffset;
  174. else
  175. iks.curTargetData.TgtOffset += v3ofs[num_].v3HandLOffset;
  176. }
  177. else
  178. {
  179. Vector3 v3rot = Vector3.zero;
  180. if (h == "右手")
  181. v3rot = v3ofs[num_].v3HandROffsetRot;
  182. else
  183. v3rot = v3ofs[num_].v3HandLOffsetRot;
  184. iks.curTargetData.TgtOffset.x = fixAngle(ikm.curTargetData.TgtOffset.x + v3rot.x);
  185. iks.curTargetData.TgtOffset.y = fixAngle(ikm.curTargetData.TgtOffset.y + v3rot.y);
  186. iks.curTargetData.TgtOffset.z = fixAngle(ikm.curTargetData.TgtOffset.z + v3rot.z);
  187. }
  188. }
  189. });
  190. });
  191. //needInit = true;
  192. }
  193. public override void SetHandIKRotate(string handName, Maid master, Maid slave, string boneTgtname, Vector3 v3HandLOffsetRot)
  194. {
  195. slave.IKTargetToBone(handName, master, boneTgtname, v3HandLOffsetRot, IKCtrlData.IKAttachType.Rotate, false, boAnime, IKCtrlData.IKExecTiming.Normal);
  196. //slave.IKTargetToBone(handName, master, boneTgtname, v3HandLOffsetRot, IKCtrlData.IKAttachType.Rotate, false, 0f, boAnime, false);
  197. }
  198. public override void SetHandIKTarget(XtMasterSlave.MsLinkConfig mscfg, string handName, Maid master, Maid slave, int slot_no, string attach_name, Transform target, Vector3 v3HandLOffset)
  199. {
  200. /*if (needInit)
  201. {
  202. needInit = false;
  203. if (mscfg.doIK159NewPointToDef)
  204. IKInit(slave, mscfg);
  205. #if DEBUG
  206. else
  207. IKInit4OldPoint(slave);
  208. #endif
  209. }*/
  210. slave.IKCtrl.GetIKData(handName, IKBend).SetIKSetting(GetDefType(mscfg), IKCtrlData.IKExecTiming.Normal, master, slot_no, attach_name, null, target, v3HandLOffset, boAnime);
  211. HandFootIKData ikdata = slave.IKCtrl.GetIKData<HandFootIKData>(handName);
  212. ikdata.CorrectType = HandFootIKData.BorderCorrectType.Bone;
  213. }
  214. bool needInit = true;
  215. static string bodytgt = "dummyBodyTgtXt";
  216. //RootMotion.FinalIK.FullBodyBipedIK m_FullbodyIK;
  217. #if DEBUG
  218. /*GameObject goBodyTgt = new GameObject("dummyBodyTgtXt");
  219. RootMotion.FinalIK.FullBodyBipedIK m_FullbodyIK;
  220. GameObject goTgtSR = new GameObject("xtTgtSR");
  221. GameObject goTgtSL = new GameObject("xtTgtSL");*/
  222. #endif
  223. Dictionary<Maid, string> lastAnimeFNs = new Dictionary<Maid, string>();
  224. private void IKInit(Maid slave, XtMasterSlave.MsLinks ms, XtMasterSlave.MsLinkConfig mscfg)
  225. {
  226. var fik = slave.body0.IKCtrl.GetNonPublicField<RootMotion.FinalIK.FullBodyBipedIK>("m_FullbodyIK");
  227. RootMotion.FinalIK.FullBodyBipedIK FullbodyIK = fik;
  228. var solver = FullbodyIK.solver;
  229. string[] tgtlist = new string[] { "IKTarget", "BendBone", "ChainRootBone" };
  230. bool animStop = true; //モーション停止中
  231. if (ms.doMasterSlave && !mscfg.doStackSlave_PosSyncMode)
  232. {
  233. animStop = false;
  234. }
  235. else
  236. {
  237. Animation anim = slave.body0.m_Bones.GetComponent<Animation>();
  238. animStop = !anim.isPlaying;
  239. }
  240. if (!lastAnimeFNs.ContainsKey(slave) || slave.body0.LastAnimeFN != lastAnimeFNs[slave])
  241. {
  242. lastAnimeFNs[slave] = slave.body0.LastAnimeFN;
  243. animStop = false;
  244. }
  245. solver.spineStiffness = 1f; //背骨の硬さ
  246. solver.pullBodyVertical = 0.5f; //ボディエフェクター位置補正
  247. solver.pullBodyHorizontal = 0f;
  248. solver.spineMapping.twistWeight = 0f;
  249. foreach (var e in solver.effectors)
  250. {
  251. if (animStop)
  252. {
  253. e.positionWeight = 1f;
  254. e.rotationWeight = 0f;
  255. }
  256. else
  257. {
  258. e.PinToBone(1f, 0f);
  259. }
  260. var tgtname = e.target.gameObject.name;
  261. if (tgtlist.Contains(tgtname))
  262. {
  263. // COM3D2標準ターゲット
  264. e.target.transform.position = e.bone.position;
  265. e.target.transform.rotation = e.bone.rotation;
  266. }
  267. }
  268. solver.rightShoulderEffector.positionWeight = 0.95f;
  269. solver.leftShoulderEffector.positionWeight = 0.95f;
  270. solver.bodyEffector.rotationWeight = 1f;
  271. solver.rightThighEffector.positionWeight = 0.95f;
  272. solver.leftThighEffector.positionWeight = 0.95f;
  273. if (mscfg != null && mscfg.doFinalIKShoulderMove)
  274. {
  275. solver.rightShoulderEffector.positionWeight = 0f;
  276. solver.leftShoulderEffector.positionWeight = 0f;
  277. }
  278. if (mscfg != null && mscfg.doFinalIKThighMove)
  279. {
  280. solver.bodyEffector.rotationWeight = 0f;
  281. solver.rightThighEffector.positionWeight = 0f;
  282. solver.leftThighEffector.positionWeight = 0f;
  283. }
  284. foreach (var m in solver.limbMappings)
  285. {
  286. m.weight = 1f;
  287. m.maintainRotationWeight = 0f;
  288. }
  289. if (mscfg != null)
  290. {
  291. solver.rightLegMapping.weight = mscfg.fFinalIKLegWeight; //0.5f;
  292. solver.leftLegMapping.weight = mscfg.fFinalIKLegWeight; //0.5f;
  293. }
  294. solver.rightLegMapping.maintainRotationWeight = 1f;
  295. solver.leftLegMapping.maintainRotationWeight = 1f;
  296. }
  297. #region for TEST
  298. #if DEBUG
  299. private void IKInit4OldPoint(Maid slave)
  300. {
  301. var fik = slave.body0.IKCtrl.GetNonPublicField<RootMotion.FinalIK.FullBodyBipedIK>("m_FullbodyIK");
  302. RootMotion.FinalIK.FullBodyBipedIK FullbodyIK = fik;
  303. var solver = FullbodyIK.solver;
  304. string[] tgtlist = new string[] { "IKTarget", "BendBone", "ChainRootBone" };
  305. solver.spineStiffness = 1f; //背骨の硬さ
  306. solver.pullBodyVertical = 0f; //ボディエフェクター位置補正
  307. solver.pullBodyHorizontal = 0f;
  308. solver.spineMapping.twistWeight = 0f;
  309. foreach (var e in solver.effectors)
  310. {
  311. e.PinToBone(1f, 1f);
  312. var tgtname = e.target.gameObject.name;
  313. if (tgtlist.Contains(tgtname))
  314. {
  315. // COM3D2標準ターゲット
  316. e.target.transform.position = e.bone.position;
  317. e.target.transform.rotation = e.bone.rotation;
  318. }
  319. }
  320. foreach(var m in solver.limbMappings)
  321. {
  322. m.weight = 0f;
  323. m.maintainRotationWeight = 1f;
  324. }
  325. }
  326. private void IKInit2(Maid slave)
  327. {
  328. var fik = slave.body0.IKCtrl.GetNonPublicField<RootMotion.FinalIK.FullBodyBipedIK>("m_FullbodyIK");
  329. RootMotion.FinalIK.FullBodyBipedIK FullbodyIK = fik;
  330. var solver = FullbodyIK.solver;
  331. string[] tgtlist = new string[] { "IKTarget", "BendBone", "ChainRootBone" };
  332. string[] bendlist = new string[] { "BendBone", };
  333. /*
  334. #if DEBUG
  335. solver.spineStiffness = 1f; //背骨の硬さ
  336. //solver.rightLegMapping.weight = 0f;
  337. //solver.leftLegMapping.weight = 0f;
  338. if (!solver.bodyEffector.target || solver.bodyEffector.target.gameObject.name != bodytgt)
  339. solver.bodyEffector.target = new GameObject(bodytgt).transform;
  340. solver.bodyEffector.positionWeight = 1f;
  341. solver.bodyEffector.rotationWeight = 1f;
  342. solver.rightShoulderEffector.positionWeight = 0.95f;
  343. solver.leftShoulderEffector.positionWeight = 0.95f;
  344. solver.rightShoulderEffector.rotationWeight = 0.5f;
  345. solver.leftShoulderEffector.rotationWeight = 0.5f;
  346. solver.leftThighEffector.positionWeight = 1f;
  347. solver.leftThighEffector.rotationWeight = 0.5f;
  348. solver.rightThighEffector.positionWeight = 1f;
  349. solver.rightThighEffector.rotationWeight = 0.5f;
  350. solver.leftFootEffector.positionWeight = 1.0f;
  351. solver.rightFootEffector.positionWeight = 1.0f;
  352. if (!solver.rightThighEffector.target || solver.rightThighEffector.target.gameObject.name != bodytgt)
  353. solver.rightThighEffector.target = new GameObject(bodytgt).transform;
  354. if (!solver.leftThighEffector.target || solver.leftThighEffector.target.gameObject.name != bodytgt)
  355. solver.leftThighEffector.target = new GameObject(bodytgt).transform;
  356. if (!solver.rightFootEffector.target || solver.rightFootEffector.target.gameObject.name != bodytgt)
  357. solver.rightFootEffector.target = new GameObject(bodytgt).transform;
  358. if (!solver.leftFootEffector.target || solver.leftFootEffector.target.gameObject.name != bodytgt)
  359. solver.leftFootEffector.target = new GameObject(bodytgt).transform;
  360. solver.rightLegMapping.weight = 0.2f;
  361. solver.leftLegMapping.weight = 0.2f;
  362. //Sync(solver.leftArmChain.bendConstraint.bendGoal.transform, solver.leftArmMapping.bone2);
  363. //Sync(solver.rightArmChain.bendConstraint.bendGoal.transform, solver.rightArmMapping.bone2);
  364. foreach (var e in solver.effectors)
  365. {
  366. Sync(e);
  367. var tgtname = e.target.gameObject.name;
  368. if (tgtlist.Contains(tgtname) || tgtname == bodytgt)
  369. {
  370. // COM3D2標準ターゲット
  371. e.target.transform.position = e.bone.position;
  372. e.target.transform.rotation = e.bone.rotation;
  373. }
  374. else if (bendlist.Contains(tgtname))
  375. {
  376. // COM3D2標準ターゲット
  377. e.target.transform.position = e.bone.position;
  378. e.target.transform.rotation = e.bone.rotation;
  379. }
  380. }
  381. return;
  382. #endif
  383. */
  384. solver.spineStiffness = 1f; //背骨の硬さ
  385. solver.pullBodyVertical = 0.5f; //ボディエフェクター位置補正
  386. solver.pullBodyHorizontal = 0f;
  387. foreach (var e in solver.effectors)
  388. {
  389. e.PinToBone(1f, 0f);
  390. #if DEBUG
  391. e.PinToBone(1f, 1f);
  392. #endif
  393. var tgtname = e.target.gameObject.name;
  394. if (tgtlist.Contains(tgtname))
  395. {
  396. // COM3D2標準ターゲット
  397. e.target.transform.position = e.bone.position;
  398. e.target.transform.rotation = e.bone.rotation;
  399. }
  400. }
  401. solver.rightShoulderEffector.positionWeight = 0.95f;
  402. solver.leftShoulderEffector.positionWeight = 0.95f;
  403. solver.bodyEffector.rotationWeight = 1f;
  404. solver.rightLegMapping.maintainRotationWeight = 0f;
  405. solver.leftLegMapping.maintainRotationWeight = 0f;
  406. solver.rightLegMapping.weight = 0f;
  407. solver.leftLegMapping.weight = 0f;
  408. solver.rightArmMapping.maintainRotationWeight = 0f;
  409. solver.leftArmMapping.maintainRotationWeight = 0f;
  410. solver.rightArmMapping.weight = 1f;
  411. solver.leftArmMapping.weight = 1f;
  412. }
  413. private void IKInitTest(Maid slave, string handName)
  414. {
  415. var fik = slave.body0.IKCtrl.GetNonPublicField<RootMotion.FinalIK.FullBodyBipedIK>("m_FullbodyIK");
  416. RootMotion.FinalIK.FullBodyBipedIK FullbodyIK = fik;
  417. var solver = FullbodyIK.solver;
  418. if (handName.Contains("右"))
  419. {
  420. //solver.rightLegMapping.weight = 0f;
  421. //solver.spineMapping.twistWeight = 0f;
  422. //m_FullbodyIK.references.root = slave.gameObject.transform;
  423. //solver.SetToReferences(FullbodyIK.references, null);
  424. solver.spineStiffness = 1f;
  425. solver.pullBodyVertical = 0f;
  426. solver.pullBodyHorizontal = 0f;
  427. if (Input.GetKey(KeyCode.LeftAlt))
  428. {
  429. solver.spineStiffness = 0f;
  430. solver.pullBodyVertical = 1f;
  431. }
  432. foreach (var e in solver.effectors)
  433. {
  434. if (Input.GetKey(KeyCode.RightAlt))
  435. e.PinToBone(0.25f, 1f);
  436. else if (Input.GetKey(KeyCode.RightShift))
  437. e.PinToBone(0.5f, 0f);
  438. else if (Input.GetKey(KeyCode.RightControl))
  439. e.PinToBone(0f, 1f);
  440. else
  441. e.PinToBone(1f, 1f);
  442. }
  443. solver.rightLegMapping.maintainRotationWeight = 1f;
  444. solver.leftLegMapping.maintainRotationWeight = 1f;
  445. if (Input.GetKey(KeyCode.Space))
  446. solver.rightLegMapping.weight = 1f;
  447. else if (Input.GetKey(KeyCode.Keypad0))
  448. solver.rightLegMapping.weight = 0f;
  449. else
  450. solver.rightLegMapping.weight = 0.5f;
  451. /*Sync(solver.bodyEffector);
  452. Sync(solver.rightFootEffector);
  453. Sync(solver.rightThighEffector);
  454. Sync(solver.rightShoulderEffector);
  455. WeightZero(solver.rightFootEffector);
  456. WeightZero(solver.rightThighEffector);
  457. WeightZero(solver.rightShoulderEffector);*/
  458. }
  459. else
  460. {
  461. solver.leftLegMapping.maintainRotationWeight = 1f;
  462. if (Input.GetKey(KeyCode.Space))
  463. solver.leftLegMapping.weight = 1f;
  464. else if (Input.GetKey(KeyCode.Keypad0))
  465. solver.leftLegMapping.weight = 0f;
  466. else
  467. solver.leftLegMapping.weight = 0.5f;
  468. /*Sync(solver.leftFootEffector);
  469. Sync(solver.leftThighEffector);
  470. Sync(solver.leftShoulderEffector);
  471. WeightZero(solver.leftFootEffector);
  472. WeightZero(solver.leftThighEffector);
  473. WeightZero(solver.leftShoulderEffector);*/
  474. }
  475. }
  476. #endif
  477. #endregion
  478. private static void Sync(Transform tr1, Transform tr2)
  479. {
  480. tr1.position = tr2.position;
  481. tr1.rotation = tr2.rotation;
  482. }
  483. private static void Sync(RootMotion.FinalIK.IKEffector eff)
  484. {
  485. eff.position = eff.bone.position;
  486. eff.rotation = eff.bone.rotation;
  487. /*eff.target.position = eff.bone.position;
  488. eff.target.rotation = eff.bone.rotation;
  489. eff.PinToBone(1f, 1f);*/
  490. }
  491. private static void WeightZero(RootMotion.FinalIK.IKEffector eff)
  492. {
  493. eff.positionWeight = 0f;
  494. eff.rotationWeight = 0f;
  495. }
  496. public override object GetIKCmo(TBody body, string hand = "右手")
  497. {
  498. return body.IKCtrl.GetIKData(hand, IKBend).IKCmo;
  499. /*
  500. if (hand == "右手")
  501. return body.IKCtrl.GetIKData("右手").IKCmo;
  502. else
  503. return body.IKCtrl.GetIKData("左手").IKCmo;
  504. */
  505. }
  506. public override bool IKUpdate(TBody body)
  507. {
  508. body.IKCtrl.IKUpdate();
  509. return true;
  510. }
  511. public override bool GetIKCmoPosRot(TBody body, out Vector3 pos, out Quaternion rot, string hand = "右手")
  512. {
  513. var ctrl = body.IKCtrl.GetIKData(hand, IKBend);
  514. bool proc = false;
  515. pos = Vector3.zero;
  516. rot = Quaternion.identity;
  517. var data = ctrl.GetIKSettingData(IKCtrlData.IKAttachType.Point);
  518. if (data.curTargetData.Target != null)
  519. {
  520. pos = data.curTargetData.Target.position;
  521. rot = data.curTargetData.Target.rotation;
  522. proc = true;
  523. }
  524. else if (data.curTargetData.Tgt_AttachName != string.Empty)
  525. {
  526. if (data.curTargetData.TgtChara != null && data.curTargetData.TgtChara.body0 != null && data.curTargetData.Tgt_AttachSlot >= 0 && data.curTargetData.TgtChara.body0.goSlot[data.curTargetData.Tgt_AttachSlot].morph != null)
  527. {
  528. Vector3 vector;
  529. data.curTargetData.TgtChara.body0.goSlot[data.curTargetData.Tgt_AttachSlot].morph.GetAttachPoint(data.curTargetData.Tgt_AttachName, out pos, out rot, out vector, false);
  530. proc = true;
  531. }
  532. else
  533. {
  534. data.curTargetData.Tgt_AttachName = string.Empty;
  535. }
  536. }
  537. return proc;
  538. }
  539. public override bool IKCmoUpdate(TBody body, Transform trh, Vector3 offset, string hand = "右手")
  540. {
  541. var ctrl = body.IKCtrl.GetIKData(hand, IKBend);
  542. /*ctrl.MyIKCtrl.GetType().GetProperty("IsUpdateEnd").SetValue(ctrl.MyIKCtrl, true, null);
  543. ctrl.MyIKCtrl.IsUpdateLate = false;
  544. ctrl.ApplyIKSetting();
  545. */
  546. Vector3 pos = Vector3.zero;
  547. Quaternion rot = Quaternion.identity;
  548. bool proc = GetIKCmoPosRot(body, out pos, out rot, hand);
  549. if (proc)
  550. {
  551. ctrl.IKCmo.Porc(trh.parent.parent, trh.parent, trh, pos, rot * offset, ctrl);
  552. return true;
  553. }
  554. return false;
  555. }
  556. public override bool UpdateFinalIK(Maid maid, XtMasterSlave.MsLinks ms, XtMasterSlave.MsLinkConfig mscfg)
  557. {
  558. if (!maid || !maid.body0)
  559. return false;
  560. needInit = false;
  561. if (mscfg.doIK159NewPointToDef)
  562. IKInit(maid, ms, mscfg);
  563. #if DEBUG
  564. else
  565. IKInit4OldPoint(maid);
  566. #endif
  567. return true; // 実行できたか
  568. }
  569. }
  570. #endif
  571. }
  572. public static class Extentions
  573. {
  574. public static T GetNonPublicField<T>(this object obj, string name)
  575. {
  576. var ret = obj.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic).GetValue(obj);
  577. if (ret is T)
  578. return (T)ret;
  579. return default(T);
  580. }
  581. }