MegaPointCacheRef.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. [AddComponentMenu("Modifiers/Point Cache Ref")]
  5. public class MegaPointCacheRef : MegaModifier
  6. {
  7. public float time = 0.0f;
  8. public bool animated = false;
  9. public float speed = 1.0f;
  10. public float maxtime = 1.0f;
  11. public MegaRepeatMode LoopMode = MegaRepeatMode.PingPong;
  12. public MegaInterpMethod interpMethod = MegaInterpMethod.Linear;
  13. public MegaPointCache source;
  14. public float weight = 1.0f;
  15. public MegaBlendAnimMode blendMode = MegaBlendAnimMode.Additive; // local space
  16. float t;
  17. float alpha = 0.0f;
  18. float dalpha = 0.0f;
  19. int sindex;
  20. int sindex1;
  21. public override string ModName() { return "Point Cache Ref"; }
  22. public override string GetHelpURL() { return "?page_id=1335"; }
  23. void LinearAbs(MegaModifiers mc, int start, int end)
  24. {
  25. for ( int i = start; i < end; i++ )
  26. {
  27. Vector3 p = source.Verts[i].points[sindex];
  28. Vector3 p1 = source.Verts[i].points[sindex1];
  29. p.x = p.x + ((p1.x - p.x) * dalpha);
  30. p.y = p.y + ((p1.y - p.y) * dalpha);
  31. p.z = p.z + ((p1.z - p.z) * dalpha);
  32. for ( int v = 0; v < source.Verts[i].indices.Length; v++ )
  33. sverts[source.Verts[i].indices[v]] = p;
  34. }
  35. }
  36. void LinearAbsWeighted(MegaModifiers mc, int start, int end)
  37. {
  38. for ( int i = start; i < end; i++ )
  39. {
  40. Vector3 p = source.Verts[i].points[sindex];
  41. Vector3 p1 = source.Verts[i].points[sindex1];
  42. p.x = p.x + ((p1.x - p.x) * dalpha);
  43. p.y = p.y + ((p1.y - p.y) * dalpha);
  44. p.z = p.z + ((p1.z - p.z) * dalpha);
  45. float w = mc.selection[source.Verts[i].indices[0]]; //[wc];
  46. p1 = verts[source.Verts[i].indices[0]];
  47. p = p1 + ((p - p1) * w);
  48. for ( int v = 0; v < source.Verts[i].indices.Length; v++ )
  49. sverts[source.Verts[i].indices[v]] = p;
  50. }
  51. }
  52. void LinearRel(MegaModifiers mc, int start, int end)
  53. {
  54. for ( int i = start; i < end; i++ )
  55. {
  56. int ix = source.Verts[i].indices[0];
  57. Vector3 basep = mc.verts[ix];
  58. Vector3 p = source.Verts[i].points[sindex];
  59. Vector3 p1 = source.Verts[i].points[sindex1];
  60. p.x += (((p1.x - p.x) * dalpha) - basep.x); // * weight; //mc.verts[ix].x;
  61. p.y += (((p1.y - p.y) * dalpha) - basep.y); // * weight; //mc.verts[ix].y;
  62. p.z += (((p1.z - p.z) * dalpha) - basep.z); // * weight; //mc.verts[ix].z;
  63. p1 = verts[source.Verts[i].indices[0]];
  64. p.x = p1.x + (p.x * weight);
  65. p.y = p1.y + (p.y * weight);
  66. p.z = p1.z + (p.z * weight);
  67. for ( int v = 0; v < source.Verts[i].indices.Length; v++ )
  68. {
  69. int idx = source.Verts[i].indices[v];
  70. sverts[idx] = p;
  71. }
  72. }
  73. }
  74. void LinearRelWeighted(MegaModifiers mc, int start, int end)
  75. {
  76. for ( int i = start; i < end; i++ )
  77. {
  78. int ix = source.Verts[i].indices[0];
  79. Vector3 basep = verts[ix];
  80. Vector3 p = source.Verts[i].points[sindex];
  81. Vector3 p1 = source.Verts[i].points[sindex1];
  82. p.x += (((p1.x - p.x) * dalpha) - basep.x); // * weight; //mc.verts[ix].x;
  83. p.y += (((p1.y - p.y) * dalpha) - basep.y); // * weight; //mc.verts[ix].y;
  84. p.z += (((p1.z - p.z) * dalpha) - basep.z); // * weight; //mc.verts[ix].z;
  85. float w = mc.selection[source.Verts[i].indices[0]] * weight; //[wc];
  86. p1 = verts[source.Verts[i].indices[0]];
  87. p.x = p1.x + (p.x * w);
  88. p.y = p1.y + (p.y * w);
  89. p.z = p1.z + (p.z * w);
  90. for ( int v = 0; v < source.Verts[i].indices.Length; v++ )
  91. {
  92. int idx = source.Verts[i].indices[v];
  93. sverts[idx] = p;
  94. }
  95. }
  96. }
  97. void NoInterpAbs(MegaModifiers mc, int start, int end)
  98. {
  99. for ( int i = start; i < end; i++ )
  100. {
  101. Vector3 p = source.Verts[i].points[sindex];
  102. for ( int v = 0; v < source.Verts[i].indices.Length; v++ )
  103. sverts[source.Verts[i].indices[v]] = p;
  104. }
  105. }
  106. void NoInterpAbsWeighted(MegaModifiers mc, int start, int end)
  107. {
  108. for ( int i = start; i < end; i++ )
  109. {
  110. Vector3 p = source.Verts[i].points[sindex];
  111. float w = mc.selection[source.Verts[i].indices[0]] * weight; //[wc];
  112. Vector3 p1 = verts[source.Verts[i].indices[0]];
  113. p = p1 + ((p - p1) * w);
  114. for ( int v = 0; v < source.Verts[i].indices.Length; v++ )
  115. sverts[source.Verts[i].indices[v]] = p;
  116. }
  117. }
  118. void NoInterpRel(MegaModifiers mc, int start, int end)
  119. {
  120. for ( int i = start; i < end; i++ )
  121. {
  122. int ix = source.Verts[i].indices[0];
  123. Vector3 p = source.Verts[i].points[sindex] - verts[ix];
  124. Vector3 p1 = verts[source.Verts[i].indices[0]];
  125. p.x = p1.x + (p.x * weight);
  126. p.y = p1.y + (p.y * weight);
  127. p.z = p1.z + (p.z * weight);
  128. for ( int v = 0; v < source.Verts[i].indices.Length; v++ )
  129. {
  130. int idx = source.Verts[i].indices[v];
  131. sverts[idx] = p;
  132. }
  133. }
  134. }
  135. void NoInterpRelWeighted(MegaModifiers mc, int start, int end)
  136. {
  137. for ( int i = start; i < end; i++ )
  138. {
  139. int ix = source.Verts[i].indices[0];
  140. Vector3 p = source.Verts[i].points[sindex] - verts[ix];
  141. float w = mc.selection[source.Verts[i].indices[0]] * weight; //[wc];
  142. Vector3 p1 = verts[source.Verts[i].indices[0]];
  143. p = p1 + ((p - p1) * w);
  144. for ( int v = 0; v < source.Verts[i].indices.Length; v++ )
  145. {
  146. int idx = source.Verts[i].indices[v];
  147. sverts[idx] = p;
  148. }
  149. }
  150. }
  151. // TODO: Option to lerp or even bez, depends on how many samples
  152. public override void Modify(MegaModifiers mc)
  153. {
  154. if ( source != null && source.Verts != null )
  155. {
  156. switch ( interpMethod )
  157. {
  158. case MegaInterpMethod.Linear:
  159. switch ( blendMode )
  160. {
  161. case MegaBlendAnimMode.Additive: LinearRel(mc, 0, source.Verts.Length); break;
  162. case MegaBlendAnimMode.Replace: LinearAbs(mc, 0, source.Verts.Length); break;
  163. }
  164. break;
  165. case MegaInterpMethod.Bez:
  166. switch ( blendMode )
  167. {
  168. case MegaBlendAnimMode.Additive: LinearRel(mc, 0, source.Verts.Length); break;
  169. case MegaBlendAnimMode.Replace: LinearAbs(mc, 0, source.Verts.Length); break;
  170. }
  171. break;
  172. case MegaInterpMethod.None:
  173. switch ( blendMode )
  174. {
  175. case MegaBlendAnimMode.Additive: NoInterpRel(mc, 0, source.Verts.Length); break;
  176. case MegaBlendAnimMode.Replace: NoInterpAbs(mc, 0, source.Verts.Length); break;
  177. }
  178. break;
  179. }
  180. }
  181. else
  182. {
  183. for ( int i = 0; i < verts.Length; i++ )
  184. sverts[i] = verts[i];
  185. }
  186. }
  187. public void ModifyInstance(MegaModifiers mc, float itime)
  188. {
  189. if ( source != null && source.Verts != null )
  190. {
  191. switch ( LoopMode )
  192. {
  193. case MegaRepeatMode.Loop: t = Mathf.Repeat(itime, maxtime); break;
  194. case MegaRepeatMode.PingPong: t = Mathf.PingPong(itime, maxtime); break;
  195. case MegaRepeatMode.Clamp: t = Mathf.Clamp(itime, 0.0f, maxtime); break;
  196. }
  197. alpha = t / maxtime;
  198. float val = (float)(source.Verts[0].points.Length - 1) * alpha;
  199. sindex = (int)val;
  200. dalpha = val - sindex;
  201. if ( sindex == source.Verts[0].points.Length - 1 )
  202. {
  203. sindex1 = sindex;
  204. dalpha = 0.0f;
  205. }
  206. else
  207. {
  208. sindex1 = sindex + 1;
  209. }
  210. switch ( interpMethod )
  211. {
  212. case MegaInterpMethod.Linear:
  213. switch ( blendMode )
  214. {
  215. case MegaBlendAnimMode.Additive: LinearRel(mc, 0, source.Verts.Length); break;
  216. case MegaBlendAnimMode.Replace: LinearAbs(mc, 0, source.Verts.Length); break;
  217. }
  218. break;
  219. case MegaInterpMethod.Bez:
  220. switch ( blendMode )
  221. {
  222. case MegaBlendAnimMode.Additive: LinearRel(mc, 0, source.Verts.Length); break;
  223. case MegaBlendAnimMode.Replace: LinearAbs(mc, 0, source.Verts.Length); break;
  224. }
  225. break;
  226. case MegaInterpMethod.None:
  227. switch ( blendMode )
  228. {
  229. case MegaBlendAnimMode.Additive: NoInterpRel(mc, 0, source.Verts.Length); break;
  230. case MegaBlendAnimMode.Replace: NoInterpAbs(mc, 0, source.Verts.Length); break;
  231. }
  232. break;
  233. }
  234. }
  235. else
  236. {
  237. for ( int i = 0; i < verts.Length; i++ )
  238. sverts[i] = verts[i];
  239. }
  240. }
  241. public void SetAnim(float _t)
  242. {
  243. time = _t;
  244. t = _t;
  245. }
  246. public override bool ModLateUpdate(MegaModContext mc)
  247. {
  248. if ( !Prepare(mc) )
  249. return false;
  250. if ( animated )
  251. {
  252. if ( Application.isPlaying )
  253. time += Time.deltaTime * speed;
  254. }
  255. switch ( LoopMode )
  256. {
  257. case MegaRepeatMode.Loop: t = Mathf.Repeat(time, maxtime); break;
  258. case MegaRepeatMode.PingPong: t = Mathf.PingPong(time, maxtime); break;
  259. case MegaRepeatMode.Clamp: t = Mathf.Clamp(time, 0.0f, maxtime); break;
  260. }
  261. alpha = t / maxtime;
  262. float val = (float)(source.Verts[0].points.Length - 1) * alpha;
  263. sindex = (int)val;
  264. dalpha = val - sindex;
  265. if ( sindex == source.Verts[0].points.Length - 1 )
  266. {
  267. sindex1 = sindex;
  268. dalpha = 0.0f;
  269. }
  270. else
  271. sindex1 = sindex + 1;
  272. return true;
  273. }
  274. public override bool Prepare(MegaModContext mc)
  275. {
  276. if ( source != null && source.Verts != null && source.Verts.Length > 0 && source.Verts[0].indices != null && source.Verts[0].indices.Length > 0 )
  277. return true;
  278. return false;
  279. }
  280. public override void DoWork(MegaModifiers mc, int index, int start, int end, int cores)
  281. {
  282. ModifyCompressedMT(mc, index, cores);
  283. }
  284. public void ModifyCompressedMT(MegaModifiers mc, int tindex, int cores)
  285. {
  286. if ( source != null && source.Verts != null )
  287. {
  288. int step = source.Verts.Length / cores;
  289. int startvert = (tindex * step);
  290. int endvert = startvert + step;
  291. if ( tindex == cores - 1 )
  292. endvert = source.Verts.Length;
  293. switch ( interpMethod )
  294. {
  295. case MegaInterpMethod.Linear:
  296. switch ( blendMode )
  297. {
  298. case MegaBlendAnimMode.Additive: LinearRel(mc, startvert, endvert); break;
  299. case MegaBlendAnimMode.Replace: LinearAbs(mc, startvert, endvert); break;
  300. }
  301. break;
  302. case MegaInterpMethod.Bez:
  303. switch ( blendMode )
  304. {
  305. case MegaBlendAnimMode.Additive: LinearRel(mc, startvert, endvert); break;
  306. case MegaBlendAnimMode.Replace: LinearAbs(mc, startvert, endvert); break;
  307. }
  308. break;
  309. case MegaInterpMethod.None:
  310. switch ( blendMode )
  311. {
  312. case MegaBlendAnimMode.Additive: NoInterpRel(mc, startvert, endvert); break;
  313. case MegaBlendAnimMode.Replace: NoInterpAbs(mc, startvert, endvert); break;
  314. }
  315. break;
  316. }
  317. }
  318. }
  319. }