EditorAnimInfo.cs 20 KB

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