EditorAnimInfo.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. bool isMorphCurve = false;
  220. string curvePath = KVP.Key;
  221. fieldCurves.curveInfos = new CurveDrawInfo[numCurves];
  222. for (int i = 0; i < numCurves; i++)
  223. {
  224. int curveIdx = KVP.Value[i].Item1;
  225. int tangentIdx = KVP.Value[i].Item2;
  226. TangentMode[] tangents = null;
  227. if (tangentIdx != -1)
  228. tangents = editorCurveData.floatCurves[tangentIdx].tangents;
  229. fieldCurves.curveInfos[i] = new CurveDrawInfo();
  230. fieldCurves.curveInfos[i].curve = new EdAnimationCurve(clipCurves.FloatCurves[curveIdx].Curve, tangents);
  231. fieldCurves.curveInfos[i].color = GUICurveDrawing.GetUniqueColor(globalCurveIdx++);
  232. if (clipCurves.FloatCurves[curveIdx].Flags.HasFlag(AnimationCurveFlags.MorphFrame))
  233. {
  234. curvePath = "MorphShapes/Frames/" + KVP.Key;
  235. isMorphCurve = true;
  236. }
  237. else if (clipCurves.FloatCurves[curveIdx].Flags.HasFlag(AnimationCurveFlags.MorphWeight))
  238. {
  239. curvePath = "MorphShapes/Weight/" + KVP.Key;
  240. isMorphCurve = true;
  241. }
  242. }
  243. fieldCurves.isPropertyCurve = !clipInfo.isImported && !isMorphCurve;
  244. clipInfo.curves[curvePath] = fieldCurves;
  245. }
  246. // Add events
  247. clipInfo.events = clip.Events;
  248. return clipInfo;
  249. }
  250. /// <summary>
  251. /// Checks is the specified animation clip is imported from an external file, or created within the editor.
  252. /// </summary>
  253. /// <param name="clip">Clip to check.</param>
  254. /// <returns>True if the clip is imported from an external file (e.g. FBX file), or false if the clip is a native
  255. /// resource created within the editor.</returns>
  256. public static bool IsClipImported(AnimationClip clip)
  257. {
  258. string resourcePath = ProjectLibrary.GetPath(clip);
  259. return ProjectLibrary.IsSubresource(resourcePath);
  260. }
  261. /// <summary>
  262. /// Checks does a curve with the specified path represent a curve affecting a morph shape.
  263. /// </summary>
  264. /// <param name="path">Path of the curve to check.</param>
  265. /// <returns>True if morph shape frame or weight animation, false otherwise.</returns>
  266. public static bool IsMorphShapeCurve(string path)
  267. {
  268. if (string.IsNullOrEmpty(path))
  269. return false;
  270. string trimmedPath = path.Trim('/');
  271. string[] entries = trimmedPath.Split('/');
  272. if (entries.Length < 3)
  273. return false;
  274. if (entries[entries.Length - 3] != "MorphShapes")
  275. return false;
  276. return entries[entries.Length - 2] == "Weight" || entries[entries.Length - 2] == "Frames";
  277. }
  278. /// <summary>
  279. /// Applies any changes made to the animation curves or events to the actual animation clip. Only works for
  280. /// non-imported animation clips.
  281. /// </summary>
  282. /// <param name="tangents">Tangent modes for all the saved animation curves.</param>
  283. public void Apply(out EditorAnimClipTangents tangents)
  284. {
  285. if (isImported || clip == null)
  286. {
  287. tangents = null;
  288. return;
  289. }
  290. List<NamedVector3Curve> positionCurves = new List<NamedVector3Curve>();
  291. List<NamedVector3Curve> rotationCurves = new List<NamedVector3Curve>();
  292. List<NamedVector3Curve> scaleCurves = new List<NamedVector3Curve>();
  293. List<NamedFloatCurve> floatCurves = new List<NamedFloatCurve>();
  294. List<EditorVector3CurveTangents> positionTangents = new List<EditorVector3CurveTangents>();
  295. List<EditorVector3CurveTangents> rotationTangents = new List<EditorVector3CurveTangents>();
  296. List<EditorVector3CurveTangents> scaleTangents = new List<EditorVector3CurveTangents>();
  297. List<EditorFloatCurveTangents> floatTangents = new List<EditorFloatCurveTangents>();
  298. foreach (var kvp in curves)
  299. {
  300. string[] pathEntries = kvp.Key.Split('/');
  301. if (pathEntries.Length == 0)
  302. continue;
  303. string lastEntry = pathEntries[pathEntries.Length - 1];
  304. if (lastEntry == "Position" || lastEntry == "Rotation" || lastEntry == "Scale")
  305. {
  306. StringBuilder sb = new StringBuilder();
  307. for (int i = 0; i < pathEntries.Length - 2; i++)
  308. sb.Append(pathEntries[i] + "/");
  309. if (pathEntries.Length > 1)
  310. sb.Append(pathEntries[pathEntries.Length - 2]);
  311. string curvePath = sb.ToString();
  312. NamedVector3Curve curve = new NamedVector3Curve(curvePath,
  313. new AnimationCurve(kvp.Value.curveInfos[0].curve.KeyFrames),
  314. new AnimationCurve(kvp.Value.curveInfos[1].curve.KeyFrames),
  315. new AnimationCurve(kvp.Value.curveInfos[2].curve.KeyFrames));
  316. EditorVector3CurveTangents curveTangents = new EditorVector3CurveTangents();
  317. curveTangents.name = curvePath;
  318. curveTangents.tangentsX = kvp.Value.curveInfos[0].curve.TangentModes;
  319. curveTangents.tangentsY = kvp.Value.curveInfos[1].curve.TangentModes;
  320. curveTangents.tangentsZ = kvp.Value.curveInfos[2].curve.TangentModes;
  321. if (lastEntry == "Position")
  322. {
  323. positionCurves.Add(curve);
  324. positionTangents.Add(curveTangents);
  325. }
  326. else if (lastEntry == "Rotation")
  327. {
  328. rotationCurves.Add(curve);
  329. rotationTangents.Add(curveTangents);
  330. }
  331. else if (lastEntry == "Scale")
  332. {
  333. scaleCurves.Add(curve);
  334. scaleTangents.Add(curveTangents);
  335. }
  336. }
  337. else
  338. {
  339. Action<int, string, string, AnimationCurveFlags> addCurve = (idx, path, subPath, flags) =>
  340. {
  341. string fullPath = path + subPath;
  342. NamedFloatCurve curve = new NamedFloatCurve(fullPath,
  343. new AnimationCurve(kvp.Value.curveInfos[idx].curve.KeyFrames));
  344. curve.Flags = flags;
  345. EditorFloatCurveTangents curveTangents = new EditorFloatCurveTangents();
  346. curveTangents.name = fullPath;
  347. curveTangents.tangents = kvp.Value.curveInfos[idx].curve.TangentModes;
  348. floatCurves.Add(curve);
  349. floatTangents.Add(curveTangents);
  350. };
  351. switch (kvp.Value.type)
  352. {
  353. case SerializableProperty.FieldType.Vector2:
  354. addCurve(0, kvp.Key, ".x", 0);
  355. addCurve(1, kvp.Key, ".y", 0);
  356. break;
  357. case SerializableProperty.FieldType.Vector3:
  358. addCurve(0, kvp.Key, ".x", 0);
  359. addCurve(1, kvp.Key, ".y", 0);
  360. addCurve(2, kvp.Key, ".z", 0);
  361. break;
  362. case SerializableProperty.FieldType.Vector4:
  363. addCurve(0, kvp.Key, ".x", 0);
  364. addCurve(1, kvp.Key, ".y", 0);
  365. addCurve(2, kvp.Key, ".z", 0);
  366. addCurve(3, kvp.Key, ".w", 0);
  367. break;
  368. case SerializableProperty.FieldType.Color:
  369. addCurve(0, kvp.Key, ".r", 0);
  370. addCurve(1, kvp.Key, ".g", 0);
  371. addCurve(2, kvp.Key, ".b", 0);
  372. addCurve(3, kvp.Key, ".a", 0);
  373. break;
  374. case SerializableProperty.FieldType.Bool:
  375. case SerializableProperty.FieldType.Int:
  376. case SerializableProperty.FieldType.Float:
  377. {
  378. AnimationCurveFlags flags = 0;
  379. string path = kvp.Key;
  380. if (IsMorphShapeCurve(kvp.Key))
  381. {
  382. string trimmedPath = path.Trim('/');
  383. string[] entries = trimmedPath.Split('/');
  384. bool isWeight = entries[entries.Length - 2] == "Weight";
  385. if (isWeight)
  386. flags = AnimationCurveFlags.MorphWeight;
  387. else
  388. flags = AnimationCurveFlags.MorphFrame;
  389. path = entries[entries.Length - 1];
  390. }
  391. addCurve(0, path, "", flags);
  392. }
  393. break;
  394. }
  395. }
  396. }
  397. AnimationCurves newClipCurves = new AnimationCurves();
  398. newClipCurves.PositionCurves = positionCurves.ToArray();
  399. newClipCurves.RotationCurves = rotationCurves.ToArray();
  400. newClipCurves.ScaleCurves = scaleCurves.ToArray();
  401. newClipCurves.FloatCurves = floatCurves.ToArray();
  402. clip.Curves = newClipCurves;
  403. clip.Events = events;
  404. clip.SampleRate = sampleRate;
  405. tangents = new EditorAnimClipTangents();
  406. tangents.positionCurves = positionTangents.ToArray();
  407. tangents.rotationCurves = rotationTangents.ToArray();
  408. tangents.scaleCurves = scaleTangents.ToArray();
  409. tangents.floatCurves = floatTangents.ToArray();
  410. }
  411. /// <summary>
  412. /// Saves the animation curves and events stored in this object, into the associated animation clip resource.
  413. /// Relevant animation clip resource must already be created and exist in the project library.
  414. /// </summary>
  415. public void SaveToClip()
  416. {
  417. if (!isImported)
  418. {
  419. EditorAnimClipTangents tangents;
  420. Apply(out tangents);
  421. string resourcePath = ProjectLibrary.GetPath(clip);
  422. ProjectLibrary.Save(clip);
  423. ProjectLibrary.SetEditorData(resourcePath, tangents);
  424. }
  425. else
  426. {
  427. string resourcePath = ProjectLibrary.GetPath(clip);
  428. LibraryEntry entry = ProjectLibrary.GetEntry(resourcePath);
  429. if (entry != null && entry.Type == LibraryEntryType.File)
  430. {
  431. FileEntry fileEntry = (FileEntry)entry;
  432. MeshImportOptions meshImportOptions = (MeshImportOptions)fileEntry.Options;
  433. string clipName = PathEx.GetTail(resourcePath);
  434. List<ImportedAnimationEvents> newEvents = new List<ImportedAnimationEvents>();
  435. newEvents.AddRange(meshImportOptions.AnimationEvents);
  436. bool isExisting = false;
  437. for (int i = 0; i < newEvents.Count; i++)
  438. {
  439. if (newEvents[i].name == clipName)
  440. {
  441. newEvents[i].events = events;
  442. isExisting = true;
  443. break;
  444. }
  445. }
  446. if (!isExisting)
  447. {
  448. ImportedAnimationEvents newEntry = new ImportedAnimationEvents();
  449. newEntry.name = clipName;
  450. newEntry.events = events;
  451. newEvents.Add(newEntry);
  452. }
  453. meshImportOptions.AnimationEvents = newEvents.ToArray();
  454. ProjectLibrary.Reimport(resourcePath, meshImportOptions, true);
  455. }
  456. }
  457. }
  458. }
  459. /** @} */
  460. }