RotationByVelocityModule.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // RotationByVelocityModule.cpp
  3. // libcocos2d Mac
  4. //
  5. // Created by 徐俊杰 on 2020/4/24.
  6. //
  7. #include "rparticle/Modules/RotationByVelocityModule.h"
  8. //#include "UnityPrefix.h"
  9. //#include "rparticle/Modules/RotationByVelocityModule.h"
  10. //#include "Runtime/BaseClasses/ObjectDefines.h"
  11. #include "rparticle/Serialize/TransferFunctions/SerializeTransfer.h"
  12. #include "rparticle/ParticleSystemUtils.h"
  13. NS_RRP_BEGIN
  14. RotationBySpeedModule::RotationBySpeedModule () : ParticleSystemModule(false)
  15. , m_Range (0.0f, 1.0f)
  16. {}
  17. template<ParticleSystemCurveEvalMode mode>
  18. void UpdateTpl(const MinMaxCurve& curve, ParticleSystemParticles& ps, size_t fromIndex, size_t toIndex, const Vector2f offsetScale)
  19. {
  20. if (!ps.usesRotationalSpeed) return;
  21. for (size_t q = fromIndex; q < toIndex; ++q)
  22. {
  23. const Vector3f vel = ps.velocity[q] + ps.animatedVelocity[q];
  24. const float t = InverseLerpFast01 (offsetScale, Magnitude(vel));
  25. const float random = GenerateRandom(ps.randomSeed[q] + kParticleSystemRotationBySpeedCurveId);
  26. ps.rotationalSpeed[q] += Evaluate<mode> (curve, t, random);
  27. }
  28. }
  29. void RotationBySpeedModule::Update (const ParticleSystemReadOnlyState& roState, const ParticleSystemState& state, ParticleSystemParticles& ps, const size_t fromIndex, const size_t toIndex)
  30. {
  31. Vector2f offsetScale = CalculateInverseLerpOffsetScale (m_Range * roState.GetRenderScale());
  32. if (m_Curve.minMaxState == kMMCScalar)
  33. UpdateTpl<kEMScalar>(m_Curve, ps, fromIndex, toIndex, offsetScale);
  34. else if(m_Curve.IsOptimized() && m_Curve.UsesMinMax())
  35. UpdateTpl<kEMOptimizedMinMax>(m_Curve, ps, fromIndex, toIndex, offsetScale);
  36. else if(m_Curve.IsOptimized())
  37. UpdateTpl<kEMOptimized>(m_Curve, ps, fromIndex, toIndex, offsetScale);
  38. else
  39. UpdateTpl<kEMSlow>(m_Curve, ps, fromIndex, toIndex, offsetScale);
  40. }
  41. void RotationBySpeedModule::CheckConsistency ()
  42. {
  43. const float MyEpsilon = 0.001f;
  44. m_Range.y = std::max (m_Range.x + MyEpsilon, m_Range.y);
  45. }
  46. template<class TransferFunction>
  47. void RotationBySpeedModule::Transfer (TransferFunction& transfer)
  48. {
  49. ParticleSystemModule::Transfer (transfer);
  50. transfer.Transfer (m_Curve, "curve");
  51. transfer.Transfer (m_Range, "range");
  52. }
  53. INSTANTIATE_TEMPLATE_TRANSFER(RotationBySpeedModule)
  54. NS_RRP_END