afxXM_Oscillate.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #include "afx/arcaneFX.h"
  25. #include "math/mathIO.h"
  26. #include "math/mathUtils.h"
  27. #include "afx/afxEffectWrapper.h"
  28. #include "afx/afxChoreographer.h"
  29. #include "afx/xm/afxXfmMod.h"
  30. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  31. class afxXM_OscillateData : public afxXM_WeightedBaseData
  32. {
  33. typedef afxXM_WeightedBaseData Parent;
  34. public:
  35. U32 mask;
  36. Point3F min;
  37. Point3F max;
  38. F32 speed;
  39. Point3F axis;
  40. bool additive_scale;
  41. bool local_offset;
  42. public:
  43. /*C*/ afxXM_OscillateData();
  44. /*C*/ afxXM_OscillateData(const afxXM_OscillateData&, bool = false);
  45. void packData(BitStream* stream);
  46. void unpackData(BitStream* stream);
  47. virtual bool allowSubstitutions() const { return true; }
  48. static void initPersistFields();
  49. afxXM_Base* create(afxEffectWrapper* fx, bool on_server);
  50. DECLARE_CONOBJECT(afxXM_OscillateData);
  51. DECLARE_CATEGORY("AFX");
  52. };
  53. class afxXM_Oscillate_rot : public afxXM_WeightedBase
  54. {
  55. typedef afxXM_WeightedBase Parent;
  56. afxXM_OscillateData* db;
  57. public:
  58. /*C*/ afxXM_Oscillate_rot(afxXM_OscillateData*, afxEffectWrapper*);
  59. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  60. };
  61. class afxXM_Oscillate_scale : public afxXM_WeightedBase
  62. {
  63. typedef afxXM_WeightedBase Parent;
  64. afxXM_OscillateData* db;
  65. public:
  66. /*C*/ afxXM_Oscillate_scale(afxXM_OscillateData*, afxEffectWrapper*);
  67. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  68. };
  69. class afxXM_Oscillate_position : public afxXM_WeightedBase
  70. {
  71. typedef afxXM_WeightedBase Parent;
  72. afxXM_OscillateData* db;
  73. public:
  74. /*C*/ afxXM_Oscillate_position(afxXM_OscillateData*, afxEffectWrapper*);
  75. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  76. };
  77. class afxXM_Oscillate_position2 : public afxXM_WeightedBase
  78. {
  79. typedef afxXM_WeightedBase Parent;
  80. afxXM_OscillateData* db;
  81. public:
  82. /*C*/ afxXM_Oscillate_position2(afxXM_OscillateData*, afxEffectWrapper*);
  83. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  84. };
  85. class afxXM_Oscillate : public afxXM_WeightedBase
  86. {
  87. typedef afxXM_WeightedBase Parent;
  88. afxXM_OscillateData* db;
  89. public:
  90. /*C*/ afxXM_Oscillate(afxXM_OscillateData*, afxEffectWrapper*);
  91. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  92. };
  93. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  94. IMPLEMENT_CO_DATABLOCK_V1(afxXM_OscillateData);
  95. ConsoleDocClass( afxXM_OscillateData,
  96. "@brief An xmod datablock.\n\n"
  97. "@ingroup afxXMods\n"
  98. "@ingroup AFX\n"
  99. "@ingroup Datablocks\n"
  100. );
  101. afxXM_OscillateData::afxXM_OscillateData()
  102. {
  103. mask = POSITION;
  104. min.set(0,0,0);
  105. max.set(1,1,1);
  106. speed = 1.0f;
  107. axis.set(0,0,1);
  108. additive_scale = false;
  109. local_offset = true;
  110. }
  111. afxXM_OscillateData::afxXM_OscillateData(const afxXM_OscillateData& other, bool temp_clone) : afxXM_WeightedBaseData(other, temp_clone)
  112. {
  113. mask = other.mask;
  114. min = other.min;
  115. max = other.max;
  116. speed = other.speed;
  117. axis = other.axis;
  118. additive_scale = other.additive_scale;
  119. local_offset = other.local_offset;
  120. }
  121. void afxXM_OscillateData::initPersistFields()
  122. {
  123. addField("mask", TypeS32, Offset(mask, afxXM_OscillateData),
  124. "...");
  125. addField("min", TypePoint3F, Offset(min, afxXM_OscillateData),
  126. "...");
  127. addField("max", TypePoint3F, Offset(max, afxXM_OscillateData),
  128. "...");
  129. addField("speed", TypeF32, Offset(speed, afxXM_OscillateData),
  130. "...");
  131. addField("axis", TypePoint3F, Offset(axis, afxXM_OscillateData),
  132. "...");
  133. addField("additiveScale", TypeBool, Offset(additive_scale, afxXM_OscillateData),
  134. "...");
  135. addField("localOffset", TypeBool, Offset(local_offset, afxXM_OscillateData),
  136. "...");
  137. Parent::initPersistFields();
  138. }
  139. void afxXM_OscillateData::packData(BitStream* stream)
  140. {
  141. Parent::packData(stream);
  142. stream->write(mask);
  143. mathWrite(*stream, min);
  144. mathWrite(*stream, max);
  145. stream->write(speed);
  146. mathWrite(*stream, axis);
  147. stream->writeFlag(additive_scale);
  148. stream->writeFlag(local_offset);
  149. }
  150. void afxXM_OscillateData::unpackData(BitStream* stream)
  151. {
  152. Parent::unpackData(stream);
  153. stream->read(&mask);
  154. mathRead(*stream, &min);
  155. mathRead(*stream, &max);
  156. stream->read(&speed);
  157. mathRead(*stream, &axis);
  158. additive_scale = stream->readFlag();
  159. local_offset = stream->readFlag();
  160. }
  161. afxXM_Base* afxXM_OscillateData::create(afxEffectWrapper* fx, bool on_server)
  162. {
  163. afxXM_OscillateData* datablock = this;
  164. if (getSubstitutionCount() > 0)
  165. {
  166. datablock = new afxXM_OscillateData(*this, true);
  167. this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex());
  168. }
  169. if (datablock->mask == ORIENTATION)
  170. return new afxXM_Oscillate_rot(datablock, fx);
  171. if (datablock->mask == SCALE)
  172. return new afxXM_Oscillate_scale(datablock, fx);
  173. if (datablock->mask == POSITION)
  174. return new afxXM_Oscillate_position(datablock, fx);
  175. if (datablock->mask == POSITION2)
  176. return new afxXM_Oscillate_position2(datablock, fx);
  177. return new afxXM_Oscillate(datablock, fx);
  178. }
  179. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  180. inline F32 lerp(F32 t, F32 a, F32 b)
  181. {
  182. return a + t * (b - a);
  183. }
  184. inline Point3F lerpV(F32 t, const Point3F& a, const Point3F& b)
  185. {
  186. return Point3F( a.x + t * (b.x - a.x),
  187. a.y + t * (b.y - a.y),
  188. a.z + t * (b.z - a.z) );
  189. }
  190. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  191. afxXM_Oscillate_rot::afxXM_Oscillate_rot(afxXM_OscillateData* db, afxEffectWrapper* fxw)
  192. : afxXM_WeightedBase(db, fxw)
  193. {
  194. this->db = db;
  195. }
  196. void afxXM_Oscillate_rot::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  197. {
  198. F32 wt_factor = calc_weight_factor(elapsed);
  199. F32 t = mSin(db->speed*elapsed); // [-1,1]
  200. F32 theta = lerp((t+1)/2, db->min.x*wt_factor, db->max.x*wt_factor);
  201. theta = mDegToRad(theta);
  202. AngAxisF rot_aa(db->axis, theta);
  203. MatrixF rot_xfm; rot_aa.setMatrix(&rot_xfm);
  204. params.ori.mul(rot_xfm);
  205. }
  206. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  207. afxXM_Oscillate_scale::afxXM_Oscillate_scale(afxXM_OscillateData* db, afxEffectWrapper* fxw)
  208. : afxXM_WeightedBase(db, fxw)
  209. {
  210. this->db = db;
  211. }
  212. void afxXM_Oscillate_scale::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  213. {
  214. F32 wt_factor = calc_weight_factor(elapsed);
  215. F32 t = mSin(db->speed*elapsed); // [-1,1]
  216. F32 s = lerp((t+1)/2, db->min.x*wt_factor, db->max.x*wt_factor);
  217. Point3F xm_scale = db->axis*s;
  218. if (db->additive_scale)
  219. params.scale += xm_scale;
  220. else
  221. params.scale *= xm_scale;
  222. }
  223. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  224. afxXM_Oscillate_position::afxXM_Oscillate_position(afxXM_OscillateData* db, afxEffectWrapper* fxw)
  225. : afxXM_WeightedBase(db, fxw)
  226. {
  227. this->db = db;
  228. }
  229. void afxXM_Oscillate_position::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  230. {
  231. F32 wt_factor = calc_weight_factor(elapsed);
  232. F32 t = mSin(db->speed*elapsed); // [-1,1]
  233. Point3F offset = lerpV(t, db->min*wt_factor, db->max*wt_factor);
  234. if (db->local_offset)
  235. {
  236. params.ori.mulV(offset);
  237. params.pos += offset;
  238. }
  239. else
  240. params.pos += offset;
  241. }
  242. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  243. afxXM_Oscillate_position2::afxXM_Oscillate_position2(afxXM_OscillateData* db, afxEffectWrapper* fxw)
  244. : afxXM_WeightedBase(db, fxw)
  245. {
  246. this->db = db;
  247. }
  248. void afxXM_Oscillate_position2::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  249. {
  250. F32 wt_factor = calc_weight_factor(elapsed);
  251. F32 t = mSin(db->speed*elapsed); // [-1,1]
  252. Point3F offset = lerpV(t, db->min*wt_factor, db->max*wt_factor);
  253. params.pos2 += offset;
  254. }
  255. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  256. afxXM_Oscillate::afxXM_Oscillate(afxXM_OscillateData* db, afxEffectWrapper* fxw)
  257. : afxXM_WeightedBase(db, fxw)
  258. {
  259. this->db = db;
  260. }
  261. void afxXM_Oscillate::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  262. {
  263. F32 wt_factor = calc_weight_factor(elapsed);
  264. F32 t = mSin(db->speed*elapsed); // [-1,1]
  265. if (db->mask & POSITION)
  266. {
  267. Point3F offset = lerpV(t, db->min*wt_factor, db->max*wt_factor);
  268. if (db->local_offset)
  269. {
  270. params.ori.mulV(offset);
  271. params.pos += offset;
  272. }
  273. else
  274. params.pos += offset;
  275. }
  276. if (db->mask & POSITION2)
  277. {
  278. Point3F offset = lerpV(t, db->min*wt_factor, db->max*wt_factor);
  279. params.pos2 += offset;
  280. }
  281. if (db->mask & SCALE)
  282. {
  283. F32 s = lerp((t+1)/2, db->min.x*wt_factor, db->max.x*wt_factor);
  284. Point3F xm_scale = db->axis*s;
  285. if (db->additive_scale)
  286. params.scale += xm_scale;
  287. else
  288. params.scale *= xm_scale;
  289. }
  290. if (db->mask & ORIENTATION)
  291. {
  292. F32 theta = lerp((t+1)/2, db->min.x*wt_factor, db->max.x*wt_factor);
  293. theta = mDegToRad(theta);
  294. AngAxisF rot_aa(db->axis, theta);
  295. MatrixF rot_xfm; rot_aa.setMatrix(&rot_xfm);
  296. params.ori.mul(rot_xfm);
  297. }
  298. }
  299. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//