RotationModule.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // RotationModule.cpp
  3. // cocos2d_libs
  4. //
  5. // Created by 徐俊杰 on 2020/4/24.
  6. //
  7. #include "rparticle/Modules/RotationModule.h"
  8. //#include "UnityPrefix.h"
  9. //#include "rparticle/Modules/RotationModule.h"
  10. //#include "Runtime/BaseClasses/ObjectDefines.h"
  11. #include "rparticle/Serialize/TransferFunctions/SerializeTransfer.h"
  12. #include "rparticle/ParticleSystemUtils.h"
  13. #include "rparticle/Math/Random/Random.h"
  14. //#include "Runtime/Math/Vector2.h"
  15. NS_RRP_BEGIN
  16. struct DualMinMaxPolyCurves
  17. {
  18. MinMaxOptimizedPolyCurves optRot;
  19. MinMaxPolyCurves rot;
  20. };
  21. template<ParticleSystemCurveEvalMode mode>
  22. void UpdateTpl(const MinMaxCurve& curve, ParticleSystemParticles& ps, const size_t fromIndex, const size_t toIndex)
  23. {
  24. if ( !ps.usesRotationalSpeed ) return;
  25. for (size_t q = fromIndex; q < toIndex; ++q)
  26. {
  27. const float time = NormalizedTime(ps, q);
  28. const float random = GenerateRandom(ps.randomSeed[q] + kParticleSystemRotationCurveId);
  29. ps.rotationalSpeed[q] += Evaluate<mode> (curve, time, random);
  30. }
  31. }
  32. template<bool isOptimized>
  33. void UpdateProceduralTpl(const DualMinMaxPolyCurves& curves, ParticleSystemParticles& ps)
  34. {
  35. const size_t count = ps.array_size ();
  36. for (int q=0; q<count; q++)
  37. {
  38. float time = NormalizedTime(ps, q);
  39. float random = GenerateRandom(ps.randomSeed[q] + kParticleSystemRotationCurveId);
  40. float range = ps.startLifetime[q];
  41. float value;
  42. if(isOptimized)
  43. value = EvaluateIntegrated (curves.optRot, time, random);
  44. else
  45. value = EvaluateIntegrated (curves.rot, time, random);
  46. ps.rotation[q] += value * range;
  47. }
  48. }
  49. RotationModule::RotationModule() : ParticleSystemModule(false)
  50. {}
  51. void RotationModule::Update (const ParticleSystemReadOnlyState& roState, const ParticleSystemState& state, ParticleSystemParticles& ps, const size_t fromIndex, const size_t toIndex)
  52. {
  53. if (m_Curve.minMaxState == kMMCScalar)
  54. UpdateTpl<kEMScalar>(m_Curve, ps, fromIndex, toIndex);
  55. else if(m_Curve.IsOptimized() && m_Curve.UsesMinMax())
  56. UpdateTpl<kEMOptimizedMinMax>(m_Curve, ps, fromIndex, toIndex);
  57. else if(m_Curve.IsOptimized())
  58. UpdateTpl<kEMOptimized>(m_Curve, ps, fromIndex, toIndex);
  59. else
  60. UpdateTpl<kEMSlow>(m_Curve, ps, fromIndex, toIndex);
  61. }
  62. void RotationModule::UpdateProcedural (const ParticleSystemState& state, ParticleSystemParticles& ps)
  63. {
  64. DualMinMaxPolyCurves curves;
  65. if(m_Curve.IsOptimized())
  66. {
  67. curves.optRot = m_Curve.polyCurves; curves.optRot.Integrate();
  68. UpdateProceduralTpl<true>(curves, ps);
  69. }
  70. else
  71. {
  72. DebugAssert(CurvesSupportProcedural (m_Curve.editorCurves, m_Curve.minMaxState));
  73. BuildCurves(curves.rot, m_Curve.editorCurves, m_Curve.GetScalar(), m_Curve.minMaxState); curves.rot.Integrate();
  74. UpdateProceduralTpl<false>(curves, ps);
  75. }
  76. }
  77. template<class TransferFunction>
  78. void RotationModule::Transfer (TransferFunction& transfer)
  79. {
  80. ParticleSystemModule::Transfer (transfer);
  81. transfer.Transfer (m_Curve, "curve");
  82. }
  83. INSTANTIATE_TEMPLATE_TRANSFER(RotationModule)
  84. NS_RRP_END