2
0

EditorAnimInfo.cs 21 KB

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