EditorAnimInfo.cs 19 KB

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