afxEA_ZodiacPlane.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 <typeinfo>
  25. #include "afx/arcaneFX.h"
  26. #include "math/mathUtils.h"
  27. #include "afx/afxEffectDefs.h"
  28. #include "afx/afxEffectWrapper.h"
  29. #include "afx/afxChoreographer.h"
  30. #include "afx/util/afxEase.h"
  31. #include "afx/afxResidueMgr.h"
  32. #include "afx/ce/afxZodiacPlane.h"
  33. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  34. // afxEA_ZodiacPlane
  35. class afxEA_ZodiacPlane : public afxEffectWrapper
  36. {
  37. typedef afxEffectWrapper Parent;
  38. afxZodiacPlaneData* zode_data;
  39. afxZodiacPlane* pzode;
  40. F32 zode_angle_offset;
  41. AngAxisF aa_rot;
  42. F32 live_color_factor;
  43. LinearColorF live_color;
  44. F32 calc_facing_angle();
  45. void do_runtime_substitutions();
  46. public:
  47. /*C*/ afxEA_ZodiacPlane();
  48. /*D*/ ~afxEA_ZodiacPlane();
  49. virtual void ea_set_datablock(SimDataBlock*);
  50. virtual bool ea_start();
  51. virtual bool ea_update(F32 dt);
  52. virtual void ea_finish(bool was_stopped);
  53. virtual void ea_set_scope_status(bool flag);
  54. virtual void onDeleteNotify(SimObject*);
  55. virtual void getUpdatedBoxCenter(Point3F& pos);
  56. virtual void getBaseColor(LinearColorF& color) { color = zode_data->color; }
  57. };
  58. F32 afxEA_ZodiacPlane::calc_facing_angle()
  59. {
  60. // get direction player is facing
  61. VectorF shape_vec;
  62. MatrixF shape_xfm;
  63. afxConstraint* orient_constraint = getOrientConstraint();
  64. if (orient_constraint)
  65. orient_constraint->getTransform(shape_xfm);
  66. else
  67. shape_xfm.identity();
  68. shape_xfm.getColumn(1, &shape_vec);
  69. shape_vec.z = 0.0f;
  70. shape_vec.normalize();
  71. F32 pitch, yaw;
  72. MathUtils::getAnglesFromVector(shape_vec, yaw, pitch);
  73. return mRadToDeg(yaw);
  74. }
  75. afxEA_ZodiacPlane::afxEA_ZodiacPlane()
  76. {
  77. zode_data = 0;
  78. pzode = 0;
  79. zode_angle_offset = 0;
  80. live_color.set(1,1,1,1);
  81. live_color_factor = 0.0f;
  82. }
  83. afxEA_ZodiacPlane::~afxEA_ZodiacPlane()
  84. {
  85. if (pzode)
  86. pzode->deleteObject();
  87. if (zode_data && zode_data->isTempClone())
  88. delete zode_data;
  89. zode_data = 0;
  90. }
  91. void afxEA_ZodiacPlane::ea_set_datablock(SimDataBlock* db)
  92. {
  93. zode_data = dynamic_cast<afxZodiacPlaneData*>(db);
  94. }
  95. bool afxEA_ZodiacPlane::ea_start()
  96. {
  97. if (!zode_data)
  98. {
  99. Con::errorf("afxEA_ZodiacPlane::ea_start() -- missing or incompatible datablock.");
  100. return false;
  101. }
  102. do_runtime_substitutions();
  103. if (!zode_data->use_full_xfm)
  104. zode_angle_offset = calc_facing_angle();
  105. switch (zode_data->face_dir)
  106. {
  107. case afxZodiacPlaneData::FACES_UP:
  108. aa_rot.set(Point3F(0.0f,0.0f,1.0f),0.0f);
  109. break;
  110. case afxZodiacPlaneData::FACES_DOWN:
  111. aa_rot.set(Point3F(0.0f,0.0f,-1.0f),0.0f);
  112. break;
  113. case afxZodiacPlaneData::FACES_FORWARD:
  114. aa_rot.set(Point3F(0.0f,1.0f,0.0f),0.0f);
  115. break;
  116. case afxZodiacPlaneData::FACES_BACK:
  117. aa_rot.set(Point3F(0.0f,-1.0f,0.0f),0.0f);
  118. break;
  119. case afxZodiacPlaneData::FACES_RIGHT:
  120. aa_rot.set(Point3F(1.0f,0.0f,0.0f),0.0f);
  121. break;
  122. case afxZodiacPlaneData::FACES_LEFT:
  123. aa_rot.set(Point3F(-1.0f,0.0f,0.0f),0.0f);
  124. break;
  125. }
  126. return true;
  127. }
  128. bool afxEA_ZodiacPlane::ea_update(F32 dt)
  129. {
  130. if (!pzode)
  131. {
  132. // create and register effect
  133. pzode = new afxZodiacPlane();
  134. pzode->onNewDataBlock(zode_data, false);
  135. if (!pzode->registerObject())
  136. {
  137. delete pzode;
  138. pzode = 0;
  139. Con::errorf("afxEA_ZodiacPlane::ea_update() -- effect failed to register.");
  140. return false;
  141. }
  142. deleteNotify(pzode);
  143. ///pzode->setSequenceRateFactor(datablock->rate_factor/prop_time_factor);
  144. ///pzode->setSortPriority(datablock->sort_priority);
  145. }
  146. if (pzode)
  147. {
  148. //LinearColorF zode_color = zode_data->color;
  149. LinearColorF zode_color = mUpdated_color;
  150. if (live_color_factor > 0.0)
  151. zode_color.interpolate(zode_color, live_color, live_color_factor);
  152. if (mDo_fades)
  153. {
  154. if (zode_data->blend_flags == afxZodiacDefs::BLEND_SUBTRACTIVE)
  155. zode_color *= mFade_value *mLive_fade_factor;
  156. else
  157. zode_color.alpha *= mFade_value * mLive_fade_factor;
  158. }
  159. // scale and grow zode
  160. //F32 zode_radius = zode_data->radius_xy*updated_scale.x + life_elapsed*zode_data->growth_rate;
  161. F32 zode_radius = zode_data->radius_xy + mLife_elapsed *zode_data->growth_rate;
  162. // zode is growing
  163. if (mLife_elapsed < zode_data->grow_in_time)
  164. {
  165. F32 t = mLife_elapsed /zode_data->grow_in_time;
  166. zode_radius = afxEase::eq(t, 0.001f, zode_radius, 0.2f, 0.8f);
  167. }
  168. // zode is shrinking
  169. else if (mFull_lifetime - mLife_elapsed < zode_data->shrink_out_time)
  170. {
  171. F32 t = (mFull_lifetime - mLife_elapsed)/zode_data->shrink_out_time;
  172. zode_radius = afxEase::eq(t, 0.001f, zode_radius, 0.0f, 0.9f);
  173. }
  174. zode_radius *= mLive_scale_factor;
  175. if (zode_data->respect_ori_cons && !zode_data->use_full_xfm)
  176. {
  177. VectorF shape_vec;
  178. mUpdated_xfm.getColumn(1, &shape_vec);
  179. shape_vec.normalize();
  180. F32 ang;
  181. switch (zode_data->face_dir)
  182. {
  183. case afxZodiacPlaneData::FACES_FORWARD:
  184. case afxZodiacPlaneData::FACES_BACK:
  185. ang = mAtan2(shape_vec.x, shape_vec.z);
  186. break;
  187. case afxZodiacPlaneData::FACES_RIGHT:
  188. case afxZodiacPlaneData::FACES_LEFT:
  189. ang = mAtan2(shape_vec.y, shape_vec.z);
  190. break;
  191. case afxZodiacPlaneData::FACES_UP:
  192. case afxZodiacPlaneData::FACES_DOWN:
  193. default:
  194. ang = mAtan2(shape_vec.x, shape_vec.y);
  195. break;
  196. }
  197. if (ang < 0.0f)
  198. ang += M_2PI_F;
  199. switch (zode_data->face_dir)
  200. {
  201. case afxZodiacPlaneData::FACES_DOWN:
  202. case afxZodiacPlaneData::FACES_BACK:
  203. case afxZodiacPlaneData::FACES_LEFT:
  204. ang = -ang;
  205. break;
  206. }
  207. zode_angle_offset = mRadToDeg(ang);
  208. }
  209. F32 zode_angle = zode_data->calcRotationAngle(mLife_elapsed, mDatablock->rate_factor/ mProp_time_factor);
  210. zode_angle = mFmod(zode_angle + zode_angle_offset, 360.0f);
  211. aa_rot.angle = mDegToRad(zode_angle);
  212. MatrixF spin_xfm;
  213. aa_rot.setMatrix(&spin_xfm);
  214. // set color, radius
  215. pzode->setColor(zode_color);
  216. pzode->setRadius(zode_radius);
  217. if (zode_data->use_full_xfm)
  218. {
  219. mUpdated_xfm.mul(spin_xfm);
  220. pzode->setTransform(mUpdated_xfm);
  221. }
  222. else
  223. pzode->setTransform(spin_xfm);
  224. pzode->setPosition(mUpdated_pos);
  225. pzode->setScale(mUpdated_scale);
  226. }
  227. return true;
  228. }
  229. void afxEA_ZodiacPlane::ea_finish(bool was_stopped)
  230. {
  231. if (!pzode)
  232. return;
  233. pzode->deleteObject();
  234. pzode = 0;
  235. }
  236. void afxEA_ZodiacPlane::ea_set_scope_status(bool in_scope)
  237. {
  238. if (pzode)
  239. pzode->setVisibility(in_scope);
  240. }
  241. void afxEA_ZodiacPlane::onDeleteNotify(SimObject* obj)
  242. {
  243. if (pzode == dynamic_cast<afxZodiacPlane*>(obj))
  244. pzode = 0;
  245. Parent::onDeleteNotify(obj);
  246. }
  247. void afxEA_ZodiacPlane::getUpdatedBoxCenter(Point3F& pos)
  248. {
  249. if (pzode)
  250. pos = pzode->getBoxCenter();
  251. }
  252. void afxEA_ZodiacPlane::do_runtime_substitutions()
  253. {
  254. // only clone the datablock if there are substitutions
  255. if (zode_data->getSubstitutionCount() > 0)
  256. {
  257. // clone the datablock and perform substitutions
  258. afxZodiacPlaneData* orig_db = zode_data;
  259. zode_data = new afxZodiacPlaneData(*orig_db, true);
  260. orig_db->performSubstitutions(zode_data, mChoreographer, mGroup_index);
  261. }
  262. }
  263. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  264. class afxEA_ZodiacPlaneDesc : public afxEffectAdapterDesc, public afxEffectDefs
  265. {
  266. static afxEA_ZodiacPlaneDesc desc;
  267. public:
  268. virtual bool testEffectType(const SimDataBlock*) const;
  269. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  270. virtual bool runsOnServer(const afxEffectWrapperData*) const { return false; }
  271. virtual bool runsOnClient(const afxEffectWrapperData*) const { return true; }
  272. virtual afxEffectWrapper* create() const { return new afxEA_ZodiacPlane; }
  273. };
  274. //~~~~~~~~~~~~~~~~~~~~//
  275. afxEA_ZodiacPlaneDesc afxEA_ZodiacPlaneDesc::desc;
  276. bool afxEA_ZodiacPlaneDesc::testEffectType(const SimDataBlock* db) const
  277. {
  278. return (typeid(afxZodiacPlaneData) == typeid(*db));
  279. }
  280. bool afxEA_ZodiacPlaneDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  281. {
  282. return (timing.lifetime < 0);
  283. }
  284. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//