afxXM_PivotNodeOffset.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "ts/tsShapeInstance.h"
  26. #include "afx/afxEffectWrapper.h"
  27. #include "afx/afxChoreographer.h"
  28. #include "afx/xm/afxXfmMod.h"
  29. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  30. class afxXM_PivotNodeOffsetData : public afxXM_BaseData
  31. {
  32. typedef afxXM_BaseData Parent;
  33. public:
  34. StringTableEntry node_name;
  35. bool node_is_static;
  36. public:
  37. /*C*/ afxXM_PivotNodeOffsetData();
  38. /*C*/ afxXM_PivotNodeOffsetData(const afxXM_PivotNodeOffsetData&, bool = false);
  39. void packData(BitStream* stream);
  40. void unpackData(BitStream* stream);
  41. virtual bool allowSubstitutions() const { return true; }
  42. static void initPersistFields();
  43. afxXM_Base* create(afxEffectWrapper* fx, bool on_server);
  44. DECLARE_CONOBJECT(afxXM_PivotNodeOffsetData);
  45. DECLARE_CATEGORY("AFX");
  46. };
  47. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  48. class afxXM_PivotNodeOffset : public afxXM_Base
  49. {
  50. typedef afxXM_Base Parent;
  51. StringTableEntry node_name;
  52. bool node_is_static;
  53. S32 node_ID;
  54. Point3F pivot_offset;
  55. bool offset_calculated;
  56. public:
  57. /*C*/ afxXM_PivotNodeOffset(afxXM_PivotNodeOffsetData*, afxEffectWrapper*);
  58. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  59. };
  60. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  61. IMPLEMENT_CO_DATABLOCK_V1(afxXM_PivotNodeOffsetData);
  62. ConsoleDocClass( afxXM_PivotNodeOffsetData,
  63. "@brief An xmod datablock.\n\n"
  64. "@ingroup afxXMods\n"
  65. "@ingroup AFX\n"
  66. "@ingroup Datablocks\n"
  67. );
  68. afxXM_PivotNodeOffsetData::afxXM_PivotNodeOffsetData()
  69. {
  70. node_name = ST_NULLSTRING;
  71. node_is_static = true;
  72. }
  73. afxXM_PivotNodeOffsetData::afxXM_PivotNodeOffsetData(const afxXM_PivotNodeOffsetData& other, bool temp_clone) : afxXM_BaseData(other, temp_clone)
  74. {
  75. node_name = other.node_name;
  76. node_is_static = other.node_is_static;
  77. }
  78. void afxXM_PivotNodeOffsetData::initPersistFields()
  79. {
  80. addField("nodeName", TypeString, Offset(node_name, afxXM_PivotNodeOffsetData),
  81. "...");
  82. addField("nodeIsStatic", TypeBool, Offset(node_is_static, afxXM_PivotNodeOffsetData),
  83. "...");
  84. Parent::initPersistFields();
  85. }
  86. void afxXM_PivotNodeOffsetData::packData(BitStream* stream)
  87. {
  88. Parent::packData(stream);
  89. stream->writeString(node_name);
  90. stream->writeFlag(node_is_static);
  91. }
  92. void afxXM_PivotNodeOffsetData::unpackData(BitStream* stream)
  93. {
  94. Parent::unpackData(stream);
  95. node_name = stream->readSTString();
  96. node_is_static = stream->readFlag();
  97. }
  98. afxXM_Base* afxXM_PivotNodeOffsetData::create(afxEffectWrapper* fx, bool on_server)
  99. {
  100. afxXM_PivotNodeOffsetData* datablock = this;
  101. if (getSubstitutionCount() > 0)
  102. {
  103. datablock = new afxXM_PivotNodeOffsetData(*this, true);
  104. this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex());
  105. }
  106. return new afxXM_PivotNodeOffset(datablock, fx);
  107. }
  108. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  109. afxXM_PivotNodeOffset::afxXM_PivotNodeOffset(afxXM_PivotNodeOffsetData* db, afxEffectWrapper* fxw)
  110. : afxXM_Base(db, fxw)
  111. {
  112. node_name = db->node_name;
  113. node_is_static = db->node_is_static;
  114. node_ID = -1;
  115. pivot_offset.set(0,0,0);
  116. offset_calculated = false;
  117. }
  118. void afxXM_PivotNodeOffset::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  119. {
  120. if (node_ID < 0)
  121. {
  122. TSShape* ts_shape = fx_wrapper->getTSShape();
  123. node_ID = (ts_shape) ? ts_shape->findNode(node_name) : -1;
  124. }
  125. if (node_ID >= 0)
  126. {
  127. if (!node_is_static || !offset_calculated)
  128. {
  129. TSShapeInstance* ts_shape_inst = fx_wrapper->getTSShapeInstance();
  130. if (ts_shape_inst)
  131. {
  132. const MatrixF& pivot_xfm = ts_shape_inst->mNodeTransforms[node_ID];
  133. pivot_offset = -pivot_xfm.getPosition();
  134. offset_calculated = true;
  135. }
  136. }
  137. }
  138. // re-orient pivot offset then add to position
  139. Point3F pivot_offset_temp;
  140. params.ori.mulV(pivot_offset, &pivot_offset_temp);
  141. params.pos += pivot_offset_temp;
  142. }
  143. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//