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.

119 lines
3.0 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "materialsystem/imaterialproxy.h"
  9. #include "materialsystem/imaterial.h"
  10. #include "materialsystem/imaterialvar.h"
  11. #include <keyvalues.h>
  12. #include "mathlib/vmatrix.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. // $textureScrollVar
  18. // $textureScrollRate
  19. // $textureScrollAngle
  20. class CTextureScrollMaterialProxy : public IMaterialProxy
  21. {
  22. public:
  23. CTextureScrollMaterialProxy();
  24. virtual ~CTextureScrollMaterialProxy();
  25. virtual bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
  26. virtual void OnBind( void *pC_BaseEntity );
  27. virtual void Release( void ) { delete this; }
  28. virtual IMaterial *GetMaterial();
  29. private:
  30. IMaterialVar *m_pTextureScrollVar;
  31. CFloatInput m_TextureScrollRate;
  32. CFloatInput m_TextureScrollAngle;
  33. CFloatInput m_TextureScale;
  34. };
  35. CTextureScrollMaterialProxy::CTextureScrollMaterialProxy()
  36. {
  37. m_pTextureScrollVar = NULL;
  38. }
  39. CTextureScrollMaterialProxy::~CTextureScrollMaterialProxy()
  40. {
  41. }
  42. bool CTextureScrollMaterialProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
  43. {
  44. char const* pScrollVarName = pKeyValues->GetString( "textureScrollVar" );
  45. if( !pScrollVarName )
  46. return false;
  47. bool foundVar;
  48. m_pTextureScrollVar = pMaterial->FindVar( pScrollVarName, &foundVar, false );
  49. if( !foundVar )
  50. return false;
  51. m_TextureScrollRate.Init( pMaterial, pKeyValues, "textureScrollRate", 1.0f );
  52. m_TextureScrollAngle.Init( pMaterial, pKeyValues, "textureScrollAngle", 0.0f );
  53. m_TextureScale.Init( pMaterial, pKeyValues, "textureScale", 1.0f );
  54. return true;
  55. }
  56. void CTextureScrollMaterialProxy::OnBind( void *pC_BaseEntity )
  57. {
  58. if( !m_pTextureScrollVar )
  59. {
  60. return;
  61. }
  62. float rate, angle, scale;
  63. // set default values if these variables don't exist.
  64. rate = m_TextureScrollRate.GetFloat();
  65. angle = m_TextureScrollAngle.GetFloat();
  66. scale = m_TextureScale.GetFloat();
  67. float sOffset, tOffset;
  68. sOffset = gpGlobals->curtime * cos( angle * ( M_PI / 180.0f ) ) * rate;
  69. tOffset = gpGlobals->curtime * sin( angle * ( M_PI / 180.0f ) ) * rate;
  70. // make sure that we are positive
  71. if( sOffset < 0.0f )
  72. {
  73. sOffset += 1.0f + -( int )sOffset;
  74. }
  75. if( tOffset < 0.0f )
  76. {
  77. tOffset += 1.0f + -( int )tOffset;
  78. }
  79. // make sure that we are in a [0,1] range
  80. sOffset = sOffset - ( int )sOffset;
  81. tOffset = tOffset - ( int )tOffset;
  82. if (m_pTextureScrollVar->GetType() == MATERIAL_VAR_TYPE_MATRIX)
  83. {
  84. VMatrix mat( scale, 0.0f, 0.0f, sOffset,
  85. 0.0f, scale, 0.0f, tOffset,
  86. 0.0f, 0.0f, 1.0f, 0.0f,
  87. 0.0f, 0.0f, 0.0f, 1.0f );
  88. m_pTextureScrollVar->SetMatrixValue( mat );
  89. }
  90. else
  91. {
  92. m_pTextureScrollVar->SetVecValue( sOffset, tOffset, 0.0f );
  93. }
  94. }
  95. IMaterial *CTextureScrollMaterialProxy::GetMaterial()
  96. {
  97. return m_pTextureScrollVar->GetOwningMaterial();
  98. }
  99. EXPOSE_MATERIAL_PROXY( CTextureScrollMaterialProxy, TextureScroll );