afxXM_Oscillate.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. docsURL;
  124. addField("mask", TypeS32, Offset(mask, afxXM_OscillateData),
  125. "...");
  126. addField("min", TypePoint3F, Offset(min, afxXM_OscillateData),
  127. "...");
  128. addField("max", TypePoint3F, Offset(max, afxXM_OscillateData),
  129. "...");
  130. addField("speed", TypeF32, Offset(speed, afxXM_OscillateData),
  131. "...");
  132. addField("axis", TypePoint3F, Offset(axis, afxXM_OscillateData),
  133. "...");
  134. addField("additiveScale", TypeBool, Offset(additive_scale, afxXM_OscillateData),
  135. "...");
  136. addField("localOffset", TypeBool, Offset(local_offset, afxXM_OscillateData),
  137. "...");
  138. Parent::initPersistFields();
  139. }
  140. void afxXM_OscillateData::packData(BitStream* stream)
  141. {
  142. Parent::packData(stream);
  143. stream->write(mask);
  144. mathWrite(*stream, min);
  145. mathWrite(*stream, max);
  146. stream->write(speed);
  147. mathWrite(*stream, axis);
  148. stream->writeFlag(additive_scale);
  149. stream->writeFlag(local_offset);
  150. }
  151. void afxXM_OscillateData::unpackData(BitStream* stream)
  152. {
  153. Parent::unpackData(stream);
  154. stream->read(&mask);
  155. mathRead(*stream, &min);
  156. mathRead(*stream, &max);
  157. stream->read(&speed);
  158. mathRead(*stream, &axis);
  159. additive_scale = stream->readFlag();
  160. local_offset = stream->readFlag();
  161. }
  162. afxXM_Base* afxXM_OscillateData::create(afxEffectWrapper* fx, bool on_server)
  163. {
  164. afxXM_OscillateData* datablock = this;
  165. if (getSubstitutionCount() > 0)
  166. {
  167. datablock = new afxXM_OscillateData(*this, true);
  168. this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex());
  169. }
  170. if (datablock->mask == ORIENTATION)
  171. return new afxXM_Oscillate_rot(datablock, fx);
  172. if (datablock->mask == SCALE)
  173. return new afxXM_Oscillate_scale(datablock, fx);
  174. if (datablock->mask == POSITION)
  175. return new afxXM_Oscillate_position(datablock, fx);
  176. if (datablock->mask == POSITION2)
  177. return new afxXM_Oscillate_position2(datablock, fx);
  178. return new afxXM_Oscillate(datablock, fx);
  179. }
  180. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  181. afxXM_Oscillate_rot::afxXM_Oscillate_rot(afxXM_OscillateData* db, afxEffectWrapper* fxw)
  182. : afxXM_WeightedBase(db, fxw)
  183. {
  184. this->db = db;
  185. }
  186. void afxXM_Oscillate_rot::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  187. {
  188. F32 wt_factor = calc_weight_factor(elapsed);
  189. F32 t = mSin(db->speed*elapsed); // [-1,1]
  190. F32 theta = mLerp(db->min.x*wt_factor, db->max.x*wt_factor, (t + 1) / 2);
  191. theta = mDegToRad(theta);
  192. AngAxisF rot_aa(db->axis, theta);
  193. MatrixF rot_xfm; rot_aa.setMatrix(&rot_xfm);
  194. params.ori.mul(rot_xfm);
  195. }
  196. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  197. afxXM_Oscillate_scale::afxXM_Oscillate_scale(afxXM_OscillateData* db, afxEffectWrapper* fxw)
  198. : afxXM_WeightedBase(db, fxw)
  199. {
  200. this->db = db;
  201. }
  202. void afxXM_Oscillate_scale::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  203. {
  204. F32 wt_factor = calc_weight_factor(elapsed);
  205. F32 t = mSin(db->speed*elapsed); // [-1,1]
  206. F32 s = mLerp( db->min.x*wt_factor, db->max.x*wt_factor, (t + 1) / 2);
  207. Point3F xm_scale = db->axis*s;
  208. if (db->additive_scale)
  209. params.scale += xm_scale;
  210. else
  211. params.scale *= xm_scale;
  212. }
  213. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  214. afxXM_Oscillate_position::afxXM_Oscillate_position(afxXM_OscillateData* db, afxEffectWrapper* fxw)
  215. : afxXM_WeightedBase(db, fxw)
  216. {
  217. this->db = db;
  218. }
  219. void afxXM_Oscillate_position::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  220. {
  221. F32 wt_factor = calc_weight_factor(elapsed);
  222. F32 t = mSin(db->speed*elapsed); // [-1,1]
  223. Point3F offset = mLerp(db->min*wt_factor, db->max*wt_factor,t);
  224. if (db->local_offset)
  225. {
  226. params.ori.mulV(offset);
  227. params.pos += offset;
  228. }
  229. else
  230. params.pos += offset;
  231. }
  232. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  233. afxXM_Oscillate_position2::afxXM_Oscillate_position2(afxXM_OscillateData* db, afxEffectWrapper* fxw)
  234. : afxXM_WeightedBase(db, fxw)
  235. {
  236. this->db = db;
  237. }
  238. void afxXM_Oscillate_position2::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  239. {
  240. F32 wt_factor = calc_weight_factor(elapsed);
  241. F32 t = mSin(db->speed*elapsed); // [-1,1]
  242. Point3F offset = mLerp(db->min*wt_factor, db->max*wt_factor,t);
  243. params.pos2 += offset;
  244. }
  245. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  246. afxXM_Oscillate::afxXM_Oscillate(afxXM_OscillateData* db, afxEffectWrapper* fxw)
  247. : afxXM_WeightedBase(db, fxw)
  248. {
  249. this->db = db;
  250. }
  251. void afxXM_Oscillate::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  252. {
  253. F32 wt_factor = calc_weight_factor(elapsed);
  254. F32 t = mSin(db->speed*elapsed); // [-1,1]
  255. if (db->mask & POSITION)
  256. {
  257. Point3F offset = mLerp(db->min*wt_factor, db->max*wt_factor,t);
  258. if (db->local_offset)
  259. {
  260. params.ori.mulV(offset);
  261. params.pos += offset;
  262. }
  263. else
  264. params.pos += offset;
  265. }
  266. if (db->mask & POSITION2)
  267. {
  268. Point3F offset = mLerp(db->min*wt_factor, db->max*wt_factor, t);
  269. params.pos2 += offset;
  270. }
  271. if (db->mask & SCALE)
  272. {
  273. F32 s = mLerp(db->min.x*wt_factor, db->max.x*wt_factor, (t + 1) / 2);
  274. Point3F xm_scale = db->axis*s;
  275. if (db->additive_scale)
  276. params.scale += xm_scale;
  277. else
  278. params.scale *= xm_scale;
  279. }
  280. if (db->mask & ORIENTATION)
  281. {
  282. F32 theta = mLerp(db->min.x*wt_factor, db->max.x*wt_factor, (t + 1) / 2);
  283. theta = mDegToRad(theta);
  284. AngAxisF rot_aa(db->axis, theta);
  285. MatrixF rot_xfm; rot_aa.setMatrix(&rot_xfm);
  286. params.ori.mul(rot_xfm);
  287. }
  288. }
  289. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//