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) override;
  44. void unpackData(BitStream* stream) override;
  45. bool allowSubstitutions() const override { return true; }
  46. static void initPersistFields();
  47. afxXM_Base* create(afxEffectWrapper* fx, bool on_server) override;
  48. DECLARE_CONOBJECT(afxXM_LocalOffsetData);
  49. };
  50. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  51. class afxXM_LocalOffset_weighted : public afxXM_WeightedBase
  52. {
  53. typedef afxXM_WeightedBase Parent;
  54. Point3F local_offset;
  55. public:
  56. /*C*/ afxXM_LocalOffset_weighted(afxXM_LocalOffsetData*, afxEffectWrapper*);
  57. void updateParams(F32 dt, F32 elapsed, afxXM_Params& params) override;
  58. };
  59. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  60. // this fixed variation is used when
  61. // the weight factors are constant.
  62. class afxXM_LocalOffset_fixed : public afxXM_Base
  63. {
  64. typedef afxXM_Base Parent;
  65. Point3F local_offset;
  66. public:
  67. /*C*/ afxXM_LocalOffset_fixed(afxXM_LocalOffsetData*, afxEffectWrapper*);
  68. void updateParams(F32 dt, F32 elapsed, afxXM_Params& params) override;
  69. };
  70. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  71. class afxXM_LocalOffset2_weighted : public afxXM_WeightedBase
  72. {
  73. typedef afxXM_WeightedBase Parent;
  74. Point3F local_offset;
  75. public:
  76. /*C*/ afxXM_LocalOffset2_weighted(afxXM_LocalOffsetData*, afxEffectWrapper*);
  77. void updateParams(F32 dt, F32 elapsed, afxXM_Params& params) override;
  78. };
  79. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  80. // this fixed variation is used when
  81. // the weight factors are constant.
  82. class afxXM_LocalOffset2_fixed : public afxXM_Base
  83. {
  84. typedef afxXM_Base Parent;
  85. Point3F local_offset;
  86. public:
  87. /*C*/ afxXM_LocalOffset2_fixed(afxXM_LocalOffsetData*, afxEffectWrapper*);
  88. void updateParams(F32 dt, F32 elapsed, afxXM_Params& params) override;
  89. };
  90. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  91. // WORLD OFFSET
  92. class afxXM_WorldOffsetData : public afxXM_WeightedBaseData
  93. {
  94. typedef afxXM_WeightedBaseData Parent;
  95. public:
  96. Point3F world_offset;
  97. bool offset_pos2;
  98. public:
  99. /*C*/ afxXM_WorldOffsetData();
  100. /*C*/ afxXM_WorldOffsetData(const afxXM_WorldOffsetData&, bool = false);
  101. void packData(BitStream* stream) override;
  102. void unpackData(BitStream* stream) override;
  103. bool allowSubstitutions() const override { return true; }
  104. static void initPersistFields();
  105. afxXM_Base* create(afxEffectWrapper* fx, bool on_server) override;
  106. DECLARE_CONOBJECT(afxXM_WorldOffsetData);
  107. };
  108. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  109. class afxXM_WorldOffset_weighted : public afxXM_WeightedBase
  110. {
  111. typedef afxXM_WeightedBase Parent;
  112. Point3F world_offset;
  113. public:
  114. /*C*/ afxXM_WorldOffset_weighted(afxXM_WorldOffsetData*, afxEffectWrapper*);
  115. void updateParams(F32 dt, F32 elapsed, afxXM_Params& params) override;
  116. };
  117. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  118. // this fixed variation is used when
  119. // the weight factors are constant.
  120. class afxXM_WorldOffset_fixed : public afxXM_Base
  121. {
  122. typedef afxXM_WeightedBase Parent;
  123. Point3F world_offset;
  124. public:
  125. /*C*/ afxXM_WorldOffset_fixed(afxXM_WorldOffsetData*, afxEffectWrapper*);
  126. void updateParams(F32 dt, F32 elapsed, afxXM_Params& params) override;
  127. };
  128. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  129. class afxXM_WorldOffset2_weighted : public afxXM_WeightedBase
  130. {
  131. typedef afxXM_WeightedBase Parent;
  132. Point3F world_offset;
  133. public:
  134. /*C*/ afxXM_WorldOffset2_weighted(afxXM_WorldOffsetData*, afxEffectWrapper*);
  135. void updateParams(F32 dt, F32 elapsed, afxXM_Params& params) override;
  136. };
  137. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  138. // this fixed variation is used when
  139. // the weight factors are constant.
  140. class afxXM_WorldOffset2_fixed : public afxXM_Base
  141. {
  142. typedef afxXM_WeightedBase Parent;
  143. Point3F world_offset;
  144. public:
  145. /*C*/ afxXM_WorldOffset2_fixed(afxXM_WorldOffsetData*, afxEffectWrapper*);
  146. void updateParams(F32 dt, F32 elapsed, afxXM_Params& params) override;
  147. };
  148. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  149. // LOCAL OFFSET
  150. IMPLEMENT_CO_DATABLOCK_V1(afxXM_LocalOffsetData);
  151. ConsoleDocClass( afxXM_LocalOffsetData,
  152. "@brief An xmod datablock.\n\n"
  153. "@ingroup afxXMods\n"
  154. "@ingroup AFX\n"
  155. "@ingroup Datablocks\n"
  156. );
  157. afxXM_LocalOffsetData::afxXM_LocalOffsetData()
  158. {
  159. local_offset.zero();
  160. offset_pos2 = false;
  161. }
  162. afxXM_LocalOffsetData::afxXM_LocalOffsetData(const afxXM_LocalOffsetData& other, bool temp_clone) : afxXM_WeightedBaseData(other, temp_clone)
  163. {
  164. local_offset = other.local_offset;
  165. offset_pos2 = other.offset_pos2;
  166. }
  167. void afxXM_LocalOffsetData::initPersistFields()
  168. {
  169. docsURL;
  170. addField("localOffset", TypePoint3F, Offset(local_offset, afxXM_LocalOffsetData),
  171. "...");
  172. addField("offsetPos2", TypeBool, Offset(offset_pos2, afxXM_LocalOffsetData),
  173. "...");
  174. Parent::initPersistFields();
  175. }
  176. void afxXM_LocalOffsetData::packData(BitStream* stream)
  177. {
  178. Parent::packData(stream);
  179. mathWrite(*stream, local_offset);
  180. stream->writeFlag(offset_pos2);
  181. }
  182. void afxXM_LocalOffsetData::unpackData(BitStream* stream)
  183. {
  184. Parent::unpackData(stream);
  185. mathRead(*stream, &local_offset);
  186. offset_pos2 = stream->readFlag();
  187. }
  188. afxXM_Base* afxXM_LocalOffsetData::create(afxEffectWrapper* fx, bool on_server)
  189. {
  190. afxXM_LocalOffsetData* datablock = this;
  191. if (getSubstitutionCount() > 0)
  192. {
  193. datablock = new afxXM_LocalOffsetData(*this, true);
  194. this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex());
  195. }
  196. if (datablock->offset_pos2)
  197. {
  198. if (datablock->hasFixedWeight())
  199. return new afxXM_LocalOffset2_fixed(datablock, fx);
  200. else
  201. return new afxXM_LocalOffset2_weighted(datablock, fx);
  202. }
  203. else
  204. {
  205. if (datablock->hasFixedWeight())
  206. return new afxXM_LocalOffset_fixed(datablock, fx);
  207. else
  208. return new afxXM_LocalOffset_weighted(datablock, fx);
  209. }
  210. }
  211. //~~~~~~~~~~~~~~~~~~~~//
  212. afxXM_LocalOffset_weighted::afxXM_LocalOffset_weighted(afxXM_LocalOffsetData* db, afxEffectWrapper* fxw)
  213. : afxXM_WeightedBase(db, fxw)
  214. {
  215. local_offset = db->local_offset*db->getWeightFactor();
  216. }
  217. void afxXM_LocalOffset_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  218. {
  219. F32 wt_factor = calc_weight_factor(elapsed);
  220. Point3F offset(local_offset*wt_factor);
  221. params.ori.mulV(offset);
  222. params.pos += offset;
  223. }
  224. //~~~~~~~~~~~~~~~~~~~~//
  225. afxXM_LocalOffset_fixed::afxXM_LocalOffset_fixed(afxXM_LocalOffsetData* db, afxEffectWrapper* fxw)
  226. : afxXM_Base(db, fxw)
  227. {
  228. local_offset = db->local_offset*db->getWeightFactor();
  229. }
  230. void afxXM_LocalOffset_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  231. {
  232. Point3F offset(local_offset);
  233. params.ori.mulV(offset);
  234. params.pos += offset;
  235. }
  236. //~~~~~~~~~~~~~~~~~~~~//
  237. afxXM_LocalOffset2_weighted::afxXM_LocalOffset2_weighted(afxXM_LocalOffsetData* db, afxEffectWrapper* fxw)
  238. : afxXM_WeightedBase(db, fxw)
  239. {
  240. local_offset = db->local_offset*db->getWeightFactor();
  241. }
  242. void afxXM_LocalOffset2_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  243. {
  244. F32 wt_factor = calc_weight_factor(elapsed);
  245. Point3F offset(local_offset*wt_factor);
  246. afxConstraint* pos2_cons = fx_wrapper->getAimConstraint();
  247. if (pos2_cons)
  248. {
  249. MatrixF ori2;
  250. pos2_cons->getTransform(ori2);
  251. ori2.mulV(offset);
  252. }
  253. params.pos2 += offset;
  254. }
  255. //~~~~~~~~~~~~~~~~~~~~//
  256. afxXM_LocalOffset2_fixed::afxXM_LocalOffset2_fixed(afxXM_LocalOffsetData* db, afxEffectWrapper* fxw)
  257. : afxXM_Base(db, fxw)
  258. {
  259. local_offset = db->local_offset*db->getWeightFactor();
  260. }
  261. void afxXM_LocalOffset2_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  262. {
  263. Point3F offset(local_offset);
  264. afxConstraint* pos2_cons = fx_wrapper->getAimConstraint();
  265. if (pos2_cons)
  266. {
  267. MatrixF ori2;
  268. pos2_cons->getTransform(ori2);
  269. ori2.mulV(offset);
  270. }
  271. params.pos2 += offset;
  272. }
  273. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  274. // WORLD OFFSET
  275. IMPLEMENT_CO_DATABLOCK_V1(afxXM_WorldOffsetData);
  276. ConsoleDocClass( afxXM_WorldOffsetData,
  277. "@brief An xmod datablock.\n\n"
  278. "@ingroup afxXMods\n"
  279. "@ingroup AFX\n"
  280. "@ingroup Datablocks\n"
  281. );
  282. afxXM_WorldOffsetData::afxXM_WorldOffsetData()
  283. {
  284. world_offset.zero();
  285. offset_pos2 = false;
  286. }
  287. afxXM_WorldOffsetData::afxXM_WorldOffsetData(const afxXM_WorldOffsetData& other, bool temp_clone) : afxXM_WeightedBaseData(other, temp_clone)
  288. {
  289. world_offset = other.world_offset;
  290. offset_pos2 = other.offset_pos2;
  291. }
  292. void afxXM_WorldOffsetData::initPersistFields()
  293. {
  294. docsURL;
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//