Muto 0.0.1
Cross-Platform Application Template
GitHub Repo | Muto Updates | Muto's Author
Loading...
Searching...
No Matches
Muto::ParticleSystem Class Reference

A simple particle system for stress testing the renderer. This is a temporary starter particle system and will be replaced with a more robust implementation in the future. More...

#include <ParticleSystem.h>

Public Member Functions

void Emit (const ParticleProps &particleProps)
 
void OnRender ()
 
void OnUpdate (Timestep ts)
 
 ParticleSystem ()
 
 ParticleSystem (uint32_t maxParticles)
 
void ResetParticle (Particle &particle, const ParticleProps &particleProps)
 
void ResetSystem ()
 
void SetParticleProps (const ParticleProps &particleProps)
 

Public Attributes

int m_EmitRate = 100
 
bool m_IsEmitting = true
 
bool m_Loop = true
 
ParticleProps m_Props
 
float m_TimeSinceLastEmit = 0.0f
 

Private Attributes

int m_activeParticleCount = 0
 
std::vector< Particle > m_ParticlePool
 
uint32_t m_PoolIndex = 999
 

Detailed Description

A simple particle system for stress testing the renderer. This is a temporary starter particle system and will be replaced with a more robust implementation in the future.

Note
Temporary implementation is not optimized for performance and is intended for testing purposes only. It uses a simple pool of particles and updates them each frame. The particle properties can be set using the ParticleProps struct, which allows for variation in velocity, size, rotation, and lifetime.

Constructor & Destructor Documentation

◆ ParticleSystem() [1/2]

Muto::ParticleSystem::ParticleSystem ( )
12 {
13 m_PoolIndex = 999;
14 m_ParticlePool.resize(1000);
16 }
uint32_t m_PoolIndex
Definition ParticleSystem.h:58
std::vector< Particle > m_ParticlePool
Definition ParticleSystem.h:57
int m_activeParticleCount
Definition ParticleSystem.h:59

References m_activeParticleCount, and m_PoolIndex.

◆ ParticleSystem() [2/2]

Muto::ParticleSystem::ParticleSystem ( uint32_t  maxParticles)
20 {
22 }
constexpr Scope< T > CreateScope(Args &&... args)
Creates a Scope (unique_ptr) for the given type and constructor arguments.
Definition Defines_Macros.h:51

References m_activeParticleCount, and m_PoolIndex.

Member Function Documentation

◆ Emit()

void Muto::ParticleSystem::Emit ( const ParticleProps &  particleProps)
97 {
99 particle.Active = true;
100 particle.Position = particleProps.Position + particleProps.PositionVariation * (Muto::Random::Float3() - glm::vec3(0.5f));
101 particle.Rotation = particleProps.Rotation + particleProps.RotationVariation * (Muto::Random::Float1() - 0.5f);
102
103 particle.Velocity = particleProps.Velocity;
104 particle.Velocity.x += particleProps.VelocityVariation.x * (Muto::Random::Float1() - 0.5f);
105 particle.Velocity.y += particleProps.VelocityVariation.y * (Muto::Random::Float1() - 0.5f);
106
107 particle.ColorBegin = particleProps.ColorBegin;
108 particle.ColorEnd = particleProps.ColorEnd;
109
110 particle.SizeBegin = particleProps.SizeBegin + particleProps.SizeVariation * (Muto::Random::Float1() - 0.5f);
111 particle.SizeEnd = particleProps.SizeEnd;
112
113 particle.Lifetime = particleProps.Lifetime + particleProps.LifetimeVariation * (Muto::Random::Float1() - 0.5f);
114 particle.LifeRemaining = particle.Lifetime;
115
116 uint32_t poolSize = static_cast<uint32_t>(m_ParticlePool.size());
117 m_PoolIndex = (m_PoolIndex == 0) ? (poolSize - 1) : (m_PoolIndex - 1);
118 }
glm::vec3 Float3()
Definition Random.h:124
float Float1()
Definition Random.h:79

References Muto::Particle::Active, Muto::Random::Float1(), Muto::Particle::LifeRemaining, Muto::ParticleProps::Lifetime, Muto::Particle::Lifetime, Muto::ParticleProps::LifetimeVariation, m_PoolIndex, Muto::ParticleProps::Rotation, Muto::Particle::Rotation, Muto::ParticleProps::RotationVariation, Muto::ParticleProps::SizeBegin, Muto::Particle::SizeBegin, Muto::ParticleProps::SizeEnd, Muto::Particle::SizeEnd, and Muto::ParticleProps::SizeVariation.

Referenced by Muto::SceneHierarchyPanel::DrawComponents().

◆ OnRender()

void Muto::ParticleSystem::OnRender ( )
77 {
78 for (auto& particle : m_ParticlePool)
79 {
80 if (!particle.Active)
81 continue;
82
83 float life = particle.LifeRemaining / particle.Lifetime;
84 glm::vec4 color = glm::lerp(particle.ColorEnd, particle.ColorBegin, life);
85 float size = glm::lerp(particle.SizeEnd, particle.SizeBegin, life);
86
88 { particle.Position },
89 { size, size },
91 particle.Rotation, 1.0f, color);
92
93 }
94 }
static Ref< Texture2D > GetWhiteTexture()
Returns a reference to the default white texture that allows for coloring.
Definition Renderer2D.cpp:511
static void DrawRotatedQuad(const glm::mat4 &transform, const glm::vec4 &color)
Draws a rotated colored quad with the given transform and color.
Definition Renderer2D.cpp:365

◆ OnUpdate()

void Muto::ParticleSystem::OnUpdate ( Muto::Timestep  ts)
25 {
26 if (m_Loop && m_activeParticleCount == 0)
27 {
29 }
30 else if (!m_Loop && m_activeParticleCount == 0)
31 {
32 m_IsEmitting = false;
33 }
34 else if (!m_Loop && m_activeParticleCount > 0)
35 {
36 m_IsEmitting = false;
37 }
38 else if (m_Loop) {
39
40 //m_IsEmitting = true;
42 {
43 int particlesToEmit = static_cast<int>(m_EmitRate * m_TimeSinceLastEmit);
44 for (int i = 0; i < particlesToEmit; i++)
45 {
47 }
48 if (particlesToEmit > 0 && m_EmitRate > 0.0f)
49 m_TimeSinceLastEmit -= static_cast<float>(particlesToEmit) / m_EmitRate;
50 }
51 }
52
53
54
56 for (auto& particle : m_ParticlePool)
57 {
58 if (!particle.Active) {
59 continue;
60 }
61
62 particle.LifeRemaining -= ts;
63 if (particle.LifeRemaining <= 0.0f)
64 {
65 particle.Active = false;
66 continue;
67 }
68
69 particle.Position += particle.Velocity * (float)ts;
70 particle.Rotation += 0.01f * ts;
72 }
74 }
int m_EmitRate
Definition ParticleSystem.h:64
bool m_Loop
Definition ParticleSystem.h:65
bool m_IsEmitting
Definition ParticleSystem.h:63
ParticleProps m_Props
Definition ParticleSystem.h:61
float m_TimeSinceLastEmit
Definition ParticleSystem.h:62
void Emit(const ParticleProps &particleProps)
Definition ParticleSystem.cpp:96
void ResetSystem()
Definition ParticleSystem.h:54

References m_activeParticleCount, m_EmitRate, m_IsEmitting, m_Loop, m_TimeSinceLastEmit, and ResetSystem().

◆ ResetParticle()

void Muto::ParticleSystem::ResetParticle ( Particle &  particle,
const ParticleProps &  particleProps 
)
121 {
122 particle.Active = true;
123 particle.Position = particleProps.Position;
124 particle.Rotation = particleProps.Rotation + particleProps.RotationVariation * (Muto::Random::Float1() - 0.5f);
125 particle.Velocity = particleProps.Velocity;
126 particle.Velocity.x += particleProps.VelocityVariation.x * (Muto::Random::Float1() - 0.5f);
127 particle.Velocity.y += particleProps.VelocityVariation.y * (Muto::Random::Float1() - 0.5f);
128 particle.ColorBegin = particleProps.ColorBegin;
129 particle.ColorEnd = particleProps.ColorEnd;
130 particle.SizeBegin = particleProps.SizeBegin + particleProps.SizeVariation * (Muto::Random::Float1() - 0.5f);
131 particle.SizeEnd = particleProps.SizeEnd;
132 particle.Lifetime = particleProps.Lifetime + particleProps.LifetimeVariation * (Muto::Random::Float1() - 0.5f);
133 particle.LifeRemaining = particle.Lifetime;
134 }

References Muto::Particle::Active, Muto::Random::Float1(), Muto::Particle::LifeRemaining, Muto::ParticleProps::Lifetime, Muto::Particle::Lifetime, Muto::ParticleProps::LifetimeVariation, Muto::ParticleProps::Rotation, Muto::Particle::Rotation, Muto::ParticleProps::RotationVariation, Muto::ParticleProps::SizeBegin, Muto::Particle::SizeBegin, Muto::ParticleProps::SizeEnd, Muto::Particle::SizeEnd, and Muto::ParticleProps::SizeVariation.

◆ ResetSystem()

void Muto::ParticleSystem::ResetSystem ( )
inline

◆ SetParticleProps()

void Muto::ParticleSystem::SetParticleProps ( const ParticleProps &  particleProps)
inline

Member Data Documentation

◆ m_activeParticleCount

int Muto::ParticleSystem::m_activeParticleCount = 0
private

◆ m_EmitRate

int Muto::ParticleSystem::m_EmitRate = 100

Referenced by OnUpdate().

◆ m_IsEmitting

bool Muto::ParticleSystem::m_IsEmitting = true

Referenced by OnUpdate(), and ResetSystem().

◆ m_Loop

bool Muto::ParticleSystem::m_Loop = true

Referenced by OnUpdate().

◆ m_ParticlePool

std::vector<Particle> Muto::ParticleSystem::m_ParticlePool
private

◆ m_PoolIndex

uint32_t Muto::ParticleSystem::m_PoolIndex = 999
private

◆ m_Props

ParticleProps Muto::ParticleSystem::m_Props

◆ m_TimeSinceLastEmit

float Muto::ParticleSystem::m_TimeSinceLastEmit = 0.0f

Referenced by OnUpdate(), and ResetSystem().


The documentation for this class was generated from the following files: