afxEA_SpotLight_T3D.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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/spotLight.h"
  27. #include "afx/ce/afxSpotLight_T3D.h"
  28. #include "afx/afxEffectDefs.h"
  29. #include "afx/afxEffectWrapper.h"
  30. #include "afx/afxChoreographer.h"
  31. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  32. // afxEA_T3DSpotLight
  33. class SpotLightProxy;
  34. class afxEA_T3DSpotLight : public afxEffectWrapper
  35. {
  36. typedef afxEffectWrapper Parent;
  37. afxT3DSpotLightData* light_data;
  38. SpotLightProxy* light;
  39. void do_runtime_substitutions();
  40. public:
  41. /*C*/ afxEA_T3DSpotLight();
  42. /*D*/ ~afxEA_T3DSpotLight();
  43. virtual void ea_set_datablock(SimDataBlock*);
  44. virtual bool ea_start();
  45. virtual bool ea_update(F32 dt);
  46. virtual void ea_finish(bool was_stopped);
  47. virtual void ea_set_scope_status(bool flag);
  48. virtual void onDeleteNotify(SimObject*);
  49. virtual void getBaseColor(LinearColorF& color);
  50. virtual bool ea_is_enabled() { return true; }
  51. };
  52. //~~~~~~~~~~~~~~~~~~~~//
  53. class SpotLightProxy : public SpotLight
  54. {
  55. F32 mFade_amt;
  56. public:
  57. SpotLightProxy() { mFade_amt = 1.0f; }
  58. void force_ghost()
  59. {
  60. mNetFlags.clear(Ghostable | ScopeAlways);
  61. mNetFlags.set(IsGhost);
  62. }
  63. void setFadeAmount(F32 fade_amt)
  64. {
  65. mFade_amt = fade_amt;
  66. mLight->setBrightness(mBrightness*fade_amt);
  67. }
  68. void updateTransform(const MatrixF& xfm)
  69. {
  70. mLight->setTransform(xfm);
  71. LightBase::setTransform(xfm);
  72. }
  73. void initWithDataBlock(const afxT3DSpotLightData* db)
  74. {
  75. mRange = getMax(db->mRange, 0.05f);
  76. mInnerConeAngle = db->mInnerConeAngle;
  77. mOuterConeAngle = db->mOuterConeAngle;
  78. mColor = db->mColor;
  79. mBrightness = db->mBrightness;
  80. mCastShadows = db->mCastShadows;
  81. mPriority = db->mPriority;
  82. mFlareData = db->mFlareData;
  83. mAnimationData = db->mAnimationData;
  84. mAnimState.active = (mAnimationData != 0);
  85. mLocalRenderViz = db->mLocalRenderViz;
  86. mLight->setType( LightInfo::Spot );
  87. mLight->setBrightness( db->mBrightness );
  88. mLight->setRange( db->mRange );
  89. mLight->setColor( db->mColor );
  90. mLight->setCastShadows( db->mCastShadows );
  91. mLight->setPriority( db->mPriority );
  92. mLight->setInnerConeAngle( db->mInnerConeAngle );
  93. mLight->setOuterConeAngle( db->mOuterConeAngle );
  94. // Update the bounds and scale to fit our light.
  95. F32 radius = mRange * mSin( mDegToRad( mOuterConeAngle ) * 0.5f );
  96. mObjBox.minExtents.set( -1, -1, -1 );
  97. mObjBox.maxExtents.set( 1, 1, 1 );
  98. mObjScale.set( radius, mRange, radius );
  99. //_conformLights();
  100. }
  101. void setLiveColor(const LinearColorF& live_color)
  102. {
  103. mLight->setColor(live_color);
  104. }
  105. void submitLights(LightManager* lm, bool staticLighting)
  106. {
  107. if (mAnimState.active && mAnimationData && mFade_amt < 1.0f)
  108. {
  109. F32 mBrightness_save = mBrightness;
  110. mBrightness *= mFade_amt;
  111. SpotLight::submitLights(lm, staticLighting);
  112. mBrightness = mBrightness_save;
  113. return;
  114. }
  115. SpotLight::submitLights(lm, staticLighting);
  116. }
  117. };
  118. //~~~~~~~~~~~~~~~~~~~~//
  119. afxEA_T3DSpotLight::afxEA_T3DSpotLight()
  120. {
  121. light_data = 0;
  122. light = 0;
  123. }
  124. afxEA_T3DSpotLight::~afxEA_T3DSpotLight()
  125. {
  126. if (light)
  127. light->deleteObject();
  128. if (light_data && light_data->isTempClone())
  129. delete light_data;
  130. light_data = 0;
  131. }
  132. void afxEA_T3DSpotLight::ea_set_datablock(SimDataBlock* db)
  133. {
  134. light_data = dynamic_cast<afxT3DSpotLightData*>(db);
  135. }
  136. bool afxEA_T3DSpotLight::ea_start()
  137. {
  138. if (!light_data)
  139. {
  140. Con::errorf("afxEA_T3DSpotLight::ea_start() -- missing or incompatible datablock.");
  141. return false;
  142. }
  143. do_runtime_substitutions();
  144. // create and register effect
  145. light = new SpotLightProxy();
  146. light->force_ghost();
  147. if (!light->registerObject())
  148. {
  149. delete light;
  150. light = 0;
  151. Con::errorf("afxEA_T3DSpotLight::ea_update() -- effect failed to register.");
  152. return false;
  153. }
  154. deleteNotify(light);
  155. light->initWithDataBlock(light_data);
  156. return true;
  157. }
  158. bool afxEA_T3DSpotLight::ea_update(F32 dt)
  159. {
  160. if (light)
  161. {
  162. #if 0 // AFX_T3D_DISABLED
  163. // With sgLightObject lights, the following code block would hook
  164. // the constraint object up to the light in case the light was
  165. // configured to exclude it from flare occusions. The code remains
  166. // here in case we need to implement the same feature for T3D light.
  167. afxConstraint* pos_cons = getPosConstraint();
  168. SceneObject* cons_obj = (pos_cons) ? pos_cons->getSceneObject() : 0;
  169. light->setConstraintObject(cons_obj);
  170. #endif
  171. light->setLiveColor(mUpdated_color);
  172. if (mDo_fades)
  173. light->setFadeAmount(mFade_value);
  174. light->updateTransform(mUpdated_xfm);
  175. // scale should not be updated this way. It messes up the culling.
  176. //light->setScale(updated_scale);
  177. }
  178. return true;
  179. }
  180. void afxEA_T3DSpotLight::ea_finish(bool was_stopped)
  181. {
  182. if (light)
  183. {
  184. light->deleteObject();
  185. light = 0;
  186. }
  187. }
  188. void afxEA_T3DSpotLight::ea_set_scope_status(bool in_scope)
  189. {
  190. if (light)
  191. light->setLightEnabled(in_scope);
  192. }
  193. void afxEA_T3DSpotLight::onDeleteNotify(SimObject* obj)
  194. {
  195. if (light == dynamic_cast<SpotLight*>(obj))
  196. light = 0;
  197. Parent::onDeleteNotify(obj);
  198. }
  199. void afxEA_T3DSpotLight::getBaseColor(LinearColorF& color)
  200. {
  201. if (light_data)
  202. color = light_data->mColor;
  203. }
  204. void afxEA_T3DSpotLight::do_runtime_substitutions()
  205. {
  206. // only clone the datablock if there are substitutions
  207. if (light_data->getSubstitutionCount() > 0)
  208. {
  209. // clone the datablock and perform substitutions
  210. afxT3DSpotLightData* orig_db = light_data;
  211. light_data = new afxT3DSpotLightData(*orig_db, true);
  212. orig_db->performSubstitutions(light_data, mChoreographer, mGroup_index);
  213. }
  214. }
  215. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  216. class afxEA_T3DSpotLightDesc : public afxEffectAdapterDesc, public afxEffectDefs
  217. {
  218. static afxEA_T3DSpotLightDesc desc;
  219. public:
  220. virtual bool testEffectType(const SimDataBlock*) const;
  221. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  222. virtual bool runsOnServer(const afxEffectWrapperData*) const { return false; }
  223. virtual bool runsOnClient(const afxEffectWrapperData*) const { return true; }
  224. virtual afxEffectWrapper* create() const { return new afxEA_T3DSpotLight; }
  225. };
  226. afxEA_T3DSpotLightDesc afxEA_T3DSpotLightDesc::desc;
  227. bool afxEA_T3DSpotLightDesc::testEffectType(const SimDataBlock* db) const
  228. {
  229. return (typeid(afxT3DSpotLightData) == typeid(*db));
  230. }
  231. bool afxEA_T3DSpotLightDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  232. {
  233. return (timing.lifetime < 0);
  234. }
  235. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//