InitialModule.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // InitialModule.cpp
  3. // libcocos2d Mac
  4. //
  5. // Created by 徐俊杰 on 2020/4/24.
  6. //
  7. #include "rparticle/Modules/InitialModule.h"
  8. //#include "UnityPrefix.h"
  9. //#include "Runtime/BaseClasses/ObjectDefines.h"
  10. #include "rparticle/Serialize/TransferFunctions/SerializeTransfer.h"
  11. #include "rparticle/ParticleSystemUtils.h"
  12. #include "rparticle/Math/Random/Random.h"
  13. //#include "Runtime/Interfaces/IPhysics.h"
  14. //#include "Runtime/BaseClasses/IsPlaying.h"
  15. #define zero ZERO
  16. #define one ONE
  17. #define zAxis UNIT_Z
  18. NS_RRP_BEGIN
  19. InitialModule::InitialModule () : ParticleSystemModule(true)
  20. , m_GravityModifier(0.0f)
  21. , m_InheritVelocity(0.0f)
  22. , m_MaxNumParticles(1000)
  23. {
  24. }
  25. Vector3f InitialModule::GetGravity (const ParticleSystemReadOnlyState& roState, const ParticleSystemState& state) const
  26. {
  27. #if ENABLE_PHYSICS
  28. //IPhysics* physicsModule = GetIPhysics();
  29. //if (!physicsModule)
  30. // return Vector3f::zero;
  31. //Vector3f gravity = m_GravityModifier * physicsModule->GetGravity ();
  32. Vector3f gravity = m_GravityModifier * roState.GetRenderScale() * Vector3f(0, -9.8f, 0);
  33. if(roState.useLocalSpace)
  34. {
  35. Matrix4x4f worldToLocal;
  36. Matrix4x4f::Invert_General3D(state.localToWorld, worldToLocal);
  37. gravity = worldToLocal.MultiplyVector3(gravity);
  38. }
  39. return gravity;
  40. #else
  41. return Vector3f::zero;
  42. #endif
  43. }
  44. void InitialModule::Start (const ParticleSystemReadOnlyState& roState, const ParticleSystemState& state, ParticleSystemParticles& ps, const Matrix4x4f& matrix, size_t fromIndex, float t)
  45. {
  46. DebugAssert(roState.lengthInSec > 0.0001f);
  47. float normalizedT = t / roState.lengthInSec;
  48. DebugAssert (normalizedT >= 0.0f);
  49. DebugAssert (normalizedT <= 1.0f);
  50. Rand& random = GetRandom();
  51. Vector3f origin = matrix.GetPosition ();
  52. auto inheritedColor = ColorRGBA32(m_InheritedValues.color);
  53. auto inheritedSize = m_InheritedValues.size;
  54. auto inheritedRotation = m_InheritedValues.rotation;
  55. auto lifetimeMultiplier = m_InheritedValues.lifetimeMultiplier;
  56. auto parentRandomSeed = m_InheritedValues.randomSeed;
  57. auto axisOfRotation = m_InheritedValues.axisOfRotation;
  58. normalizedT = m_InheritedValues.normalizedT == std::numeric_limits<float>::infinity() ? normalizedT : m_InheritedValues.normalizedT;
  59. const size_t count = ps.array_size ();
  60. for (size_t q = fromIndex; q < count; ++q)
  61. {
  62. UInt32 randUInt32 = random.Get ();
  63. float rand = Rand::GetFloatFromInt (randUInt32);
  64. UInt32 randByte = Rand::GetByteFromInt (randUInt32);
  65. ColorRGBA32 col = Evaluate (m_Color, normalizedT, randByte);
  66. col *= inheritedColor;
  67. float sz = std::max<float> (0.0f, Evaluate (m_Size, normalizedT, rand)) * roState.GetRenderScale() * inheritedSize;
  68. Vector3f vel = matrix.MultiplyVector3 (Vector3f::zAxis);
  69. float ttl = std::max<float> (0.0f, Evaluate (m_Lifetime, normalizedT, rand)) * lifetimeMultiplier;
  70. float rot = Evaluate (m_Rotation, normalizedT, rand) + inheritedRotation;
  71. ps.position[q] = origin;
  72. ps.velocity[q] = vel;
  73. ps.animatedVelocity[q] = Vector3f::zero;
  74. ps.lifetime[q] = ttl;
  75. ps.startLifetime[q] = ttl;
  76. ps.size[q] = sz;
  77. ps.rotation[q] = rot;
  78. if(ps.usesRotationalSpeed)
  79. ps.rotationalSpeed[q] = 0.0f;
  80. ps.color[q] = col;
  81. ps.randomSeed[q] = random.Get(); // One more iteration to avoid visible patterns between random spawned parameters and those used in update
  82. if(ps.usesAxisOfRotation)
  83. ps.axisOfRotation[q] = Vector3f::zAxis;
  84. for(int acc = 0; acc < ps.numEmitAccumulators; acc++)
  85. ps.emitAccumulator[acc][q] = 0.0f;
  86. }
  87. }
  88. void InitialModule::Update (const ParticleSystemReadOnlyState& roState, const ParticleSystemState& state, ParticleSystemParticles& ps, const size_t fromIndex, const size_t toIndex, float dt) const
  89. {
  90. Vector3f gravityDelta = GetGravity(roState, state) * dt;
  91. // if (toIndex > 0)
  92. // {
  93. // CCLOG("velocity: (%f, %f, %f)", ps.velocity[0].x, ps.velocity[0].y, ps.velocity[0].z);
  94. // }
  95. if(!CompareApproximately(gravityDelta, Vector3f::zero, 0.0001f))
  96. for (size_t q = fromIndex; q < toIndex; ++q)
  97. ps.velocity[q] += gravityDelta;
  98. for (size_t q = fromIndex; q < toIndex; ++q)
  99. ps.animatedVelocity[q] = Vector3f::zero;
  100. if(ps.usesRotationalSpeed)
  101. for (size_t q = fromIndex; q < toIndex; ++q)
  102. ps.rotationalSpeed[q] = 0.0f;
  103. }
  104. void InitialModule::GenerateProcedural (const ParticleSystemReadOnlyState& roState, const ParticleSystemState& state, ParticleSystemParticles& ps, const ParticleSystemEmitReplay& emit)
  105. {
  106. size_t count = emit.particlesToEmit;
  107. float t = emit.t;
  108. float alreadyPassedTime = emit.aliveTime;
  109. DebugAssert(roState.lengthInSec > 0.0001f);
  110. const float normalizedT = t / roState.lengthInSec;
  111. DebugAssert (normalizedT >= 0.0f);
  112. DebugAssert (normalizedT <= 1.0f);
  113. Rand& random = GetRandom();
  114. const Matrix4x4f localToWorld = !roState.useLocalSpace ? state.localToWorld : Matrix4x4f::IDENTITY;
  115. Vector3f origin = localToWorld.GetPosition ();
  116. for (size_t i = 0; i < count; ++i)
  117. {
  118. UInt32 randUInt32 = random.Get ();
  119. float rand = Rand::GetFloatFromInt (randUInt32);
  120. UInt32 randByte = Rand::GetByteFromInt (randUInt32);
  121. float frameOffset = (float(i) + emit.emissionOffset) * emit.emissionGap * float(i < emit.numContinuous);
  122. const ColorRGBA32 col = Evaluate (m_Color, normalizedT, randByte);
  123. float sz = std::max<float> (0.0f, Evaluate (m_Size, normalizedT, rand)) * roState.GetRenderScale();
  124. Vector3f vel = localToWorld.MultiplyVector3 (Vector3f::zAxis);
  125. float ttlStart = std::max<float> (0.0f, Evaluate (m_Lifetime, normalizedT, rand));
  126. float ttl = ttlStart - alreadyPassedTime - frameOffset;
  127. float rot = Evaluate (m_Rotation, normalizedT, rand);
  128. if (ttl < 0.0F)
  129. continue;
  130. size_t q = ps.array_size();
  131. ps.array_resize(ps.array_size() + 1);
  132. ps.position[q] = origin;
  133. ps.velocity[q] = vel;
  134. ps.animatedVelocity[q] = Vector3f::zero;
  135. ps.lifetime[q] = ttl;
  136. ps.startLifetime[q] = ttlStart;
  137. ps.size[q] = sz;
  138. ps.rotation[q] = rot;
  139. if(ps.usesRotationalSpeed)
  140. ps.rotationalSpeed[q] = 0.0f;
  141. ps.color[q] = col;
  142. ps.randomSeed[q] = random.Get(); // One more iteration to avoid visible patterns between random spawned parameters and those used in update
  143. if(ps.usesAxisOfRotation)
  144. ps.axisOfRotation[q] = Vector3f::zAxis;
  145. for(int acc = 0; acc < ps.numEmitAccumulators; acc++)
  146. ps.emitAccumulator[acc][q] = 0.0f;
  147. }
  148. }
  149. void InitialModule::CheckConsistency ()
  150. {
  151. m_Lifetime.SetScalar(clamp<float> (m_Lifetime.GetScalar(), 0.05f, 100000.0f));
  152. m_Size.SetScalar(std::max<float> (0.0f, m_Size.GetScalar()));
  153. m_MaxNumParticles = std::max<int> (0, m_MaxNumParticles);
  154. }
  155. void InitialModule::AwakeFromLoad (RParticleSystem* system, const ParticleSystemReadOnlyState& roState)
  156. {
  157. ResetSeed(roState);
  158. }
  159. void InitialModule::SetInheritedParams(const SubEmitterInheritValues& inheritValues)
  160. {
  161. m_InheritedValues = inheritValues;
  162. }
  163. void InitialModule::ResetSeed(const ParticleSystemReadOnlyState& roState)
  164. {
  165. if(roState.randomSeed == 0)
  166. m_Random.SetSeed(GetGlobalRandomSeed ());
  167. else
  168. m_Random.SetSeed(roState.randomSeed);
  169. }
  170. Rand& InitialModule::GetRandom()
  171. {
  172. #if UNITY_EDITOR
  173. if(!IsWorldPlaying())
  174. return m_EditorRandom;
  175. else
  176. #endif
  177. return m_Random;
  178. }
  179. //TODO: Serialize
  180. ///*
  181. template<class TransferFunction>
  182. void InitialModule::Transfer (TransferFunction& transfer)
  183. {
  184. SetEnabled(true); // always enabled
  185. ParticleSystemModule::Transfer (transfer);
  186. transfer.Transfer (m_Lifetime, "startLifetime");
  187. transfer.Transfer (m_Speed, "startSpeed");
  188. transfer.Transfer (m_Color, "startColor");
  189. transfer.Transfer (m_Size, "startSize");
  190. transfer.Transfer (m_Rotation, "startRotation");
  191. transfer.Transfer (m_GravityModifier, "gravityModifier");
  192. transfer.Transfer (m_InheritVelocity, "inheritVelocity");
  193. transfer.Transfer (m_MaxNumParticles, "maxNumParticles");
  194. }
  195. INSTANTIATE_TEMPLATE_TRANSFER(InitialModule)
  196. // */
  197. NS_RRP_END