afxXM_Offset.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. #include "afx/util/afxPath3D.h"
  31. #include "afx/util/afxPath.h"
  32. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  33. // LOCAL OFFSET
  34. class afxXM_LocalOffsetData : public afxXM_WeightedBaseData
  35. {
  36. typedef afxXM_WeightedBaseData Parent;
  37. public:
  38. Point3F local_offset;
  39. bool offset_pos2;
  40. public:
  41. /*C*/ afxXM_LocalOffsetData();
  42. /*C*/ afxXM_LocalOffsetData(const afxXM_LocalOffsetData&, bool = false);
  43. void packData(BitStream* stream);
  44. void unpackData(BitStream* stream);
  45. virtual bool allowSubstitutions() const { return true; }
  46. static void initPersistFields();
  47. afxXM_Base* create(afxEffectWrapper* fx, bool on_server);
  48. DECLARE_CONOBJECT(afxXM_LocalOffsetData);
  49. DECLARE_CATEGORY("AFX");
  50. };
  51. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  52. class afxXM_LocalOffset_weighted : public afxXM_WeightedBase
  53. {
  54. typedef afxXM_WeightedBase Parent;
  55. Point3F local_offset;
  56. public:
  57. /*C*/ afxXM_LocalOffset_weighted(afxXM_LocalOffsetData*, afxEffectWrapper*);
  58. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  59. };
  60. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  61. // this fixed variation is used when
  62. // the weight factors are constant.
  63. class afxXM_LocalOffset_fixed : public afxXM_Base
  64. {
  65. typedef afxXM_Base Parent;
  66. Point3F local_offset;
  67. public:
  68. /*C*/ afxXM_LocalOffset_fixed(afxXM_LocalOffsetData*, afxEffectWrapper*);
  69. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  70. };
  71. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  72. class afxXM_LocalOffset2_weighted : public afxXM_WeightedBase
  73. {
  74. typedef afxXM_WeightedBase Parent;
  75. Point3F local_offset;
  76. public:
  77. /*C*/ afxXM_LocalOffset2_weighted(afxXM_LocalOffsetData*, afxEffectWrapper*);
  78. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  79. };
  80. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  81. // this fixed variation is used when
  82. // the weight factors are constant.
  83. class afxXM_LocalOffset2_fixed : public afxXM_Base
  84. {
  85. typedef afxXM_Base Parent;
  86. Point3F local_offset;
  87. public:
  88. /*C*/ afxXM_LocalOffset2_fixed(afxXM_LocalOffsetData*, afxEffectWrapper*);
  89. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  90. };
  91. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  92. // WORLD OFFSET
  93. class afxXM_WorldOffsetData : public afxXM_WeightedBaseData
  94. {
  95. typedef afxXM_WeightedBaseData Parent;
  96. public:
  97. Point3F world_offset;
  98. bool offset_pos2;
  99. public:
  100. /*C*/ afxXM_WorldOffsetData();
  101. /*C*/ afxXM_WorldOffsetData(const afxXM_WorldOffsetData&, bool = false);
  102. void packData(BitStream* stream);
  103. void unpackData(BitStream* stream);
  104. virtual bool allowSubstitutions() const { return true; }
  105. static void initPersistFields();
  106. afxXM_Base* create(afxEffectWrapper* fx, bool on_server);
  107. DECLARE_CONOBJECT(afxXM_WorldOffsetData);
  108. DECLARE_CATEGORY("AFX");
  109. };
  110. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  111. class afxXM_WorldOffset_weighted : public afxXM_WeightedBase
  112. {
  113. typedef afxXM_WeightedBase Parent;
  114. Point3F world_offset;
  115. public:
  116. /*C*/ afxXM_WorldOffset_weighted(afxXM_WorldOffsetData*, afxEffectWrapper*);
  117. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  118. };
  119. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  120. // this fixed variation is used when
  121. // the weight factors are constant.
  122. class afxXM_WorldOffset_fixed : public afxXM_Base
  123. {
  124. typedef afxXM_WeightedBase Parent;
  125. Point3F world_offset;
  126. public:
  127. /*C*/ afxXM_WorldOffset_fixed(afxXM_WorldOffsetData*, afxEffectWrapper*);
  128. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  129. };
  130. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  131. class afxXM_WorldOffset2_weighted : public afxXM_WeightedBase
  132. {
  133. typedef afxXM_WeightedBase Parent;
  134. Point3F world_offset;
  135. public:
  136. /*C*/ afxXM_WorldOffset2_weighted(afxXM_WorldOffsetData*, afxEffectWrapper*);
  137. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  138. };
  139. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  140. // this fixed variation is used when
  141. // the weight factors are constant.
  142. class afxXM_WorldOffset2_fixed : public afxXM_Base
  143. {
  144. typedef afxXM_WeightedBase Parent;
  145. Point3F world_offset;
  146. public:
  147. /*C*/ afxXM_WorldOffset2_fixed(afxXM_WorldOffsetData*, afxEffectWrapper*);
  148. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  149. };
  150. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  151. // LOCAL OFFSET
  152. IMPLEMENT_CO_DATABLOCK_V1(afxXM_LocalOffsetData);
  153. ConsoleDocClass( afxXM_LocalOffsetData,
  154. "@brief An xmod datablock.\n\n"
  155. "@ingroup afxXMods\n"
  156. "@ingroup AFX\n"
  157. "@ingroup Datablocks\n"
  158. );
  159. afxXM_LocalOffsetData::afxXM_LocalOffsetData()
  160. {
  161. local_offset.zero();
  162. offset_pos2 = false;
  163. }
  164. afxXM_LocalOffsetData::afxXM_LocalOffsetData(const afxXM_LocalOffsetData& other, bool temp_clone) : afxXM_WeightedBaseData(other, temp_clone)
  165. {
  166. local_offset = other.local_offset;
  167. offset_pos2 = other.offset_pos2;
  168. }
  169. void afxXM_LocalOffsetData::initPersistFields()
  170. {
  171. addField("localOffset", TypePoint3F, Offset(local_offset, afxXM_LocalOffsetData),
  172. "...");
  173. addField("offsetPos2", TypeBool, Offset(offset_pos2, afxXM_LocalOffsetData),
  174. "...");
  175. Parent::initPersistFields();
  176. }
  177. void afxXM_LocalOffsetData::packData(BitStream* stream)
  178. {
  179. Parent::packData(stream);
  180. mathWrite(*stream, local_offset);
  181. stream->writeFlag(offset_pos2);
  182. }
  183. void afxXM_LocalOffsetData::unpackData(BitStream* stream)
  184. {
  185. Parent::unpackData(stream);
  186. mathRead(*stream, &local_offset);
  187. offset_pos2 = stream->readFlag();
  188. }
  189. afxXM_Base* afxXM_LocalOffsetData::create(afxEffectWrapper* fx, bool on_server)
  190. {
  191. afxXM_LocalOffsetData* datablock = this;
  192. if (getSubstitutionCount() > 0)
  193. {
  194. datablock = new afxXM_LocalOffsetData(*this, true);
  195. this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex());
  196. }
  197. if (datablock->offset_pos2)
  198. {
  199. if (datablock->hasFixedWeight())
  200. return new afxXM_LocalOffset2_fixed(datablock, fx);
  201. else
  202. return new afxXM_LocalOffset2_weighted(datablock, fx);
  203. }
  204. else
  205. {
  206. if (datablock->hasFixedWeight())
  207. return new afxXM_LocalOffset_fixed(datablock, fx);
  208. else
  209. return new afxXM_LocalOffset_weighted(datablock, fx);
  210. }
  211. }
  212. //~~~~~~~~~~~~~~~~~~~~//
  213. afxXM_LocalOffset_weighted::afxXM_LocalOffset_weighted(afxXM_LocalOffsetData* db, afxEffectWrapper* fxw)
  214. : afxXM_WeightedBase(db, fxw)
  215. {
  216. local_offset = db->local_offset*db->getWeightFactor();
  217. }
  218. void afxXM_LocalOffset_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  219. {
  220. F32 wt_factor = calc_weight_factor(elapsed);
  221. Point3F offset(local_offset*wt_factor);
  222. params.ori.mulV(offset);
  223. params.pos += offset;
  224. }
  225. //~~~~~~~~~~~~~~~~~~~~//
  226. afxXM_LocalOffset_fixed::afxXM_LocalOffset_fixed(afxXM_LocalOffsetData* db, afxEffectWrapper* fxw)
  227. : afxXM_Base(db, fxw)
  228. {
  229. local_offset = db->local_offset*db->getWeightFactor();
  230. }
  231. void afxXM_LocalOffset_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  232. {
  233. Point3F offset(local_offset);
  234. params.ori.mulV(offset);
  235. params.pos += offset;
  236. }
  237. //~~~~~~~~~~~~~~~~~~~~//
  238. afxXM_LocalOffset2_weighted::afxXM_LocalOffset2_weighted(afxXM_LocalOffsetData* db, afxEffectWrapper* fxw)
  239. : afxXM_WeightedBase(db, fxw)
  240. {
  241. local_offset = db->local_offset*db->getWeightFactor();
  242. }
  243. void afxXM_LocalOffset2_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  244. {
  245. F32 wt_factor = calc_weight_factor(elapsed);
  246. Point3F offset(local_offset*wt_factor);
  247. afxConstraint* pos2_cons = fx_wrapper->getAimConstraint();
  248. if (pos2_cons)
  249. {
  250. MatrixF ori2;
  251. pos2_cons->getTransform(ori2);
  252. ori2.mulV(offset);
  253. }
  254. params.pos2 += offset;
  255. }
  256. //~~~~~~~~~~~~~~~~~~~~//
  257. afxXM_LocalOffset2_fixed::afxXM_LocalOffset2_fixed(afxXM_LocalOffsetData* db, afxEffectWrapper* fxw)
  258. : afxXM_Base(db, fxw)
  259. {
  260. local_offset = db->local_offset*db->getWeightFactor();
  261. }
  262. void afxXM_LocalOffset2_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  263. {
  264. Point3F offset(local_offset);
  265. afxConstraint* pos2_cons = fx_wrapper->getAimConstraint();
  266. if (pos2_cons)
  267. {
  268. MatrixF ori2;
  269. pos2_cons->getTransform(ori2);
  270. ori2.mulV(offset);
  271. }
  272. params.pos2 += offset;
  273. }
  274. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  275. // WORLD OFFSET
  276. IMPLEMENT_CO_DATABLOCK_V1(afxXM_WorldOffsetData);
  277. ConsoleDocClass( afxXM_WorldOffsetData,
  278. "@brief An xmod datablock.\n\n"
  279. "@ingroup afxXMods\n"
  280. "@ingroup AFX\n"
  281. "@ingroup Datablocks\n"
  282. );
  283. afxXM_WorldOffsetData::afxXM_WorldOffsetData()
  284. {
  285. world_offset.zero();
  286. offset_pos2 = false;
  287. }
  288. afxXM_WorldOffsetData::afxXM_WorldOffsetData(const afxXM_WorldOffsetData& other, bool temp_clone) : afxXM_WeightedBaseData(other, temp_clone)
  289. {
  290. world_offset = other.world_offset;
  291. offset_pos2 = other.offset_pos2;
  292. }
  293. void afxXM_WorldOffsetData::initPersistFields()
  294. {
  295. addField("worldOffset", TypePoint3F, Offset(world_offset, afxXM_WorldOffsetData),
  296. "...");
  297. addField("offsetPos2", TypeBool, Offset(offset_pos2, afxXM_WorldOffsetData),
  298. "...");
  299. Parent::initPersistFields();
  300. }
  301. void afxXM_WorldOffsetData::packData(BitStream* stream)
  302. {
  303. Parent::packData(stream);
  304. mathWrite(*stream, world_offset);
  305. stream->writeFlag(offset_pos2);
  306. }
  307. void afxXM_WorldOffsetData::unpackData(BitStream* stream)
  308. {
  309. Parent::unpackData(stream);
  310. mathRead(*stream, &world_offset);
  311. offset_pos2 = stream->readFlag();
  312. }
  313. afxXM_Base* afxXM_WorldOffsetData::create(afxEffectWrapper* fx, bool on_server)
  314. {
  315. afxXM_WorldOffsetData* datablock = this;
  316. if (getSubstitutionCount() > 0)
  317. {
  318. datablock = new afxXM_WorldOffsetData(*this, true);
  319. this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex());
  320. }
  321. if (datablock->offset_pos2)
  322. {
  323. if (datablock->hasFixedWeight())
  324. return new afxXM_WorldOffset2_fixed(datablock, fx);
  325. else
  326. return new afxXM_WorldOffset2_weighted(datablock, fx);
  327. }
  328. else
  329. {
  330. if (datablock->hasFixedWeight())
  331. return new afxXM_WorldOffset_fixed(datablock, fx);
  332. else
  333. return new afxXM_WorldOffset_weighted(datablock, fx);
  334. }
  335. }
  336. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  337. afxXM_WorldOffset_weighted::afxXM_WorldOffset_weighted(afxXM_WorldOffsetData* db, afxEffectWrapper* fxw)
  338. : afxXM_WeightedBase(db, fxw)
  339. {
  340. world_offset = db->world_offset*db->getWeightFactor();
  341. }
  342. void afxXM_WorldOffset_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  343. {
  344. F32 wt_factor = calc_weight_factor(elapsed);
  345. params.pos += world_offset*wt_factor;
  346. }
  347. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  348. afxXM_WorldOffset_fixed::afxXM_WorldOffset_fixed(afxXM_WorldOffsetData* db, afxEffectWrapper* fxw)
  349. : afxXM_Base(db, fxw)
  350. {
  351. world_offset = db->world_offset*db->getWeightFactor();
  352. }
  353. void afxXM_WorldOffset_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  354. {
  355. params.pos += world_offset;
  356. }
  357. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  358. afxXM_WorldOffset2_weighted::afxXM_WorldOffset2_weighted(afxXM_WorldOffsetData* db, afxEffectWrapper* fxw)
  359. : afxXM_WeightedBase(db, fxw)
  360. {
  361. world_offset = db->world_offset*db->getWeightFactor();
  362. }
  363. void afxXM_WorldOffset2_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  364. {
  365. F32 wt_factor = calc_weight_factor(elapsed);
  366. params.pos2 += world_offset*wt_factor;
  367. }
  368. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  369. afxXM_WorldOffset2_fixed::afxXM_WorldOffset2_fixed(afxXM_WorldOffsetData* db, afxEffectWrapper* fxw)
  370. : afxXM_Base(db, fxw)
  371. {
  372. world_offset = db->world_offset*db->getWeightFactor();
  373. }
  374. void afxXM_WorldOffset2_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  375. {
  376. params.pos2 += world_offset;
  377. }
  378. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//