PointCachePlayerInspector.cs 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.IO;
  6. namespace VertexAnimationTools_30{
  7. [CanEditMultipleObjects]
  8. [CustomEditor(typeof(PointCachePlayer))]
  9. public class PointCachePlayerInspector : InspectorsBase {
  10. #region PLAYBACKCLIP
  11. public class PlaybackClip {
  12. PointCachePlayerInspector Inspector;
  13. int Idx;
  14. GUIStyle coloredIdxStyle;
  15. SerializedProperty prp_AutoPlaybackTypeEnum;
  16. SerializedProperty prp_NormalizedTime;
  17. SerializedProperty prp_Weight;
  18. SerializedProperty prp_DurationInSeconds;
  19. SerializedProperty prp_DrawMotionPath;
  20. SerializedProperty prp_PrpMotionPathIconSize;
  21. public PlaybackClip(PointCachePlayerInspector inspector, int idx) {
  22. Inspector = inspector;
  23. Idx = idx;
  24. SerializedProperty prp_clip = Inspector.so_player.FindProperty("Clips").GetArrayElementAtIndex(idx);
  25. prp_AutoPlaybackTypeEnum = prp_clip.FindPropertyRelative("AutoPlaybackType");
  26. prp_NormalizedTime = Inspector.so_player.FindProperty(string.Format("Clip{0}NormalizedTime", idx));
  27. prp_Weight = inspector.so_player.FindProperty(string.Format("Clip{0}Weight", idx));
  28. prp_DurationInSeconds = prp_clip.FindPropertyRelative("DurationInSeconds");
  29. prp_DrawMotionPath = prp_clip.FindPropertyRelative("DrawMotionPath");
  30. prp_PrpMotionPathIconSize = prp_clip.FindPropertyRelative("MotionPathIconSize");
  31. coloredIdxStyle = new GUIStyle(Inspector.BoldLabelStyle);
  32. coloredIdxStyle.normal.textColor = PointCachePlayer.GizmosClipColors[idx];
  33. }
  34. public void DrawClipPlaybackInspector(PointCache pointCache) {
  35. PointCache.Clip pcclip = pointCache.Clips[Idx];
  36. string clipLabel = string.Format("{0} frames", pcclip.PostImport.FramesCount);
  37. if (pcclip.PostImport.IsLoop) {
  38. clipLabel += ", loop ";
  39. }
  40. GUILayout.BeginHorizontal();
  41. GUILayout.Space(16);
  42. GUILayout.Label (string.Format("#{0}", Idx ), coloredIdxStyle, GUILayout.Width(20));
  43. GUILayout.Label (pcclip.PostImport.Name, Inspector.BoldLabelStyle );
  44. GUILayout.EndHorizontal();
  45. EditorGUI.indentLevel++;
  46. EditorGUILayout.LabelField(clipLabel, Inspector.LabelStyle);
  47. prp_NormalizedTime.floatValue = EditorGUILayout.Slider(Inspector.s_clipNormalizedTime, prp_NormalizedTime.floatValue, 0, 1f );
  48. EditorGUILayout.Slider( prp_Weight, 0, 1f, Inspector.s_clipWeight);
  49. EditorGUILayout.PropertyField(prp_AutoPlaybackTypeEnum, Inspector.s_clipPlaybackMode);
  50. if (prp_AutoPlaybackTypeEnum.enumValueIndex != 3) {
  51. EditorGUI.indentLevel++;
  52. prp_DurationInSeconds.floatValue = EditorGUILayout.FloatField("Duration", prp_DurationInSeconds.floatValue);
  53. EditorGUI.indentLevel--;
  54. }
  55. if (pointCache.Clips[Idx].MotionPathsCount > 0) {
  56. EditorGUILayout.PropertyField(prp_DrawMotionPath, Inspector.s_DrawMotionPath);
  57. if (prp_DrawMotionPath.boolValue) {
  58. EditorGUI.indentLevel++;
  59. EditorGUILayout.PropertyField(prp_PrpMotionPathIconSize, Inspector.s_MotionPathIconSize);
  60. EditorGUI.indentLevel--;
  61. }
  62. }
  63. EditorGUI.indentLevel--;
  64. }
  65. }
  66. #endregion
  67. #region POINTCACHECLIP
  68. public class PointCacheClip {
  69. int ThisIdx;
  70. PointCachePlayerInspector Inspector;
  71. public SerializedProperty prp_FoldoutIsOpen;
  72. public SerializedProperty prp_Name;
  73. public SerializedProperty prp_FilePath;
  74. public SerializedProperty prp_SwapYZAxis;
  75. public SerializedProperty prp_Scale;
  76. public SerializedProperty prp_ChangeFramesCount;
  77. public SerializedProperty prp_CustomFramesCount;
  78. public SerializedProperty prp_SubFrameInterpolation;
  79. public SerializedProperty prp_EnableCustomRange;
  80. public SerializedProperty prp_CustomRangeFrom;
  81. public SerializedProperty prp_CustomRangeTo;
  82. public SerializedProperty prp_TransitionMode;
  83. public SerializedProperty prp_TransitionLength;
  84. public SerializedProperty prp_IsLoop;
  85. public SerializedProperty prp_EnableMotionSmoothing;
  86. public SerializedProperty prp_MotionSmoothIterations;
  87. public SerializedProperty prp_MotionSmoothAmountMin;
  88. public SerializedProperty prp_MotionSmoothAmountMax;
  89. public SerializedProperty prp_MotionSmoothEaseOffset;
  90. public SerializedProperty prp_MotionSmoothEaseLength;
  91. public SerializedProperty prp_GenerageMotionPaths;
  92. public SerializedProperty prp_MotionPathsIndexStep;
  93. public SerializedProperty prp_EnableNormalizeSpeed;
  94. public SerializedProperty prp_NormalizeSpeedPersentage;
  95. public GUIStyle coloredName;
  96. SmoothCurveGraphTexture _smoothCurveGraph;
  97. public SmoothCurveGraphTexture SmoothCurveGraph {
  98. get {
  99. if (_smoothCurveGraph == null) {
  100. _smoothCurveGraph = new SmoothCurveGraphTexture(prp_MotionSmoothAmountMin.floatValue, prp_MotionSmoothAmountMax.floatValue, prp_MotionSmoothEaseOffset.floatValue, prp_MotionSmoothEaseLength.floatValue, Inspector.IsDarkTheme);
  101. }
  102. return _smoothCurveGraph;
  103. }
  104. }
  105. public PointCacheClip(PointCachePlayerInspector inspector, int idx) {
  106. Inspector = inspector;
  107. ThisIdx = idx;
  108. SerializedProperty prp_PCclip = Inspector.so_pointCache.FindProperty("Clips").GetArrayElementAtIndex(idx);
  109. SerializedProperty prp_PCclipPreImport = prp_PCclip.FindPropertyRelative("PreImport");
  110. prp_Name = prp_PCclipPreImport.FindPropertyRelative("Name");
  111. prp_FoldoutIsOpen = prp_PCclipPreImport.FindPropertyRelative("FoldoutIsOpen");
  112. prp_FilePath = prp_PCclipPreImport.FindPropertyRelative("FilePath");
  113. prp_SwapYZAxis = prp_PCclipPreImport.FindPropertyRelative("SwapYZAxis");
  114. prp_Scale = prp_PCclipPreImport.FindPropertyRelative("Scale");
  115. prp_EnableCustomRange = prp_PCclipPreImport.FindPropertyRelative("EnableCustomRange");
  116. prp_CustomRangeFrom = prp_PCclipPreImport.FindPropertyRelative("CustomRangeFrom");
  117. prp_CustomRangeTo = prp_PCclipPreImport.FindPropertyRelative("CustomRangeTo");
  118. prp_TransitionMode = prp_PCclipPreImport.FindPropertyRelative("TransitionMode");
  119. prp_TransitionLength = prp_PCclipPreImport.FindPropertyRelative("TransitionLength");
  120. prp_ChangeFramesCount = prp_PCclipPreImport.FindPropertyRelative("ChangeFramesCount");
  121. prp_CustomFramesCount = prp_PCclipPreImport.FindPropertyRelative("CustomFramesCount");
  122. prp_SubFrameInterpolation = prp_PCclipPreImport.FindPropertyRelative("SubFrameInterpolation");
  123. prp_IsLoop = prp_PCclipPreImport.FindPropertyRelative("IsLoop");
  124. prp_EnableMotionSmoothing = prp_PCclipPreImport.FindPropertyRelative("EnableMotionSmoothing");
  125. prp_MotionSmoothIterations = prp_PCclipPreImport.FindPropertyRelative("MotionSmoothIterations");
  126. prp_MotionSmoothAmountMin = prp_PCclipPreImport.FindPropertyRelative("MotionSmoothAmountMin");
  127. prp_MotionSmoothAmountMax = prp_PCclipPreImport.FindPropertyRelative("MotionSmoothAmountMax");
  128. prp_MotionSmoothEaseOffset = prp_PCclipPreImport.FindPropertyRelative("MotionSmoothEaseOffset");
  129. prp_MotionSmoothEaseLength = prp_PCclipPreImport.FindPropertyRelative("MotionSmoothEaseLength");
  130. prp_GenerageMotionPaths = prp_PCclipPreImport.FindPropertyRelative("GenerageMotionPaths");
  131. prp_MotionPathsIndexStep = prp_PCclipPreImport.FindPropertyRelative("MotionPathsIndexStep");
  132. prp_EnableNormalizeSpeed = prp_PCclipPreImport.FindPropertyRelative("EnableNormalizeSpeed");
  133. prp_NormalizeSpeedPersentage = prp_PCclipPreImport.FindPropertyRelative("NormalizeSpeedPercentage");
  134. coloredName = new GUIStyle(Inspector.BoldLabelStyle);
  135. coloredName.normal.textColor = PointCachePlayer.GizmosClipColors[idx];
  136. }
  137. void DrawSourceButton( PointCache pointCache ) {
  138. string buttonName = prp_FilePath.stringValue;
  139. if (string.IsNullOrEmpty(buttonName)) {
  140. buttonName = "none";
  141. }
  142. EditorGUILayout.LabelField("Path to .pc2 file or .obj files sequence", Inspector.LabelStyle);
  143. GUILayout.BeginHorizontal();
  144. GUILayout.Space(16);
  145. if (GUILayout.Button(buttonName)) {
  146. string dir = UnityEditor.EditorPrefs.GetString("pc2unityLastOpenedDirectory");
  147. string nselect = EditorUtility.OpenFilePanelWithFilters("Select point cache", dir, new string[4] { "PC2", "pc2", "OBJ", "obj" });
  148. if (File.Exists(nselect)) {
  149. UnityEditor.EditorPrefs.SetString("pc2unityLastOpenedDirectory", new FileInfo(nselect).Directory.FullName);
  150. }
  151. prp_FilePath.stringValue = nselect;
  152. Inspector.so_pointCache.ApplyModifiedProperties();
  153. pointCache.Clips[ThisIdx].PreImport.CheckAndUpdateInfo(pointCache.Meshes[0].VertsCount);
  154. }
  155. GUILayout.EndHorizontal();
  156. GUILayout.BeginHorizontal();
  157. GUILayout.Space(16);
  158. if (GUILayout.Button(Inspector.s_RefreshPointCacheClip, Inspector.QuadButtonStyle )) {
  159. pointCache.Clips[ThisIdx].PreImport.CheckAndUpdateInfo(pointCache.Meshes[0].VertsCount);
  160. }
  161. GUILayout.Label(pointCache.Clips[ThisIdx].PreImport.FileInfo, Inspector.LabelStyle );
  162. GUILayout.EndHorizontal();
  163. GUILayout.Space(4);
  164. }
  165. void DrawCustomRange( PointCache pointCache ) {
  166. EditorGUILayout.PropertyField(prp_EnableCustomRange, Inspector.s_importCustomRange);
  167. if (prp_EnableCustomRange.boolValue) {
  168. EditorGUI.indentLevel++;
  169. int _from = EditorGUILayout.IntField("From", prp_CustomRangeFrom.intValue);
  170. int _to = EditorGUILayout.IntField("To", prp_CustomRangeTo.intValue);
  171. prp_CustomRangeFrom.intValue = _from < 0 ? 0 : _from;
  172. prp_CustomRangeTo.intValue = _to<_from ? _from : _to;
  173. EditorGUI.indentLevel--;
  174. }
  175. }
  176. void DrawTransition( PointCache pointCache ) {
  177. prp_TransitionLength.intValue = pointCache.Clips[ThisIdx].PreImport.TransitionFramesCount;
  178. EditorGUILayout.PropertyField(prp_TransitionMode, Inspector.s_TransitionMode);
  179. int maxBegin = pointCache.Clips[ThisIdx].PreImport.MaxBeginTransitionLength;
  180. int maxEnd = pointCache.Clips[ThisIdx].PreImport.MaxEndTransitionLength;
  181. if (prp_TransitionMode.intValue == 1) {
  182. EditorGUI.indentLevel++;
  183. if (maxBegin > 0) {
  184. EditorGUILayout.IntSlider(prp_TransitionLength, 0, maxBegin, Inspector.s_TransitionLengthBegin);
  185. } else {
  186. EditorGUILayout.HelpBox("To build Begin transition the last frame of the Import Custom Range should be less than source clip length.", MessageType.Warning);
  187. }
  188. EditorGUI.indentLevel--;
  189. }
  190. if (prp_TransitionMode.intValue == 2) {
  191. EditorGUI.indentLevel++;
  192. if (maxEnd > 0) {
  193. EditorGUILayout.IntSlider(prp_TransitionLength, 0, maxEnd, Inspector.s_TransitionLengthEnd);
  194. } else {
  195. EditorGUILayout.HelpBox("To build End transition the first frame of the Import Custom Range should be greater than 0", MessageType.Warning);
  196. }
  197. EditorGUI.indentLevel--;
  198. }
  199. }
  200. public void DrawInspector(PointCache pointCache) {
  201. GUILayout.Space(2);
  202. GUILayout.BeginHorizontal();
  203. prp_FoldoutIsOpen.boolValue = EditorGUILayout.Toggle(prp_FoldoutIsOpen.boolValue, Inspector.FoldoutToggleStyle, GUILayout.Width(13));
  204. EditorGUILayout.LabelField(string.Format("#{0} ", ThisIdx ), coloredName, GUILayout.Width(20));
  205. EditorGUILayout.LabelField(string.Format("{0} ", prp_Name.stringValue), Inspector.BoldLabelStyle);
  206. GUILayout.EndHorizontal();
  207. if (prp_FoldoutIsOpen.boolValue) {
  208. EditorGUI.indentLevel++;
  209. DrawSourceButton(pointCache);
  210. EditorGUILayout.PropertyField(prp_Name);
  211. EditorGUILayout.PropertyField(prp_SwapYZAxis, new GUIContent("Swap YZ"));
  212. EditorGUILayout.PropertyField(prp_Scale, new GUIContent("Scale"));
  213. prp_IsLoop.boolValue = EditorGUILayout.Toggle("Loop", prp_IsLoop.boolValue);
  214. DrawCustomRange(pointCache);
  215. DrawTransition(pointCache);
  216. EditorGUILayout.PropertyField(prp_ChangeFramesCount, Inspector.s_ChangeFramesCount);
  217. if (prp_ChangeFramesCount.boolValue) {
  218. EditorGUI.indentLevel++;
  219. EditorGUILayout.PropertyField(prp_CustomFramesCount, Inspector.s_CustomFramesCount);
  220. EditorGUILayout.PropertyField(prp_SubFrameInterpolation, Inspector.s_SubFramesInterpolationMode);
  221. EditorGUI.indentLevel--;
  222. }
  223. EditorGUILayout.PropertyField(prp_EnableMotionSmoothing, Inspector.s_EnableMotionSmoothing);
  224. if (prp_EnableMotionSmoothing.boolValue) {
  225. EditorGUI.indentLevel++;
  226. EditorGUILayout.PropertyField(prp_MotionSmoothIterations, Inspector.s_MotionSmoothIterations);
  227. float smoothAmountMin = prp_MotionSmoothAmountMin.floatValue;
  228. float smoothAmountMax = prp_MotionSmoothAmountMax.floatValue;
  229. EditorGUILayout.MinMaxSlider(Inspector.s_MotionSmoothAmount, ref smoothAmountMin, ref smoothAmountMax, 0, 1f);
  230. float smoothEaseOffset = prp_MotionSmoothEaseOffset.floatValue;
  231. float smoothEaseLength = prp_MotionSmoothEaseLength.floatValue;
  232. EditorGUILayout.MinMaxSlider(Inspector.s_MotionSmoothEase, ref smoothEaseOffset, ref smoothEaseLength, 0, 0.5f);
  233. if (smoothAmountMin != prp_MotionSmoothAmountMin.floatValue || smoothAmountMax != prp_MotionSmoothAmountMax.floatValue || smoothEaseOffset != prp_MotionSmoothEaseOffset.floatValue || smoothEaseLength != prp_MotionSmoothEaseLength.floatValue) {
  234. prp_MotionSmoothAmountMin.floatValue = Mathf.Clamp(smoothAmountMin, 0, 1f);
  235. prp_MotionSmoothAmountMax.floatValue = Mathf.Clamp(smoothAmountMax, 0, 1f);
  236. prp_MotionSmoothEaseOffset.floatValue = Mathf.Clamp(smoothEaseOffset, 0, 0.5f);
  237. prp_MotionSmoothEaseLength.floatValue = Mathf.Clamp(smoothEaseLength, 0, 0.5f);
  238. SmoothCurveGraph.RepaintTexture(smoothAmountMin, smoothAmountMax, smoothEaseOffset, smoothEaseLength);
  239. }
  240. EditorGUI.indentLevel--;
  241. if (Event.current.type == EventType.Repaint) {
  242. Rect lastrect = GUILayoutUtility.GetLastRect();
  243. Rect graphRect = new Rect(lastrect.center.x - 64 + 17, lastrect.yMax + 9, 100, 50);
  244. GUI.DrawTexture(graphRect, SmoothCurveGraph.GraphTexture);
  245. Rect backgroundRect = new Rect(lastrect.center.x - 64, lastrect.yMax + 4, 128, 64);
  246. GUI.DrawTexture(backgroundRect, Inspector.GraphBackgroundTexture );
  247. }
  248. GUILayout.Space(70);
  249. }
  250. EditorGUILayout.PropertyField(prp_EnableNormalizeSpeed, Inspector.s_EnableNormalizeSpeed);
  251. if (prp_EnableNormalizeSpeed.boolValue) {
  252. EditorGUI.indentLevel++;
  253. EditorGUILayout.Slider(prp_NormalizeSpeedPersentage, 0, 1, Inspector.s_NormalizeSpeedPersentage);
  254. EditorGUI.indentLevel--;
  255. }
  256. EditorGUILayout.PropertyField(prp_GenerageMotionPaths, Inspector.s_GenerateGizmoPaths);
  257. if (prp_GenerageMotionPaths.boolValue) {
  258. EditorGUI.indentLevel++;
  259. EditorGUILayout.PropertyField(prp_MotionPathsIndexStep, Inspector.s_MotionPathsIndexStep);
  260. EditorGUI.indentLevel--;
  261. }
  262. EditorGUI.indentLevel--;
  263. }
  264. }
  265. }
  266. #endregion
  267. #region CONSTRAINT
  268. public class Constraint {
  269. int ThisIdx;
  270. PointCachePlayerInspector Inspector;
  271. SerializedProperty prp_Name;
  272. PointCache.PreImportConstraint constraint;
  273. public Constraint(PointCachePlayerInspector inspector, int idx, PointCache.PreImportConstraint _constraint) {
  274. Inspector = inspector;
  275. ThisIdx = idx;
  276. constraint = _constraint;
  277. SerializedProperty prp_costraint = Inspector.so_pointCache.FindProperty("PreConstraints").GetArrayElementAtIndex(idx);
  278. prp_Name = prp_costraint.FindPropertyRelative("Name");
  279. }
  280. public void DrawInspector() {
  281. GUILayout.Space(12);
  282. GUILayout.Label( string.Format( "#{0} {1}", ThisIdx.ToString(), prp_Name.stringValue), Inspector.BoldLabelStyle);
  283. EditorGUI.indentLevel++;
  284. EditorGUILayout.PropertyField(prp_Name);
  285. GUILayout.BeginHorizontal();
  286. GUILayout.Space(16);
  287. if (Inspector.currentEditedConstraint == constraint) {
  288. if (GUILayout.Button("Edit mode", ResourceHolder.PressedButton)) {
  289. Inspector.currentEditedConstraint = null;
  290. SceneView.RepaintAll();
  291. }
  292. } else {
  293. if (GUILayout.Button("Edit mode")) {
  294. Inspector.currentEditedConstraint = constraint;
  295. SceneView.RepaintAll();
  296. }
  297. }
  298. GUILayout.EndHorizontal();
  299. EditorGUI.indentLevel--;
  300. }
  301. }
  302. #endregion
  303. #region PLAYBACKCONSTRAINT
  304. public class PlaybackConstraint {
  305. int ThisIdx;
  306. PointCachePlayerInspector Inspector;
  307. SerializedProperty prp_transform;
  308. string name;
  309. GUIContent s_field;
  310. public PlaybackConstraint(PointCachePlayerInspector inspector, int idx ) {
  311. Inspector = inspector;
  312. ThisIdx = idx;
  313. prp_transform = Inspector.so_player.FindProperty("Constraints").GetArrayElementAtIndex(idx).FindPropertyRelative("Tr");
  314. name = Inspector.so_player.FindProperty("Constraints").GetArrayElementAtIndex(idx).FindPropertyRelative("Name").stringValue;
  315. //Debug.LogFormat("name: {0}", name);
  316. s_field = new GUIContent(name, string.Format( "constrained transform #{0} {1}", ThisIdx, name ) );
  317. }
  318. public void DrawInspector() {
  319. EditorGUI.indentLevel++;
  320. EditorGUILayout.PropertyField(prp_transform, s_field);
  321. EditorGUI.indentLevel--;
  322. }
  323. }
  324. #endregion
  325. #region MESH
  326. public class PolygonMesh {
  327. int ThisIdx;
  328. PointCachePlayerInspector Inspector;
  329. PointCache pc;
  330. SerializedProperty prp_Path;
  331. SerializedProperty prp_Name;
  332. public PolygonMesh( PointCachePlayerInspector inspector, int idx, PointCache pc ) {
  333. this.pc = pc;
  334. Inspector = inspector;
  335. ThisIdx = idx;
  336. prp_Path = Inspector.so_pointCache.FindProperty("Meshes").GetArrayElementAtIndex(idx).FindPropertyRelative("Path");
  337. prp_Name = Inspector.so_pointCache.FindProperty("Meshes").GetArrayElementAtIndex(idx).FindPropertyRelative("Name");
  338. }
  339. public void DrawInspector() {
  340. GUILayout.Space(12);
  341. GUIContent buttonContent = new GUIContent();
  342. string labelString = ThisIdx == 0 ? string.Format("#{0} ( Main ) {1} ", ThisIdx, prp_Name.stringValue) : string.Format("#{0} {1}", ThisIdx, prp_Name.stringValue);
  343. if (ThisIdx == 0) {
  344. buttonContent.tooltip = " Mesh 0 (Main) vertices count should match up with point cache source";
  345. } else {
  346. buttonContent.tooltip = "Can contains arbitary vertex count";
  347. }
  348. EditorGUILayout.LabelField(labelString, Inspector.BoldLabelStyle);
  349. EditorGUI.indentLevel++;
  350. EditorGUILayout.PropertyField(prp_Name);
  351. buttonContent.text = prp_Path.stringValue;
  352. if(string.IsNullOrEmpty(buttonContent.text)){
  353. buttonContent.text = "none";
  354. }
  355. EditorGUILayout.LabelField("Path to mesh .obj file");
  356. GUILayout.BeginHorizontal();
  357. GUILayout.Space(16);
  358. if (GUILayout.Button( buttonContent )){
  359. string dir = EditorPrefs.GetString("pc2unityLastOpenedDirectory");
  360. string nselect = EditorUtility.OpenFilePanelWithFilters( "Select .obj file", dir, new string[2]{"obj","OBJ"});
  361. if(File.Exists(nselect)){
  362. EditorPrefs.SetString( "pc2unityLastOpenedDirectory", new FileInfo(nselect).Directory.FullName);
  363. }
  364. prp_Path.stringValue = nselect;
  365. prp_Path.serializedObject.ApplyModifiedProperties();
  366. pc.Meshes[ThisIdx].CheckAndUpdateInfo();
  367. }
  368. GUILayout.EndHorizontal();
  369. GUILayout.BeginHorizontal();
  370. GUILayout.Space(16);
  371. if (GUILayout.Button(Inspector.s_RefreshMesh, Inspector.QuadButtonStyle)) {
  372. pc.Meshes[ThisIdx].CheckAndUpdateInfo();
  373. Inspector.so_pointCache.Update();
  374. }
  375. GUILayout.Label(pc.Meshes[ThisIdx].Info, Inspector.LabelStyle);
  376. GUILayout.EndHorizontal();
  377. EditorGUI.indentLevel--;
  378. }
  379. }
  380. #endregion
  381. SerializedObject so_player;
  382. SerializedObject so_pointCache;
  383. PlaybackClip[] playbackClips;
  384. PointCacheClip[] importClips;
  385. Constraint[] costraints;
  386. PolygonMesh[] meshed;
  387. PlaybackConstraint[] playbackConstraints;
  388. SerializedProperty prp_pointCache;
  389. SerializedProperty prp_constraintHandlesSize;
  390. SerializedProperty prp_drawConstraintHandlesName;
  391. SerializedProperty prp_selectedTabIdx;
  392. SerializedProperty prp_selectedImportTabIdx;
  393. static GUIContent s_addMeshButton;
  394. static GUIContent s_removeMeshButton;
  395. static GUIContent s_addClipButton;
  396. static GUIContent s_removeClipButton;
  397. static GUIContent s_addConstraintButton;
  398. static GUIContent s_removeConstraintButton;
  399. internal SerializedProperty prp_ImportUsedClipsCount;
  400. internal SerializedProperty prp_PlaybackUsedClipsCount;
  401. internal SerializedProperty prp_PlaybackUsedMeshCount;
  402. internal SerializedProperty prp_ImportUsedMeshCount;
  403. internal SerializedProperty prp_activeMeshIdx;
  404. internal SerializedProperty prp_drawMeshGizmo;
  405. string[] tabNames = new string[2] { "Playback", "Import" };
  406. string[] importToolbarNames = new string[3] { "Meshes", "Clips", "Constraints" };
  407. GUIContent s_RefreshMesh ;
  408. GUIContent s_RefreshPointCacheClip;
  409. GUIContent s_ChangeFramesCount = new GUIContent("Change frames count","Enables custom frames count. As a result, the clip plays over a greater or lesser number of frames. Descreasing frames count reduce memory usage.");
  410. GUIContent s_CustomFramesCount = new GUIContent("Frames count","The new frames count");
  411. GUIContent s_SubFramesInterpolationMode = new GUIContent("Interpolation","Interpolation mode controls set how generates new frame. Linear mode uses linear interpolation, Hermite uses curve based algorthm");
  412. GUIContent s_EnableMotionSmoothing = new GUIContent("Motion smoothing", "Enable motion smoothing with following settings. This option respects Loop option so you can use Motion Smoothing for bluring start-end transition and make clip seamless repeated");
  413. GUIContent s_MotionSmoothIterations = new GUIContent("Iterations", "Increasing this value increase motion smooth");
  414. GUIContent s_MotionSmoothAmount = new GUIContent("Min-Max", "Amount of smoothing over time");
  415. GUIContent s_MotionSmoothEase = new GUIContent("Offset-Length", "Define amount ease start-end of the clip over time");
  416. GUIContent s_GenerateGizmoPaths = new GUIContent("Generate Paths", "Motion paths is a position data for each vertex over clip time in object local space. Useful for debug preview of clip motion. To display generated paths, enable Draw Motion Paths in Playback tab after importing. Not forget to disable Motion Paths before final build because paths data increased asset size.");
  417. GUIContent s_MotionPathsIndexStep = new GUIContent("Index step", "Defines index stride of vertices which will be used generate motion path. 1 mean all vertices will generate own path. ");
  418. GUIContent s_EnableNormalizeSpeed = new GUIContent("Normalize Speed", "This options makes motion speed constant with given value of blend with original speed. When Normalize Speed is 1.0, all vertices moves with constant speed. ");
  419. GUIContent s_NormalizeSpeedPersentage = new GUIContent("Normalize Speed", "Blend normalized speed with original over this value");
  420. GUIContent s_costrainGizmoSize = new GUIContent("Gizmo size", "Size of constrant gizmo on Scene View");
  421. GUIContent s_drawCostrainHandlesName = new GUIContent("Draw name", "Enables name label");
  422. GUIContent s_activeMesh = new GUIContent("Active mesh", "Which mesh will be displayed");
  423. GUIContent s_drawMeshGizmo = new GUIContent("Draw gizmo", "Enables gizmo of active mesh");
  424. bool toolsIsHiddenOnEnable;
  425. PointCache.PreImportConstraint currentEditedConstraint;
  426. void OnEnable(){
  427. OnEnableBase();
  428. toolsIsHiddenOnEnable = Tools.hidden;
  429. s_addMeshButton = new GUIContent( AddButtonTexture, "Add mesh");
  430. s_removeMeshButton = new GUIContent(RemoveButtonTexture, "Remove last mesh");
  431. s_addClipButton = new GUIContent( AddButtonTexture, "Add clip" );
  432. s_removeClipButton = new GUIContent( RemoveButtonTexture, "Remove last clip" );
  433. s_addConstraintButton = new GUIContent( AddButtonTexture, "Add constraint");
  434. s_removeConstraintButton = new GUIContent( RemoveButtonTexture, "Remove last constraint");
  435. s_RefreshMesh = new GUIContent(RefreshButtonTexture, "Refresh source mesh statistic" );
  436. s_RefreshPointCacheClip = new GUIContent( RefreshButtonTexture, "Refresh source info" );
  437. CollectProperties();
  438. currentEditedConstraint = null;
  439. }
  440. void OnDisable() {
  441. Tools.hidden = toolsIsHiddenOnEnable;
  442. currentEditedConstraint = null;
  443. }
  444. void CollectProperties(){
  445. so_player = new SerializedObject(targets);
  446. PointCachePlayer player = target as PointCachePlayer;
  447. prp_pointCache = so_player.FindProperty( "pointCache" );
  448. prp_activeMeshIdx = so_player.FindProperty("ActiveMesh");
  449. prp_drawMeshGizmo = so_player.FindProperty("DrawMeshGizmo");
  450. prp_playerUpdateMode = so_player.FindProperty("UpdateMode");
  451. prp_useTimescale = so_player.FindProperty("UseTimescale");
  452. playbackClips = new PlaybackClip[8];
  453. for(int i = 0; i<playbackClips.Length; i++){
  454. playbackClips[i] = new PlaybackClip(this, i);
  455. }
  456. if (prp_pointCache.objectReferenceValue == null){
  457. return;
  458. }
  459. PointCache pc = (target as PointCachePlayer).pointCache;
  460. playbackConstraints = new PlaybackConstraint[player.Constraints.Length];
  461. for (int i = 0; i < playbackConstraints.Length; i++) {
  462. playbackConstraints[i] = new PlaybackConstraint(this, i );
  463. }
  464. so_pointCache = new SerializedObject( pc );
  465. prp_constraintHandlesSize = so_pointCache.FindProperty("ConstraintHandlesSize");
  466. prp_drawConstraintHandlesName = so_pointCache.FindProperty("DrawConstraintHandlesName");
  467. prp_selectedTabIdx = so_pointCache.FindProperty("SelectedTabIdx");
  468. prp_selectedImportTabIdx = so_pointCache.FindProperty("SelectedImportTabIdx");
  469. SerializedProperty prp_postImport = so_pointCache.FindProperty("PostImport");
  470. prp_PlaybackUsedClipsCount = prp_postImport.FindPropertyRelative("UsedClipsCount");
  471. prp_PlaybackUsedMeshCount = prp_postImport.FindPropertyRelative("UsedMeshesCount");
  472. SerializedProperty prp_preImport = so_pointCache.FindProperty("PreImport");
  473. prp_swapYZAxis = prp_preImport.FindPropertyRelative("SwapYZAxis");
  474. prp_scaleFactor = prp_preImport.FindPropertyRelative("ScaleFactor");
  475. prp_flipNormals = prp_preImport.FindPropertyRelative("FlipNormals");
  476. prp_smoothingGroupImportMode = prp_preImport.FindPropertyRelative("SmoothingGroupImportMode");
  477. prp_normalRecalculationMode = prp_preImport.FindPropertyRelative("NormalRecalculationMode");
  478. prp_meshCompression = prp_preImport.FindPropertyRelative("MeshCompression");
  479. prp_OptimizeMesh = prp_preImport.FindPropertyRelative("OptimizeMesh");
  480. prp_ImportUsedClipsCount = prp_preImport.FindPropertyRelative("UsedClipsCount");
  481. prp_ImportUsedMeshCount = prp_preImport.FindPropertyRelative("UsedMeshesCount");
  482. prp_generateMaterials = prp_preImport.FindPropertyRelative("GenerateMaterials");
  483. prp_savePortableData = prp_preImport.FindPropertyRelative("SavePortableData");
  484. #if UNITY_2017_3_OR_NEWER
  485. prp_IndexFormat = prp_preImport.FindPropertyRelative("IndexFormat");
  486. #endif
  487. costraints = new Constraint[pc.PreConstraints.Count];
  488. for (int i = 0; i< costraints.Length; i++) {
  489. costraints[i] = new Constraint(this, i, pc.PreConstraints[i]);
  490. }
  491. importClips = new PointCacheClip[8];
  492. for(int i = 0; i<importClips.Length; i++){
  493. importClips[i] = new PointCacheClip(this, i);
  494. }
  495. meshed = new PolygonMesh[4];
  496. for (int i = 0; i<meshed.Length; i++){
  497. meshed[i] = new PolygonMesh(this, i, pc);
  498. //Debug.LogFormat("lod {0}", lods[i]);
  499. }
  500. }
  501. public override void OnInspectorGUI(){
  502. PointCachePlayer player = target as PointCachePlayer;
  503. so_player.Update();
  504. EditorGUI.showMixedValue = so_player.isEditingMultipleObjects;
  505. if (DrawPointCacheField()) {
  506. so_pointCache.Update();
  507. EditorGUI.BeginChangeCheck();
  508. prp_selectedTabIdx.intValue = GUILayout.Toolbar(prp_selectedTabIdx.intValue, tabNames);
  509. if (EditorGUI.EndChangeCheck()) {
  510. so_pointCache.ApplyModifiedProperties();
  511. }
  512. if (prp_selectedTabIdx.intValue == 0) {
  513. DrawPlaybackInspector(player);
  514. } else if (prp_selectedTabIdx.intValue == 1) {
  515. ImportInspector(player, player.pointCache);
  516. }
  517. }
  518. }
  519. bool DrawPointCacheField() {
  520. EditorGUI.BeginChangeCheck();
  521. prp_pointCache.objectReferenceValue = EditorGUILayout.ObjectField("Point Cache", prp_pointCache.objectReferenceValue, typeof(PointCache), false);
  522. if (EditorGUI.EndChangeCheck()) {
  523. so_player.ApplyModifiedProperties();
  524. CollectProperties();
  525. }
  526. if (prp_pointCache.objectReferenceValue == null) {
  527. EditorGUILayout.HelpBox("Select Point Cache asset", MessageType.Warning);
  528. return false;
  529. }
  530. return true;
  531. }
  532. public void DrawPlaybackInspector(PointCachePlayer player) {
  533. so_player.Update();
  534. EditorGUI.BeginChangeCheck();
  535. EditorGUILayout.PropertyField(prp_playerUpdateMode, s_playerUpdateMode);
  536. EditorGUILayout.PropertyField(prp_useTimescale, s_useTimescale);
  537. EditorGUILayout.LabelField("Meshes", LabelStyle);
  538. List<GUIContent> meshesNames = new List<GUIContent>();
  539. for (int i = 0; i< player.pointCache.PostImport.UsedMeshesCount; i++) {
  540. meshesNames.Add( new GUIContent( player.pointCache.Meshes[i].Name ));
  541. }
  542. EditorGUI.indentLevel++;
  543. prp_activeMeshIdx.intValue = EditorGUILayout.Popup(s_activeMesh, prp_activeMeshIdx.intValue, meshesNames.ToArray());
  544. EditorGUILayout.PropertyField(prp_drawMeshGizmo, s_drawMeshGizmo);
  545. EditorGUI.indentLevel--;
  546. EditorGUILayout.LabelField("Clips", LabelStyle);
  547. EditorGUI.indentLevel++;
  548. for (int i = 0; i < prp_PlaybackUsedClipsCount.intValue; i++) {
  549. playbackClips[i].DrawClipPlaybackInspector(player.pointCache);
  550. GUILayout.Space(6);
  551. }
  552. EditorGUI.indentLevel--;
  553. if (playbackConstraints.Length > 0) {
  554. GUILayout.Label("Constraints", LabelStyle );
  555. for (int i = 0; i < playbackConstraints.Length; i++) {
  556. playbackConstraints[i].DrawInspector();
  557. }
  558. }
  559. if (EditorGUI.EndChangeCheck()) {
  560. so_player.ApplyModifiedProperties();
  561. }
  562. }
  563. public void ImportInspector(PointCachePlayer player, PointCache pointCache) {
  564. GUILayout.Space(6);
  565. so_pointCache.Update();
  566. EditorGUI.BeginChangeCheck();
  567. EditorGUILayout.PropertyField(prp_savePortableData, s_savePortableData);
  568. bool nBrightIcons = EditorGUILayout.Toggle(s_brightIcons, brightIcons);
  569. if (nBrightIcons != brightIcons) {
  570. brightIcons = nBrightIcons;
  571. OnEnable();
  572. }
  573. GUILayout.Space(6);
  574. prp_selectedImportTabIdx.intValue = GUILayout.Toolbar(prp_selectedImportTabIdx.intValue, importToolbarNames);
  575. if (prp_selectedImportTabIdx.intValue == 0) {
  576. DrawImportGeometryTab();
  577. } else if (prp_selectedImportTabIdx.intValue == 1) {
  578. DrawImportClipsTab( pointCache );
  579. } else if (prp_selectedImportTabIdx.intValue == 2) {
  580. DrawImportConstraintsTab( pointCache );
  581. }
  582. if (EditorGUI.EndChangeCheck()) {
  583. so_pointCache.ApplyModifiedProperties();
  584. }
  585. GUILayout.Space(6);
  586. if (GUILayout.Button(pointCache.ImportSettingsIsDirty ? s_importButtonRequireReimport : s_importButton ) ){
  587. Import( );
  588. }
  589. GUILayout.Space(6);
  590. }
  591. void DrawImportGeometryTab() {
  592. GUILayout.Space(6);
  593. EditorGUILayout.PropertyField(prp_smoothingGroupImportMode, s_smoothingGroupImportMode );
  594. EditorGUILayout.PropertyField(prp_normalRecalculationMode, s_normalRecalculationMode );
  595. EditorGUILayout.PropertyField(prp_flipNormals, s_flipNormals);
  596. EditorGUILayout.PropertyField(prp_swapYZAxis, s_swapYZAxis);
  597. EditorGUILayout.PropertyField(prp_scaleFactor, s_scaleFactor);
  598. prp_meshCompression.intValue = EditorGUILayout.IntPopup(s_meshCompression, prp_meshCompression.intValue, MeshCompressionNames, MeshCompressionIndeces);
  599. EditorGUILayout.PropertyField(prp_OptimizeMesh, s_OptimizeMesh);
  600. EditorGUILayout.PropertyField(prp_generateMaterials, s_generateMaterials);
  601. #if UNITY_2017_3_OR_NEWER
  602. EditorGUILayout.PropertyField(prp_IndexFormat, s_IndexFormat);
  603. #endif
  604. GUILayout.Space(8);
  605. GUILayout.BeginHorizontal();
  606. if (GUILayout.Button(s_addMeshButton, QuadButtonStyle, GUILayout.Width(20))) {
  607. prp_ImportUsedMeshCount.intValue = Mathf.Clamp(prp_ImportUsedMeshCount.intValue + 1, 1, 4);
  608. }
  609. if (GUILayout.Button(s_removeMeshButton, QuadButtonStyle, GUILayout.Width(20))) {
  610. prp_ImportUsedMeshCount.intValue = Mathf.Clamp(prp_ImportUsedMeshCount.intValue - 1, 1, 4);
  611. }
  612. GUILayout.EndHorizontal();
  613. for (int i = 0; i < prp_ImportUsedMeshCount.intValue; i++) {
  614. meshed[i].DrawInspector();
  615. }
  616. }
  617. void DrawImportClipsTab(PointCache pointCache) {
  618. GUILayout.Space(6);
  619. GUILayout.BeginHorizontal();
  620. if (GUILayout.Button(s_addClipButton, QuadButtonStyle)) {
  621. prp_ImportUsedClipsCount.intValue = Mathf.Clamp(prp_ImportUsedClipsCount.intValue + 1, 0, 8);
  622. }
  623. if (GUILayout.Button(s_removeClipButton, QuadButtonStyle)) {
  624. prp_ImportUsedClipsCount.intValue = Mathf.Clamp(prp_ImportUsedClipsCount.intValue - 1, 0, 8);
  625. }
  626. GUILayout.EndHorizontal();
  627. for (int i = 0; i < prp_ImportUsedClipsCount.intValue; i++) {
  628. importClips[i].DrawInspector(pointCache);
  629. }
  630. }
  631. void DrawImportConstraintsTab(PointCache pointCache) {
  632. GUILayout.Space(6);
  633. GUILayout.BeginHorizontal();
  634. if (GUILayout.Button(s_addConstraintButton, QuadButtonStyle)) {
  635. PointCache.PreImportConstraint newItem = new PointCache.PreImportConstraint( string.Format("New constraint {0}", pointCache.PreConstraints.Count) );
  636. pointCache.PreConstraints.Add(newItem);
  637. currentEditedConstraint = newItem;
  638. CollectProperties();
  639. }
  640. if (GUILayout.Button(s_removeConstraintButton, QuadButtonStyle)) {
  641. if (pointCache.PreConstraints.Count > 0) {
  642. PointCache.PreImportConstraint toRemove = pointCache.PreConstraints[pointCache.PreConstraints.Count - 1];
  643. if (toRemove == currentEditedConstraint) {
  644. currentEditedConstraint = null;
  645. }
  646. pointCache.PreConstraints.Remove(toRemove);
  647. }
  648. CollectProperties();
  649. }
  650. GUILayout.EndHorizontal();
  651. EditorGUILayout.Slider(prp_constraintHandlesSize, 0, 10, s_costrainGizmoSize);
  652. prp_drawConstraintHandlesName.boolValue = EditorGUILayout.Toggle(s_drawCostrainHandlesName, prp_drawConstraintHandlesName.boolValue);
  653. SceneView.RepaintAll();
  654. for (int i = 0; i < costraints.Length; i++) {
  655. costraints[i].DrawInspector();
  656. }
  657. }
  658. struct ConstraintHandleHelper {
  659. public PointCache.PreImportConstraint Constraint;
  660. public float dist;
  661. public Matrix4x4 wtm;
  662. public static int Comparer(ConstraintHandleHelper a, ConstraintHandleHelper b) {
  663. return (int)(b.dist - a.dist);
  664. }
  665. }
  666. ConstraintHandleHelper[] handlesHelpers;
  667. Camera sceneViewCamera;
  668. void OnSceneGUI() {
  669. PointCachePlayer player = target as PointCachePlayer;
  670. PointCache pointCache = player.pointCache;
  671. if (SceneView.lastActiveSceneView.camera != null) {
  672. sceneViewCamera = SceneView.lastActiveSceneView.camera;
  673. }
  674. if (pointCache == null) {
  675. Tools.hidden = toolsIsHiddenOnEnable;
  676. return;
  677. }
  678. if (prp_selectedTabIdx.intValue != 1 || prp_selectedImportTabIdx.intValue != 2) {
  679. Tools.hidden = toolsIsHiddenOnEnable;
  680. return;
  681. }
  682. DrawConstraintsGizmos(player, pointCache);
  683. if (currentEditedConstraint != null) {
  684. Tools.hidden = true;
  685. Matrix4x4 wtm = player.transform.localToWorldMatrix * currentEditedConstraint.ObjSpace.Matrix;
  686. Vector3 bPos = wtm.GetColumn(3);
  687. Quaternion bRot = Quaternion.LookRotation((Vector3)wtm.GetColumn(2), (Vector3)wtm.GetColumn(1));
  688. bool changed = false;
  689. if (!Event.current.alt && Tools.current == Tool.Move) {
  690. Vector3 pos = Handles.PositionHandle(bPos, bRot);
  691. if (pos != bPos) {
  692. bPos = pos;
  693. changed = true;
  694. }
  695. }
  696. if (!Event.current.alt && Tools.current == Tool.Rotate) {
  697. Quaternion rot = Handles.RotationHandle(bRot, bPos);
  698. if (rot != bRot) {
  699. bRot = rot;
  700. changed = true;
  701. }
  702. }
  703. if (changed) {
  704. wtm = Matrix4x4.TRS(bPos, bRot, Vector3.one);
  705. currentEditedConstraint.ObjSpace.Matrix = player.transform.worldToLocalMatrix * wtm;
  706. pointCache.ImportSettingsIsDirtyFlag = true;
  707. Repaint();
  708. }
  709. } else {
  710. Tools.hidden = toolsIsHiddenOnEnable;
  711. }
  712. }
  713. void DrawConstraintsGizmos(PointCachePlayer player, PointCache pointCache ) {
  714. if (handlesHelpers == null || handlesHelpers.Length != pointCache.PreConstraints.Count) {
  715. handlesHelpers = new ConstraintHandleHelper[pointCache.PreConstraints.Count];
  716. }
  717. Matrix4x4 trltw = player.transform.localToWorldMatrix;
  718. for (int i = 0; i < handlesHelpers.Length; i++) {
  719. handlesHelpers[i].Constraint = pointCache.PreConstraints[i];
  720. handlesHelpers[i].wtm = trltw * pointCache.PreConstraints[i].ObjSpace.Matrix;
  721. handlesHelpers[i].dist = sceneViewCamera.WorldToViewportPoint(handlesHelpers[i].wtm.GetColumn(3)).z;
  722. }
  723. System.Array.Sort(handlesHelpers, ConstraintHandleHelper.Comparer);
  724. for (int i = 0; i < handlesHelpers.Length; i++) {
  725. PointCache.PreImportConstraint c = handlesHelpers[i].Constraint;
  726. Matrix4x4 wtm = handlesHelpers[i].wtm;
  727. Vector3 bPos = wtm.GetColumn(3);
  728. if (pointCache.ConstraintHandlesSize > 1) {
  729. DrawTM(wtm, pointCache.ConstraintHandlesSize);
  730. }
  731. Handles.BeginGUI();
  732. Vector2 labelSize = ResourceHolder.ConstraintLabel.CalcSize(new GUIContent(c.Name));
  733. Vector2 guiPos = HandleUtility.WorldToGUIPoint(bPos);
  734. labelSize.x += 4;
  735. Vector2 labelPos = new Vector2(guiPos.x - labelSize.x / 2, guiPos.y + 2);
  736. Vector2 ikonPos = new Vector2(guiPos.x - 8, guiPos.y - 8);
  737. bool isEdit = c == currentEditedConstraint;
  738. if (pointCache.DrawConstraintHandlesName) {
  739. GUI.Label(new Rect(labelPos, labelSize), c.Name, isEdit ? ResourceHolder.ConstraintEditLabel : ResourceHolder.ConstraintLabel);
  740. }
  741. GUI.DrawTexture(new Rect(ikonPos, new Vector2(16, 16)), isEdit ? ResourceHolder.ConstraintEditIcon : ResourceHolder.ConstraintIcon);
  742. Handles.EndGUI();
  743. }
  744. }
  745. #region IMPORT
  746. public void Import(){
  747. PointCachePlayer player = target as PointCachePlayer;
  748. PointCache pc = player.pointCache;
  749. foreach (TaskInfo ti in player.ImportIE()) {
  750. if (ti.Persentage < 0) {
  751. EditorUtility.ClearProgressBar();
  752. EditorUtility.DisplayDialog("Error:", ti.Name, "OK", "");
  753. return;
  754. }
  755. EditorUtility.DisplayProgressBar(string.Format("Import {0} {1}%", player.pointCache.name, (ti.Persentage * 100).ToString("F0")), ti.Name, ti.Persentage);
  756. }
  757. List<Object> usedAssets = new List<Object>();
  758. for (int i = 0; i < pc.PreImport.UsedMeshesCount; i++) {
  759. MeshUtility.SetMeshCompression(pc.Meshes[i].mesh, (ModelImporterMeshCompression)pc.PreImport.MeshCompression);
  760. if (pc.PreImport.OptimizeMesh) {
  761. MeshUtility.Optimize(pc.Meshes[i].mesh);
  762. }
  763. //#if UNITY_2019_2
  764. Mesh meshInstance = Instantiate(pc.Meshes[i].mesh);
  765. meshInstance.name = pc.Meshes[i].mesh.name;
  766. pc.Meshes[i].mesh = (Mesh)AddToAsset(pc, meshInstance);
  767. //#else
  768. //pc.Meshes[i].mesh = (Mesh)AddToAsset( pc, pc.Meshes[i].mesh);
  769. //#endif
  770. usedAssets.Add( pc.Meshes[i].mesh );
  771. }
  772. for (int m = 0; m < pc.Materials.Count; m++) {
  773. pc.Materials[m].Mat = (Material)AddToAsset(pc, pc.Materials[m].Mat);
  774. if (!usedAssets.Contains(pc.Materials[m].Mat)) {
  775. usedAssets.Add(pc.Materials[m].Mat);
  776. }
  777. }
  778. CleanupAsset(pc, usedAssets) ;
  779. EditorUtility.SetDirty(pc);
  780. AssetDatabase.SaveAssets();
  781. AssetDatabase.Refresh();
  782. currentEditedConstraint = null;
  783. CollectProperties();
  784. string pcAssetPath = Application.dataPath + AssetDatabase.GetAssetPath(pc).Remove(0, 6);
  785. FileInfo fi = new FileInfo(pcAssetPath);
  786. pc.AssetFileSize = fi.Length / 1000000f;
  787. pc.ImportingDate = System.DateTime.Now.ToString();
  788. EditorUtility.SetDirty(pc);
  789. AssetDatabase.SaveAssets();
  790. AssetDatabase.Refresh();
  791. player.Init();
  792. if (pc.PostImport.SavePortableData) {
  793. string portableOutputDirectory = AssetDatabase.GetAssetPath(pc);
  794. portableOutputDirectory = portableOutputDirectory.Remove(portableOutputDirectory.Length - 6 - pc.name.Length);
  795. for (int i = 0; i < pc.PostImport.UsedMeshesCount; i++) {
  796. string portableMeshPath = string.Format("{0}{1} {2}.asset", portableOutputDirectory, pc.name, pc.Meshes[i].Name);
  797. Mesh m = AssetDatabase.LoadAssetAtPath<Mesh>(portableMeshPath);
  798. if (m == null) {
  799. m = Instantiate(pc.Meshes[i].mesh);
  800. AssetDatabase.CreateAsset(m, portableMeshPath);
  801. } else {
  802. pc.Meshes[i].mesh.CopyDataTo(m);
  803. }
  804. }
  805. for (int i = 0; i < pc.PostImport.UsedClipsCount; i++) {
  806. PointCache.Clip pcclip = pc.Clips[i];
  807. string portableClipPath = string.Format("{0}{1} {2}.anim", portableOutputDirectory, pc.name, pcclip.PostImport.Name);
  808. AnimationClip animClip = AssetDatabase.LoadAssetAtPath<AnimationClip>(portableClipPath);
  809. if (animClip == null) {
  810. animClip = new AnimationClip();
  811. AssetDatabase.CreateAsset(animClip, portableClipPath);
  812. }
  813. FillAnimClip(animClip, player.smr, pcclip.PostImport.FrameIdxOffset, pcclip.PostImport.FramesCount, player.Clips[i].DurationInSeconds, pcclip.PostImport.IsLoop );
  814. }
  815. AssetDatabase.SaveAssets();
  816. AssetDatabase.Refresh();
  817. }
  818. EditorUtility.ClearProgressBar();
  819. GUIUtility.ExitGUI();
  820. }
  821. void FillAnimClip(AnimationClip clip, SkinnedMeshRenderer smr, int firstFrame, int framesCount, float duration, bool isLoop) {
  822. clip.ClearCurves();
  823. if (isLoop) {
  824. float step = duration / framesCount ;
  825. float tangent = 100f / step;
  826. for (int i = 0; i < framesCount; i++) {
  827. string name = "blendShape." + smr.sharedMesh.GetBlendShapeName( firstFrame+i );
  828. Keyframe[] keys = null;
  829. if (i == 0) { // first
  830. keys = new Keyframe[4];
  831. keys[0] = new Keyframe(0, 100f, tangent, -tangent);
  832. keys[1] = new Keyframe(step, 0f, -tangent, 0);
  833. keys[2] = new Keyframe((framesCount-1)*step, 0f, 0, tangent);
  834. keys[3] = new Keyframe(framesCount * step, 100f, tangent, -tangent);
  835. } else {
  836. keys = new Keyframe[3];
  837. keys[0] = new Keyframe((i - 1) * step, 0f, tangent, tangent);
  838. keys[1] = new Keyframe(i * step, 100f, tangent, -tangent);
  839. keys[2] = new Keyframe((i + 1) * step, 0f, -tangent, tangent);
  840. }
  841. AnimationCurve ac = new AnimationCurve(keys);
  842. clip.SetCurve("", typeof(SkinnedMeshRenderer), name, ac);
  843. }
  844. } else {
  845. float step = duration / (framesCount-1);
  846. float tangent = 100f / step;
  847. for (int i = 0; i < framesCount; i++) {
  848. string name = "blendShape." + smr.sharedMesh.GetBlendShapeName( firstFrame+i );
  849. Keyframe[] keys = null;
  850. if (i == 0) { // first
  851. keys = new Keyframe[2];
  852. keys[0] = new Keyframe(0, 100f, tangent, -tangent);
  853. keys[1] = new Keyframe(step, 0f, -tangent, tangent);
  854. } else if (i == framesCount - 1) { //last
  855. keys = new Keyframe[2];
  856. keys[0] = new Keyframe((i - 1) * step, 0f, tangent, tangent);
  857. keys[1] = new Keyframe(i * step, 100f, tangent, -tangent);
  858. } else {
  859. keys = new Keyframe[3];
  860. keys[0] = new Keyframe((i - 1) * step, 0f, tangent, tangent);
  861. keys[1] = new Keyframe(i * step, 100f, tangent, -tangent);
  862. keys[2] = new Keyframe((i + 1) * step, 0f, -tangent, tangent);
  863. }
  864. AnimationCurve ac = new AnimationCurve(keys);
  865. clip.SetCurve("", typeof(SkinnedMeshRenderer), name, ac);
  866. }
  867. }
  868. }
  869. #endregion
  870. }
  871. }