Merge pull request #29696 from akien-mga/cpuparticles-randomness

CPUParticles: Do randomness ratio computations in phase instead of time
This commit is contained in:
Rémi Verschelde 2019-06-12 10:54:02 +02:00 committed by GitHub
commit f160c81f68
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 8 deletions

View file

@ -545,6 +545,8 @@ void CPUParticles2D::_particles_process(float p_delta) {
velocity_xform[2] = Vector2(); velocity_xform[2] = Vector2();
} }
float system_phase = time / lifetime;
for (int i = 0; i < pcount; i++) { for (int i = 0; i < pcount; i++) {
Particle &p = parray[i]; Particle &p = parray[i];
@ -552,21 +554,26 @@ void CPUParticles2D::_particles_process(float p_delta) {
if (!emitting && !p.active) if (!emitting && !p.active)
continue; continue;
float restart_time = (float(i) / float(pcount)) * lifetime;
float local_delta = p_delta; float local_delta = p_delta;
// The phase is a ratio between 0 (birth) and 1 (end of life) for each particle.
// While we use time in tests later on, for randomness we use the phase as done in the
// original shader code, and we later multiply by lifetime to get the time.
float restart_phase = float(i) / float(pcount);
if (randomness_ratio > 0.0) { if (randomness_ratio > 0.0) {
uint32_t seed = cycle; uint32_t seed = cycle;
if (restart_time >= time) { if (restart_phase >= system_phase) {
seed -= uint32_t(1); seed -= uint32_t(1);
} }
seed *= uint32_t(pcount); seed *= uint32_t(pcount);
seed += uint32_t(i); seed += uint32_t(i);
float random = float(idhash(seed) % uint32_t(65536)) / 65536.0; float random = float(idhash(seed) % uint32_t(65536)) / 65536.0;
restart_time += randomness_ratio * random * 1.0 / float(pcount); restart_phase += randomness_ratio * random * 1.0 / float(pcount);
} }
restart_time *= (1.0 - explosiveness_ratio); restart_phase *= (1.0 - explosiveness_ratio);
float restart_time = restart_phase * lifetime;
bool restart = false; bool restart = false;
if (time > prev_time) { if (time > prev_time) {

View file

@ -515,6 +515,8 @@ void CPUParticles::_particles_process(float p_delta) {
velocity_xform = emission_xform.basis; velocity_xform = emission_xform.basis;
} }
float system_phase = time / lifetime;
for (int i = 0; i < pcount; i++) { for (int i = 0; i < pcount; i++) {
Particle &p = parray[i]; Particle &p = parray[i];
@ -522,21 +524,26 @@ void CPUParticles::_particles_process(float p_delta) {
if (!emitting && !p.active) if (!emitting && !p.active)
continue; continue;
float restart_time = (float(i) / float(pcount)) * lifetime;
float local_delta = p_delta; float local_delta = p_delta;
// The phase is a ratio between 0 (birth) and 1 (end of life) for each particle.
// While we use time in tests later on, for randomness we use the phase as done in the
// original shader code, and we later multiply by lifetime to get the time.
float restart_phase = float(i) / float(pcount);
if (randomness_ratio > 0.0) { if (randomness_ratio > 0.0) {
uint32_t seed = cycle; uint32_t seed = cycle;
if (restart_time >= time) { if (restart_phase >= system_phase) {
seed -= uint32_t(1); seed -= uint32_t(1);
} }
seed *= uint32_t(pcount); seed *= uint32_t(pcount);
seed += uint32_t(i); seed += uint32_t(i);
float random = float(idhash(seed) % uint32_t(65536)) / 65536.0; float random = float(idhash(seed) % uint32_t(65536)) / 65536.0;
restart_time += randomness_ratio * random * 1.0 / float(pcount); restart_phase += randomness_ratio * random * 1.0 / float(pcount);
} }
restart_time *= (1.0 - explosiveness_ratio); restart_phase *= (1.0 - explosiveness_ratio);
float restart_time = restart_phase * lifetime;
bool restart = false; bool restart = false;
if (time > prev_time) { if (time > prev_time) {