EditorAnimInfo.cs 19 KB

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