afxXM_Oscillate.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. afxXM_Oscillate_rot::afxXM_Oscillate_rot(afxXM_OscillateData* db, afxEffectWrapper* fxw)
  181. : afxXM_WeightedBase(db, fxw)
  182. {
  183. this->db = db;
  184. }
  185. void afxXM_Oscillate_rot::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  186. {
  187. F32 wt_factor = calc_weight_factor(elapsed);
  188. F32 t = mSin(db->speed*elapsed); // [-1,1]
  189. F32 theta = mLerp(db->min.x*wt_factor, db->max.x*wt_factor, (t + 1) / 2);
  190. theta = mDegToRad(theta);
  191. AngAxisF rot_aa(db->axis, theta);
  192. MatrixF rot_xfm; rot_aa.setMatrix(&rot_xfm);
  193. params.ori.mul(rot_xfm);
  194. }
  195. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  196. afxXM_Oscillate_scale::afxXM_Oscillate_scale(afxXM_OscillateData* db, afxEffectWrapper* fxw)
  197. : afxXM_WeightedBase(db, fxw)
  198. {
  199. this->db = db;
  200. }
  201. void afxXM_Oscillate_scale::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  202. {
  203. F32 wt_factor = calc_weight_factor(elapsed);
  204. F32 t = mSin(db->speed*elapsed); // [-1,1]
  205. F32 s = mLerp( db->min.x*wt_factor, db->max.x*wt_factor, (t + 1) / 2);
  206. Point3F xm_scale = db->axis*s;
  207. if (db->additive_scale)
  208. params.scale += xm_scale;
  209. else
  210. params.scale *= xm_scale;
  211. }
  212. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  213. afxXM_Oscillate_position::afxXM_Oscillate_position(afxXM_OscillateData* db, afxEffectWrapper* fxw)
  214. : afxXM_WeightedBase(db, fxw)
  215. {
  216. this->db = db;
  217. }
  218. void afxXM_Oscillate_position::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  219. {
  220. F32 wt_factor = calc_weight_factor(elapsed);
  221. F32 t = mSin(db->speed*elapsed); // [-1,1]
  222. Point3F offset = mLerp(db->min*wt_factor, db->max*wt_factor,t);
  223. if (db->local_offset)
  224. {
  225. params.ori.mulV(offset);
  226. params.pos += offset;
  227. }
  228. else
  229. params.pos += offset;
  230. }
  231. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  232. afxXM_Oscillate_position2::afxXM_Oscillate_position2(afxXM_OscillateData* db, afxEffectWrapper* fxw)
  233. : afxXM_WeightedBase(db, fxw)
  234. {
  235. this->db = db;
  236. }
  237. void afxXM_Oscillate_position2::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  238. {
  239. F32 wt_factor = calc_weight_factor(elapsed);
  240. F32 t = mSin(db->speed*elapsed); // [-1,1]
  241. Point3F offset = mLerp(db->min*wt_factor, db->max*wt_factor,t);
  242. params.pos2 += offset;
  243. }
  244. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  245. afxXM_Oscillate::afxXM_Oscillate(afxXM_OscillateData* db, afxEffectWrapper* fxw)
  246. : afxXM_WeightedBase(db, fxw)
  247. {
  248. this->db = db;
  249. }
  250. void afxXM_Oscillate::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  251. {
  252. F32 wt_factor = calc_weight_factor(elapsed);
  253. F32 t = mSin(db->speed*elapsed); // [-1,1]
  254. if (db->mask & POSITION)
  255. {
  256. Point3F offset = mLerp(db->min*wt_factor, db->max*wt_factor,t);
  257. if (db->local_offset)
  258. {
  259. params.ori.mulV(offset);
  260. params.pos += offset;
  261. }
  262. else
  263. params.pos += offset;
  264. }
  265. if (db->mask & POSITION2)
  266. {
  267. Point3F offset = mLerp(db->min*wt_factor, db->max*wt_factor, t);
  268. params.pos2 += offset;
  269. }
  270. if (db->mask & SCALE)
  271. {
  272. F32 s = mLerp(db->min.x*wt_factor, db->max.x*wt_factor, (t + 1) / 2);
  273. Point3F xm_scale = db->axis*s;
  274. if (db->additive_scale)
  275. params.scale += xm_scale;
  276. else
  277. params.scale *= xm_scale;
  278. }
  279. if (db->mask & ORIENTATION)
  280. {
  281. F32 theta = mLerp(db->min.x*wt_factor, db->max.x*wt_factor, (t + 1) / 2);
  282. theta = mDegToRad(theta);
  283. AngAxisF rot_aa(db->axis, theta);
  284. MatrixF rot_xfm; rot_aa.setMatrix(&rot_xfm);
  285. params.ori.mul(rot_xfm);
  286. }
  287. }
  288. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//