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.

93 lines
2.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: This is an example of a material that modifies vertex data
  4. // in the shader. NOTE: Every pass is given a clean set of vertex data.
  5. // Modifications made in the first pass are *not* carried over to the next pass
  6. // Modifications must take place during the DYNAMIC_STATE block.
  7. // Use the function MeshBuilder() to build the mesh
  8. //
  9. // Also note: Using thie feature is *really expensive*! It makes a copy of
  10. // the vertex data *per pass!* If you wish to modify vertex data to be used
  11. // with all passes, your best bet is to construct a dynamic mesh instead.
  12. //
  13. // $Header: $
  14. // $NoKeywords: $
  15. //===========================================================================//
  16. #include "shaderlib/cshader.h"
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include "tier0/memdbgon.h"
  19. BEGIN_SHADER_FLAGS( DebugModifyVertex, "Help for DebugModifyVertex", SHADER_NOT_EDITABLE )
  20. BEGIN_SHADER_PARAMS
  21. SHADER_PARAM( WAVE, SHADER_PARAM_TYPE_FLOAT, "1.0", "wave amplitude" )
  22. END_SHADER_PARAMS
  23. SHADER_INIT
  24. {
  25. LoadTexture( BASETEXTURE );
  26. }
  27. SHADER_DRAW
  28. {
  29. if( g_pHardwareConfig->GetSamplerCount() >= 2 )
  30. {
  31. // lightmap
  32. SHADOW_STATE
  33. {
  34. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  35. pShaderShadow->EnableVertexDataPreprocess( true );
  36. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_TEXCOORD0 );
  37. FogToFogColor();
  38. }
  39. DYNAMIC_STATE
  40. {
  41. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  42. float amp = params[WAVE]->GetFloatValue();
  43. float currTime = pShaderAPI->CurrentTime();
  44. for (int i = 0; i < MeshBuilder()->NumVertices(); ++i)
  45. {
  46. float const* pPos = MeshBuilder()->Position();
  47. MeshBuilder()->Position3f( pPos[0] + amp * sin( currTime + pPos[2] / 4 ),
  48. pPos[1] + amp * sin( currTime + pPos[2] / 4 + 2 * 3.14 / 3 ),
  49. pPos[2] + amp * sin( currTime + pPos[2] / 4 + 4 * 3.14 / 3 ) );
  50. MeshBuilder()->AdvanceVertex();
  51. }
  52. }
  53. Draw();
  54. // base * vertex color
  55. SHADOW_STATE
  56. {
  57. pShaderShadow->DrawFlags( SHADER_DRAW_POSITION | SHADER_DRAW_COLOR |
  58. SHADER_DRAW_TEXCOORD0 );
  59. FogToFogColor();
  60. }
  61. DYNAMIC_STATE
  62. {
  63. BindTexture( SHADER_SAMPLER0, BASETEXTURE, FRAME );
  64. // Notice here that since we didn't modify the position, and this is a second
  65. // pass, the position has been reset to it's initial, unmodified position
  66. float currTime = pShaderAPI->CurrentTime();
  67. for (int i = 0; i < MeshBuilder()->NumVertices(); ++i)
  68. {
  69. float const* pPos = MeshBuilder()->Position();
  70. MeshBuilder()->Color3f( ( sin( currTime + pPos[0] ) + 1.0F) * 0.5,
  71. ( sin( currTime + pPos[1] ) + 1.0F) * 0.5,
  72. ( sin( currTime + pPos[2] ) + 1.0F) * 0.5 );
  73. MeshBuilder()->AdvanceVertex();
  74. }
  75. }
  76. Draw();
  77. }
  78. else
  79. {
  80. ShaderWarning( "DebugModifyVertex: not "
  81. "implemented for single-texturing hardware\n" );
  82. }
  83. }
  84. END_SHADER