AMOrientationTrack.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [Serializable]
  5. public class AMOrientationTrack : AMTrack
  6. {
  7. public override string getTrackType()
  8. {
  9. return "Orientation";
  10. }
  11. public bool setObject(Transform obj)
  12. {
  13. if (this.obj != obj)
  14. {
  15. this.obj = obj;
  16. return true;
  17. }
  18. return false;
  19. }
  20. public void addKey(int _frame, Transform target)
  21. {
  22. foreach (AMKey amkey in this.keys)
  23. {
  24. AMOrientationKey amorientationKey = (AMOrientationKey)amkey;
  25. if (amorientationKey.frame == _frame)
  26. {
  27. amorientationKey.target = target;
  28. this.updateCache();
  29. return;
  30. }
  31. }
  32. AMOrientationKey amorientationKey2 = ScriptableObject.CreateInstance<AMOrientationKey>();
  33. amorientationKey2.frame = _frame;
  34. amorientationKey2.target = target;
  35. amorientationKey2.easeType = 21;
  36. this.keys.Add(amorientationKey2);
  37. this.updateCache();
  38. }
  39. public override void updateCache()
  40. {
  41. this.updateOrientationCache(this.parentTake, false);
  42. base.updateCache();
  43. }
  44. public void updateOrientationCache(AMTake curTake, bool restoreRotation = false)
  45. {
  46. Quaternion rotation = this.obj.rotation;
  47. base.sortKeys();
  48. base.destroyCache();
  49. this.cache = new List<AMAction>();
  50. AMTranslationTrack translationTrackForTransform = curTake.getTranslationTrackForTransform(this.obj);
  51. for (int i = 0; i < this.keys.Count; i++)
  52. {
  53. AMOrientationAction amorientationAction = ScriptableObject.CreateInstance<AMOrientationAction>();
  54. amorientationAction.startFrame = this.keys[i].frame;
  55. if (this.keys.Count > i + 1)
  56. {
  57. amorientationAction.endFrame = this.keys[i + 1].frame;
  58. }
  59. else
  60. {
  61. amorientationAction.endFrame = -1;
  62. }
  63. amorientationAction.obj = this.obj;
  64. amorientationAction.startTarget = (this.keys[i] as AMOrientationKey).target;
  65. if (amorientationAction.endFrame != -1)
  66. {
  67. amorientationAction.endTarget = (this.keys[i + 1] as AMOrientationKey).target;
  68. }
  69. amorientationAction.easeType = (this.keys[i] as AMOrientationKey).easeType;
  70. amorientationAction.customEase = new List<float>(this.keys[i].customEase);
  71. if (translationTrackForTransform != null && !amorientationAction.isLookFollow())
  72. {
  73. amorientationAction.isSetStartPosition = true;
  74. amorientationAction.startPosition = translationTrackForTransform.getPositionAtFrame((float)amorientationAction.startFrame);
  75. amorientationAction.isSetEndPosition = true;
  76. amorientationAction.endPosition = translationTrackForTransform.getPositionAtFrame((float)amorientationAction.endFrame);
  77. }
  78. this.cache.Add(amorientationAction);
  79. }
  80. if (restoreRotation)
  81. {
  82. this.obj.rotation = rotation;
  83. }
  84. }
  85. public Transform getInitialTarget()
  86. {
  87. return (this.keys[0] as AMOrientationKey).target;
  88. }
  89. public override void previewFrame(float frame, AMTrack extraTrack = null)
  90. {
  91. if (this.cache == null || this.cache.Count <= 0)
  92. {
  93. return;
  94. }
  95. int i = 0;
  96. while (i < this.cache.Count)
  97. {
  98. if (frame <= (float)(this.cache[i] as AMOrientationAction).startFrame)
  99. {
  100. if (!(this.cache[i] as AMOrientationAction).startTarget)
  101. {
  102. return;
  103. }
  104. Vector3 worldPosition;
  105. if (this.cachedTranslationTrackStartTarget == null)
  106. {
  107. worldPosition = (this.cache[i] as AMOrientationAction).startTarget.position;
  108. }
  109. else
  110. {
  111. worldPosition = (this.cachedTranslationTrackStartTarget as AMTranslationTrack).getPositionAtFrame((float)(this.cache[i] as AMOrientationAction).startFrame);
  112. }
  113. this.obj.LookAt(worldPosition);
  114. return;
  115. }
  116. else if (frame <= (float)(this.cache[i] as AMOrientationAction).endFrame)
  117. {
  118. if (!(this.cache[i] as AMOrientationAction).startTarget || !(this.cache[i] as AMOrientationAction).endTarget)
  119. {
  120. return;
  121. }
  122. float num = frame - (float)this.cache[i].startFrame;
  123. if (num < 0f)
  124. {
  125. num = 0f;
  126. }
  127. float percentage = num / (float)this.cache[i].getNumberOfFrames();
  128. if ((this.cache[i] as AMOrientationAction).isLookFollow())
  129. {
  130. this.obj.rotation = (this.cache[i] as AMOrientationAction).getQuaternionAtPercent(percentage, null, null);
  131. }
  132. else
  133. {
  134. Vector3? startVector = (!(this.cachedTranslationTrackStartTarget == null)) ? new Vector3?((this.cachedTranslationTrackStartTarget as AMTranslationTrack).getPositionAtFrame((float)(this.cache[i] as AMOrientationAction).startFrame)) : null;
  135. Vector3? endVector = (!(this.cachedTranslationTrackEndTarget == null)) ? new Vector3?((this.cachedTranslationTrackEndTarget as AMTranslationTrack).getPositionAtFrame((float)(this.cache[i] as AMOrientationAction).endFrame)) : null;
  136. this.obj.rotation = (this.cache[i] as AMOrientationAction).getQuaternionAtPercent(percentage, startVector, endVector);
  137. }
  138. return;
  139. }
  140. else if (i == this.cache.Count - 2)
  141. {
  142. if (!(this.cache[i] as AMOrientationAction).endTarget)
  143. {
  144. return;
  145. }
  146. Vector3 worldPosition2;
  147. if (this.cachedTranslationTrackEndTarget == null)
  148. {
  149. worldPosition2 = (this.cache[i] as AMOrientationAction).endTarget.position;
  150. }
  151. else
  152. {
  153. worldPosition2 = (this.cachedTranslationTrackEndTarget as AMTranslationTrack).getPositionAtFrame((float)(this.cache[i] as AMOrientationAction).endFrame);
  154. }
  155. this.obj.LookAt(worldPosition2);
  156. return;
  157. }
  158. else
  159. {
  160. i++;
  161. }
  162. }
  163. }
  164. public Transform getStartTargetForFrame(float frame)
  165. {
  166. foreach (AMAction amaction in this.cache)
  167. {
  168. AMOrientationAction amorientationAction = (AMOrientationAction)amaction;
  169. if ((int)frame <= amorientationAction.endFrame)
  170. {
  171. return amorientationAction.startTarget;
  172. }
  173. }
  174. return null;
  175. }
  176. public Transform getEndTargetForFrame(float frame)
  177. {
  178. if (this.cache.Count > 1)
  179. {
  180. return (this.cache[this.cache.Count - 2] as AMOrientationAction).endTarget;
  181. }
  182. return null;
  183. }
  184. public Transform getTargetForFrame(float frame)
  185. {
  186. if (this.isFrameBeyondLastKeyFrame(frame))
  187. {
  188. return this.getEndTargetForFrame(frame);
  189. }
  190. return this.getStartTargetForFrame(frame);
  191. }
  192. public void drawGizmos(float gizmo_size, bool inPlayMode, int frame)
  193. {
  194. if (!this.obj)
  195. {
  196. return;
  197. }
  198. if (!inPlayMode)
  199. {
  200. foreach (AMAction amaction in this.cache)
  201. {
  202. if ((amaction as AMOrientationAction).startFrame > frame)
  203. {
  204. break;
  205. }
  206. if (frame >= (amaction as AMOrientationAction).startFrame && frame <= (amaction as AMOrientationAction).endFrame)
  207. {
  208. if ((amaction as AMOrientationAction).isLookFollow() && (amaction as AMOrientationAction).startTarget)
  209. {
  210. Gizmos.color = new Color(0.9607843f, 0.419607848f, 0.117647059f, 0.2f);
  211. Gizmos.DrawLine(this.obj.transform.position, (amaction as AMOrientationAction).startTarget.transform.position);
  212. }
  213. break;
  214. }
  215. }
  216. }
  217. Gizmos.color = new Color(0.9607843f, 0.419607848f, 0.117647059f, 1f);
  218. Vector3 position = this.obj.transform.position;
  219. float num = 1.2f * (gizmo_size / 0.1f);
  220. if (num < 0.1f)
  221. {
  222. num = 0.1f;
  223. }
  224. Vector3 vector = this.obj.forward * num;
  225. float num2 = 20f;
  226. float d = 0.3f * num;
  227. Gizmos.DrawRay(position, vector);
  228. Vector3 a = Quaternion.LookRotation(vector) * Quaternion.Euler(0f, 180f + num2, 0f) * new Vector3(0f, 0f, 1f);
  229. Vector3 a2 = Quaternion.LookRotation(vector) * Quaternion.Euler(0f, 180f - num2, 0f) * new Vector3(0f, 0f, 1f);
  230. Gizmos.DrawRay(position + vector, a * d);
  231. Gizmos.DrawRay(position + vector, a2 * d);
  232. }
  233. public bool isFrameBeyondLastKeyFrame(float frame)
  234. {
  235. return this.keys.Count > 0 && (int)frame > this.keys[this.keys.Count - 1].frame;
  236. }
  237. public bool hasTarget(Transform obj)
  238. {
  239. foreach (AMAction amaction in this.cache)
  240. {
  241. AMOrientationAction amorientationAction = (AMOrientationAction)amaction;
  242. if (amorientationAction.startTarget == obj || amorientationAction.endTarget == obj)
  243. {
  244. return true;
  245. }
  246. }
  247. return false;
  248. }
  249. public override AnimatorTimeline.JSONInit getJSONInit()
  250. {
  251. if (!this.obj || this.keys.Count <= 0)
  252. {
  253. return null;
  254. }
  255. AnimatorTimeline.JSONInit jsoninit = new AnimatorTimeline.JSONInit();
  256. jsoninit.type = "orientation";
  257. jsoninit.go = this.obj.gameObject.name;
  258. Transform initialTarget = this.getInitialTarget();
  259. int frame = this.keys[0].frame;
  260. AMTrack amtrack = null;
  261. if (frame > 0)
  262. {
  263. amtrack = this.parentTake.getTranslationTrackForTransform(initialTarget);
  264. }
  265. Vector3 value = initialTarget.transform.position;
  266. if (amtrack)
  267. {
  268. value = (amtrack as AMTranslationTrack).getPositionAtFrame((float)frame);
  269. }
  270. AnimatorTimeline.JSONVector3 jsonvector = new AnimatorTimeline.JSONVector3();
  271. jsonvector.setValue(value);
  272. jsoninit.position = jsonvector;
  273. return jsoninit;
  274. }
  275. public override List<GameObject> getDependencies()
  276. {
  277. List<GameObject> list = new List<GameObject>();
  278. if (this.obj)
  279. {
  280. list.Add(this.obj.gameObject);
  281. }
  282. foreach (AMKey amkey in this.keys)
  283. {
  284. AMOrientationKey amorientationKey = (AMOrientationKey)amkey;
  285. if (amorientationKey.target)
  286. {
  287. list.Add(amorientationKey.target.gameObject);
  288. }
  289. }
  290. return list;
  291. }
  292. public override List<GameObject> updateDependencies(List<GameObject> newReferences, List<GameObject> oldReferences)
  293. {
  294. bool flag = false;
  295. for (int i = 0; i < oldReferences.Count; i++)
  296. {
  297. if (!flag && this.obj && oldReferences[i] == this.obj.gameObject)
  298. {
  299. this.obj = newReferences[i].transform;
  300. flag = true;
  301. }
  302. foreach (AMKey amkey in this.keys)
  303. {
  304. AMOrientationKey amorientationKey = (AMOrientationKey)amkey;
  305. if (amorientationKey.target && oldReferences[i] == amorientationKey.target.gameObject)
  306. {
  307. amorientationKey.target = newReferences[i].transform;
  308. }
  309. }
  310. }
  311. return new List<GameObject>();
  312. }
  313. public Transform obj;
  314. public AMTrack cachedTranslationTrackStartTarget;
  315. public AMTrack cachedTranslationTrackEndTarget;
  316. }