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.

127 lines
3.3 KiB

  1. //========= Copyright 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 "toolframework_client.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. // forward declarations
  18. void ToolFramework_RecordMaterialParams( IMaterial *pMaterial );
  19. // $textureScrollVar
  20. // $textureScrollRate
  21. // $textureScrollAngle
  22. class CTextureScrollMaterialProxy : public IMaterialProxy
  23. {
  24. public:
  25. CTextureScrollMaterialProxy();
  26. virtual ~CTextureScrollMaterialProxy();
  27. virtual bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
  28. virtual void OnBind( void *pC_BaseEntity );
  29. virtual void Release( void ) { delete this; }
  30. virtual IMaterial *GetMaterial();
  31. private:
  32. IMaterialVar *m_pTextureScrollVar;
  33. CFloatInput m_TextureScrollRate;
  34. CFloatInput m_TextureScrollAngle;
  35. CFloatInput m_TextureScale;
  36. };
  37. CTextureScrollMaterialProxy::CTextureScrollMaterialProxy()
  38. {
  39. m_pTextureScrollVar = NULL;
  40. }
  41. CTextureScrollMaterialProxy::~CTextureScrollMaterialProxy()
  42. {
  43. }
  44. bool CTextureScrollMaterialProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
  45. {
  46. char const* pScrollVarName = pKeyValues->GetString( "textureScrollVar" );
  47. if( !pScrollVarName )
  48. return false;
  49. bool foundVar;
  50. m_pTextureScrollVar = pMaterial->FindVar( pScrollVarName, &foundVar, false );
  51. if( !foundVar )
  52. return false;
  53. m_TextureScrollRate.Init( pMaterial, pKeyValues, "textureScrollRate", 1.0f );
  54. m_TextureScrollAngle.Init( pMaterial, pKeyValues, "textureScrollAngle", 0.0f );
  55. m_TextureScale.Init( pMaterial, pKeyValues, "textureScale", 1.0f );
  56. return true;
  57. }
  58. void CTextureScrollMaterialProxy::OnBind( void *pC_BaseEntity )
  59. {
  60. if( !m_pTextureScrollVar )
  61. {
  62. return;
  63. }
  64. float rate, angle, scale;
  65. // set default values if these variables don't exist.
  66. rate = m_TextureScrollRate.GetFloat();
  67. angle = m_TextureScrollAngle.GetFloat();
  68. scale = m_TextureScale.GetFloat();
  69. float sOffset, tOffset;
  70. sOffset = gpGlobals->curtime * cos( angle * ( M_PI / 180.0f ) ) * rate;
  71. tOffset = gpGlobals->curtime * sin( angle * ( M_PI / 180.0f ) ) * rate;
  72. // make sure that we are positive
  73. if( sOffset < 0.0f )
  74. {
  75. sOffset += 1.0f + -( int )sOffset;
  76. }
  77. if( tOffset < 0.0f )
  78. {
  79. tOffset += 1.0f + -( int )tOffset;
  80. }
  81. // make sure that we are in a [0,1] range
  82. sOffset = sOffset - ( int )sOffset;
  83. tOffset = tOffset - ( int )tOffset;
  84. if (m_pTextureScrollVar->GetType() == MATERIAL_VAR_TYPE_MATRIX)
  85. {
  86. VMatrix mat( scale, 0.0f, 0.0f, sOffset,
  87. 0.0f, scale, 0.0f, tOffset,
  88. 0.0f, 0.0f, 1.0f, 0.0f,
  89. 0.0f, 0.0f, 0.0f, 1.0f );
  90. m_pTextureScrollVar->SetMatrixValue( mat );
  91. }
  92. else
  93. {
  94. m_pTextureScrollVar->SetVecValue( sOffset, tOffset, 0.0f );
  95. }
  96. if ( ToolsEnabled() )
  97. {
  98. ToolFramework_RecordMaterialParams( GetMaterial() );
  99. }
  100. }
  101. IMaterial *CTextureScrollMaterialProxy::GetMaterial()
  102. {
  103. return m_pTextureScrollVar->GetOwningMaterial();
  104. }
  105. EXPOSE_INTERFACE( CTextureScrollMaterialProxy, IMaterialProxy, "TextureScroll" IMATERIAL_PROXY_INTERFACE_VERSION );