afxEA_Zodiac.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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/afxResidueMgr.h"
  31. #include "afx/util/afxEase.h"
  32. #include "afx/ce/afxZodiacMgr.h"
  33. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  34. // afxEA_Zodiac
  35. class afxEA_Zodiac : public afxEffectWrapper
  36. {
  37. typedef afxEffectWrapper Parent;
  38. afxZodiacData* zode_data;
  39. Point3F zode_pos;
  40. F32 zode_radius;
  41. Point2F zode_vrange;
  42. LinearColorF zode_color;
  43. F32 zode_angle;
  44. F32 zode_angle_offset;
  45. F32 live_color_factor;
  46. LinearColorF live_color;
  47. bool became_residue;
  48. bool do_altitude_bias;
  49. F32 altitude_falloff_range;
  50. F32 calc_facing_angle();
  51. F32 calc_terrain_alt_bias();
  52. F32 calc_interior_alt_bias();
  53. void do_runtime_substitutions();
  54. public:
  55. /*C*/ afxEA_Zodiac();
  56. /*C*/ ~afxEA_Zodiac();
  57. virtual void ea_set_datablock(SimDataBlock*);
  58. virtual bool ea_start();
  59. virtual bool ea_update(F32 dt);
  60. virtual void ea_finish(bool was_stopped);
  61. virtual bool ea_is_enabled() { return true; }
  62. virtual void getBaseColor(LinearColorF& color) { color = zode_data->color; }
  63. static void initPersistFields();
  64. //DECLARE_CONOBJECT(afxEA_Zodiac);
  65. DECLARE_CATEGORY("AFX");
  66. };
  67. //IMPLEMENT_CONOBJECT(afxEA_Zodiac);
  68. //~~~~~~~~~~~~~~~~~~~~//
  69. F32 afxEA_Zodiac::calc_facing_angle()
  70. {
  71. // get direction player is facing
  72. VectorF shape_vec;
  73. MatrixF shape_xfm;
  74. afxConstraint* orient_constraint = getOrientConstraint();
  75. if (orient_constraint)
  76. orient_constraint->getTransform(shape_xfm);
  77. else
  78. shape_xfm.identity();
  79. shape_xfm.getColumn(1, &shape_vec);
  80. shape_vec.z = 0.0f;
  81. shape_vec.normalize();
  82. F32 pitch, yaw;
  83. MathUtils::getAnglesFromVector(shape_vec, yaw, pitch);
  84. return mRadToDeg(yaw);
  85. }
  86. inline F32 afxEA_Zodiac::calc_terrain_alt_bias()
  87. {
  88. if (mTerrain_altitude >= zode_data->altitude_max)
  89. return 0.0f;
  90. return 1.0f - (mTerrain_altitude - zode_data->altitude_falloff)/altitude_falloff_range;
  91. }
  92. inline F32 afxEA_Zodiac::calc_interior_alt_bias()
  93. {
  94. if (mInterior_altitude >= zode_data->altitude_max)
  95. return 0.0f;
  96. return 1.0f - (mInterior_altitude - zode_data->altitude_falloff)/altitude_falloff_range;
  97. }
  98. afxEA_Zodiac::afxEA_Zodiac()
  99. {
  100. zode_data = 0;
  101. zode_pos.zero();
  102. zode_radius = 1;
  103. zode_vrange.set(1,1);
  104. zode_color.set(1,1,1,1);
  105. zode_angle = 0;
  106. zode_angle_offset = 0;
  107. live_color.set(1,1,1,1);
  108. live_color_factor = 0.0f;
  109. do_altitude_bias = false;
  110. altitude_falloff_range = 0.0f;
  111. became_residue = false;
  112. }
  113. afxEA_Zodiac::~afxEA_Zodiac()
  114. {
  115. if (!became_residue && zode_data && zode_data->isTempClone())
  116. delete zode_data;
  117. zode_data = 0;
  118. }
  119. void afxEA_Zodiac::ea_set_datablock(SimDataBlock* db)
  120. {
  121. zode_data = dynamic_cast<afxZodiacData*>(db);
  122. if (zode_data)
  123. {
  124. do_altitude_bias = (zode_data->altitude_max > 0.0f && (zode_data->altitude_shrinks || zode_data->altitude_fades));
  125. altitude_falloff_range = zode_data->altitude_max - zode_data->altitude_falloff;
  126. }
  127. }
  128. bool afxEA_Zodiac::ea_start()
  129. {
  130. if (!zode_data)
  131. {
  132. Con::errorf("afxEA_Zodiac::ea_start() -- missing or incompatible datablock.");
  133. return false;
  134. }
  135. do_runtime_substitutions();
  136. zode_angle_offset = calc_facing_angle();
  137. return true;
  138. }
  139. bool afxEA_Zodiac::ea_update(F32 dt)
  140. {
  141. if (!mIn_scope)
  142. return true;
  143. //~~~~~~~~~~~~~~~~~~~~//
  144. // Zodiac Color
  145. zode_color = mUpdated_color;
  146. if (live_color_factor > 0.0)
  147. {
  148. zode_color.interpolate(zode_color, live_color, live_color_factor);
  149. //Con::printf("LIVE-COLOR %g %g %g %g FACTOR is %g",
  150. // live_color.red, live_color.green, live_color.blue, live_color.alpha,
  151. // live_color_factor);
  152. }
  153. else
  154. {
  155. //Con::printf("LIVE-COLOR-FACTOR is ZERO");
  156. }
  157. if (mDo_fades)
  158. {
  159. if (mFade_value < 0.01f)
  160. return true; // too transparent
  161. if (zode_data->blend_flags == afxZodiacDefs::BLEND_SUBTRACTIVE)
  162. zode_color *= mFade_value * mLive_fade_factor;
  163. else
  164. zode_color.alpha *= mFade_value * mLive_fade_factor;
  165. }
  166. if (zode_color.alpha < 0.01f)
  167. return true;
  168. //~~~~~~~~~~~~~~~~~~~~//
  169. // Zodiac
  170. // scale and grow zode
  171. zode_radius = zode_data->radius_xy*mUpdated_scale.x + mLife_elapsed *zode_data->growth_rate;
  172. // zode is growing
  173. if (mLife_elapsed < zode_data->grow_in_time)
  174. {
  175. F32 t = mLife_elapsed /zode_data->grow_in_time;
  176. zode_radius = afxEase::eq(t, 0.001f, zode_radius, 0.2f, 0.8f);
  177. }
  178. // zode is shrinking
  179. else if (mFull_lifetime - mLife_elapsed < zode_data->shrink_out_time)
  180. {
  181. F32 t = (mFull_lifetime - mLife_elapsed)/zode_data->shrink_out_time;
  182. zode_radius = afxEase::eq(t, 0.001f, zode_radius, 0.0f, 0.9f);
  183. }
  184. zode_radius *= mLive_scale_factor;
  185. if (zode_radius < 0.001f)
  186. return true; // too small
  187. zode_vrange = zode_data->vert_range;
  188. if (zode_data->scale_vert_range)
  189. {
  190. F32 scale_factor = zode_radius/zode_data->radius_xy;
  191. zode_vrange *= scale_factor;
  192. }
  193. //~~~~~~~~~~~~~~~~~~~~//
  194. // Zodiac Position
  195. zode_pos = mUpdated_pos;
  196. //~~~~~~~~~~~~~~~~~~~~//
  197. // Zodiac Rotation
  198. if (zode_data->respect_ori_cons)
  199. {
  200. afxConstraint* orient_constraint = getOrientConstraint();
  201. if (orient_constraint)
  202. {
  203. VectorF shape_vec;
  204. mUpdated_xfm.getColumn(1, &shape_vec);
  205. shape_vec.z = 0.0f;
  206. shape_vec.normalize();
  207. F32 pitch, yaw;
  208. MathUtils::getAnglesFromVector(shape_vec, yaw, pitch);
  209. zode_angle_offset = mRadToDeg(yaw);
  210. }
  211. }
  212. zode_angle = zode_data->calcRotationAngle(mLife_elapsed, mDatablock->rate_factor/ mProp_time_factor);
  213. zode_angle = mFmod(zode_angle + zode_angle_offset, 360.0f);
  214. //~~~~~~~~~~~~~~~~~~~~//
  215. // post zodiac
  216. if ((zode_data->zflags & afxZodiacDefs::SHOW_ON_TERRAIN) != 0)
  217. {
  218. if (do_altitude_bias && mTerrain_altitude > zode_data->altitude_falloff)
  219. {
  220. F32 alt_bias = calc_terrain_alt_bias();
  221. if (alt_bias > 0.0f)
  222. {
  223. F32 alt_rad = zode_radius;
  224. if (zode_data->altitude_shrinks)
  225. alt_rad *= alt_bias;
  226. LinearColorF alt_clr(zode_color.red, zode_color.green, zode_color.blue, zode_color.alpha);
  227. if (zode_data->altitude_fades)
  228. alt_clr.alpha *= alt_bias;
  229. afxZodiacMgr::addTerrainZodiac(zode_pos, alt_rad, alt_clr, zode_angle, zode_data);
  230. }
  231. }
  232. else
  233. {
  234. afxZodiacMgr::addTerrainZodiac(zode_pos, zode_radius, zode_color, zode_angle, zode_data);
  235. }
  236. }
  237. if ((zode_data->zflags & afxZodiacDefs::SHOW_ON_INTERIORS) != 0)
  238. {
  239. if (do_altitude_bias && mInterior_altitude > zode_data->altitude_falloff)
  240. {
  241. F32 alt_bias = calc_interior_alt_bias();
  242. if (alt_bias > 0.0f)
  243. {
  244. F32 alt_rad = zode_radius;
  245. if (zode_data->altitude_shrinks)
  246. alt_rad *= alt_bias;
  247. LinearColorF alt_clr(zode_color.red, zode_color.green, zode_color.blue, zode_color.alpha);
  248. if (zode_data->altitude_fades)
  249. alt_clr.alpha *= alt_bias;
  250. afxZodiacMgr::addInteriorZodiac(zode_pos, alt_rad, zode_vrange, alt_clr, zode_angle, zode_data);
  251. }
  252. }
  253. else
  254. afxZodiacMgr::addInteriorZodiac(zode_pos, zode_radius, zode_vrange, zode_color, zode_angle, zode_data);
  255. }
  256. return true;
  257. }
  258. void afxEA_Zodiac::ea_finish(bool was_stopped)
  259. {
  260. if (mIn_scope && mEW_timing.residue_lifetime > 0)
  261. {
  262. if (mDo_fades)
  263. {
  264. if (mFade_value < 0.01f)
  265. return;
  266. zode_color.alpha *= mFade_value;
  267. }
  268. if ((zode_data->zflags & afxZodiacDefs::SHOW_ON_TERRAIN) != 0)
  269. {
  270. if (do_altitude_bias && mTerrain_altitude > zode_data->altitude_falloff)
  271. {
  272. F32 alt_bias = calc_terrain_alt_bias();
  273. if (alt_bias > 0.0f)
  274. {
  275. F32 alt_rad = zode_radius;
  276. if (zode_data->altitude_shrinks)
  277. alt_rad *= alt_bias;
  278. LinearColorF alt_clr(zode_color.red, zode_color.green, zode_color.blue, zode_color.alpha);
  279. if (zode_data->altitude_fades)
  280. zode_color.alpha *= alt_bias;
  281. became_residue = true;
  282. afxResidueMgr::add_terrain_zodiac(mEW_timing.residue_lifetime, mEW_timing.residue_fadetime, zode_data, zode_pos, alt_rad,
  283. alt_clr, zode_angle);
  284. }
  285. }
  286. else
  287. {
  288. became_residue = true;
  289. afxResidueMgr::add_terrain_zodiac(mEW_timing.residue_lifetime, mEW_timing.residue_fadetime, zode_data, zode_pos, zode_radius,
  290. zode_color, zode_angle);
  291. }
  292. }
  293. if ((zode_data->zflags & afxZodiacDefs::SHOW_ON_INTERIORS) != 0)
  294. {
  295. if (do_altitude_bias && mInterior_altitude > zode_data->altitude_falloff)
  296. {
  297. F32 alt_bias = calc_interior_alt_bias();
  298. if (alt_bias > 0.0f)
  299. {
  300. F32 alt_rad = zode_radius;
  301. if (zode_data->altitude_shrinks)
  302. alt_rad *= alt_bias;
  303. LinearColorF alt_clr(zode_color.red, zode_color.green, zode_color.blue, zode_color.alpha);
  304. if (zode_data->altitude_fades)
  305. zode_color.alpha *= alt_bias;
  306. afxZodiacData* temp_zode = zode_data;
  307. if (became_residue)
  308. temp_zode = new afxZodiacData(*zode_data, true);
  309. became_residue = true;
  310. afxResidueMgr::add_interior_zodiac(mEW_timing.residue_lifetime, mEW_timing.residue_fadetime, temp_zode, zode_pos, alt_rad,
  311. zode_vrange, alt_clr, zode_angle);
  312. }
  313. }
  314. else
  315. {
  316. afxZodiacData* temp_zode = zode_data;
  317. if (became_residue)
  318. temp_zode = new afxZodiacData(*zode_data, true);
  319. became_residue = true;
  320. afxResidueMgr::add_interior_zodiac(mEW_timing.residue_lifetime, mEW_timing.residue_fadetime, temp_zode, zode_pos, zode_radius,
  321. zode_vrange, zode_color, zode_angle);
  322. }
  323. }
  324. }
  325. }
  326. void afxEA_Zodiac::do_runtime_substitutions()
  327. {
  328. // only clone the datablock if there are substitutions
  329. if (zode_data->getSubstitutionCount() > 0)
  330. {
  331. // clone the datablock and perform substitutions
  332. afxZodiacData* orig_db = zode_data;
  333. zode_data = new afxZodiacData(*orig_db, true);
  334. orig_db->performSubstitutions(zode_data, mChoreographer, mGroup_index);
  335. }
  336. }
  337. #undef myOffset
  338. #define myOffset(field) Offset(field, afxEA_Zodiac)
  339. void afxEA_Zodiac::initPersistFields()
  340. {
  341. addField("liveColor", TypeColorF, myOffset(live_color),
  342. "...");
  343. addField("liveColorFactor", TypeF32, myOffset(live_color_factor),
  344. "...");
  345. Parent::initPersistFields();
  346. }
  347. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  348. class afxEA_ZodiacDesc : public afxEffectAdapterDesc, public afxEffectDefs
  349. {
  350. static afxEA_ZodiacDesc desc;
  351. public:
  352. virtual bool testEffectType(const SimDataBlock*) const;
  353. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  354. virtual bool runsOnServer(const afxEffectWrapperData*) const { return false; }
  355. virtual bool runsOnClient(const afxEffectWrapperData*) const { return true; }
  356. virtual afxEffectWrapper* create() const { return new afxEA_Zodiac; }
  357. };
  358. afxEA_ZodiacDesc afxEA_ZodiacDesc::desc;
  359. bool afxEA_ZodiacDesc::testEffectType(const SimDataBlock* db) const
  360. {
  361. return (typeid(afxZodiacData) == typeid(*db));
  362. }
  363. bool afxEA_ZodiacDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  364. {
  365. return (timing.lifetime < 0);
  366. }
  367. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//