Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "proxyentity.h"
  9. #include "materialsystem/imaterialvar.h"
  10. #include "materialsystem/imaterial.h"
  11. #include "view.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. //-----------------------------------------------------------------------------
  15. // Purpose: Used for halos on lamps, this material fades the sprite IN
  16. // as the viewer nears.
  17. //-----------------------------------------------------------------------------
  18. class CLampHaloProxy : public CEntityMaterialProxy
  19. {
  20. public:
  21. CLampHaloProxy( void );
  22. virtual ~CLampHaloProxy( void );
  23. virtual bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
  24. virtual void OnBind( C_BaseEntity *pC_BaseEntity );
  25. virtual IMaterial * GetMaterial();
  26. private:
  27. IMaterialVar *m_pFadeValue;
  28. };
  29. //-----------------------------------------------------------------------------
  30. // Purpose:
  31. //-----------------------------------------------------------------------------
  32. CLampHaloProxy::CLampHaloProxy( void )
  33. {
  34. m_pFadeValue = NULL;
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Purpose:
  38. //-----------------------------------------------------------------------------
  39. CLampHaloProxy::~CLampHaloProxy( void )
  40. {
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Purpose: Get pointer to the color value
  44. // Input : *pMaterial -
  45. //-----------------------------------------------------------------------------
  46. bool CLampHaloProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
  47. {
  48. assert( pMaterial );
  49. // Get pointers to material vars.
  50. // Need to get the color variable.
  51. bool found;
  52. m_pFadeValue = pMaterial->FindVar( "$alpha", &found );
  53. return found;
  54. }
  55. //-----------------------------------------------------------------------------
  56. // Purpose:
  57. // Input : *pC_BaseEntity -
  58. //-----------------------------------------------------------------------------
  59. #define FADE_DIST 150
  60. void CLampHaloProxy::OnBind( C_BaseEntity *pEnt )
  61. {
  62. if ( !m_pFadeValue )
  63. return;
  64. Vector vecLocal = pEnt->GetAbsOrigin() - CurrentViewOrigin();
  65. VectorNormalize( vecLocal );
  66. float fade = fabs( vecLocal.z );
  67. // I hate these magic numbers here, will have to revise
  68. // (sjb)
  69. if( fade < 0.25 )
  70. {
  71. fade = 0.0;
  72. }
  73. else
  74. {
  75. fade = MIN( (fade - 0.25) * 1.35, 1.0f );
  76. }
  77. m_pFadeValue->SetFloatValue( fade );
  78. }
  79. IMaterial *CLampHaloProxy::GetMaterial()
  80. {
  81. if ( !m_pFadeValue )
  82. return NULL;
  83. return m_pFadeValue->GetOwningMaterial();
  84. }
  85. EXPOSE_INTERFACE( CLampHaloProxy, IMaterialProxy, "lamphalo" IMATERIAL_PROXY_INTERFACE_VERSION );