UVModule.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // UVModule.cpp
  3. // cocos2d_libs
  4. //
  5. // Created by 徐俊杰 on 2020/4/24.
  6. //
  7. #include "rparticle/Modules/UVModule.h"
  8. //#include "UnityPrefix.h"
  9. //#include "rparticle/Modules/UVModule.h"
  10. //#include "Runtime/BaseClasses/ObjectDefines.h"
  11. #include "rparticle/Serialize/TransferFunctions/SerializeTransfer.h"
  12. #include "rparticle/ParticleSystemParticle.h"
  13. #include "rparticle/ParticleSystemCurves.h"
  14. #include "rparticle/ParticleSystemUtils.h"
  15. #include "rparticle/Math/Random/Random.h"
  16. #include "2d/CCSpriteFrameCache.h"
  17. NS_RRP_BEGIN
  18. template<ParticleSystemCurveEvalMode mode>
  19. void UpdateWholeSheetTpl(float cycles, const MinMaxCurve& curve, const ParticleSystemParticles& ps, float* tempSheetIndex, size_t fromIndex, size_t toIndex)
  20. {
  21. for (size_t q = fromIndex; q < toIndex; ++q)
  22. tempSheetIndex[q] = Repeat (cycles * Evaluate(curve, NormalizedTime(ps, q), GenerateRandom(ps.randomSeed[q] + kParticleSystemUVCurveId)), 1.0f);
  23. }
  24. UVModule::UVModule () : ParticleSystemModule(false)
  25. , m_Mode(kModeGrid)
  26. , m_TilesX (1), m_TilesY (1)
  27. , m_AnimationType (kWholeSheet)
  28. , m_RowIndex (0)
  29. , m_Cycles (1.0f)
  30. , m_RandomRow (true)
  31. , _frameNamePrefix("")
  32. , _frameStartIndex(0)
  33. , _frameEndIndex(0)
  34. , _isFramesDirty(true)
  35. {}
  36. void UVModule::RefreshFrames (std::vector<RRP_PARTICLEQUAD_VERTEX_INFO>& vertextInfos)
  37. {
  38. if (m_Mode != kModeSprites) return;
  39. if (!_isFramesDirty) return;
  40. if (_frameNamePrefix.empty() || _frameStartIndex < 0 || _frameEndIndex < 0 || _frameStartIndex >= _frameEndIndex) return;
  41. vertextInfos.clear();
  42. for (int i = _frameStartIndex; i <= _frameEndIndex; i++)
  43. {
  44. //char name[_frameNamePrefix.size() + 15];
  45. //snprintf(name, 0x100, "%s%i.png", _frameNamePrefix.c_str(), i);
  46. auto name = _frameNamePrefix + std::to_string(i) + ".png";
  47. auto sprite = cocos2d::SpriteFrameCache::getInstance()->getSpriteFrameByName(name);
  48. if (!sprite) continue;
  49. vertextInfos.push_back(RRP_PARTICLEQUAD_VERTEX_INFO::Create(sprite->getTexture(), sprite->getRect(), sprite->isRotated(), sprite->getOffset(), sprite->getOriginalSize()));
  50. }
  51. #if REDREAM_EDITOR
  52. _frameCount = vertextInfos.size();
  53. #endif
  54. _isFramesDirty = false;
  55. }
  56. void UVModule::Update (const ParticleSystemParticles& ps, float* tempSheetIndex, size_t fromIndex, size_t toIndex)
  57. {
  58. const float cycles = m_Cycles;
  59. DebugAssert(toIndex <= ps.array_size ());
  60. if (m_AnimationType == kSingleRow) // row
  61. {
  62. int rows = m_TilesY;
  63. float animRange = (1.0f / (m_TilesX * rows)) * m_TilesX;
  64. if(m_RandomRow)
  65. {
  66. for (size_t q = fromIndex; q < toIndex; ++q)
  67. {
  68. const float t = cycles * Evaluate(m_Curve, NormalizedTime(ps, q), GenerateRandom(ps.randomSeed[q] + kParticleSystemUVCurveId));
  69. const float x = Repeat (t, 1.0f);
  70. const float randomValue = GenerateRandom(ps.randomSeed[q] + kParticleSystemUVRowSelectionId);
  71. const float startRow = Floorf (randomValue * rows);
  72. float from = startRow * animRange;
  73. float to = from + animRange;
  74. tempSheetIndex[q] = Lerp (from, to, x);
  75. }
  76. }
  77. else
  78. {
  79. const float startRow = Floorf(m_RowIndex * animRange * rows);
  80. float from = startRow * animRange;
  81. float to = from + animRange;
  82. for (size_t q = fromIndex; q < toIndex; ++q)
  83. {
  84. const float t = cycles * Evaluate(m_Curve, NormalizedTime(ps, q), GenerateRandom(ps.randomSeed[q] + kParticleSystemUVCurveId));
  85. const float x = Repeat (t, 1.0f);
  86. tempSheetIndex[q] = Lerp (from, to, x);
  87. }
  88. }
  89. }
  90. else if (m_AnimationType == kWholeSheet) // grid || row
  91. {
  92. if(m_Curve.minMaxState == kMMCScalar)
  93. UpdateWholeSheetTpl<kEMScalar>(m_Cycles, m_Curve, ps, tempSheetIndex, fromIndex, toIndex);
  94. else if (m_Curve.IsOptimized() && m_Curve.UsesMinMax ())
  95. UpdateWholeSheetTpl<kEMOptimizedMinMax>(m_Cycles, m_Curve, ps, tempSheetIndex, fromIndex, toIndex);
  96. else if(m_Curve.IsOptimized())
  97. UpdateWholeSheetTpl<kEMOptimized>(m_Cycles, m_Curve, ps, tempSheetIndex, fromIndex, toIndex);
  98. else
  99. UpdateWholeSheetTpl<kEMSlow>(m_Cycles, m_Curve, ps, tempSheetIndex, fromIndex, toIndex);
  100. }
  101. else
  102. {
  103. Assert(!"Animation mode not implemented!");
  104. }
  105. }
  106. void UVModule::CheckConsistency ()
  107. {
  108. m_Mode = clamp<int> (m_Mode, 0, kNumModes-1);
  109. m_AnimationType = clamp<int> (m_AnimationType, 0, kNumAnimationTypes-1);
  110. m_TilesX = std::max<int> (1, m_TilesX);
  111. m_TilesY = std::max<int> (1, m_TilesY);
  112. m_Cycles = std::max<int> (1, (int)m_Cycles);
  113. m_RowIndex = clamp<int> (m_RowIndex, 0, m_TilesY-1);
  114. m_Curve.SetScalar(clamp<float> (m_Curve.GetScalar(), 0.0f, 1.0f));
  115. }
  116. void UVModule::GetNumTiles(int& uvTilesX, int& uvTilesY) const
  117. {
  118. uvTilesX = m_TilesX;
  119. uvTilesY = m_TilesY;
  120. }
  121. template<class TransferFunction>
  122. void UVModule::Transfer (TransferFunction& transfer)
  123. {
  124. ParticleSystemModule::Transfer (transfer);
  125. transfer.Transfer (m_Curve, "frameOverTime");
  126. transfer.Transfer (m_TilesX, "tilesX");
  127. transfer.Transfer (m_TilesY, "tilesY");
  128. transfer.Transfer (m_AnimationType, "animationType");
  129. transfer.Transfer (m_RowIndex, "rowIndex");
  130. transfer.Transfer (m_Cycles, "cycles");
  131. transfer.Transfer (m_RandomRow, "randomRow"); transfer.Align ();
  132. }
  133. INSTANTIATE_TEMPLATE_TRANSFER(UVModule)
  134. NS_RRP_END