afxXM_Offset.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. docsURL;
  172. addField("localOffset", TypePoint3F, Offset(local_offset, afxXM_LocalOffsetData),
  173. "...");
  174. addField("offsetPos2", TypeBool, Offset(offset_pos2, afxXM_LocalOffsetData),
  175. "...");
  176. Parent::initPersistFields();
  177. }
  178. void afxXM_LocalOffsetData::packData(BitStream* stream)
  179. {
  180. Parent::packData(stream);
  181. mathWrite(*stream, local_offset);
  182. stream->writeFlag(offset_pos2);
  183. }
  184. void afxXM_LocalOffsetData::unpackData(BitStream* stream)
  185. {
  186. Parent::unpackData(stream);
  187. mathRead(*stream, &local_offset);
  188. offset_pos2 = stream->readFlag();
  189. }
  190. afxXM_Base* afxXM_LocalOffsetData::create(afxEffectWrapper* fx, bool on_server)
  191. {
  192. afxXM_LocalOffsetData* datablock = this;
  193. if (getSubstitutionCount() > 0)
  194. {
  195. datablock = new afxXM_LocalOffsetData(*this, true);
  196. this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex());
  197. }
  198. if (datablock->offset_pos2)
  199. {
  200. if (datablock->hasFixedWeight())
  201. return new afxXM_LocalOffset2_fixed(datablock, fx);
  202. else
  203. return new afxXM_LocalOffset2_weighted(datablock, fx);
  204. }
  205. else
  206. {
  207. if (datablock->hasFixedWeight())
  208. return new afxXM_LocalOffset_fixed(datablock, fx);
  209. else
  210. return new afxXM_LocalOffset_weighted(datablock, fx);
  211. }
  212. }
  213. //~~~~~~~~~~~~~~~~~~~~//
  214. afxXM_LocalOffset_weighted::afxXM_LocalOffset_weighted(afxXM_LocalOffsetData* db, afxEffectWrapper* fxw)
  215. : afxXM_WeightedBase(db, fxw)
  216. {
  217. local_offset = db->local_offset*db->getWeightFactor();
  218. }
  219. void afxXM_LocalOffset_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  220. {
  221. F32 wt_factor = calc_weight_factor(elapsed);
  222. Point3F offset(local_offset*wt_factor);
  223. params.ori.mulV(offset);
  224. params.pos += offset;
  225. }
  226. //~~~~~~~~~~~~~~~~~~~~//
  227. afxXM_LocalOffset_fixed::afxXM_LocalOffset_fixed(afxXM_LocalOffsetData* db, afxEffectWrapper* fxw)
  228. : afxXM_Base(db, fxw)
  229. {
  230. local_offset = db->local_offset*db->getWeightFactor();
  231. }
  232. void afxXM_LocalOffset_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  233. {
  234. Point3F offset(local_offset);
  235. params.ori.mulV(offset);
  236. params.pos += offset;
  237. }
  238. //~~~~~~~~~~~~~~~~~~~~//
  239. afxXM_LocalOffset2_weighted::afxXM_LocalOffset2_weighted(afxXM_LocalOffsetData* db, afxEffectWrapper* fxw)
  240. : afxXM_WeightedBase(db, fxw)
  241. {
  242. local_offset = db->local_offset*db->getWeightFactor();
  243. }
  244. void afxXM_LocalOffset2_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  245. {
  246. F32 wt_factor = calc_weight_factor(elapsed);
  247. Point3F offset(local_offset*wt_factor);
  248. afxConstraint* pos2_cons = fx_wrapper->getAimConstraint();
  249. if (pos2_cons)
  250. {
  251. MatrixF ori2;
  252. pos2_cons->getTransform(ori2);
  253. ori2.mulV(offset);
  254. }
  255. params.pos2 += offset;
  256. }
  257. //~~~~~~~~~~~~~~~~~~~~//
  258. afxXM_LocalOffset2_fixed::afxXM_LocalOffset2_fixed(afxXM_LocalOffsetData* db, afxEffectWrapper* fxw)
  259. : afxXM_Base(db, fxw)
  260. {
  261. local_offset = db->local_offset*db->getWeightFactor();
  262. }
  263. void afxXM_LocalOffset2_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  264. {
  265. Point3F offset(local_offset);
  266. afxConstraint* pos2_cons = fx_wrapper->getAimConstraint();
  267. if (pos2_cons)
  268. {
  269. MatrixF ori2;
  270. pos2_cons->getTransform(ori2);
  271. ori2.mulV(offset);
  272. }
  273. params.pos2 += offset;
  274. }
  275. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  276. // WORLD OFFSET
  277. IMPLEMENT_CO_DATABLOCK_V1(afxXM_WorldOffsetData);
  278. ConsoleDocClass( afxXM_WorldOffsetData,
  279. "@brief An xmod datablock.\n\n"
  280. "@ingroup afxXMods\n"
  281. "@ingroup AFX\n"
  282. "@ingroup Datablocks\n"
  283. );
  284. afxXM_WorldOffsetData::afxXM_WorldOffsetData()
  285. {
  286. world_offset.zero();
  287. offset_pos2 = false;
  288. }
  289. afxXM_WorldOffsetData::afxXM_WorldOffsetData(const afxXM_WorldOffsetData& other, bool temp_clone) : afxXM_WeightedBaseData(other, temp_clone)
  290. {
  291. world_offset = other.world_offset;
  292. offset_pos2 = other.offset_pos2;
  293. }
  294. void afxXM_WorldOffsetData::initPersistFields()
  295. {
  296. docsURL;
  297. addField("worldOffset", TypePoint3F, Offset(world_offset, afxXM_WorldOffsetData),
  298. "...");
  299. addField("offsetPos2", TypeBool, Offset(offset_pos2, afxXM_WorldOffsetData),
  300. "...");
  301. Parent::initPersistFields();
  302. }
  303. void afxXM_WorldOffsetData::packData(BitStream* stream)
  304. {
  305. Parent::packData(stream);
  306. mathWrite(*stream, world_offset);
  307. stream->writeFlag(offset_pos2);
  308. }
  309. void afxXM_WorldOffsetData::unpackData(BitStream* stream)
  310. {
  311. Parent::unpackData(stream);
  312. mathRead(*stream, &world_offset);
  313. offset_pos2 = stream->readFlag();
  314. }
  315. afxXM_Base* afxXM_WorldOffsetData::create(afxEffectWrapper* fx, bool on_server)
  316. {
  317. afxXM_WorldOffsetData* datablock = this;
  318. if (getSubstitutionCount() > 0)
  319. {
  320. datablock = new afxXM_WorldOffsetData(*this, true);
  321. this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex());
  322. }
  323. if (datablock->offset_pos2)
  324. {
  325. if (datablock->hasFixedWeight())
  326. return new afxXM_WorldOffset2_fixed(datablock, fx);
  327. else
  328. return new afxXM_WorldOffset2_weighted(datablock, fx);
  329. }
  330. else
  331. {
  332. if (datablock->hasFixedWeight())
  333. return new afxXM_WorldOffset_fixed(datablock, fx);
  334. else
  335. return new afxXM_WorldOffset_weighted(datablock, fx);
  336. }
  337. }
  338. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  339. afxXM_WorldOffset_weighted::afxXM_WorldOffset_weighted(afxXM_WorldOffsetData* db, afxEffectWrapper* fxw)
  340. : afxXM_WeightedBase(db, fxw)
  341. {
  342. world_offset = db->world_offset*db->getWeightFactor();
  343. }
  344. void afxXM_WorldOffset_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  345. {
  346. F32 wt_factor = calc_weight_factor(elapsed);
  347. params.pos += world_offset*wt_factor;
  348. }
  349. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  350. afxXM_WorldOffset_fixed::afxXM_WorldOffset_fixed(afxXM_WorldOffsetData* db, afxEffectWrapper* fxw)
  351. : afxXM_Base(db, fxw)
  352. {
  353. world_offset = db->world_offset*db->getWeightFactor();
  354. }
  355. void afxXM_WorldOffset_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  356. {
  357. params.pos += world_offset;
  358. }
  359. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  360. afxXM_WorldOffset2_weighted::afxXM_WorldOffset2_weighted(afxXM_WorldOffsetData* db, afxEffectWrapper* fxw)
  361. : afxXM_WeightedBase(db, fxw)
  362. {
  363. world_offset = db->world_offset*db->getWeightFactor();
  364. }
  365. void afxXM_WorldOffset2_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  366. {
  367. F32 wt_factor = calc_weight_factor(elapsed);
  368. params.pos2 += world_offset*wt_factor;
  369. }
  370. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  371. afxXM_WorldOffset2_fixed::afxXM_WorldOffset2_fixed(afxXM_WorldOffsetData* db, afxEffectWrapper* fxw)
  372. : afxXM_Base(db, fxw)
  373. {
  374. world_offset = db->world_offset*db->getWeightFactor();
  375. }
  376. void afxXM_WorldOffset2_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  377. {
  378. params.pos2 += world_offset;
  379. }
  380. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//