PointCacheData.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5. namespace VertexAnimationTools_30 {
  6. public class PointCacheData {
  7. public string Name;
  8. public FramesArray Frames;
  9. public PointCache.Clip clip;
  10. PointCache.Clip.ImportSettings cis;
  11. public PointCache pointCache;
  12. public int geometryVerticesCount;
  13. public bool IsMeshSequence;
  14. TasksStack tstack;
  15. public PointCacheData(PointCache.Clip _clip, PointCache _pointCache, int _geometryVerticesCount) {
  16. clip = _clip;
  17. cis = clip.PreImport;
  18. pointCache = _pointCache;
  19. geometryVerticesCount = _geometryVerticesCount;
  20. IsMeshSequence = Path.GetExtension(cis.FilePath) == ".obj";
  21. tstack = new TasksStack(string.Format("Build clip {0} data", cis.Name));
  22. if (IsMeshSequence) {
  23. MeshSequenceInfo msi = new MeshSequenceInfo(cis.FilePath, MeshSequenceInfo.SortModeEnum.ByNumber);
  24. tstack.Add("Read .obj sequence", msi.infos.Length + cis.TransitionFramesCount, 3f );
  25. } else {
  26. tstack.Add("Read .pc2 file", cis.ImportRangeLength + cis.TransitionFramesCount);
  27. }
  28. if (cis.ChangeFramesCount) {
  29. tstack.Add("Change frames count", cis.ClampedCustomFramesCount);
  30. }
  31. if (cis.EnableMotionSmoothing) {
  32. tstack.Add("Motion smoothing", geometryVerticesCount);
  33. }
  34. if (cis.EnableNormalizeSpeed) {
  35. tstack.Add("Normalize speed", geometryVerticesCount);
  36. }
  37. if (cis.GenerageMotionPaths) {
  38. tstack.Add("Generage Motion Paths", geometryVerticesCount);
  39. }
  40. tstack.Normalize();
  41. }
  42. Vector3[] ReadFrame(BinaryReader br, int vertsCount ) {
  43. Vector3[] vertices = new Vector3[vertsCount];
  44. for (int v = 0; v < vertsCount; v++) {
  45. Vector3 pos = new Vector3();
  46. pos.x = br.ReadSingle();
  47. pos.y = br.ReadSingle();
  48. pos.z = br.ReadSingle();
  49. if (cis.SwapYZAxis) {
  50. pos.Set(pos.x, pos.z, pos.y);
  51. }
  52. pos = pos * cis.Scale;
  53. vertices[v] = pos;
  54. }
  55. return vertices;
  56. }
  57. public IEnumerable<TaskInfo> Build (){
  58. Frames = new FramesArray(cis.IsLoop, cis.SubFrameInterpolation);
  59. FramesArray TransitionFrames = new FramesArray(false, InterpolateModeEnum.Linear);
  60. int readFramesCounter = 0;
  61. if (IsMeshSequence) {
  62. MeshSequenceInfo msi = new MeshSequenceInfo( cis.FilePath, MeshSequenceInfo.SortModeEnum.ByNumber );
  63. for (int f = cis.EndTransitionFrom; f<cis.EndTransitionTo; f++) {
  64. Vector3[] objVertices = ObjData.GetVerticesFromObj(msi.infos[f].fi.FullName, cis.SwapYZAxis, cis.Scale);
  65. TransitionFrames.AddFrame(objVertices);
  66. readFramesCounter++;
  67. yield return tstack["Read .obj sequence"].GetInfo(readFramesCounter);
  68. }
  69. for (int f = cis.ImportRangeFrom; f < cis.ImportRangeTo; f++ ) {
  70. Vector3[] objVertices = ObjData.GetVerticesFromObj( msi.infos[f].fi.FullName, cis.SwapYZAxis, cis.Scale);
  71. if (objVertices.Length != geometryVerticesCount) {
  72. yield return new TaskInfo(string.Format("Error: frame {0} vertex count mismatch", msi.infos[f].fi.FullName), -1);
  73. }
  74. Frames.AddFrame(objVertices);
  75. readFramesCounter++;
  76. yield return tstack["Read .obj sequence"].GetInfo(readFramesCounter);
  77. }
  78. for (int f = cis.BeginTransitionFrom; f < cis.BeginTransitionTo; f++) {
  79. Vector3[] objVertices = ObjData.GetVerticesFromObj(msi.infos[f].fi.FullName, cis.SwapYZAxis, cis.Scale);
  80. TransitionFrames.AddFrame(objVertices);
  81. readFramesCounter++;
  82. yield return tstack["Read .obj sequence"].GetInfo(readFramesCounter);
  83. }
  84. } else {
  85. Name = (new FileInfo(cis.FilePath)).Name;
  86. FileStream fs = new FileStream(cis.FilePath, FileMode.Open);
  87. BinaryReader binReader = new BinaryReader(fs);
  88. binReader.ReadChars(12); //signature
  89. binReader.ReadInt32(); //file version
  90. int VerticesCount = binReader.ReadInt32();
  91. binReader.ReadSingle(); // start frame
  92. binReader.ReadSingle(); //sample rate
  93. binReader.ReadInt32(); // int sourceFramesCount;
  94. for (int f = 0; f < cis.EndTransitionFrom; f++) {
  95. for (int v = 0; v < VerticesCount; v++) {
  96. binReader.ReadSingle();
  97. binReader.ReadSingle();
  98. binReader.ReadSingle();
  99. }
  100. }
  101. // end transition
  102. for (int f = cis.EndTransitionFrom; f < cis.EndTransitionTo; f++) {
  103. TransitionFrames.AddFrame( ReadFrame(binReader, geometryVerticesCount));
  104. readFramesCounter++;
  105. yield return tstack["Read .pc2 file"].GetInfo(readFramesCounter);
  106. }
  107. // frames
  108. for (int f = cis.ImportRangeFrom; f < cis.ImportRangeTo; f++) {
  109. Frames.AddFrame(ReadFrame(binReader, geometryVerticesCount));
  110. readFramesCounter++;
  111. yield return tstack["Read .pc2 file"].GetInfo(readFramesCounter);
  112. }
  113. // begin transition
  114. for (int f = cis.BeginTransitionFrom; f < cis.BeginTransitionTo; f++) {
  115. TransitionFrames.AddFrame(ReadFrame(binReader, geometryVerticesCount));
  116. readFramesCounter++;
  117. yield return tstack["Read .pc2 file"].GetInfo(readFramesCounter);
  118. }
  119. #if UNITY_WSA
  120. binReader.Dispose();
  121. fs.Dispose();
  122. #else
  123. binReader.Close();
  124. fs.Close();
  125. #endif
  126. }
  127. if (cis.TransitionMode == TransitionModeEnum.Begin ) {
  128. for (int f = 0; f < TransitionFrames.Count; f++) {
  129. float flv = f / (float)TransitionFrames.Count ;
  130. flv = Extension.LinearToSin(flv);
  131. for (int v = 0; v < geometryVerticesCount; v++) {
  132. Vector3 sVert = Vector3.LerpUnclamped(TransitionFrames[f, v], Frames[f, v], flv);
  133. Frames[f, v] = sVert;
  134. }
  135. }
  136. }
  137. if (cis.TransitionMode == TransitionModeEnum.End) {
  138. for (int f = 0; f < TransitionFrames.Count; f++) {
  139. float flv = f / (float)TransitionFrames.Count;
  140. flv = Extension.LinearToSin(flv);
  141. int frameIdx = Frames.Count - f-1;
  142. int transitionIdx = TransitionFrames.Count - f-1;
  143. for (int v = 0; v < geometryVerticesCount; v++) {
  144. Vector3 sVert = Vector3.LerpUnclamped( TransitionFrames[transitionIdx, v], Frames[frameIdx, v], flv);
  145. Frames[frameIdx, v] = sVert;
  146. }
  147. }
  148. }
  149. if (cis.ChangeFramesCount){
  150. FramesArray retimedFrames = new FramesArray(cis.ClampedCustomFramesCount, Frames.VerticesCount, cis.IsLoop, cis.SubFrameInterpolation);
  151. float step = cis.IsLoop ? 1f/(float)cis.CustomFramesCount : 1f/(cis.ClampedCustomFramesCount - 1);
  152. for(int f = 0; f<retimedFrames.Count; f++){
  153. float persentage = f*step;
  154. for (int v = 0; v < Frames.VerticesCount; v++) {
  155. retimedFrames[f,v] = Frames[ persentage, v ];
  156. }
  157. yield return tstack["Change frames count"].GetInfo(f);
  158. }
  159. Frames = retimedFrames;
  160. }
  161. if (cis.EnableMotionSmoothing){
  162. foreach (TaskInfo ti in Frames.SmoothIE(cis.MotionSmoothIterations, cis.MotionSmoothAmountMin, cis.MotionSmoothAmountMax, cis.MotionSmoothEaseOffset, cis.MotionSmoothEaseLength)) {
  163. yield return tstack["Motion smoothing"].GetInfo(ti.Persentage) ;
  164. }
  165. }
  166. if (cis.EnableNormalizeSpeed) {
  167. for (int v = 0; v < Frames.VerticesCount; v++) {
  168. Vector3[] modified = Frames[v];
  169. if (cis.IsLoop) {
  170. Vector3[] modifiedLooped = new Vector3[modified.Length + 1];
  171. modified.CopyTo(modifiedLooped, 0);
  172. modifiedLooped[modifiedLooped.Length-1] = modifiedLooped[0];
  173. modified = modifiedLooped;
  174. }
  175. NormalizedSpline ns = new NormalizedSpline(modified);
  176. float stepMult = 1f / (modified.Length - 1);
  177. for (int f = 0; f < modified.Length; f++) {
  178. modified[f] = Vector3.LerpUnclamped(modified[f], ns.GetPoint(f * stepMult), cis.NormalizeSpeedPercentage);
  179. }
  180. Frames[v] = modified;
  181. yield return tstack["Normalize speed"].GetInfo(v);
  182. }
  183. }
  184. clip.MotionPathVertices = null;
  185. clip.MotionPathsCount = 0;
  186. if (cis.GenerageMotionPaths) {
  187. List<Vector3> mPathVertList = new List<Vector3>();
  188. cis.MotionPathsIndexStep = Mathf.Clamp(cis.MotionPathsIndexStep, 1, 1000);
  189. for (int v = 0; v< Frames.VerticesCount; v+= cis.MotionPathsIndexStep) {
  190. for (int f = 0; f < Frames.Count; f++) {
  191. mPathVertList.Add(Frames[f, v]);
  192. }
  193. clip.MotionPathsCount++;
  194. yield return tstack["Generage Motion Paths"].GetInfo(v);
  195. }
  196. clip.MotionPathVertices = mPathVertList.ToArray();
  197. }
  198. yield return new TaskInfo("done", 1f);
  199. }
  200. public Vector3[] GetFrameVertices(int frameIdx ){
  201. Vector3[] result = new Vector3[Frames.VerticesCount];
  202. for(int v = 0; v<result.Length; v++){
  203. result[v] = Frames [frameIdx, v];
  204. }
  205. return result;
  206. }
  207. public void SetFrame(int frameIdx, Vector3[] arr){
  208. for(int v = 0; v<Frames.VerticesCount; v++){
  209. Frames[frameIdx, v] = arr[v];
  210. }
  211. }
  212. public void EncapsulateToBounds(ref Bounds b){
  213. for(int f = 0; f<Frames.Count; f++){
  214. for(int v = 0; v<Frames.VerticesCount; v++){
  215. b.Encapsulate(Frames[f,v]);
  216. }
  217. }
  218. }
  219. }
  220. }