summaryrefslogtreecommitdiff
path: root/scene
diff options
context:
space:
mode:
authorJuan Linietsky <reduzio@gmail.com>2018-10-08 00:51:48 -0300
committerJuan Linietsky <reduzio@gmail.com>2018-10-08 00:55:43 -0300
commit14494dddd04bb9b6964940cdde97fe0364299b96 (patch)
treefb91b72e0f8d160a336d02e6e8e2cbfbd5bb0693 /scene
parentb17e71b6e5e035f49b5b3b5b55b9cdac80215d72 (diff)
Fixes to CPU particles for performance and avoiding NaNs.
Diffstat (limited to 'scene')
-rw-r--r--scene/3d/cpu_particles.cpp21
-rw-r--r--scene/3d/cpu_particles.h2
-rw-r--r--scene/resources/particles_material.cpp2
3 files changed, 20 insertions, 5 deletions
diff --git a/scene/3d/cpu_particles.cpp b/scene/3d/cpu_particles.cpp
index ec51c31674..b396397c6d 100644
--- a/scene/3d/cpu_particles.cpp
+++ b/scene/3d/cpu_particles.cpp
@@ -592,7 +592,7 @@ void CPUParticles::_particles_process(float p_delta) {
Vector3 direction_xz = Vector3(Math::sin(angle1_rad), 0, Math::cos(angle1_rad));
Vector3 direction_yz = Vector3(0, Math::sin(angle2_rad), Math::cos(angle2_rad));
- direction_yz.z = direction_yz.z / Math::sqrt(direction_yz.z); //better uniform distribution
+ direction_yz.z = direction_yz.z / MAX(0.0001, Math::sqrt(ABS(direction_yz.z))); //better uniform distribution
Vector3 direction = Vector3(direction_xz.x * direction_yz.z, direction_yz.y, direction_xz.z * direction_yz.z);
direction.normalize();
p.velocity = direction * parameters[PARAM_INITIAL_LINEAR_VELOCITY] * Math::lerp(1.0f, float(Math::randf()), randomness[PARAM_INITIAL_LINEAR_VELOCITY]);
@@ -977,6 +977,8 @@ void CPUParticles::_update_particle_data_buffer() {
ptr += 17;
}
+
+ can_update = true;
}
#ifndef NO_THREADS
@@ -989,8 +991,10 @@ void CPUParticles::_update_render_thread() {
#ifndef NO_THREADS
update_mutex->lock();
#endif
-
- VS::get_singleton()->multimesh_set_as_bulk_array(multimesh, particle_data);
+ if (can_update) {
+ VS::get_singleton()->multimesh_set_as_bulk_array(multimesh, particle_data);
+ can_update = false; //wait for next time
+ }
#ifndef NO_THREADS
update_mutex->unlock();
@@ -1061,6 +1065,8 @@ void CPUParticles::_notification(int p_what) {
}
}
+ bool processed = false;
+
if (time == 0 && pre_process_time > 0.0) {
float frame_time;
@@ -1073,6 +1079,7 @@ void CPUParticles::_notification(int p_what) {
while (todo >= 0) {
_particles_process(frame_time);
+ processed = true;
todo -= frame_time;
}
}
@@ -1091,6 +1098,7 @@ void CPUParticles::_notification(int p_what) {
while (todo >= frame_time) {
_particles_process(frame_time);
+ processed = true;
todo -= decr;
}
@@ -1098,9 +1106,12 @@ void CPUParticles::_notification(int p_what) {
} else {
_particles_process(delta);
+ processed = true;
}
- _update_particle_data_buffer();
+ if (processed) {
+ _update_particle_data_buffer();
+ }
}
}
@@ -1424,6 +1435,8 @@ CPUParticles::CPUParticles() {
flags[i] = false;
}
+ can_update = false;
+
set_color(Color(1, 1, 1, 1));
#ifndef NO_THREADS
diff --git a/scene/3d/cpu_particles.h b/scene/3d/cpu_particles.h
index 4e29d8d4ce..77a144b70b 100644
--- a/scene/3d/cpu_particles.h
+++ b/scene/3d/cpu_particles.h
@@ -142,6 +142,8 @@ private:
int fixed_fps;
bool fractional_delta;
+ volatile bool can_update;
+
DrawOrder draw_order;
Ref<Mesh> mesh;
diff --git a/scene/resources/particles_material.cpp b/scene/resources/particles_material.cpp
index 6f67ba8af1..ba48982fda 100644
--- a/scene/resources/particles_material.cpp
+++ b/scene/resources/particles_material.cpp
@@ -308,7 +308,7 @@ void ParticlesMaterial::_update_shader() {
code += " float angle2_rad = rand_from_seed_m1_p1(alt_seed) * spread_rad * (1.0 - flatness);\n";
code += " vec3 direction_xz = vec3(sin(angle1_rad), 0, cos(angle1_rad));\n";
code += " vec3 direction_yz = vec3(0, sin(angle2_rad), cos(angle2_rad));\n";
- code += " direction_yz.z = direction_yz.z / sqrt(direction_yz.z); // better uniform distribution\n";
+ code += " direction_yz.z = direction_yz.z / max(0.0001,sqrt(abs(direction_yz.z))); // better uniform distribution\n";
code += " vec3 direction = vec3(direction_xz.x * direction_yz.z, direction_yz.y, direction_xz.z * direction_yz.z);\n";
code += " direction = normalize(direction);\n";
code += " VELOCITY = direction * initial_linear_velocity * mix(1.0, rand_from_seed(alt_seed), initial_linear_velocity_random);\n";