EditorAnimInfo.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using BansheeEngine;
  7. namespace BansheeEditor
  8. {
  9. /** @addtogroup AnimationEditor
  10. * @{
  11. */
  12. /// <summary>
  13. /// A set of animation curves for a field of a certain type.
  14. /// </summary>
  15. internal struct FieldAnimCurves
  16. {
  17. public SerializableProperty.FieldType type;
  18. public CurveDrawInfo[] curveInfos;
  19. }
  20. /// <summary>
  21. /// Stores tangent modes for an 3D vector animation curve (one mode for each keyframe).
  22. /// </summary>
  23. [SerializeObject]
  24. internal class EditorVector3CurveTangents
  25. {
  26. public string name;
  27. public TangentMode[] tangentsX;
  28. public TangentMode[] tangentsY;
  29. public TangentMode[] tangentsZ;
  30. }
  31. /// <summary>
  32. /// Stores tangent modes for an float animation curve (one mode for each keyframe).
  33. /// </summary>
  34. [SerializeObject]
  35. internal class EditorFloatCurveTangents
  36. {
  37. public string name;
  38. public TangentMode[] tangents;
  39. }
  40. /// <summary>
  41. /// Stores tangent information for all curves in an animation clip.
  42. /// </summary>
  43. [SerializeObject]
  44. internal class EditorAnimClipTangents
  45. {
  46. public EditorVector3CurveTangents[] positionCurves;
  47. public EditorVector3CurveTangents[] rotationCurves;
  48. public EditorVector3CurveTangents[] scaleCurves;
  49. public EditorFloatCurveTangents[] floatCurves;
  50. }
  51. /// <summary>
  52. /// Stores animation clip data for clips that are currently being edited.
  53. /// </summary>
  54. internal class EditorAnimClipInfo
  55. {
  56. public AnimationClip clip;
  57. public bool isImported;
  58. public int sampleRate;
  59. public Dictionary<string, FieldAnimCurves> curves = new Dictionary<string, FieldAnimCurves>();
  60. public AnimationEvent[] events = new AnimationEvent[0];
  61. /// <summary>
  62. /// Loads curve and event information from the provided clip, and creates a new instance of this object containing
  63. /// the required data for editing the source clip in the animation editor.
  64. /// </summary>
  65. /// <param name="clip">Clip to load.</param>
  66. /// <returns>Editor specific editable information about an animation clip.</returns>
  67. public static EditorAnimClipInfo Create(AnimationClip clip)
  68. {
  69. EditorAnimClipInfo clipInfo = new EditorAnimClipInfo();
  70. clipInfo.clip = clip;
  71. clipInfo.isImported = IsClipImported(clip);
  72. clipInfo.sampleRate = clip.SampleRate;
  73. AnimationCurves clipCurves = clip.Curves;
  74. EditorAnimClipTangents editorCurveData = null;
  75. string resourcePath = ProjectLibrary.GetPath(clip);
  76. if (!string.IsNullOrEmpty(resourcePath))
  77. {
  78. LibraryEntry entry = ProjectLibrary.GetEntry(resourcePath);
  79. string clipName = PathEx.GetTail(resourcePath);
  80. if (entry != null && entry.Type == LibraryEntryType.File)
  81. {
  82. FileEntry fileEntry = (FileEntry)entry;
  83. ResourceMeta[] metas = fileEntry.ResourceMetas;
  84. if (clipInfo.isImported)
  85. {
  86. for (int i = 0; i < metas.Length; i++)
  87. {
  88. if (clipName == metas[i].SubresourceName)
  89. {
  90. editorCurveData = metas[i].EditorData as EditorAnimClipTangents;
  91. break;
  92. }
  93. }
  94. }
  95. else
  96. {
  97. if(metas.Length > 0)
  98. editorCurveData = metas[0].EditorData as EditorAnimClipTangents;
  99. }
  100. }
  101. }
  102. if (editorCurveData == null)
  103. editorCurveData = new EditorAnimClipTangents();
  104. int globalCurveIdx = 0;
  105. Action<NamedVector3Curve[], EditorVector3CurveTangents[], string> loadVector3Curve =
  106. (curves, tangents, subPath) =>
  107. {
  108. foreach (var curveEntry in curves)
  109. {
  110. TangentMode[] tangentsX = null;
  111. TangentMode[] tangentsY = null;
  112. TangentMode[] tangentsZ = null;
  113. if (tangents != null)
  114. {
  115. foreach (var tangentEntry in tangents)
  116. {
  117. if (tangentEntry.name == curveEntry.Name)
  118. {
  119. tangentsX = tangentEntry.tangentsX;
  120. tangentsY = tangentEntry.tangentsY;
  121. tangentsZ = tangentEntry.tangentsZ;
  122. break;
  123. }
  124. }
  125. }
  126. FieldAnimCurves fieldCurves = new FieldAnimCurves();
  127. fieldCurves.type = SerializableProperty.FieldType.Vector3;
  128. fieldCurves.curveInfos = new CurveDrawInfo[3];
  129. fieldCurves.curveInfos[0] = new CurveDrawInfo();
  130. fieldCurves.curveInfos[0].curve = new EdAnimationCurve(curveEntry.X, tangentsX);
  131. fieldCurves.curveInfos[0].color = GUICurveDrawing.GetUniqueColor(globalCurveIdx++);
  132. fieldCurves.curveInfos[1] = new CurveDrawInfo();
  133. fieldCurves.curveInfos[1].curve = new EdAnimationCurve(curveEntry.Y, tangentsY);
  134. fieldCurves.curveInfos[1].color = GUICurveDrawing.GetUniqueColor(globalCurveIdx++);
  135. fieldCurves.curveInfos[2] = new CurveDrawInfo();
  136. fieldCurves.curveInfos[2].curve = new EdAnimationCurve(curveEntry.Z, tangentsZ);
  137. fieldCurves.curveInfos[2].color = GUICurveDrawing.GetUniqueColor(globalCurveIdx++);
  138. string curvePath = curveEntry.Name.TrimEnd('/') + subPath;
  139. clipInfo.curves[curvePath] = fieldCurves;
  140. }
  141. };
  142. loadVector3Curve(clipCurves.PositionCurves, editorCurveData.positionCurves, "/Position");
  143. loadVector3Curve(clipCurves.RotationCurves, editorCurveData.rotationCurves, "/Rotation");
  144. loadVector3Curve(clipCurves.ScaleCurves, editorCurveData.scaleCurves, "/Scale");
  145. // Find which individual float curves belong to the same field
  146. Dictionary<string, Tuple<int, int, bool>[]> floatCurveMapping = new Dictionary<string, Tuple<int, int, bool>[]>();
  147. {
  148. int curveIdx = 0;
  149. foreach (var curveEntry in clipCurves.FloatCurves)
  150. {
  151. string path = curveEntry.Name;
  152. string pathNoSuffix = null;
  153. string pathSuffix;
  154. if (path.Length >= 2)
  155. {
  156. pathSuffix = path.Substring(path.Length - 2, 2);
  157. pathNoSuffix = path.Substring(0, path.Length - 2);
  158. }
  159. else
  160. pathSuffix = "";
  161. int tangentIdx = -1;
  162. int currentTangentIdx = 0;
  163. foreach (var tangentEntry in editorCurveData.floatCurves)
  164. {
  165. if (tangentEntry.name == curveEntry.Name)
  166. {
  167. tangentIdx = currentTangentIdx;
  168. break;
  169. }
  170. currentTangentIdx++;
  171. }
  172. Animation.PropertySuffixInfo suffixInfo;
  173. if (Animation.PropertySuffixInfos.TryGetValue(pathSuffix, out suffixInfo))
  174. {
  175. Tuple<int, int, bool>[] curveInfo;
  176. if (!floatCurveMapping.TryGetValue(pathNoSuffix, out curveInfo))
  177. curveInfo = new Tuple<int, int, bool>[4];
  178. curveInfo[suffixInfo.elementIdx] = Tuple.Create(curveIdx, tangentIdx, suffixInfo.isVector);
  179. floatCurveMapping[pathNoSuffix] = curveInfo;
  180. }
  181. else
  182. {
  183. Tuple<int, int, bool>[] curveInfo = new Tuple<int, int, bool>[4];
  184. curveInfo[0] = Tuple.Create(curveIdx, tangentIdx, suffixInfo.isVector);
  185. floatCurveMapping[path] = curveInfo;
  186. }
  187. curveIdx++;
  188. }
  189. }
  190. foreach (var KVP in floatCurveMapping)
  191. {
  192. int numCurves = 0;
  193. for (int i = 0; i < 4; i++)
  194. {
  195. if (KVP.Value[i] == null)
  196. continue;
  197. numCurves++;
  198. }
  199. if (numCurves == 0)
  200. continue; // Invalid curve
  201. FieldAnimCurves fieldCurves = new FieldAnimCurves();
  202. // Deduce type (note that all single value types are assumed to be float even if their source type is int or bool)
  203. if (numCurves == 1)
  204. fieldCurves.type = SerializableProperty.FieldType.Float;
  205. else if (numCurves == 2)
  206. fieldCurves.type = SerializableProperty.FieldType.Vector2;
  207. else if (numCurves == 3)
  208. fieldCurves.type = SerializableProperty.FieldType.Vector3;
  209. else // 4 curves
  210. {
  211. bool isVector = KVP.Value[0].Item3;
  212. if (isVector)
  213. fieldCurves.type = SerializableProperty.FieldType.Vector4;
  214. else
  215. fieldCurves.type = SerializableProperty.FieldType.Color;
  216. }
  217. fieldCurves.curveInfos = new CurveDrawInfo[numCurves];
  218. for (int i = 0; i < numCurves; i++)
  219. {
  220. int curveIdx = KVP.Value[i].Item1;
  221. int tangentIdx = KVP.Value[i].Item2;
  222. TangentMode[] tangents = null;
  223. if (tangentIdx != -1)
  224. tangents = editorCurveData.floatCurves[tangentIdx].tangents;
  225. fieldCurves.curveInfos[i] = new CurveDrawInfo();
  226. fieldCurves.curveInfos[i].curve = new EdAnimationCurve(clipCurves.FloatCurves[curveIdx].Curve, tangents);
  227. fieldCurves.curveInfos[i].color = GUICurveDrawing.GetUniqueColor(globalCurveIdx++);
  228. }
  229. string curvePath = KVP.Key;
  230. clipInfo.curves[curvePath] = fieldCurves;
  231. }
  232. // Add events
  233. clipInfo.events = clip.Events;
  234. return clipInfo;
  235. }
  236. /// <summary>
  237. /// Checks is the specified animation clip is imported from an external file, or created within the editor.
  238. /// </summary>
  239. /// <param name="clip">Clip to check.</param>
  240. /// <returns>True if the clip is imported from an external file (e.g. FBX file), or false if the clip is a native
  241. /// resource created within the editor.</returns>
  242. public static bool IsClipImported(AnimationClip clip)
  243. {
  244. string resourcePath = ProjectLibrary.GetPath(clip);
  245. return ProjectLibrary.IsSubresource(resourcePath);
  246. }
  247. /// <summary>
  248. /// Applies any changes made to the animation curves or events to the actual animation clip. Only works for
  249. /// non-imported animation clips.
  250. /// </summary>
  251. /// <param name="tangents">Tangent modes for all the saved animation curves.</param>
  252. public void Apply(out EditorAnimClipTangents tangents)
  253. {
  254. if (isImported)
  255. {
  256. tangents = null;
  257. return;
  258. }
  259. List<NamedVector3Curve> positionCurves = new List<NamedVector3Curve>();
  260. List<NamedVector3Curve> rotationCurves = new List<NamedVector3Curve>();
  261. List<NamedVector3Curve> scaleCurves = new List<NamedVector3Curve>();
  262. List<NamedFloatCurve> floatCurves = new List<NamedFloatCurve>();
  263. List<EditorVector3CurveTangents> positionTangents = new List<EditorVector3CurveTangents>();
  264. List<EditorVector3CurveTangents> rotationTangents = new List<EditorVector3CurveTangents>();
  265. List<EditorVector3CurveTangents> scaleTangents = new List<EditorVector3CurveTangents>();
  266. List<EditorFloatCurveTangents> floatTangents = new List<EditorFloatCurveTangents>();
  267. foreach (var kvp in curves)
  268. {
  269. string[] pathEntries = kvp.Key.Split('/');
  270. if (pathEntries.Length == 0)
  271. continue;
  272. string lastEntry = pathEntries[pathEntries.Length - 1];
  273. if (lastEntry == "Position" || lastEntry == "Rotation" || lastEntry == "Scale")
  274. {
  275. StringBuilder sb = new StringBuilder();
  276. for (int i = 0; i < pathEntries.Length - 2; i++)
  277. sb.Append(pathEntries[i] + "/");
  278. if (pathEntries.Length > 1)
  279. sb.Append(pathEntries[pathEntries.Length - 2]);
  280. string curvePath = sb.ToString();
  281. NamedVector3Curve curve = new NamedVector3Curve(curvePath,
  282. new AnimationCurve(kvp.Value.curveInfos[0].curve.KeyFrames),
  283. new AnimationCurve(kvp.Value.curveInfos[1].curve.KeyFrames),
  284. new AnimationCurve(kvp.Value.curveInfos[2].curve.KeyFrames));
  285. EditorVector3CurveTangents curveTangents = new EditorVector3CurveTangents();
  286. curveTangents.name = curvePath;
  287. curveTangents.tangentsX = kvp.Value.curveInfos[0].curve.TangentModes;
  288. curveTangents.tangentsY = kvp.Value.curveInfos[1].curve.TangentModes;
  289. curveTangents.tangentsZ = kvp.Value.curveInfos[2].curve.TangentModes;
  290. if (lastEntry == "Position")
  291. {
  292. positionCurves.Add(curve);
  293. positionTangents.Add(curveTangents);
  294. }
  295. else if (lastEntry == "Rotation")
  296. {
  297. rotationCurves.Add(curve);
  298. rotationTangents.Add(curveTangents);
  299. }
  300. else if (lastEntry == "Scale")
  301. {
  302. scaleCurves.Add(curve);
  303. scaleTangents.Add(curveTangents);
  304. }
  305. }
  306. else
  307. {
  308. Action<int, string> addCurve = (idx, subPath) =>
  309. {
  310. string path = kvp.Key + subPath;
  311. NamedFloatCurve curve = new NamedFloatCurve(path,
  312. new AnimationCurve(kvp.Value.curveInfos[idx].curve.KeyFrames));
  313. EditorFloatCurveTangents curveTangents = new EditorFloatCurveTangents();
  314. curveTangents.name = path;
  315. curveTangents.tangents = kvp.Value.curveInfos[idx].curve.TangentModes;
  316. floatCurves.Add(curve);
  317. floatTangents.Add(curveTangents);
  318. };
  319. switch (kvp.Value.type)
  320. {
  321. case SerializableProperty.FieldType.Vector2:
  322. addCurve(0, ".x");
  323. addCurve(1, ".y");
  324. break;
  325. case SerializableProperty.FieldType.Vector3:
  326. addCurve(0, ".x");
  327. addCurve(1, ".y");
  328. addCurve(2, ".z");
  329. break;
  330. case SerializableProperty.FieldType.Vector4:
  331. addCurve(0, ".x");
  332. addCurve(1, ".y");
  333. addCurve(2, ".z");
  334. addCurve(3, ".w");
  335. break;
  336. case SerializableProperty.FieldType.Color:
  337. addCurve(0, ".r");
  338. addCurve(1, ".g");
  339. addCurve(2, ".b");
  340. addCurve(3, ".a");
  341. break;
  342. case SerializableProperty.FieldType.Bool:
  343. case SerializableProperty.FieldType.Int:
  344. case SerializableProperty.FieldType.Float:
  345. addCurve(0, "");
  346. break;
  347. }
  348. }
  349. }
  350. AnimationCurves newClipCurves = new AnimationCurves();
  351. newClipCurves.PositionCurves = positionCurves.ToArray();
  352. newClipCurves.RotationCurves = rotationCurves.ToArray();
  353. newClipCurves.ScaleCurves = scaleCurves.ToArray();
  354. newClipCurves.FloatCurves = floatCurves.ToArray();
  355. clip.Curves = newClipCurves;
  356. clip.Events = events;
  357. clip.SampleRate = sampleRate;
  358. tangents = new EditorAnimClipTangents();
  359. tangents.positionCurves = positionTangents.ToArray();
  360. tangents.rotationCurves = rotationTangents.ToArray();
  361. tangents.scaleCurves = scaleTangents.ToArray();
  362. tangents.floatCurves = floatTangents.ToArray();
  363. }
  364. /// <summary>
  365. /// Saves the animation curves and events stored in this object, into the associated animation clip resource.
  366. /// Relevant animation clip resource must already be created and exist in the project library.
  367. /// </summary>
  368. public void SaveToClip()
  369. {
  370. if (!isImported)
  371. {
  372. EditorAnimClipTangents tangents;
  373. Apply(out tangents);
  374. string resourcePath = ProjectLibrary.GetPath(clip);
  375. ProjectLibrary.Save(clip);
  376. ProjectLibrary.SetEditorData(resourcePath, tangents);
  377. }
  378. else
  379. {
  380. string resourcePath = ProjectLibrary.GetPath(clip);
  381. LibraryEntry entry = ProjectLibrary.GetEntry(resourcePath);
  382. if (entry != null && entry.Type == LibraryEntryType.File)
  383. {
  384. FileEntry fileEntry = (FileEntry)entry;
  385. MeshImportOptions meshImportOptions = (MeshImportOptions)fileEntry.Options;
  386. string clipName = PathEx.GetTail(resourcePath);
  387. List<ImportedAnimationEvents> newEvents = new List<ImportedAnimationEvents>();
  388. newEvents.AddRange(meshImportOptions.AnimationEvents);
  389. bool isExisting = false;
  390. for (int i = 0; i < newEvents.Count; i++)
  391. {
  392. if (newEvents[i].name == clipName)
  393. {
  394. newEvents[i].events = events;
  395. isExisting = true;
  396. break;
  397. }
  398. }
  399. if (!isExisting)
  400. {
  401. ImportedAnimationEvents newEntry = new ImportedAnimationEvents();
  402. newEntry.name = clipName;
  403. newEntry.events = events;
  404. newEvents.Add(newEntry);
  405. }
  406. meshImportOptions.AnimationEvents = newEvents.ToArray();
  407. ProjectLibrary.Reimport(resourcePath, meshImportOptions, true);
  408. }
  409. }
  410. }
  411. }
  412. /** @} */
  413. }