VelocityModule.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // VelocityModule.cpp
  3. // cocos2d_libs
  4. //
  5. // Created by 徐俊杰 on 2020/4/24.
  6. //
  7. #include "rparticle/Modules/VelocityModule.h"
  8. //#include "UnityPrefix.h"
  9. //#include "rparticle/Modules/VelocityModule.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/Matrix4x4.h"
  15. // Vec3
  16. #define zero ZERO
  17. // MinMaxAABB
  18. #define m_Max _max
  19. #define m_Min _min
  20. NS_RRP_BEGIN
  21. template<ParticleSystemCurveEvalMode mode>
  22. void UpdateTpl(const MinMaxCurve& x, const MinMaxCurve& y, const MinMaxCurve& z, const ParticleSystemReadOnlyState& roState, ParticleSystemParticles& ps, const size_t fromIndex, const size_t toIndex, bool transform, const Matrix4x4f& matrix)
  23. {
  24. for (size_t q = fromIndex; q < toIndex; ++q)
  25. {
  26. Vector3f random;
  27. GenerateRandom3(random, ps.randomSeed[q] + kParticleSystemVelocityCurveId);
  28. const float normalizedTime = NormalizedTime (ps, q);
  29. Vector3f vel = Vector3f (Evaluate<mode> (x, normalizedTime, random.x), Evaluate<mode> (y, normalizedTime, random.y), Evaluate<mode> (z, normalizedTime, random.z)) * roState.GetRenderScale();
  30. if(transform)
  31. vel = matrix.MultiplyVector3 (vel);
  32. ps.animatedVelocity[q] += vel;
  33. }
  34. }
  35. template<bool isOptimized>
  36. void UpdateProceduralTpl(const DualMinMax3DPolyCurves& curves, const MinMaxCurve& x, const MinMaxCurve& y, const MinMaxCurve& z, const ParticleSystemReadOnlyState& roState, ParticleSystemParticles& ps, const Matrix4x4f& matrix, bool transform)
  37. {
  38. const size_t count = ps.array_size ();
  39. for (int q=0;q<count;q++)
  40. {
  41. Vector3f random;
  42. GenerateRandom3(random, ps.randomSeed[q] + kParticleSystemVelocityCurveId);
  43. const float time = NormalizedTime(ps, q);
  44. Vector3f delta;
  45. if(isOptimized)
  46. delta = Vector3f(EvaluateIntegrated(curves.optX, time, random.x), EvaluateIntegrated(curves.optY, time, random.y), EvaluateIntegrated(curves.optZ, time, random.z)) * ps.startLifetime[q];
  47. else
  48. delta = Vector3f(EvaluateIntegrated(curves.x, time, random.x), EvaluateIntegrated(curves.y, time, random.y), EvaluateIntegrated(curves.z, time, random.z)) * ps.startLifetime[q];
  49. Vector3f velocity (Evaluate(x, time, random.x), Evaluate(y, time, random.y), Evaluate(z, time, random.z));
  50. velocity *= roState.GetRenderScale();
  51. if(transform)
  52. {
  53. delta = matrix.MultiplyVector3 (delta);
  54. velocity = matrix.MultiplyVector3 (velocity);
  55. }
  56. ps.position[q] += delta;
  57. ps.animatedVelocity[q] += velocity;
  58. }
  59. }
  60. VelocityModule::VelocityModule () : ParticleSystemModule(false)
  61. , m_InWorldSpace (false)
  62. {}
  63. void VelocityModule::Update (const ParticleSystemReadOnlyState& roState, const ParticleSystemState& state, ParticleSystemParticles& ps, const size_t fromIndex, const size_t toIndex)
  64. {
  65. Matrix4x4f matrix;
  66. bool transform = GetTransformationMatrix(matrix, !roState.useLocalSpace, m_InWorldSpace, state.localToWorld);
  67. bool usesScalar = (m_X.minMaxState == kMMCScalar) && (m_Y.minMaxState == kMMCScalar) && (m_Z.minMaxState == kMMCScalar);
  68. bool isOptimized = m_X.IsOptimized() && m_Y.IsOptimized() && m_Z.IsOptimized();
  69. bool usesMinMax = m_X.UsesMinMax() && m_Y.UsesMinMax() && m_Z.UsesMinMax();
  70. if(usesScalar)
  71. UpdateTpl<kEMScalar>(m_X, m_Y, m_Z, roState, ps, fromIndex, toIndex, transform, matrix);
  72. else if(isOptimized && usesMinMax)
  73. UpdateTpl<kEMOptimizedMinMax>(m_X, m_Y, m_Z, roState, ps, fromIndex, toIndex, transform, matrix);
  74. else if(isOptimized)
  75. UpdateTpl<kEMOptimized>(m_X, m_Y, m_Z, roState, ps, fromIndex, toIndex, transform, matrix);
  76. else
  77. UpdateTpl<kEMSlow>(m_X, m_Y, m_Z, roState, ps, fromIndex, toIndex, transform, matrix);
  78. }
  79. void VelocityModule::UpdateProcedural (const ParticleSystemReadOnlyState& roState, const ParticleSystemState& state, ParticleSystemParticles& ps)
  80. {
  81. Matrix4x4f matrix;
  82. bool transform = GetTransformationMatrix(matrix, !roState.useLocalSpace, m_InWorldSpace, state.localToWorld);
  83. DualMinMax3DPolyCurves curves;
  84. if(m_X.IsOptimized() && m_Y.IsOptimized() && m_Z.IsOptimized())
  85. {
  86. curves.optX = m_X.polyCurves; curves.optX.Integrate();
  87. curves.optY = m_Y.polyCurves; curves.optY.Integrate();
  88. curves.optZ = m_Z.polyCurves; curves.optZ.Integrate();
  89. UpdateProceduralTpl<true>(curves, m_X, m_Y, m_Z, roState, ps, matrix, transform);
  90. }
  91. else
  92. {
  93. DebugAssert(CurvesSupportProcedural (m_X.editorCurves, m_X.minMaxState));
  94. DebugAssert(CurvesSupportProcedural (m_Y.editorCurves, m_Y.minMaxState));
  95. DebugAssert(CurvesSupportProcedural (m_Z.editorCurves, m_Z.minMaxState));
  96. BuildCurves(curves.x, m_X.editorCurves, m_X.GetScalar(), m_X.minMaxState); curves.x.Integrate();
  97. BuildCurves(curves.y, m_Y.editorCurves, m_Y.GetScalar(), m_Y.minMaxState); curves.y.Integrate();
  98. BuildCurves(curves.z, m_Z.editorCurves, m_Z.GetScalar(), m_Z.minMaxState); curves.z.Integrate();
  99. UpdateProceduralTpl<false>(curves, m_X, m_Y, m_Z, roState, ps, matrix, transform);
  100. }
  101. }
  102. void VelocityModule::CalculateProceduralBounds(MinMaxAABB& bounds, const Matrix4x4f& localToWorld, float maxLifeTime)
  103. {
  104. Vector2f xRange = m_X.FindMinMaxIntegrated();
  105. Vector2f yRange = m_Y.FindMinMaxIntegrated();
  106. Vector2f zRange = m_Z.FindMinMaxIntegrated();
  107. bounds.m_Min = Vector3f(xRange.x, yRange.x, zRange.x) * maxLifeTime;
  108. bounds.m_Max = Vector3f(xRange.y, yRange.y, zRange.y) * maxLifeTime;
  109. if(m_InWorldSpace)
  110. {
  111. Matrix4x4f matrix;
  112. Matrix4x4f::Invert_General3D(localToWorld, matrix);
  113. matrix.SetPosition(Vector3f::zero);
  114. auto aabb = bounds;
  115. TransformAABBSlow(aabb, matrix, aabb);
  116. bounds = aabb;
  117. }
  118. }
  119. template<class TransferFunction>
  120. void VelocityModule::Transfer (TransferFunction& transfer)
  121. {
  122. ParticleSystemModule::Transfer (transfer);
  123. transfer.Transfer (m_X, "x");
  124. transfer.Transfer (m_Y, "y");
  125. transfer.Transfer (m_Z, "z");
  126. transfer.Transfer (m_InWorldSpace, "inWorldSpace"); transfer.Align();
  127. }
  128. INSTANTIATE_TEMPLATE_TRANSFER(VelocityModule)
  129. NS_RRP_END