afxXM_BoxAdapt.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/xm/afxXfmMod.h"
  29. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  30. class afxXM_BoxAdaptData : public afxXM_WeightedBaseData
  31. {
  32. typedef afxXM_WeightedBaseData Parent;
  33. public:
  34. F32 scale_factor;
  35. Point2F dim_range;
  36. public:
  37. /*C*/ afxXM_BoxAdaptData();
  38. /*C*/ afxXM_BoxAdaptData(const afxXM_BoxAdaptData&, bool = false);
  39. void packData(BitStream* stream);
  40. void unpackData(BitStream* stream);
  41. static void initPersistFields();
  42. afxXM_Base* create(afxEffectWrapper* fx, bool on_server);
  43. DECLARE_CONOBJECT(afxXM_BoxAdaptData);
  44. DECLARE_CATEGORY("AFX");
  45. };
  46. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  47. class afxConstraint;
  48. class afxXM_BoxAdapt : public afxXM_WeightedBase
  49. {
  50. typedef afxXM_WeightedBase Parent;
  51. F32 scale_factor;
  52. Point2F dim_range;
  53. public:
  54. /*C*/ afxXM_BoxAdapt(afxXM_BoxAdaptData*, afxEffectWrapper*);
  55. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  56. };
  57. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  58. IMPLEMENT_CO_DATABLOCK_V1(afxXM_BoxAdaptData);
  59. ConsoleDocClass( afxXM_BoxAdaptData,
  60. "@brief An xmod datablock.\n\n"
  61. "@ingroup afxXMods\n"
  62. "@ingroup AFX\n"
  63. "@ingroup Datablocks\n"
  64. );
  65. afxXM_BoxAdaptData::afxXM_BoxAdaptData()
  66. {
  67. scale_factor = 1.0f;
  68. dim_range.set(0.1f, 1000.0f);
  69. }
  70. afxXM_BoxAdaptData::afxXM_BoxAdaptData(const afxXM_BoxAdaptData& other, bool temp_clone)
  71. : afxXM_WeightedBaseData(other, temp_clone)
  72. {
  73. scale_factor = other.scale_factor;
  74. dim_range = other.dim_range;
  75. }
  76. void afxXM_BoxAdaptData::initPersistFields()
  77. {
  78. docsURL;
  79. addField("scaleFactor", TypeF32, Offset(scale_factor, afxXM_BoxAdaptData),
  80. "...");
  81. addField("dimensionRange", TypePoint2F, Offset(dim_range, afxXM_BoxAdaptData),
  82. "...");
  83. Parent::initPersistFields();
  84. }
  85. void afxXM_BoxAdaptData::packData(BitStream* stream)
  86. {
  87. Parent::packData(stream);
  88. stream->write(scale_factor);
  89. mathWrite(*stream, dim_range);
  90. }
  91. void afxXM_BoxAdaptData::unpackData(BitStream* stream)
  92. {
  93. Parent::unpackData(stream);
  94. stream->read(&scale_factor);
  95. mathRead(*stream, &dim_range);
  96. }
  97. afxXM_Base* afxXM_BoxAdaptData::create(afxEffectWrapper* fx, bool on_server)
  98. {
  99. return new afxXM_BoxAdapt(this, fx);
  100. }
  101. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  102. afxXM_BoxAdapt::afxXM_BoxAdapt(afxXM_BoxAdaptData* db, afxEffectWrapper* fxw)
  103. : afxXM_WeightedBase(db, fxw)
  104. {
  105. scale_factor = db->scale_factor;
  106. dim_range = db->dim_range;
  107. dim_range.x = getMax(0.001f, dim_range.x);
  108. dim_range.y = getMax(0.001f, dim_range.y);
  109. if (dim_range.x > dim_range.y)
  110. {
  111. F32 tmp = dim_range.y;
  112. dim_range.y = dim_range.x;
  113. dim_range.x = tmp;
  114. }
  115. }
  116. void afxXM_BoxAdapt::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  117. {
  118. afxConstraint* pos_cons = fx_wrapper->getPosConstraint();
  119. if (!pos_cons)
  120. return;
  121. SceneObject* obj = pos_cons->getSceneObject();
  122. if (!obj)
  123. return;
  124. F32 wt_factor = calc_weight_factor(elapsed);
  125. const Box3F& obj_box = obj->getObjBox();
  126. const VectorF obj_scale = obj->getScale();
  127. F32 x_dim = obj_box.len_x()*obj_scale.x;
  128. F32 y_dim = obj_box.len_y()*obj_scale.y;
  129. F32 dim = mClampF(getMax(x_dim, y_dim), dim_range.x, dim_range.y);
  130. dim *= scale_factor*wt_factor*0.5f;
  131. //Con::printf("SET liveScaleFactor=%g x_dim=%g, y_dim=%g", dim, obj_box.len_x(), obj_box.len_y());
  132. fx_wrapper->setField("liveScaleFactor", avar("%g", dim));
  133. }
  134. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//