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.

182 lines
4.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "mathlib/vmatrix.h"
  9. #include "functionproxy.h"
  10. #include "materialsystem/imaterialvar.h"
  11. #include <KeyValues.h>
  12. #include "materialsystem/imaterial.h"
  13. #include "toolframework_client.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. // forward declarations
  17. void ToolFramework_RecordMaterialParams( IMaterial *pMaterial );
  18. class C_BaseEntity;
  19. //-----------------------------------------------------------------------------
  20. // Texture transform proxy
  21. //-----------------------------------------------------------------------------
  22. class CTextureTransformProxy : public CResultProxy
  23. {
  24. public:
  25. bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
  26. void OnBind( void *pC_BaseEntity );
  27. private:
  28. IMaterialVar *m_pCenterVar;
  29. IMaterialVar *m_pScaleVar;
  30. IMaterialVar *m_pRotateVar;
  31. IMaterialVar *m_pTranslateVar;
  32. };
  33. bool CTextureTransformProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
  34. {
  35. // All are optional...
  36. m_pCenterVar = NULL;
  37. m_pScaleVar = NULL;
  38. m_pRotateVar = NULL;
  39. m_pTranslateVar = NULL;
  40. bool bFoundVar;
  41. char const* pVarName = pKeyValues->GetString( "centerVar" );
  42. if( pVarName && pVarName[0] )
  43. {
  44. m_pCenterVar = pMaterial->FindVar( pVarName, &bFoundVar, false );
  45. }
  46. pVarName = pKeyValues->GetString( "scaleVar" );
  47. if( pVarName && pVarName[0] )
  48. {
  49. m_pScaleVar = pMaterial->FindVar( pVarName, &bFoundVar, false );
  50. }
  51. pVarName = pKeyValues->GetString( "rotateVar" );
  52. if( pVarName && pVarName[0] )
  53. {
  54. m_pRotateVar = pMaterial->FindVar( pVarName, &bFoundVar, false );
  55. }
  56. pVarName = pKeyValues->GetString( "translateVar" );
  57. if( pVarName && pVarName[0] )
  58. {
  59. m_pTranslateVar = pMaterial->FindVar( pVarName, &bFoundVar, false );
  60. }
  61. return CResultProxy::Init( pMaterial, pKeyValues );
  62. }
  63. void CTextureTransformProxy::OnBind( void *pC_BaseEntity )
  64. {
  65. Vector2D center( 0.5, 0.5 );
  66. Vector2D translation( 0, 0 );
  67. VMatrix mat, temp;
  68. if (m_pCenterVar)
  69. {
  70. m_pCenterVar->GetVecValue( center.Base(), 2 );
  71. }
  72. MatrixBuildTranslation( mat, -center.x, -center.y, 0.0f );
  73. if (m_pScaleVar)
  74. {
  75. Vector2D scale;
  76. m_pScaleVar->GetVecValue( scale.Base(), 2 );
  77. MatrixBuildScale( temp, scale.x, scale.y, 1.0f );
  78. MatrixMultiply( temp, mat, mat );
  79. }
  80. if (m_pRotateVar)
  81. {
  82. float angle = m_pRotateVar->GetFloatValue( );
  83. MatrixBuildRotateZ( temp, angle );
  84. MatrixMultiply( temp, mat, mat );
  85. }
  86. MatrixBuildTranslation( temp, center.x, center.y, 0.0f );
  87. MatrixMultiply( temp, mat, mat );
  88. if (m_pTranslateVar)
  89. {
  90. m_pTranslateVar->GetVecValue( translation.Base(), 2 );
  91. MatrixBuildTranslation( temp, translation.x, translation.y, 0.0f );
  92. MatrixMultiply( temp, mat, mat );
  93. }
  94. m_pResult->SetMatrixValue( mat );
  95. if ( ToolsEnabled() )
  96. {
  97. ToolFramework_RecordMaterialParams( GetMaterial() );
  98. }
  99. }
  100. EXPOSE_INTERFACE( CTextureTransformProxy, IMaterialProxy, "TextureTransform" IMATERIAL_PROXY_INTERFACE_VERSION );
  101. //-----------------------------------------------------------------------------
  102. // Rotation proxy
  103. //-----------------------------------------------------------------------------
  104. class CMatrixRotateProxy : public CResultProxy
  105. {
  106. public:
  107. bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
  108. void OnBind( void *pC_BaseEntity );
  109. private:
  110. CFloatInput m_Angle;
  111. IMaterialVar *m_pAxisVar;
  112. };
  113. bool CMatrixRotateProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
  114. {
  115. // All are optional...
  116. m_pAxisVar = NULL;
  117. bool bFoundVar;
  118. char const* pVarName = pKeyValues->GetString( "axisVar" );
  119. if( pVarName && pVarName[0] )
  120. {
  121. m_pAxisVar = pMaterial->FindVar( pVarName, &bFoundVar, false );
  122. }
  123. if (!m_Angle.Init( pMaterial, pKeyValues, "angle", 0 ))
  124. return false;
  125. return CResultProxy::Init( pMaterial, pKeyValues );
  126. }
  127. void CMatrixRotateProxy::OnBind( void *pC_BaseEntity )
  128. {
  129. VMatrix mat;
  130. Vector axis( 0, 0, 1 );
  131. if (m_pAxisVar)
  132. {
  133. m_pAxisVar->GetVecValue( axis.Base(), 3 );
  134. if (VectorNormalize( axis ) < 1e-3)
  135. {
  136. axis.Init( 0, 0, 1 );
  137. }
  138. }
  139. MatrixBuildRotationAboutAxis( mat, axis, m_Angle.GetFloat() );
  140. m_pResult->SetMatrixValue( mat );
  141. if ( ToolsEnabled() )
  142. {
  143. ToolFramework_RecordMaterialParams( GetMaterial() );
  144. }
  145. }
  146. EXPOSE_INTERFACE( CMatrixRotateProxy, IMaterialProxy, "MatrixRotate" IMATERIAL_PROXY_INTERFACE_VERSION );