Counter Strike : Global Offensive Source Code
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.

128 lines
3.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include <keyvalues.h>
  9. #include "materialsystem/imaterialvar.h"
  10. #include "materialsystem/imaterial.h"
  11. #include "materialsystem/itexture.h"
  12. #include "proxyentity.h"
  13. #include "functionproxy.h"
  14. #include "imaterialproxydict.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. #define DEFAULT_OPEN_PUPIL_RATE 0.03
  18. #define DEFAULT_CLOSE_PUPIL_RATE 0.1
  19. //-----------------------------------------------------------------------------
  20. // Returns the proximity of the player to the entity
  21. //-----------------------------------------------------------------------------
  22. class CPupilProxy : public CEntityMaterialProxy
  23. {
  24. public:
  25. bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
  26. virtual void OnBind( C_BaseEntity *pBaseEntity );
  27. virtual IMaterial *GetMaterial();
  28. private:
  29. IMaterialVar *m_pAnimatedTextureVar;
  30. IMaterialVar *m_pAnimatedTextureFrameNumVar;
  31. IMaterialVar *m_pLightingVar;
  32. CFloatInput m_flPupilCloseRate;
  33. CFloatInput m_flPupilOpenRate;
  34. };
  35. bool CPupilProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
  36. {
  37. char const* pAnimatedTextureVarName = pKeyValues->GetString( "TextureVar" );
  38. if( !pAnimatedTextureVarName )
  39. return false;
  40. bool foundVar;
  41. m_pAnimatedTextureVar = pMaterial->FindVar( pAnimatedTextureVarName, &foundVar, false );
  42. if( !foundVar )
  43. return false;
  44. char const* pAnimatedTextureFrameNumVarName = pKeyValues->GetString( "TextureFrameNumVar" );
  45. if( !pAnimatedTextureFrameNumVarName )
  46. return false;
  47. m_pAnimatedTextureFrameNumVar = pMaterial->FindVar( pAnimatedTextureFrameNumVarName, &foundVar, false );
  48. if( !foundVar )
  49. return false;
  50. m_pLightingVar = pMaterial->FindVar( "$lighting", &foundVar, false );
  51. if( !foundVar )
  52. {
  53. Warning("Materials using the pupil proxy must have a field called $lighting which has a value of 0.5!\n" );
  54. return false;
  55. }
  56. m_flPupilCloseRate.Init( pMaterial, pKeyValues, "PupilCloseRate", DEFAULT_CLOSE_PUPIL_RATE );
  57. m_flPupilOpenRate.Init( pMaterial, pKeyValues, "PupilOpenRate", DEFAULT_OPEN_PUPIL_RATE );
  58. return true;
  59. }
  60. void CPupilProxy::OnBind( C_BaseEntity *pBaseEntity )
  61. {
  62. if (!pBaseEntity || !m_pAnimatedTextureVar )
  63. return;
  64. if( m_pAnimatedTextureVar->GetType() != MATERIAL_VAR_TYPE_TEXTURE )
  65. return;
  66. ITexture *pTexture = m_pAnimatedTextureVar->GetTextureValue();
  67. int nFrameCount = pTexture->GetNumAnimationFrames();
  68. // Compute the lighting at the eye position of the entity; use it to dialate the pupil
  69. Vector forward;
  70. pBaseEntity->GetVectors( &forward, NULL, NULL );
  71. Vector eyePt = pBaseEntity->EyePosition();
  72. Vector color;
  73. engine->ComputeLighting( eyePt, &forward, false, color );
  74. // Compute the intensity...
  75. float flIntensity = ( 0.299f * color[0] + 0.587f * color[1] + 0.114f * color[2] ) * 0.5;
  76. flIntensity = clamp( flIntensity, 0, 1 );
  77. float flLastIntensity = m_pLightingVar->GetFloatValue( );
  78. if ( flIntensity > flLastIntensity )
  79. {
  80. float flMaxChange = m_flPupilCloseRate.GetFloat() * gpGlobals->frametime;
  81. if ( flIntensity > (flMaxChange + flLastIntensity) )
  82. {
  83. flIntensity = flLastIntensity + flMaxChange;
  84. }
  85. }
  86. else
  87. {
  88. float flMaxChange = m_flPupilOpenRate.GetFloat() * gpGlobals->frametime;
  89. if ( flIntensity < (flLastIntensity - flMaxChange) )
  90. {
  91. flIntensity = flLastIntensity - flMaxChange;
  92. }
  93. }
  94. int nFrame = nFrameCount * flIntensity;
  95. nFrame = clamp( nFrame, 0, nFrameCount - 1 );
  96. m_pAnimatedTextureFrameNumVar->SetIntValue( nFrame );
  97. m_pLightingVar->SetFloatValue( flIntensity );
  98. }
  99. IMaterial *CPupilProxy::GetMaterial()
  100. {
  101. if ( !m_pAnimatedTextureVar )
  102. return NULL;
  103. return m_pAnimatedTextureVar->GetOwningMaterial();
  104. }
  105. EXPOSE_MATERIAL_PROXY( CPupilProxy, Pupil );