IKMgrData.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [Serializable]
  5. public class IKMgrData
  6. {
  7. public Transform KinematicTgt
  8. {
  9. get
  10. {
  11. return this.m_KinematicTgt;
  12. }
  13. }
  14. public IKMgrData.IKParam PointIK
  15. {
  16. get
  17. {
  18. return this.m_PointIK;
  19. }
  20. }
  21. public IKMgrData.IKParam RotateIK
  22. {
  23. get
  24. {
  25. return this.m_RotateIK;
  26. }
  27. }
  28. public TBody.IKCMO IKCmo
  29. {
  30. get
  31. {
  32. return this.m_IKCmo;
  33. }
  34. }
  35. public bool IsIkExec
  36. {
  37. get
  38. {
  39. return this.PointIK.IsIKExec || this.RotateIK.IsIKExec;
  40. }
  41. }
  42. private void IkCmoPorc(Vector3 tgt, Vector3 vechand_offset)
  43. {
  44. if (this.m_KinematicList.Count < 3)
  45. {
  46. return;
  47. }
  48. for (int i = 0; i < 3; i++)
  49. {
  50. this.IKCmo.Porc(this.m_KinematicList[2], this.m_KinematicList[1], this.m_KinematicList[0], tgt, vechand_offset);
  51. }
  52. }
  53. private void TransratePoint(Vector3 tgt_pos, Quaternion tgt_rot, Vector3 tgt_offset)
  54. {
  55. Vector3 vector = tgt_pos + tgt_rot * tgt_offset;
  56. Debug.DrawLine(tgt_pos, vector, Color.white);
  57. for (int i = 0; i < 5; i++)
  58. {
  59. Vector3 a = vector;
  60. if (this.m_AttachPointData != null)
  61. {
  62. Vector3 normalized = (this.KinematicTgt.position - this.KinematicTgt.parent.position).normalized;
  63. float d = Vector3.Dot(this.m_AttachPointData.AttachTrans.position - this.KinematicTgt.position, normalized);
  64. a += this.KinematicTgt.position + normalized * d - this.m_AttachPointData.AttachTrans.position;
  65. }
  66. for (int j = 1; j < this.m_KinematicList.Count; j++)
  67. {
  68. bool flag = j + 1 < this.m_KinematicList.Count;
  69. Vector3 vector2 = Vector3.zero;
  70. if (flag)
  71. {
  72. Quaternion rotation = Quaternion.FromToRotation(this.m_KinematicList[j - 1].position - this.m_KinematicList[j].position, a - this.m_KinematicList[j].position) * this.m_KinematicList[j].rotation;
  73. Vector3 point = this.m_KinematicList[j].InverseTransformPoint(this.m_KinematicList[j - 1].position);
  74. Vector3 b = this.m_KinematicList[j].position + rotation * point;
  75. vector2 = this.m_KinematicList[j].position + (a - b);
  76. this.m_KinematicList[j + 1].rotation = Quaternion.FromToRotation(this.m_KinematicList[j].position - this.m_KinematicList[j + 1].position, vector2 - this.m_KinematicList[j + 1].position) * this.m_KinematicList[j + 1].rotation;
  77. }
  78. this.m_KinematicList[j].rotation = Quaternion.FromToRotation(this.m_KinematicList[j - 1].position - this.m_KinematicList[j].position, a - this.m_KinematicList[j].position) * this.m_KinematicList[j].rotation;
  79. if (flag)
  80. {
  81. a = vector2;
  82. }
  83. }
  84. for (int k = this.m_KinematicList.Count - 1; k > 0; k--)
  85. {
  86. Debug.DrawLine(this.m_KinematicList[k].position, this.m_KinematicList[k - 1].position, Color.Lerp(Color.red, Color.yellow, (float)i / 4f));
  87. }
  88. if (this.m_AttachPointData != null)
  89. {
  90. Debug.DrawLine(this.KinematicTgt.position, this.m_AttachPointData.AttachTrans.position, Color.Lerp(Color.red, Color.yellow, (float)i / 4f));
  91. }
  92. }
  93. }
  94. private void TransratePoint(Transform tgt, Vector3 tgt_offset)
  95. {
  96. this.TransratePoint(tgt.position, tgt.rotation, tgt_offset);
  97. }
  98. private void ConstraintRot(Quaternion tgt_rot, Vector3 tgt_offset)
  99. {
  100. this.KinematicTgt.rotation = tgt_rot * Quaternion.Euler(tgt_offset);
  101. }
  102. private void ApplyIKSetting(IKMgrData.IKParam data, List<TBodySkin> goSlot)
  103. {
  104. data.IsIKExec = true;
  105. if (data.Target != null)
  106. {
  107. Vector3 position = data.Target.position;
  108. Quaternion rotation = data.Target.rotation;
  109. switch (data.MyType)
  110. {
  111. case IKMgrData.IKAttachType.Point:
  112. Debug.DrawLine(position, position + rotation * Vector3.forward * 0.2f, Color.cyan);
  113. this.IkCmoPorc(position, data.TgtOffset);
  114. break;
  115. case IKMgrData.IKAttachType.Rotate:
  116. this.ConstraintRot(rotation, data.TgtOffset);
  117. break;
  118. case IKMgrData.IKAttachType.NewPoint:
  119. this.TransratePoint(data.Target, data.TgtOffset);
  120. break;
  121. }
  122. if (data.IsTgtAxis)
  123. {
  124. KasaiUtility.DrawAxis(position, rotation, 0.125f);
  125. }
  126. }
  127. else if (data.Tgt_AttachName != string.Empty)
  128. {
  129. if (goSlot[data.Tgt_AttachSlot].morph != null)
  130. {
  131. if (data.TgtMaid != null && data.TgtMaid.body0 != null)
  132. {
  133. Vector3 vector;
  134. Quaternion rotation2;
  135. Vector3 vector2;
  136. data.TgtMaid.body0.goSlot[data.Tgt_AttachSlot].morph.GetAttachPoint(data.Tgt_AttachName, out vector, out rotation2, out vector2, false);
  137. switch (data.MyType)
  138. {
  139. case IKMgrData.IKAttachType.Point:
  140. Debug.DrawLine(vector, vector + rotation2 * Vector3.forward * 0.2f, Color.blue);
  141. this.IkCmoPorc(vector, data.TgtOffset);
  142. break;
  143. case IKMgrData.IKAttachType.Rotate:
  144. this.ConstraintRot(rotation2, data.TgtOffset);
  145. break;
  146. case IKMgrData.IKAttachType.NewPoint:
  147. if (data.AxisTgt)
  148. {
  149. rotation2 = data.AxisTgt.rotation;
  150. }
  151. this.TransratePoint(vector, rotation2, data.TgtOffset);
  152. break;
  153. }
  154. if (data.IsTgtAxis)
  155. {
  156. KasaiUtility.DrawAxis(vector, rotation2, 0.125f);
  157. }
  158. }
  159. else
  160. {
  161. data.IsIKExec = false;
  162. data.Tgt_AttachName = string.Empty;
  163. }
  164. }
  165. }
  166. else
  167. {
  168. data.IsIKExec = false;
  169. }
  170. }
  171. public void SetKinematicTgt(Transform tgt, int weight = 3)
  172. {
  173. this.m_KinematicTgt = tgt;
  174. this.m_KinematicList.Clear();
  175. int num = 0;
  176. Transform transform = tgt;
  177. while (num != weight)
  178. {
  179. this.m_KinematicList.Add(transform);
  180. Transform parent = transform.parent;
  181. if (!parent)
  182. {
  183. Debug.LogWarningFormat("IKのウェイトが大きすぎます:{0}", new object[]
  184. {
  185. weight
  186. });
  187. break;
  188. }
  189. transform = parent;
  190. num++;
  191. }
  192. }
  193. public void IkCmoInit(TBody body)
  194. {
  195. if (this.m_KinematicList.Count < 3)
  196. {
  197. return;
  198. }
  199. this.IKCmo.Init(this.m_KinematicList[2], this.m_KinematicList[1], this.m_KinematicList[0], body);
  200. }
  201. public IKMgrData.IKParam GetIKParam(IKMgrData.IKAttachType attach_type)
  202. {
  203. if (attach_type == IKMgrData.IKAttachType.Rotate)
  204. {
  205. return this.RotateIK;
  206. }
  207. return this.PointIK;
  208. }
  209. public void IKExec(List<TBodySkin> goSlot)
  210. {
  211. this.ApplyIKSetting(this.PointIK, goSlot);
  212. this.ApplyIKSetting(this.RotateIK, goSlot);
  213. if (this.IsIkExec)
  214. {
  215. if (this.m_AttachPointData != null)
  216. {
  217. KasaiUtility.DrawObjAxis(this.m_AttachPointData.AttachTrans, 0.0625f);
  218. }
  219. else
  220. {
  221. KasaiUtility.DrawObjAxis(this.KinematicTgt, 0.0625f);
  222. }
  223. if (!this.PointIK.IsIKExec)
  224. {
  225. for (int i = this.m_KinematicList.Count - 1; i > 0; i--)
  226. {
  227. Debug.DrawLine(this.m_KinematicList[i].position, this.m_KinematicList[i - 1].position, Color.cyan);
  228. }
  229. if (this.m_AttachPointData != null)
  230. {
  231. Debug.DrawLine(this.KinematicTgt.position, this.m_AttachPointData.AttachTrans.position, Color.cyan);
  232. }
  233. }
  234. }
  235. }
  236. public Transform CreateAttachPointObj(string obj_name, Transform parent, TBody body, string slot_name, string attach_name)
  237. {
  238. if (this.m_AttachPointData != null && this.m_AttachPointData.AttachTrans)
  239. {
  240. return this.m_AttachPointData.AttachTrans;
  241. }
  242. GameObject gameObject = new GameObject(obj_name);
  243. gameObject.transform.SetParent(parent, false);
  244. this.m_AttachPointData = new IKMgrData.AttachPointIKData();
  245. this.m_AttachPointData.AttachTrans = gameObject.transform;
  246. this.m_AttachPointData.Tbody = body;
  247. this.m_AttachPointData.AttachName = attach_name;
  248. this.m_AttachPointData.SlotName = slot_name;
  249. return gameObject.transform;
  250. }
  251. public void SyncAttachPoint()
  252. {
  253. if (this.m_AttachPointData == null || !this.m_AttachPointData.AttachTrans)
  254. {
  255. return;
  256. }
  257. int slotNo = this.m_AttachPointData.Tbody.GetSlotNo(this.m_AttachPointData.SlotName);
  258. if (slotNo < 0 || slotNo >= this.m_AttachPointData.Tbody.goSlot.Count)
  259. {
  260. Debug.LogErrorFormat("スロット{0}は存在しません", new object[]
  261. {
  262. this.m_AttachPointData.SlotName
  263. });
  264. return;
  265. }
  266. Vector3 position;
  267. Quaternion rotation;
  268. Vector3 localScale;
  269. this.m_AttachPointData.Tbody.goSlot[slotNo].morph.GetAttachPoint(this.m_AttachPointData.AttachName, out position, out rotation, out localScale, false);
  270. this.m_AttachPointData.AttachTrans.position = position;
  271. this.m_AttachPointData.AttachTrans.rotation = rotation;
  272. this.m_AttachPointData.AttachTrans.localScale = localScale;
  273. }
  274. private const int m_CorrectNum = 5;
  275. [SerializeField]
  276. private List<Transform> m_KinematicList = new List<Transform>();
  277. [SerializeField]
  278. private Transform m_KinematicTgt;
  279. [SerializeField]
  280. private IKMgrData.IKParam m_PointIK = new IKMgrData.IKParam(IKMgrData.IKAttachType.NewPoint);
  281. [SerializeField]
  282. private IKMgrData.IKParam m_RotateIK = new IKMgrData.IKParam(IKMgrData.IKAttachType.Rotate);
  283. private TBody.IKCMO m_IKCmo = new TBody.IKCMO();
  284. private IKMgrData.AttachPointIKData m_AttachPointData;
  285. public enum IKAttachType
  286. {
  287. Point,
  288. Rotate,
  289. NewPoint
  290. }
  291. [Serializable]
  292. public class IKParam
  293. {
  294. public IKParam(IKMgrData.IKAttachType type)
  295. {
  296. this.MyType = type;
  297. }
  298. public IKMgrData.IKAttachType MyType { get; private set; }
  299. public void ChangePointType(IKMgrData.IKAttachType type)
  300. {
  301. if (this.MyType == IKMgrData.IKAttachType.Rotate)
  302. {
  303. return;
  304. }
  305. this.MyType = type;
  306. }
  307. public bool IsPointAttach
  308. {
  309. get
  310. {
  311. return this.MyType != IKMgrData.IKAttachType.Rotate;
  312. }
  313. }
  314. public Transform Target;
  315. public Vector3 TgtOffset;
  316. public int Tgt_AttachSlot = -1;
  317. public string Tgt_AttachName = string.Empty;
  318. public Maid TgtMaid;
  319. public bool IsTgtAxis;
  320. public bool IsIKExec;
  321. public Transform AxisTgt;
  322. }
  323. private class AttachPointIKData
  324. {
  325. public Transform AttachTrans;
  326. public TBody Tbody;
  327. public string SlotName = string.Empty;
  328. public string AttachName = string.Empty;
  329. }
  330. }