AMCameraSwitcherTrack.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using UnityEngine;
  6. [Serializable]
  7. public class AMCameraSwitcherTrack : AMTrack
  8. {
  9. public Camera[] cachedAllCameras
  10. {
  11. get
  12. {
  13. if (this._cachedAllCameras == null)
  14. {
  15. this._cachedAllCameras = this.getAllCameras();
  16. }
  17. return this._cachedAllCameras;
  18. }
  19. set
  20. {
  21. this._cachedAllCameras = value;
  22. }
  23. }
  24. public override string getTrackType()
  25. {
  26. return "Camera Switcher";
  27. }
  28. public void addKey(int _frame, Camera camera = null, AMCameraSwitcherKey keyToClone = null)
  29. {
  30. foreach (AMKey amkey in this.keys)
  31. {
  32. AMCameraSwitcherKey amcameraSwitcherKey = (AMCameraSwitcherKey)amkey;
  33. if (amcameraSwitcherKey.frame == _frame)
  34. {
  35. if (camera != null)
  36. {
  37. amcameraSwitcherKey.camera = camera;
  38. amcameraSwitcherKey.type = 0;
  39. this.updateCache();
  40. }
  41. return;
  42. }
  43. }
  44. AMCameraSwitcherKey amcameraSwitcherKey2 = ScriptableObject.CreateInstance<AMCameraSwitcherKey>();
  45. if (keyToClone)
  46. {
  47. amcameraSwitcherKey2 = (AMCameraSwitcherKey)keyToClone.CreateClone();
  48. }
  49. else
  50. {
  51. amcameraSwitcherKey2.type = 0;
  52. amcameraSwitcherKey2.still = !AMTake.isProLicense;
  53. amcameraSwitcherKey2.easeType = 13;
  54. }
  55. amcameraSwitcherKey2.frame = _frame;
  56. if (camera != null)
  57. {
  58. amcameraSwitcherKey2.camera = camera;
  59. amcameraSwitcherKey2.type = 0;
  60. }
  61. this.keys.Add(amcameraSwitcherKey2);
  62. this.updateCache();
  63. }
  64. public override void updateCache()
  65. {
  66. base.destroyCache();
  67. this.cache = new List<AMAction>();
  68. base.sortKeys();
  69. for (int i = 0; i < this.keys.Count; i++)
  70. {
  71. AMCameraSwitcherAction amcameraSwitcherAction = ScriptableObject.CreateInstance<AMCameraSwitcherAction>();
  72. amcameraSwitcherAction.startFrame = this.keys[i].frame;
  73. if (this.keys.Count > i + 1)
  74. {
  75. amcameraSwitcherAction.endFrame = this.keys[i + 1].frame;
  76. }
  77. else
  78. {
  79. amcameraSwitcherAction.endFrame = -1;
  80. }
  81. amcameraSwitcherAction.startTargetType = (this.keys[i] as AMCameraSwitcherKey).type;
  82. if (amcameraSwitcherAction.startTargetType == 0)
  83. {
  84. amcameraSwitcherAction.startCamera = (this.keys[i] as AMCameraSwitcherKey).camera;
  85. }
  86. else
  87. {
  88. amcameraSwitcherAction.startColor = (this.keys[i] as AMCameraSwitcherKey).color;
  89. }
  90. if (amcameraSwitcherAction.endFrame != -1)
  91. {
  92. amcameraSwitcherAction.endTargetType = (this.keys[i + 1] as AMCameraSwitcherKey).type;
  93. if (amcameraSwitcherAction.endTargetType == 0)
  94. {
  95. amcameraSwitcherAction.endCamera = (this.keys[i + 1] as AMCameraSwitcherKey).camera;
  96. }
  97. else
  98. {
  99. amcameraSwitcherAction.endColor = (this.keys[i + 1] as AMCameraSwitcherKey).color;
  100. }
  101. }
  102. amcameraSwitcherAction.cameraFadeType = (this.keys[i] as AMCameraSwitcherKey).cameraFadeType;
  103. amcameraSwitcherAction.cameraFadeParameters = new List<float>((this.keys[i] as AMCameraSwitcherKey).cameraFadeParameters);
  104. amcameraSwitcherAction.irisShape = (this.keys[i] as AMCameraSwitcherKey).irisShape;
  105. amcameraSwitcherAction.still = (this.keys[i] as AMCameraSwitcherKey).still;
  106. amcameraSwitcherAction.easeType = (this.keys[i] as AMCameraSwitcherKey).easeType;
  107. amcameraSwitcherAction.customEase = new List<float>(this.keys[i].customEase);
  108. this.cache.Add(amcameraSwitcherAction);
  109. }
  110. this._cachedAllCameras = this.getAllCameras();
  111. base.updateCache();
  112. }
  113. public override void previewFrame(float frame, AMTrack extraTrack = null)
  114. {
  115. if (this.cache == null || this.cache.Count <= 0)
  116. {
  117. return;
  118. }
  119. bool isPreview = !Application.isPlaying;
  120. int i = 0;
  121. while (i < this.cache.Count)
  122. {
  123. if (frame <= (float)(this.cache[i] as AMCameraSwitcherAction).startFrame)
  124. {
  125. AMCameraFade.reset();
  126. if (!(this.cache[i] as AMCameraSwitcherAction).hasStartTarget())
  127. {
  128. return;
  129. }
  130. if ((this.cache[i] as AMCameraSwitcherAction).startTargetType == 0)
  131. {
  132. AMTween.SetTopCamera((this.cache[i] as AMCameraSwitcherAction).startCamera, this.cachedAllCameras);
  133. }
  134. else
  135. {
  136. this.showColor((this.cache[i] as AMCameraSwitcherAction).startColor, isPreview);
  137. }
  138. return;
  139. }
  140. else if (frame <= (float)(this.cache[i] as AMCameraSwitcherAction).endFrame)
  141. {
  142. if (!(this.cache[i] as AMCameraSwitcherAction).hasStartTarget() || !(this.cache[i] as AMCameraSwitcherAction).hasEndTarget())
  143. {
  144. return;
  145. }
  146. if ((this.cache[i] as AMCameraSwitcherAction).targetsAreEqual())
  147. {
  148. AMCameraFade.reset();
  149. if ((this.cache[i] as AMCameraSwitcherAction).startTargetType == 0)
  150. {
  151. AMTween.SetTopCamera((this.cache[i] as AMCameraSwitcherAction).startCamera, this.cachedAllCameras);
  152. }
  153. else
  154. {
  155. this.showColor((this.cache[i] as AMCameraSwitcherAction).startColor, isPreview);
  156. }
  157. }
  158. else
  159. {
  160. AMCameraFade.clearRenderTexture();
  161. this.previewCameraFade(frame, this.cache[i] as AMCameraSwitcherAction, isPreview);
  162. }
  163. return;
  164. }
  165. else if (i == this.cache.Count - 2)
  166. {
  167. AMCameraFade.reset();
  168. if (!(this.cache[i] as AMCameraSwitcherAction).hasEndTarget())
  169. {
  170. return;
  171. }
  172. if ((this.cache[i] as AMCameraSwitcherAction).endTargetType == 0)
  173. {
  174. AMTween.SetTopCamera((this.cache[i] as AMCameraSwitcherAction).endCamera, this.cachedAllCameras);
  175. }
  176. else
  177. {
  178. this.showColor((this.cache[i] as AMCameraSwitcherAction).endColor, isPreview);
  179. }
  180. return;
  181. }
  182. else
  183. {
  184. i++;
  185. }
  186. }
  187. }
  188. public Camera[] getAllCameras()
  189. {
  190. List<Camera> list = new List<Camera>();
  191. foreach (AMKey amkey in this.keys)
  192. {
  193. AMCameraSwitcherKey amcameraSwitcherKey = (AMCameraSwitcherKey)amkey;
  194. if (amcameraSwitcherKey.type == 0 && amcameraSwitcherKey.camera)
  195. {
  196. list.Add(amcameraSwitcherKey.camera);
  197. }
  198. }
  199. return list.Distinct<Camera>().ToArray<Camera>();
  200. }
  201. public Texture[] getAllTextures()
  202. {
  203. List<Texture> list = new List<Texture>();
  204. foreach (AMKey amkey in this.keys)
  205. {
  206. AMCameraSwitcherKey amcameraSwitcherKey = (AMCameraSwitcherKey)amkey;
  207. if (amcameraSwitcherKey.irisShape && AMCameraFade.needsTexture(amcameraSwitcherKey.cameraFadeType))
  208. {
  209. list.Add(amcameraSwitcherKey.irisShape);
  210. }
  211. }
  212. return list.Distinct<Texture>().ToArray<Texture>();
  213. }
  214. private void previewCameraFade(float frame, AMCameraSwitcherAction action, bool isPreview)
  215. {
  216. if (action.cameraFadeType == 5)
  217. {
  218. if (action.endTargetType == 0)
  219. {
  220. if (action.endCamera)
  221. {
  222. AMTween.SetTopCamera(action.endCamera, this.cachedAllCameras);
  223. }
  224. AMCameraFade.reset();
  225. }
  226. else
  227. {
  228. this.showColor(action.endColor, isPreview);
  229. }
  230. return;
  231. }
  232. AMCameraFade cameraFade = AMCameraFade.getCameraFade(isPreview);
  233. if (Application.isPlaying)
  234. {
  235. cameraFade.keepAlivePreview = true;
  236. }
  237. cameraFade.isReset = false;
  238. bool flag = action.isReversed();
  239. int firstTargetType = (!flag) ? action.startTargetType : action.endTargetType;
  240. int secondTargetType = (!flag) ? action.endTargetType : action.startTargetType;
  241. this.setRenderTexture(cameraFade, frame, firstTargetType, secondTargetType, flag, action, isPreview);
  242. this.setColors(cameraFade, firstTargetType, secondTargetType, flag, action);
  243. if (cameraFade.irisShape != action.irisShape)
  244. {
  245. cameraFade.irisShape = action.irisShape;
  246. }
  247. cameraFade.mode = action.cameraFadeType;
  248. cameraFade.setupMaterials();
  249. cameraFade.r = action.cameraFadeParameters.ToArray();
  250. AnimationCurve curve = null;
  251. AMTween.EasingFunction easingFunction;
  252. if (action.hasCustomEase())
  253. {
  254. if (AMCameraSwitcherTrack.<>f__mg$cache0 == null)
  255. {
  256. AMCameraSwitcherTrack.<>f__mg$cache0 = new AMTween.EasingFunction(AMTween.customEase);
  257. }
  258. easingFunction = AMCameraSwitcherTrack.<>f__mg$cache0;
  259. curve = action.easeCurve;
  260. }
  261. else
  262. {
  263. easingFunction = AMTween.GetEasingFunction((AMTween.EaseType)action.easeType);
  264. }
  265. float num = (frame - (float)action.startFrame) / (float)(action.endFrame - action.startFrame);
  266. float value = easingFunction(1f, 0f, num, curve);
  267. cameraFade.value = value;
  268. cameraFade.percent = num;
  269. }
  270. private void setColors(AMCameraFade cf, int firstTargetType, int secondTargetType, bool isReversed, AMCameraSwitcherAction action)
  271. {
  272. Color colorTex = (!isReversed) ? action.startColor : action.endColor;
  273. Color colorBG = (!isReversed) ? action.endColor : action.startColor;
  274. if (firstTargetType == 1)
  275. {
  276. cf.colorTex = colorTex;
  277. cf.hasColorTex = true;
  278. }
  279. else
  280. {
  281. cf.hasColorTex = false;
  282. }
  283. if (secondTargetType == 1)
  284. {
  285. cf.colorBG = colorBG;
  286. cf.hasColorBG = true;
  287. }
  288. else
  289. {
  290. cf.hasColorBG = false;
  291. }
  292. if (!Application.isPlaying && (firstTargetType == 1 || secondTargetType == 1))
  293. {
  294. cf.transform.position = new Vector3(cf.transform.position.x, cf.transform.position.y, cf.transform.position.z);
  295. }
  296. }
  297. private void setRenderTexture(AMCameraFade cf, float frame, int firstTargetType, int secondTargetType, bool isReversed, AMCameraSwitcherAction action, bool isPreview)
  298. {
  299. Camera camera = (!isReversed) ? action.startCamera : action.endCamera;
  300. Camera top = (!isReversed) ? action.endCamera : action.startCamera;
  301. if (isReversed && frame == (float)action.startFrame)
  302. {
  303. if (firstTargetType == 0)
  304. {
  305. AMTween.SetTopCamera(camera, this.cachedAllCameras);
  306. }
  307. }
  308. else if (secondTargetType == 0)
  309. {
  310. AMTween.SetTopCamera(top, this.cachedAllCameras);
  311. }
  312. if (action.still || (firstTargetType != 0 && secondTargetType != 0))
  313. {
  314. return;
  315. }
  316. bool isProLicense = AMTake.isProLicense;
  317. if (firstTargetType == 0 && (cf.shouldUpdateRenderTexture || (isProLicense && (!camera.targetTexture || !cf.isRenderTextureSetupFor(camera)))))
  318. {
  319. if (isProLicense)
  320. {
  321. cf.setupRenderTexture(camera);
  322. }
  323. else
  324. {
  325. cf.useRenderTexture = false;
  326. cf.colorTex = Color.white;
  327. cf.tex2d = (Texture2D)Resources.Load("am_indie_placeholder");
  328. cf.hasColorTex = false;
  329. if (!cf.placeholder)
  330. {
  331. cf.placeholder = true;
  332. }
  333. }
  334. }
  335. }
  336. public AMCameraSwitcherTrack.cfTuple getCameraFadeTupleForFrame(int frame)
  337. {
  338. if (this.cache == null || this.cache.Count <= 0)
  339. {
  340. return new AMCameraSwitcherTrack.cfTuple(0, 0, 0, null, null, false);
  341. }
  342. int i = 0;
  343. while (i < this.cache.Count)
  344. {
  345. if (frame < (this.cache[i] as AMCameraSwitcherAction).startFrame)
  346. {
  347. break;
  348. }
  349. if (frame < (this.cache[i] as AMCameraSwitcherAction).endFrame)
  350. {
  351. if (!(this.cache[i] as AMCameraSwitcherAction).still || (this.cache[i] as AMCameraSwitcherAction).cameraFadeType == 5 || (this.cache[i] as AMCameraSwitcherAction).targetsAreEqual())
  352. {
  353. break;
  354. }
  355. bool flag = (this.cache[i] as AMCameraSwitcherAction).isReversed();
  356. AMCameraSwitcherAction amcameraSwitcherAction = this.cache[i] as AMCameraSwitcherAction;
  357. if (flag)
  358. {
  359. return new AMCameraSwitcherTrack.cfTuple(amcameraSwitcherAction.endFrame, amcameraSwitcherAction.endTargetType, amcameraSwitcherAction.startTargetType, amcameraSwitcherAction.endCamera, amcameraSwitcherAction.startCamera, flag);
  360. }
  361. return new AMCameraSwitcherTrack.cfTuple(amcameraSwitcherAction.startFrame, amcameraSwitcherAction.startTargetType, amcameraSwitcherAction.endTargetType, amcameraSwitcherAction.startCamera, amcameraSwitcherAction.endCamera, flag);
  362. }
  363. else
  364. {
  365. i++;
  366. }
  367. }
  368. return new AMCameraSwitcherTrack.cfTuple(0, 0, 0, null, null, false);
  369. }
  370. private void showColor(Color color, bool isPreview)
  371. {
  372. AMCameraFade cameraFade = AMCameraFade.getCameraFade(isPreview);
  373. bool flag = false;
  374. if (!cameraFade.hasColorTex || cameraFade.colorTex != color)
  375. {
  376. cameraFade.colorTex = color;
  377. cameraFade.hasColorTex = true;
  378. flag = true;
  379. }
  380. if (cameraFade.isReset)
  381. {
  382. cameraFade.isReset = false;
  383. flag = true;
  384. }
  385. if (cameraFade.hasColorBG)
  386. {
  387. cameraFade.hasColorBG = false;
  388. flag = true;
  389. }
  390. if (cameraFade.value != 1f)
  391. {
  392. cameraFade.value = 1f;
  393. cameraFade.percent = 0f;
  394. flag = true;
  395. }
  396. if (cameraFade.mode != 0)
  397. {
  398. cameraFade.mode = 0;
  399. flag = true;
  400. }
  401. if (!Application.isPlaying && flag)
  402. {
  403. cameraFade.transform.position = new Vector3(cameraFade.transform.position.x, cameraFade.transform.position.y, cameraFade.transform.position.z);
  404. }
  405. }
  406. public override AnimatorTimeline.JSONInit getJSONInit()
  407. {
  408. if (this.keys.Count <= 0)
  409. {
  410. return null;
  411. }
  412. string text;
  413. if ((this.keys[0] as AMCameraSwitcherKey).type == 0)
  414. {
  415. if ((this.keys[0] as AMCameraSwitcherKey).camera == null)
  416. {
  417. return null;
  418. }
  419. text = "camera";
  420. }
  421. else
  422. {
  423. text = "color";
  424. }
  425. AnimatorTimeline.JSONInit jsoninit = new AnimatorTimeline.JSONInit();
  426. jsoninit.type = "cameraswitcher";
  427. jsoninit.typeExtra = text;
  428. if (text == "camera")
  429. {
  430. jsoninit.go = (this.keys[0] as AMCameraSwitcherKey).camera.gameObject.name;
  431. }
  432. else
  433. {
  434. AnimatorTimeline.JSONColor jsoncolor = new AnimatorTimeline.JSONColor();
  435. jsoncolor.setValue((this.keys[0] as AMCameraSwitcherKey).color);
  436. jsoninit._color = jsoncolor;
  437. }
  438. Camera[] allCameras = this.getAllCameras();
  439. jsoninit.strings = new string[allCameras.Length];
  440. for (int i = 0; i < allCameras.Length; i++)
  441. {
  442. jsoninit.strings[i] = allCameras[i].gameObject.name;
  443. }
  444. return jsoninit;
  445. }
  446. public override List<GameObject> getDependencies()
  447. {
  448. List<GameObject> list = new List<GameObject>();
  449. foreach (AMKey amkey in this.keys)
  450. {
  451. AMCameraSwitcherKey amcameraSwitcherKey = (AMCameraSwitcherKey)amkey;
  452. if (amcameraSwitcherKey.type == 0 && amcameraSwitcherKey.camera)
  453. {
  454. list.Add(amcameraSwitcherKey.camera.gameObject);
  455. }
  456. }
  457. return list;
  458. }
  459. public override List<GameObject> updateDependencies(List<GameObject> newReferences, List<GameObject> oldReferences)
  460. {
  461. List<GameObject> list = new List<GameObject>();
  462. for (int i = 0; i < oldReferences.Count; i++)
  463. {
  464. foreach (AMKey amkey in this.keys)
  465. {
  466. AMCameraSwitcherKey amcameraSwitcherKey = (AMCameraSwitcherKey)amkey;
  467. if (amcameraSwitcherKey.type == 0 && amcameraSwitcherKey.camera && oldReferences[i] == amcameraSwitcherKey.camera.gameObject)
  468. {
  469. Camera camera = (Camera)newReferences[i].GetComponent(typeof(Camera));
  470. if (!camera)
  471. {
  472. Debug.LogWarning("Animator: Camera Switcher component 'Camera' not found on new reference for GameObject '" + amcameraSwitcherKey.camera.gameObject.name + "'. Duplicate not replaced.");
  473. list.Add(oldReferences[i]);
  474. }
  475. else
  476. {
  477. amcameraSwitcherKey.camera = camera;
  478. }
  479. }
  480. }
  481. }
  482. return list;
  483. }
  484. private Camera[] _cachedAllCameras;
  485. [CompilerGenerated]
  486. private static AMTween.EasingFunction <>f__mg$cache0;
  487. public struct cfTuple
  488. {
  489. public cfTuple(int _frame, int _type1, int _type2, Camera _camera1, Camera _camera2, bool _isReversed)
  490. {
  491. this.frame = _frame;
  492. this.type1 = _type1;
  493. this.type2 = _type2;
  494. this.camera1 = _camera1;
  495. this.camera2 = _camera2;
  496. this.isReversed = _isReversed;
  497. }
  498. public int frame;
  499. public int type1;
  500. public int type2;
  501. public Camera camera1;
  502. public Camera camera2;
  503. public bool isReversed;
  504. }
  505. }