PointCache.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace VertexAnimationTools_30 {
  5. [HelpURL("https://polyflow.studio/VertexAnimationToolsDocumentation/VertexAnimationTools30_Documentation.html#Overview")]
  6. #if UNITY_5_5_OR_NEWER
  7. [PreferBinarySerialization]
  8. #endif
  9. public sealed class PointCache : VertexAnimationToolsAssetBase {
  10. [System.Serializable]
  11. public class PolygonMesh {
  12. public string Name;
  13. public string Path;
  14. public string MeshName;
  15. public int VertsCount;
  16. public int PolygonsCount;
  17. public int SubMeshesCount;
  18. public string Info;
  19. public Mesh mesh;
  20. public Material[] Materials;
  21. public ObjData od;
  22. public bool CheckAndUpdateInfo() {
  23. VertsCount = 0;
  24. PolygonsCount = 0;
  25. SubMeshesCount = 0;
  26. bool fileExist = System.IO.File.Exists(Path);
  27. if (!fileExist) {
  28. Info = "missing file";
  29. VertsCount = -1;
  30. return false;
  31. } else if (ObjData.GetObjInfo(Path, ref MeshName, ref VertsCount, ref PolygonsCount, ref SubMeshesCount)) {
  32. Info = string.Format("{0}.obj {1} verts, {2} polygons, {3} submeshes", MeshName, VertsCount, PolygonsCount, SubMeshesCount);
  33. return true;
  34. } else {
  35. Info = "not valid obj file";
  36. VertsCount = -1;
  37. return false;
  38. }
  39. }
  40. public PolygonMesh(string name ){
  41. Name = name;
  42. }
  43. }
  44. [System.Serializable]
  45. public class Clip{
  46. [System.Serializable]
  47. public struct ImportSettings{
  48. public string Name;
  49. public bool FoldoutIsOpen;
  50. public string FilePath;
  51. public string FileName;
  52. public int FileVertsCount;
  53. public int FileFramesCount;
  54. public string FileInfo;
  55. public bool SwapYZAxis;
  56. public float Scale;
  57. public bool IsLoop;
  58. public bool EnableCustomRange;
  59. public int CustomRangeFrom;
  60. public int CustomRangeTo;
  61. public TransitionModeEnum TransitionMode;
  62. public int TransitionLength;
  63. public bool ChangeFramesCount;
  64. public int CustomFramesCount;
  65. public InterpolateModeEnum SubFrameInterpolation;
  66. public int FrameIdxOffset;
  67. public bool EnableMotionSmoothing;
  68. public int MotionSmoothIterations;
  69. public float MotionSmoothAmountMin;
  70. public float MotionSmoothAmountMax;
  71. public float MotionSmoothEaseOffset;
  72. public float MotionSmoothEaseLength;
  73. public bool GenerageMotionPaths;
  74. public int MotionPathsIndexStep;
  75. public bool EnableNormalizeSpeed;
  76. public float NormalizeSpeedPercentage;
  77. public ImportSettings(string name){
  78. Name = name;
  79. FoldoutIsOpen = true;
  80. FilePath = "";
  81. FileName = "";
  82. FileVertsCount = 0;
  83. FileFramesCount = 0;
  84. FileInfo = "";
  85. SwapYZAxis = false;
  86. Scale = 1;
  87. IsLoop = false;
  88. ChangeFramesCount = false;
  89. CustomFramesCount = 0;
  90. SubFrameInterpolation = InterpolateModeEnum.Linear;
  91. EnableCustomRange = false;
  92. CustomRangeFrom = 0;
  93. CustomRangeTo = 100;
  94. TransitionMode = TransitionModeEnum.None;
  95. TransitionLength = 0;
  96. FrameIdxOffset = 0;
  97. EnableMotionSmoothing = false;
  98. MotionSmoothIterations = 100;
  99. MotionSmoothAmountMin = 0;
  100. MotionSmoothAmountMax = 1f;
  101. MotionSmoothEaseOffset = 0;
  102. MotionSmoothEaseLength = 0.5f;
  103. GenerageMotionPaths = false;
  104. MotionPathsIndexStep = 1;
  105. EnableNormalizeSpeed = false;
  106. NormalizeSpeedPercentage = 0.5f;
  107. }
  108. public bool CheckAndUpdateInfo( int mainMeshVertsCount ){
  109. FileName = "";
  110. FileVertsCount = 0;
  111. FileFramesCount = 0;
  112. string statisticMessage = "";
  113. // Debug.LogFormat("CheckAndUpdateInfo");
  114. if ( !System.IO.File.Exists(FilePath)){
  115. FileInfo = "missing";
  116. return false;
  117. } else if(GetPointCacheSourceStatistic(FilePath, ref FileName, ref FileVertsCount, ref FileFramesCount, ref statisticMessage)){
  118. bool matchUp = FileVertsCount == mainMeshVertsCount;
  119. FileInfo = string.Format("{0}, {1} vertices, {2} frames , vertex count {3}", FileName, FileVertsCount, FileFramesCount, (matchUp?"match up":"mismatch") );
  120. return matchUp;
  121. } else {
  122. FileInfo = "bad source "+ statisticMessage;
  123. return false;
  124. }
  125. }
  126. public int ImportRangeLength {
  127. get {
  128. int result = ImportRangeTo - ImportRangeFrom;
  129. return result;
  130. }
  131. }
  132. public int FramesCount {
  133. get {
  134. int result = 0;
  135. if (ChangeFramesCount) {
  136. result = ClampedCustomFramesCount;
  137. } else {
  138. if (EnableCustomRange) {
  139. result = ImportRangeLength;
  140. } else {
  141. result = FileFramesCount;
  142. }
  143. }
  144. return result;
  145. }
  146. }
  147. public int TransitionFramesCount {
  148. get {
  149. if (!EnableCustomRange) {
  150. return 0;
  151. }
  152. if (TransitionMode == TransitionModeEnum.Begin) {
  153. return Mathf.Clamp(TransitionLength, 0, FileFramesCount - ImportRangeTo);
  154. }
  155. if (TransitionMode == TransitionModeEnum.End) {
  156. return Mathf.Clamp(TransitionLength, 0, ImportRangeFrom);
  157. }
  158. return 0;
  159. }
  160. }
  161. public int ImportRangeFrom {
  162. get {
  163. int result = 0;
  164. if (EnableCustomRange) {
  165. result = Mathf.Clamp(CustomRangeFrom, 0, CustomRangeTo);
  166. }
  167. return result;
  168. }
  169. }
  170. public int ImportRangeTo {
  171. get {
  172. int result = FileFramesCount;
  173. if (EnableCustomRange) {
  174. result = Mathf.Clamp( CustomRangeTo, 0, FileFramesCount);
  175. }
  176. return result;
  177. }
  178. }
  179. public int EndTransitionFrom {
  180. get {
  181. int result = ImportRangeFrom;
  182. if (TransitionMode == TransitionModeEnum.End) {
  183. result -= TransitionFramesCount;
  184. }
  185. if (result < 0) {
  186. Debug.LogErrorFormat("EndTransitionFrom issue result:{0} TransitionFramesCount:{1} FileFramesCount:{2}", result, TransitionFramesCount, FileFramesCount);
  187. }
  188. return result;
  189. }
  190. }
  191. public int EndTransitionTo {
  192. get {
  193. int result = ImportRangeFrom;
  194. return result;
  195. }
  196. }
  197. public int MaxBeginTransitionLength {
  198. get {
  199. return Mathf.Clamp( FileFramesCount - ImportRangeTo, 0 , ImportRangeLength);
  200. }
  201. }
  202. public int MaxEndTransitionLength {
  203. get {
  204. return Mathf.Clamp( ImportRangeFrom, 0, ImportRangeLength );
  205. }
  206. }
  207. public int BeginTransitionFrom {
  208. get {
  209. return ImportRangeTo;
  210. }
  211. }
  212. public int BeginTransitionTo {
  213. get {
  214. int result = ImportRangeTo;
  215. if (TransitionMode == TransitionModeEnum.Begin) {
  216. result += TransitionFramesCount;
  217. }
  218. if (result > FileFramesCount) {
  219. Debug.LogErrorFormat("BeginTransitionTo issue result:{0} FileFramesCount:{1} ", result, FileFramesCount);
  220. }
  221. return result;
  222. }
  223. }
  224. public int ClampedCustomFramesCount {
  225. get {
  226. return CustomFramesCount < 2 ? 2 : CustomFramesCount ;
  227. }
  228. }
  229. bool GetPointCacheSourceStatistic(string path, ref string name, ref int vertCount, ref int framesCount, ref string message) {
  230. string extension = System.IO.Path.GetExtension(path);
  231. if (extension == ".obj") {
  232. MeshSequenceInfo msi = new MeshSequenceInfo(path, MeshSequenceInfo.SortModeEnum.ByNumber);
  233. name = msi.SequenceName;
  234. framesCount = msi.infos.Length;
  235. vertCount = 0;
  236. ObjData.GetObjInfo(path, ref vertCount);
  237. return true;
  238. } else {
  239. name = (new System.IO.FileInfo(path)).Name;
  240. System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open);
  241. System.IO.BinaryReader binReader = new System.IO.BinaryReader(fs);
  242. binReader.ReadChars(12);
  243. binReader.ReadInt32();
  244. vertCount = binReader.ReadInt32();
  245. binReader.ReadSingle();//start frame
  246. binReader.ReadSingle();//sample rate
  247. framesCount = binReader.ReadInt32();
  248. #if UNITY_WSA
  249. binReader.Dispose();
  250. fs.Dispose();
  251. #else
  252. binReader.Close();
  253. fs.Close();
  254. #endif
  255. return vertCount > 0 && framesCount > 0;
  256. }
  257. }
  258. }
  259. public ImportSettings PreImport;
  260. public ImportSettings PostImport;
  261. public bool FoldoutState = true;
  262. public int MotionPathsCount;
  263. [HideInInspector]
  264. public Vector3[] MotionPathVertices;
  265. public Clip(string name){
  266. PreImport = new ImportSettings(name);
  267. PostImport = new ImportSettings("");
  268. }
  269. public Vector3 GetGizmoPoint(int pathIdx, int frameIdx) {
  270. if (PostImport.IsLoop) {
  271. frameIdx = (int)Mathf.Repeat(frameIdx, PostImport.FramesCount);
  272. } else {
  273. frameIdx = Mathf.Clamp(frameIdx, 0, PostImport.FramesCount - 1);
  274. }
  275. return MotionPathVertices[pathIdx * PostImport.FramesCount + frameIdx];
  276. }
  277. public void SetGizmoPoint(int pathIdx, int frameIdx, Vector3 pos) {
  278. if (PostImport.IsLoop) {
  279. frameIdx = (int)Mathf.Repeat(frameIdx, PostImport.FramesCount - 1);
  280. }
  281. MotionPathVertices[pathIdx * PostImport.FramesCount + frameIdx] = pos;
  282. }
  283. }
  284. [System.Serializable]
  285. public struct ImportSettings{
  286. public bool SwapYZAxis;
  287. public float ScaleFactor;
  288. public bool FlipNormals;
  289. public ObjData.SmoothingGroupImportModeEnum SmoothingGroupImportMode;
  290. public ObjData.NormalsRecalculationModeEnum NormalRecalculationMode;
  291. public int MeshCompression;
  292. public bool OptimizeMesh;
  293. public int UsedClipsCount;
  294. public int UsedMeshesCount;
  295. public bool GenerateMaterials;
  296. public bool SavePortableData;
  297. #if UNITY_2017_3_OR_NEWER
  298. public UnityEngine.Rendering.IndexFormat IndexFormat;
  299. #endif
  300. public ImportSettings(int usedClipsCount){
  301. SwapYZAxis = false;
  302. ScaleFactor = 1f;
  303. FlipNormals = false;
  304. SmoothingGroupImportMode = ObjData.SmoothingGroupImportModeEnum.FromObjFile;
  305. NormalRecalculationMode = ObjData.NormalsRecalculationModeEnum.Default;
  306. MeshCompression = 0;
  307. OptimizeMesh = false;
  308. UsedClipsCount = usedClipsCount;
  309. UsedMeshesCount = 1;
  310. GenerateMaterials = true;
  311. SavePortableData = false;
  312. #if UNITY_2017_3_OR_NEWER
  313. IndexFormat = UnityEngine.Rendering.IndexFormat.UInt16;
  314. #endif
  315. }
  316. }
  317. [System.Serializable]
  318. public class PreImportConstraint {
  319. public string Name;
  320. public PFU ObjSpace;
  321. public BindInfo BI;
  322. public PreImportConstraint(string name) {
  323. Name = name;
  324. ObjSpace = new PFU(Matrix4x4.identity);
  325. }
  326. public PFU GetFrame (BindingHelper cbh) {
  327. return BI.TrisSpace * cbh.GetTrisTM(BI.VidxA, BI.VidxB, BI.VidxC, BI.Bary);
  328. }
  329. }
  330. [System.Serializable]
  331. public class PostImportConstraint {
  332. public string Name;
  333. public PFU ObjSpace;
  334. public ConstraintClip[] Clips;
  335. }
  336. public ImportSettings PreImport = new ImportSettings(1);
  337. public ImportSettings PostImport = new ImportSettings(0);
  338. public Clip[] Clips = new Clip[8]{new Clip("Clip 0"), new Clip("Clip 1"), new Clip("Clip 2"), new Clip("Clip 3"), new Clip("Clip 4"), new Clip("Clip 5"), new Clip("Clip 6"), new Clip("Clip 7") };
  339. public PolygonMesh[] Meshes = new PolygonMesh[8] { new PolygonMesh("Mesh 0"), new PolygonMesh("Mesh 1"), new PolygonMesh("Mesh 2"), new PolygonMesh("Mesh 3"), new PolygonMesh("Mesh 4"), new PolygonMesh("Mesh 4"), new PolygonMesh("Mesh 6"), new PolygonMesh("Mesh 7") };
  340. public List<PreImportConstraint> PreConstraints = new List<PreImportConstraint>();
  341. public PostImportConstraint[] PostConstraints = new PostImportConstraint[0];
  342. public bool ImportSettingsIsDirtyFlag;
  343. public float ConstraintHandlesSize = 3;
  344. public bool DrawConstraintHandlesName = false;
  345. public int SelectedTabIdx = 1;
  346. public int SelectedImportTabIdx = 0;
  347. public bool ImportSettingsIsDirty{
  348. get{
  349. if (ImportSettingsIsDirtyFlag) {
  350. return true;
  351. }
  352. if (!PreImport.Equals(PostImport)) {
  353. return true;
  354. }
  355. for (int c = 0; c < Clips.Length; c++) {
  356. if (!Clips[c].PreImport.Equals(Clips[c].PostImport)) {
  357. return true;
  358. }
  359. }
  360. return false;
  361. }
  362. }
  363. public float AssetFileSize = 0;
  364. public string ImportingDate = "n/a";
  365. public GameObject CreatePlayer(){
  366. GameObject go = new GameObject(string.Format("{0} Point Cache Player", this.name ));
  367. go.AddComponent<SkinnedMeshRenderer>();
  368. PointCachePlayer player = go.AddComponent<PointCachePlayer>();
  369. player.pointCache = this;
  370. player.Init();
  371. return go;
  372. }
  373. }
  374. }