ribbon.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _RIBBON_H_
  23. #define _RIBBON_H_
  24. #ifndef _GAMEBASE_H_
  25. #include "T3D/gameBase/gameBase.h"
  26. #endif
  27. #ifndef _GFXPRIMITIVEBUFFER_H_
  28. #include "gfx/gfxPrimitiveBuffer.h"
  29. #endif
  30. #ifndef _GFXVERTEXBUFFER_H_
  31. #include "gfx/gfxVertexBuffer.h"
  32. #endif
  33. #include "materials/materialParameters.h"
  34. #include "math/util/matrixSet.h"
  35. //--------------------------------------------------------------------------
  36. class RibbonData : public GameBaseData
  37. {
  38. typedef GameBaseData Parent;
  39. protected:
  40. bool onAdd();
  41. public:
  42. enum Constants
  43. {
  44. NumFields = 4
  45. };
  46. F32 mSizes[NumFields]; ///< The radius for each keyframe.
  47. LinearColorF mColours[NumFields]; ///< The colour of the ribbon for each keyframe.
  48. F32 mTimes[NumFields]; ///< The relative time for each keyframe.
  49. U32 mRibbonLength; ///< The amount of segments that will make up the ribbon.
  50. S32 segmentsPerUpdate; ///< Amount of segments to add each update.
  51. S32 mSegmentSkipAmount; ///< The amount of segments to skip each time segments are added.
  52. bool mUseFadeOut; ///< If true, the ribbon will fade away after deletion.
  53. F32 mFadeAwayStep; ///< How quickly the ribbons is faded away after deletion.
  54. StringTableEntry mMatName; ///< The material for the ribbon.
  55. F32 mTileScale; ///< A scalar to scale the texcoord.
  56. bool mFixedTexcoords; ///< If true, texcoords will stay the same over the lifetime for each segment.
  57. bool mTexcoordsRelativeToDistance; ///< If true, texcoords will not be stretched if the distance between 2 segments are long.
  58. RibbonData();
  59. void packData(BitStream*);
  60. void unpackData(BitStream*);
  61. bool preload(bool server, String &errorBuffer);
  62. static void initPersistFields();
  63. DECLARE_CONOBJECT(RibbonData);
  64. };
  65. //--------------------------------------------------------------------------
  66. class Ribbon : public GameBase
  67. {
  68. typedef GameBase Parent;
  69. RibbonData* mDataBlock;
  70. bool mDeleteOnEnd; ///< If true, the ribbon should delete itself as soon as the last segment is deleted
  71. bool mUseFadeOut; ///< If true, the ribbon will fade away upon deletion
  72. F32 mFadeAwayStep; ///< How quickly the ribbons is faded away after deletion.
  73. F32 mFadeOut;
  74. F32 mTravelledDistance; ///< How far the ribbon has travelled in it's lifetime.
  75. Vector<Point3F> mSegmentPoints; ///< The points in space where the ribbon has spawned segments.
  76. U32 mSegmentOffset;
  77. U32 mSegmentIdx;
  78. bool mUpdateBuffers; ///< If true, the vertex buffers need to be updated.
  79. BaseMatInstance *mRibbonMat;
  80. MaterialParameterHandle* mRadiusSC;
  81. MaterialParameterHandle* mRibbonProjSC;
  82. GFXPrimitiveBufferHandle mPrimBuffer;
  83. GFXVertexBufferHandle<GFXVertexPCNTT> mVerts;
  84. protected:
  85. bool onAdd();
  86. void processTick(const Move*);
  87. void advanceTime(F32);
  88. void interpolateTick(F32 delta);
  89. // Rendering
  90. void prepRenderImage(SceneRenderState *state);
  91. void setShaderParams();
  92. ///Checks to see if ribbon is too long
  93. U32 checkRibbonDistance(S32 segments);
  94. /// Construct the vertex and primitive buffers
  95. void createBuffers(SceneRenderState *state, GFXVertexBufferHandle<GFXVertexPCNTT> &verts, GFXPrimitiveBufferHandle &pb, U32 segments);
  96. public:
  97. Ribbon();
  98. ~Ribbon();
  99. DECLARE_CONOBJECT(Ribbon);
  100. static void initPersistFields();
  101. bool onNewDataBlock(GameBaseData*,bool);
  102. void onRemove();
  103. /// Used to add another segment to the ribbon.
  104. void addSegmentPoint(Point3F &point, MatrixF &mat);
  105. /// Delete all segments.
  106. void clearSegments() { mSegmentPoints.clear(); }
  107. /// Delete the ribbon when all segments have been deleted.
  108. void deleteOnEnd();
  109. };
  110. #endif // _H_RIBBON