afxEA_CameraShake.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "T3D/fx/cameraFXMgr.h"
  27. #include "afx/afxEffectDefs.h"
  28. #include "afx/afxEffectWrapper.h"
  29. #include "afx/afxChoreographer.h"
  30. #include "afx/ce/afxCameraShake.h"
  31. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  32. // afxEA_CameraShake
  33. class afxEA_CameraShake : public afxEffectWrapper
  34. {
  35. typedef afxEffectWrapper Parent;
  36. afxCameraShakeData* shake_data;
  37. CameraShake* camera_shake;
  38. void do_runtime_substitutions();
  39. public:
  40. /*C*/ afxEA_CameraShake();
  41. /*D*/ ~afxEA_CameraShake();
  42. virtual void ea_set_datablock(SimDataBlock*);
  43. virtual bool ea_start();
  44. virtual bool ea_update(F32 dt);
  45. virtual void ea_finish(bool was_stopped);
  46. };
  47. //~~~~~~~~~~~~~~~~~~~~//
  48. afxEA_CameraShake::afxEA_CameraShake()
  49. {
  50. shake_data = 0;
  51. camera_shake = 0;
  52. }
  53. afxEA_CameraShake::~afxEA_CameraShake()
  54. {
  55. delete camera_shake;
  56. if (shake_data && shake_data->isTempClone())
  57. delete shake_data;
  58. shake_data = 0;
  59. }
  60. void afxEA_CameraShake::ea_set_datablock(SimDataBlock* db)
  61. {
  62. shake_data = dynamic_cast<afxCameraShakeData*>(db);
  63. }
  64. bool afxEA_CameraShake::ea_start()
  65. {
  66. if (!shake_data)
  67. {
  68. Con::errorf("afxEA_CameraShake::ea_start() -- missing or incompatible datablock.");
  69. return false;
  70. }
  71. do_runtime_substitutions();
  72. afxConstraint* pos_constraint = getPosConstraint();
  73. afxConstraint* aim_constraint = getAimConstraint();
  74. if (aim_constraint && pos_constraint)
  75. {
  76. if (mFull_lifetime <= 0 || mFull_lifetime == INFINITE_LIFETIME)
  77. {
  78. Con::errorf("afxEA_CameraShake::ea_start() -- effect requires a finite lifetime.");
  79. return false;
  80. }
  81. SceneObject* shaken = aim_constraint->getSceneObject();
  82. if (shaken)
  83. {
  84. Point3F pos; pos_constraint->getPosition(pos);
  85. VectorF diff = shaken->getPosition() - pos;
  86. F32 dist = diff.len();
  87. if (dist < shake_data->camShakeRadius)
  88. {
  89. camera_shake = new CameraShake;
  90. camera_shake->setDuration(mFull_lifetime);
  91. camera_shake->setFrequency(shake_data->camShakeFreq);
  92. F32 falloff = dist/shake_data->camShakeRadius;
  93. falloff = 1 + falloff*10.0;
  94. falloff = 1.0 / (falloff*falloff);
  95. VectorF shakeAmp = shake_data->camShakeAmp*falloff;
  96. camera_shake->setAmplitude(shakeAmp);
  97. camera_shake->setFalloff(shake_data->camShakeFalloff);
  98. camera_shake->init();
  99. }
  100. }
  101. }
  102. return true;
  103. }
  104. bool afxEA_CameraShake::ea_update(F32 dt)
  105. {
  106. afxConstraint* aim_constraint = getAimConstraint();
  107. if (camera_shake && aim_constraint)
  108. {
  109. camera_shake->update(dt);
  110. SceneObject* shaken = aim_constraint->getSceneObject();
  111. if (shaken)
  112. {
  113. MatrixF fxTrans = camera_shake->getTrans();
  114. MatrixF curTrans = shaken->getRenderTransform();
  115. curTrans.mul(fxTrans);
  116. Point3F cameraPosWorld;
  117. curTrans.getColumn(3,&cameraPosWorld);
  118. shaken->setPosition(cameraPosWorld);
  119. }
  120. }
  121. return true;
  122. }
  123. void afxEA_CameraShake::ea_finish(bool was_stopped)
  124. {
  125. delete camera_shake;
  126. camera_shake = 0;
  127. }
  128. void afxEA_CameraShake::do_runtime_substitutions()
  129. {
  130. // only clone the datablock if there are substitutions
  131. if (shake_data->getSubstitutionCount() > 0)
  132. {
  133. // clone the datablock and perform substitutions
  134. afxCameraShakeData* orig_db = shake_data;
  135. shake_data = new afxCameraShakeData(*orig_db, true);
  136. orig_db->performSubstitutions(shake_data, mChoreographer, mGroup_index);
  137. }
  138. }
  139. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  140. class afxEA_CameraShakeDesc : public afxEffectAdapterDesc, public afxEffectDefs
  141. {
  142. static afxEA_CameraShakeDesc desc;
  143. public:
  144. virtual bool testEffectType(const SimDataBlock*) const;
  145. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  146. virtual bool runsOnServer(const afxEffectWrapperData*) const { return false; }
  147. virtual bool runsOnClient(const afxEffectWrapperData*) const { return true; }
  148. virtual bool isPositional(const afxEffectWrapperData*) const { return false; }
  149. virtual afxEffectWrapper* create() const { return new afxEA_CameraShake; }
  150. };
  151. afxEA_CameraShakeDesc afxEA_CameraShakeDesc::desc;
  152. bool afxEA_CameraShakeDesc::testEffectType(const SimDataBlock* db) const
  153. {
  154. return (typeid(afxCameraShakeData) == typeid(*db));
  155. }
  156. bool afxEA_CameraShakeDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  157. {
  158. return (timing.lifetime < 0);
  159. }
  160. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//