1 /*************************************************************************/
2 /*  cpu_particles.cpp                                                    */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #include "cpu_particles.h"
32 
33 #include "scene/3d/camera.h"
34 #include "scene/3d/particles.h"
35 #include "scene/resources/particles_material.h"
36 #include "servers/visual_server.h"
37 
get_aabb() const38 AABB CPUParticles::get_aabb() const {
39 
40 	return AABB();
41 }
get_faces(uint32_t p_usage_flags) const42 PoolVector<Face3> CPUParticles::get_faces(uint32_t p_usage_flags) const {
43 
44 	return PoolVector<Face3>();
45 }
46 
set_emitting(bool p_emitting)47 void CPUParticles::set_emitting(bool p_emitting) {
48 
49 	if (emitting == p_emitting)
50 		return;
51 
52 	emitting = p_emitting;
53 	if (emitting) {
54 		set_process_internal(true);
55 
56 		// first update before rendering to avoid one frame delay after emitting starts
57 		if (time == 0)
58 			_update_internal();
59 	}
60 }
61 
set_amount(int p_amount)62 void CPUParticles::set_amount(int p_amount) {
63 
64 	ERR_FAIL_COND_MSG(p_amount < 1, "Amount of particles must be greater than 0.");
65 
66 	particles.resize(p_amount);
67 	{
68 		PoolVector<Particle>::Write w = particles.write();
69 
70 		for (int i = 0; i < p_amount; i++) {
71 			w[i].active = false;
72 			w[i].custom[3] = 0.0; // Make sure w component isn't garbage data
73 		}
74 	}
75 
76 	particle_data.resize((12 + 4 + 1) * p_amount);
77 	VS::get_singleton()->multimesh_allocate(multimesh, p_amount, VS::MULTIMESH_TRANSFORM_3D, VS::MULTIMESH_COLOR_8BIT, VS::MULTIMESH_CUSTOM_DATA_FLOAT);
78 
79 	particle_order.resize(p_amount);
80 }
set_lifetime(float p_lifetime)81 void CPUParticles::set_lifetime(float p_lifetime) {
82 
83 	ERR_FAIL_COND_MSG(p_lifetime <= 0, "Particles lifetime must be greater than 0.");
84 	lifetime = p_lifetime;
85 }
86 
set_one_shot(bool p_one_shot)87 void CPUParticles::set_one_shot(bool p_one_shot) {
88 
89 	one_shot = p_one_shot;
90 }
91 
set_pre_process_time(float p_time)92 void CPUParticles::set_pre_process_time(float p_time) {
93 
94 	pre_process_time = p_time;
95 }
set_explosiveness_ratio(float p_ratio)96 void CPUParticles::set_explosiveness_ratio(float p_ratio) {
97 
98 	explosiveness_ratio = p_ratio;
99 }
set_randomness_ratio(float p_ratio)100 void CPUParticles::set_randomness_ratio(float p_ratio) {
101 
102 	randomness_ratio = p_ratio;
103 }
set_lifetime_randomness(float p_random)104 void CPUParticles::set_lifetime_randomness(float p_random) {
105 
106 	lifetime_randomness = p_random;
107 }
set_use_local_coordinates(bool p_enable)108 void CPUParticles::set_use_local_coordinates(bool p_enable) {
109 
110 	local_coords = p_enable;
111 }
set_speed_scale(float p_scale)112 void CPUParticles::set_speed_scale(float p_scale) {
113 
114 	speed_scale = p_scale;
115 }
116 
is_emitting() const117 bool CPUParticles::is_emitting() const {
118 
119 	return emitting;
120 }
get_amount() const121 int CPUParticles::get_amount() const {
122 
123 	return particles.size();
124 }
get_lifetime() const125 float CPUParticles::get_lifetime() const {
126 
127 	return lifetime;
128 }
get_one_shot() const129 bool CPUParticles::get_one_shot() const {
130 
131 	return one_shot;
132 }
133 
get_pre_process_time() const134 float CPUParticles::get_pre_process_time() const {
135 
136 	return pre_process_time;
137 }
get_explosiveness_ratio() const138 float CPUParticles::get_explosiveness_ratio() const {
139 
140 	return explosiveness_ratio;
141 }
get_randomness_ratio() const142 float CPUParticles::get_randomness_ratio() const {
143 
144 	return randomness_ratio;
145 }
get_lifetime_randomness() const146 float CPUParticles::get_lifetime_randomness() const {
147 
148 	return lifetime_randomness;
149 }
150 
get_use_local_coordinates() const151 bool CPUParticles::get_use_local_coordinates() const {
152 
153 	return local_coords;
154 }
155 
get_speed_scale() const156 float CPUParticles::get_speed_scale() const {
157 
158 	return speed_scale;
159 }
160 
set_draw_order(DrawOrder p_order)161 void CPUParticles::set_draw_order(DrawOrder p_order) {
162 
163 	draw_order = p_order;
164 }
165 
get_draw_order() const166 CPUParticles::DrawOrder CPUParticles::get_draw_order() const {
167 
168 	return draw_order;
169 }
170 
set_mesh(const Ref<Mesh> & p_mesh)171 void CPUParticles::set_mesh(const Ref<Mesh> &p_mesh) {
172 
173 	mesh = p_mesh;
174 	if (mesh.is_valid()) {
175 		VS::get_singleton()->multimesh_set_mesh(multimesh, mesh->get_rid());
176 	} else {
177 		VS::get_singleton()->multimesh_set_mesh(multimesh, RID());
178 	}
179 }
180 
get_mesh() const181 Ref<Mesh> CPUParticles::get_mesh() const {
182 
183 	return mesh;
184 }
185 
set_fixed_fps(int p_count)186 void CPUParticles::set_fixed_fps(int p_count) {
187 	fixed_fps = p_count;
188 }
189 
get_fixed_fps() const190 int CPUParticles::get_fixed_fps() const {
191 	return fixed_fps;
192 }
193 
set_fractional_delta(bool p_enable)194 void CPUParticles::set_fractional_delta(bool p_enable) {
195 	fractional_delta = p_enable;
196 }
197 
get_fractional_delta() const198 bool CPUParticles::get_fractional_delta() const {
199 	return fractional_delta;
200 }
201 
get_configuration_warning() const202 String CPUParticles::get_configuration_warning() const {
203 
204 	String warnings;
205 
206 	bool mesh_found = false;
207 	bool anim_material_found = false;
208 
209 	if (get_mesh().is_valid()) {
210 		mesh_found = true;
211 		for (int j = 0; j < get_mesh()->get_surface_count(); j++) {
212 			anim_material_found = Object::cast_to<ShaderMaterial>(get_mesh()->surface_get_material(j).ptr()) != NULL;
213 			SpatialMaterial *spat = Object::cast_to<SpatialMaterial>(get_mesh()->surface_get_material(j).ptr());
214 			anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == SpatialMaterial::BILLBOARD_PARTICLES);
215 		}
216 	}
217 
218 	anim_material_found = anim_material_found || Object::cast_to<ShaderMaterial>(get_material_override().ptr()) != NULL;
219 	SpatialMaterial *spat = Object::cast_to<SpatialMaterial>(get_material_override().ptr());
220 	anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == SpatialMaterial::BILLBOARD_PARTICLES);
221 
222 	if (!mesh_found) {
223 		if (warnings != String())
224 			warnings += "\n";
225 		warnings += "- " + TTR("Nothing is visible because no mesh has been assigned.");
226 	}
227 
228 	if (!anim_material_found && (get_param(PARAM_ANIM_SPEED) != 0.0 || get_param(PARAM_ANIM_OFFSET) != 0.0 ||
229 										get_param_curve(PARAM_ANIM_SPEED).is_valid() || get_param_curve(PARAM_ANIM_OFFSET).is_valid())) {
230 		if (warnings != String())
231 			warnings += "\n";
232 		warnings += "- " + TTR("CPUParticles animation requires the usage of a SpatialMaterial whose Billboard Mode is set to \"Particle Billboard\".");
233 	}
234 
235 	return warnings;
236 }
237 
restart()238 void CPUParticles::restart() {
239 
240 	time = 0;
241 	inactive_time = 0;
242 	frame_remainder = 0;
243 	cycle = 0;
244 	emitting = false;
245 
246 	{
247 		int pc = particles.size();
248 		PoolVector<Particle>::Write w = particles.write();
249 
250 		for (int i = 0; i < pc; i++) {
251 			w[i].active = false;
252 		}
253 	}
254 
255 	set_emitting(true);
256 }
257 
set_direction(Vector3 p_direction)258 void CPUParticles::set_direction(Vector3 p_direction) {
259 
260 	direction = p_direction;
261 }
262 
get_direction() const263 Vector3 CPUParticles::get_direction() const {
264 
265 	return direction;
266 }
267 
set_spread(float p_spread)268 void CPUParticles::set_spread(float p_spread) {
269 
270 	spread = p_spread;
271 }
272 
get_spread() const273 float CPUParticles::get_spread() const {
274 
275 	return spread;
276 }
277 
set_flatness(float p_flatness)278 void CPUParticles::set_flatness(float p_flatness) {
279 
280 	flatness = p_flatness;
281 }
get_flatness() const282 float CPUParticles::get_flatness() const {
283 
284 	return flatness;
285 }
286 
set_param(Parameter p_param,float p_value)287 void CPUParticles::set_param(Parameter p_param, float p_value) {
288 
289 	ERR_FAIL_INDEX(p_param, PARAM_MAX);
290 
291 	parameters[p_param] = p_value;
292 }
get_param(Parameter p_param) const293 float CPUParticles::get_param(Parameter p_param) const {
294 
295 	ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
296 
297 	return parameters[p_param];
298 }
299 
set_param_randomness(Parameter p_param,float p_value)300 void CPUParticles::set_param_randomness(Parameter p_param, float p_value) {
301 
302 	ERR_FAIL_INDEX(p_param, PARAM_MAX);
303 
304 	randomness[p_param] = p_value;
305 }
get_param_randomness(Parameter p_param) const306 float CPUParticles::get_param_randomness(Parameter p_param) const {
307 
308 	ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
309 
310 	return randomness[p_param];
311 }
312 
_adjust_curve_range(const Ref<Curve> & p_curve,float p_min,float p_max)313 static void _adjust_curve_range(const Ref<Curve> &p_curve, float p_min, float p_max) {
314 
315 	Ref<Curve> curve = p_curve;
316 	if (!curve.is_valid())
317 		return;
318 
319 	curve->ensure_default_setup(p_min, p_max);
320 }
321 
set_param_curve(Parameter p_param,const Ref<Curve> & p_curve)322 void CPUParticles::set_param_curve(Parameter p_param, const Ref<Curve> &p_curve) {
323 
324 	ERR_FAIL_INDEX(p_param, PARAM_MAX);
325 
326 	curve_parameters[p_param] = p_curve;
327 
328 	switch (p_param) {
329 		case PARAM_INITIAL_LINEAR_VELOCITY: {
330 			//do none for this one
331 		} break;
332 		case PARAM_ANGULAR_VELOCITY: {
333 			_adjust_curve_range(p_curve, -360, 360);
334 		} break;
335 		case PARAM_ORBIT_VELOCITY: {
336 			_adjust_curve_range(p_curve, -500, 500);
337 		} break;
338 		case PARAM_LINEAR_ACCEL: {
339 			_adjust_curve_range(p_curve, -200, 200);
340 		} break;
341 		case PARAM_RADIAL_ACCEL: {
342 			_adjust_curve_range(p_curve, -200, 200);
343 		} break;
344 		case PARAM_TANGENTIAL_ACCEL: {
345 			_adjust_curve_range(p_curve, -200, 200);
346 		} break;
347 		case PARAM_DAMPING: {
348 			_adjust_curve_range(p_curve, 0, 100);
349 		} break;
350 		case PARAM_ANGLE: {
351 			_adjust_curve_range(p_curve, -360, 360);
352 		} break;
353 		case PARAM_SCALE: {
354 
355 		} break;
356 		case PARAM_HUE_VARIATION: {
357 			_adjust_curve_range(p_curve, -1, 1);
358 		} break;
359 		case PARAM_ANIM_SPEED: {
360 			_adjust_curve_range(p_curve, 0, 200);
361 		} break;
362 		case PARAM_ANIM_OFFSET: {
363 		} break;
364 		default: {
365 		}
366 	}
367 }
get_param_curve(Parameter p_param) const368 Ref<Curve> CPUParticles::get_param_curve(Parameter p_param) const {
369 
370 	ERR_FAIL_INDEX_V(p_param, PARAM_MAX, Ref<Curve>());
371 
372 	return curve_parameters[p_param];
373 }
374 
set_color(const Color & p_color)375 void CPUParticles::set_color(const Color &p_color) {
376 
377 	color = p_color;
378 }
379 
get_color() const380 Color CPUParticles::get_color() const {
381 
382 	return color;
383 }
384 
set_color_ramp(const Ref<Gradient> & p_ramp)385 void CPUParticles::set_color_ramp(const Ref<Gradient> &p_ramp) {
386 
387 	color_ramp = p_ramp;
388 }
389 
get_color_ramp() const390 Ref<Gradient> CPUParticles::get_color_ramp() const {
391 
392 	return color_ramp;
393 }
394 
set_particle_flag(Flags p_flag,bool p_enable)395 void CPUParticles::set_particle_flag(Flags p_flag, bool p_enable) {
396 	ERR_FAIL_INDEX(p_flag, FLAG_MAX);
397 	flags[p_flag] = p_enable;
398 	if (p_flag == FLAG_DISABLE_Z) {
399 		_change_notify();
400 	}
401 }
402 
get_particle_flag(Flags p_flag) const403 bool CPUParticles::get_particle_flag(Flags p_flag) const {
404 	ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
405 	return flags[p_flag];
406 }
407 
set_emission_shape(EmissionShape p_shape)408 void CPUParticles::set_emission_shape(EmissionShape p_shape) {
409 	ERR_FAIL_INDEX(p_shape, EMISSION_SHAPE_MAX);
410 	emission_shape = p_shape;
411 }
412 
set_emission_sphere_radius(float p_radius)413 void CPUParticles::set_emission_sphere_radius(float p_radius) {
414 
415 	emission_sphere_radius = p_radius;
416 }
417 
set_emission_box_extents(Vector3 p_extents)418 void CPUParticles::set_emission_box_extents(Vector3 p_extents) {
419 
420 	emission_box_extents = p_extents;
421 }
422 
set_emission_points(const PoolVector<Vector3> & p_points)423 void CPUParticles::set_emission_points(const PoolVector<Vector3> &p_points) {
424 
425 	emission_points = p_points;
426 }
427 
set_emission_normals(const PoolVector<Vector3> & p_normals)428 void CPUParticles::set_emission_normals(const PoolVector<Vector3> &p_normals) {
429 
430 	emission_normals = p_normals;
431 }
432 
set_emission_colors(const PoolVector<Color> & p_colors)433 void CPUParticles::set_emission_colors(const PoolVector<Color> &p_colors) {
434 
435 	emission_colors = p_colors;
436 }
437 
get_emission_sphere_radius() const438 float CPUParticles::get_emission_sphere_radius() const {
439 
440 	return emission_sphere_radius;
441 }
get_emission_box_extents() const442 Vector3 CPUParticles::get_emission_box_extents() const {
443 
444 	return emission_box_extents;
445 }
get_emission_points() const446 PoolVector<Vector3> CPUParticles::get_emission_points() const {
447 
448 	return emission_points;
449 }
get_emission_normals() const450 PoolVector<Vector3> CPUParticles::get_emission_normals() const {
451 
452 	return emission_normals;
453 }
454 
get_emission_colors() const455 PoolVector<Color> CPUParticles::get_emission_colors() const {
456 
457 	return emission_colors;
458 }
459 
get_emission_shape() const460 CPUParticles::EmissionShape CPUParticles::get_emission_shape() const {
461 	return emission_shape;
462 }
set_gravity(const Vector3 & p_gravity)463 void CPUParticles::set_gravity(const Vector3 &p_gravity) {
464 
465 	gravity = p_gravity;
466 }
467 
get_gravity() const468 Vector3 CPUParticles::get_gravity() const {
469 
470 	return gravity;
471 }
472 
_validate_property(PropertyInfo & property) const473 void CPUParticles::_validate_property(PropertyInfo &property) const {
474 
475 	if (property.name == "color" && color_ramp.is_valid()) {
476 		property.usage = 0;
477 	}
478 
479 	if (property.name == "emission_sphere_radius" && emission_shape != EMISSION_SHAPE_SPHERE) {
480 		property.usage = 0;
481 	}
482 
483 	if (property.name == "emission_box_extents" && emission_shape != EMISSION_SHAPE_BOX) {
484 		property.usage = 0;
485 	}
486 
487 	if ((property.name == "emission_point_texture" || property.name == "emission_color_texture") && (emission_shape < EMISSION_SHAPE_POINTS)) {
488 		property.usage = 0;
489 	}
490 
491 	if (property.name == "emission_normals" && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
492 		property.usage = 0;
493 	}
494 
495 	if (property.name.begins_with("orbit_") && !flags[FLAG_DISABLE_Z]) {
496 		property.usage = 0;
497 	}
498 }
499 
idhash(uint32_t x)500 static uint32_t idhash(uint32_t x) {
501 
502 	x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
503 	x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
504 	x = (x >> uint32_t(16)) ^ x;
505 	return x;
506 }
507 
rand_from_seed(uint32_t & seed)508 static float rand_from_seed(uint32_t &seed) {
509 	int k;
510 	int s = int(seed);
511 	if (s == 0)
512 		s = 305420679;
513 	k = s / 127773;
514 	s = 16807 * (s - k * 127773) - 2836 * k;
515 	if (s < 0)
516 		s += 2147483647;
517 	seed = uint32_t(s);
518 	return float(seed % uint32_t(65536)) / 65535.0;
519 }
520 
_update_internal()521 void CPUParticles::_update_internal() {
522 
523 	if (particles.size() == 0 || !is_visible_in_tree()) {
524 		_set_redraw(false);
525 		return;
526 	}
527 
528 	float delta = get_process_delta_time();
529 	if (emitting) {
530 		inactive_time = 0;
531 	} else {
532 		inactive_time += delta;
533 		if (inactive_time > lifetime * 1.2) {
534 			set_process_internal(false);
535 			_set_redraw(false);
536 
537 			//reset variables
538 			time = 0;
539 			inactive_time = 0;
540 			frame_remainder = 0;
541 			cycle = 0;
542 			return;
543 		}
544 	}
545 	_set_redraw(true);
546 
547 	bool processed = false;
548 
549 	if (time == 0 && pre_process_time > 0.0) {
550 
551 		float frame_time;
552 		if (fixed_fps > 0)
553 			frame_time = 1.0 / fixed_fps;
554 		else
555 			frame_time = 1.0 / 30.0;
556 
557 		float todo = pre_process_time;
558 
559 		while (todo >= 0) {
560 			_particles_process(frame_time);
561 			processed = true;
562 			todo -= frame_time;
563 		}
564 	}
565 
566 	if (fixed_fps > 0) {
567 		float frame_time = 1.0 / fixed_fps;
568 		float decr = frame_time;
569 
570 		float ldelta = delta;
571 		if (ldelta > 0.1) { //avoid recursive stalls if fps goes below 10
572 			ldelta = 0.1;
573 		} else if (ldelta <= 0.0) { //unlikely but..
574 			ldelta = 0.001;
575 		}
576 		float todo = frame_remainder + ldelta;
577 
578 		while (todo >= frame_time) {
579 			_particles_process(frame_time);
580 			processed = true;
581 			todo -= decr;
582 		}
583 
584 		frame_remainder = todo;
585 
586 	} else {
587 		_particles_process(delta);
588 		processed = true;
589 	}
590 
591 	if (processed) {
592 		_update_particle_data_buffer();
593 	}
594 }
595 
_particles_process(float p_delta)596 void CPUParticles::_particles_process(float p_delta) {
597 
598 	p_delta *= speed_scale;
599 
600 	int pcount = particles.size();
601 	PoolVector<Particle>::Write w = particles.write();
602 
603 	Particle *parray = w.ptr();
604 
605 	float prev_time = time;
606 	time += p_delta;
607 	if (time > lifetime) {
608 		time = Math::fmod(time, lifetime);
609 		cycle++;
610 		if (one_shot && cycle > 0) {
611 			set_emitting(false);
612 			_change_notify();
613 		}
614 	}
615 
616 	Transform emission_xform;
617 	Basis velocity_xform;
618 	if (!local_coords) {
619 		emission_xform = get_global_transform();
620 		velocity_xform = emission_xform.basis;
621 	}
622 
623 	float system_phase = time / lifetime;
624 
625 	for (int i = 0; i < pcount; i++) {
626 
627 		Particle &p = parray[i];
628 
629 		if (!emitting && !p.active)
630 			continue;
631 
632 		float local_delta = p_delta;
633 
634 		// The phase is a ratio between 0 (birth) and 1 (end of life) for each particle.
635 		// While we use time in tests later on, for randomness we use the phase as done in the
636 		// original shader code, and we later multiply by lifetime to get the time.
637 		float restart_phase = float(i) / float(pcount);
638 
639 		if (randomness_ratio > 0.0) {
640 			uint32_t seed = cycle;
641 			if (restart_phase >= system_phase) {
642 				seed -= uint32_t(1);
643 			}
644 			seed *= uint32_t(pcount);
645 			seed += uint32_t(i);
646 			float random = float(idhash(seed) % uint32_t(65536)) / 65536.0;
647 			restart_phase += randomness_ratio * random * 1.0 / float(pcount);
648 		}
649 
650 		restart_phase *= (1.0 - explosiveness_ratio);
651 		float restart_time = restart_phase * lifetime;
652 		bool restart = false;
653 
654 		if (time > prev_time) {
655 			// restart_time >= prev_time is used so particles emit in the first frame they are processed
656 
657 			if (restart_time >= prev_time && restart_time < time) {
658 				restart = true;
659 				if (fractional_delta) {
660 					local_delta = time - restart_time;
661 				}
662 			}
663 
664 		} else if (local_delta > 0.0) {
665 			if (restart_time >= prev_time) {
666 				restart = true;
667 				if (fractional_delta) {
668 					local_delta = lifetime - restart_time + time;
669 				}
670 
671 			} else if (restart_time < time) {
672 				restart = true;
673 				if (fractional_delta) {
674 					local_delta = time - restart_time;
675 				}
676 			}
677 		}
678 
679 		if (p.time * (1.0 - explosiveness_ratio) > p.lifetime) {
680 			restart = true;
681 		}
682 
683 		if (restart) {
684 
685 			if (!emitting) {
686 				p.active = false;
687 				continue;
688 			}
689 			p.active = true;
690 
691 			/*float tex_linear_velocity = 0;
692 			if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
693 				tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->interpolate(0);
694 			}*/
695 
696 			float tex_angle = 0.0;
697 			if (curve_parameters[PARAM_ANGLE].is_valid()) {
698 				tex_angle = curve_parameters[PARAM_ANGLE]->interpolate(0);
699 			}
700 
701 			float tex_anim_offset = 0.0;
702 			if (curve_parameters[PARAM_ANGLE].is_valid()) {
703 				tex_anim_offset = curve_parameters[PARAM_ANGLE]->interpolate(0);
704 			}
705 
706 			p.seed = Math::rand();
707 
708 			p.angle_rand = Math::randf();
709 			p.scale_rand = Math::randf();
710 			p.hue_rot_rand = Math::randf();
711 			p.anim_offset_rand = Math::randf();
712 
713 			if (flags[FLAG_DISABLE_Z]) {
714 				float angle1_rad = Math::atan2(direction.y, direction.x) + (Math::randf() * 2.0 - 1.0) * Math_PI * spread / 180.0;
715 				Vector3 rot = Vector3(Math::cos(angle1_rad), Math::sin(angle1_rad), 0.0);
716 				p.velocity = rot * parameters[PARAM_INITIAL_LINEAR_VELOCITY] * Math::lerp(1.0f, float(Math::randf()), randomness[PARAM_INITIAL_LINEAR_VELOCITY]);
717 			} else {
718 				//initiate velocity spread in 3D
719 				float angle1_rad = Math::atan2(direction.x, direction.z) + (Math::randf() * 2.0 - 1.0) * Math_PI * spread / 180.0;
720 				float angle2_rad = Math::atan2(direction.y, Math::abs(direction.z)) + (Math::randf() * 2.0 - 1.0) * (1.0 - flatness) * Math_PI * spread / 180.0;
721 
722 				Vector3 direction_xz = Vector3(Math::sin(angle1_rad), 0, Math::cos(angle1_rad));
723 				Vector3 direction_yz = Vector3(0, Math::sin(angle2_rad), Math::cos(angle2_rad));
724 				direction_yz.z = direction_yz.z / MAX(0.0001, Math::sqrt(ABS(direction_yz.z))); //better uniform distribution
725 				Vector3 direction = Vector3(direction_xz.x * direction_yz.z, direction_yz.y, direction_xz.z * direction_yz.z);
726 				direction.normalize();
727 				p.velocity = direction * parameters[PARAM_INITIAL_LINEAR_VELOCITY] * Math::lerp(1.0f, float(Math::randf()), randomness[PARAM_INITIAL_LINEAR_VELOCITY]);
728 			}
729 
730 			float base_angle = (parameters[PARAM_ANGLE] + tex_angle) * Math::lerp(1.0f, p.angle_rand, randomness[PARAM_ANGLE]);
731 			p.custom[0] = Math::deg2rad(base_angle); //angle
732 			p.custom[1] = 0.0; //phase
733 			p.custom[2] = (parameters[PARAM_ANIM_OFFSET] + tex_anim_offset) * Math::lerp(1.0f, p.anim_offset_rand, randomness[PARAM_ANIM_OFFSET]); //animation offset (0-1)
734 			p.transform = Transform();
735 			p.time = 0;
736 			p.lifetime = lifetime * (1.0 - Math::randf() * lifetime_randomness);
737 			p.base_color = Color(1, 1, 1, 1);
738 
739 			switch (emission_shape) {
740 				case EMISSION_SHAPE_POINT: {
741 					//do none
742 				} break;
743 				case EMISSION_SHAPE_SPHERE: {
744 					float s = 2.0 * Math::randf() - 1.0, t = 2.0 * Math_PI * Math::randf();
745 					float radius = emission_sphere_radius * Math::sqrt(1.0 - s * s);
746 					p.transform.origin = Vector3(radius * Math::cos(t), radius * Math::sin(t), emission_sphere_radius * s);
747 				} break;
748 				case EMISSION_SHAPE_BOX: {
749 					p.transform.origin = Vector3(Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0) * emission_box_extents;
750 				} break;
751 				case EMISSION_SHAPE_POINTS:
752 				case EMISSION_SHAPE_DIRECTED_POINTS: {
753 
754 					int pc = emission_points.size();
755 					if (pc == 0)
756 						break;
757 
758 					int random_idx = Math::rand() % pc;
759 
760 					p.transform.origin = emission_points.get(random_idx);
761 
762 					if (emission_shape == EMISSION_SHAPE_DIRECTED_POINTS && emission_normals.size() == pc) {
763 						if (flags[FLAG_DISABLE_Z]) {
764 							Vector3 normal = emission_normals.get(random_idx);
765 							Vector2 normal_2d(normal.x, normal.y);
766 							Transform2D m2;
767 							m2.set_axis(0, normal_2d);
768 							m2.set_axis(1, normal_2d.tangent());
769 							Vector2 velocity_2d(p.velocity.x, p.velocity.y);
770 							velocity_2d = m2.basis_xform(velocity_2d);
771 							p.velocity.x = velocity_2d.x;
772 							p.velocity.y = velocity_2d.y;
773 						} else {
774 							Vector3 normal = emission_normals.get(random_idx);
775 							Vector3 v0 = Math::abs(normal.z) < 0.999 ? Vector3(0.0, 0.0, 1.0) : Vector3(0, 1.0, 0.0);
776 							Vector3 tangent = v0.cross(normal).normalized();
777 							Vector3 bitangent = tangent.cross(normal).normalized();
778 							Basis m3;
779 							m3.set_axis(0, tangent);
780 							m3.set_axis(1, bitangent);
781 							m3.set_axis(2, normal);
782 							p.velocity = m3.xform(p.velocity);
783 						}
784 					}
785 
786 					if (emission_colors.size() == pc) {
787 						p.base_color = emission_colors.get(random_idx);
788 					}
789 				} break;
790 				case EMISSION_SHAPE_MAX: { // Max value for validity check.
791 					break;
792 				}
793 			}
794 
795 			if (!local_coords) {
796 				p.velocity = velocity_xform.xform(p.velocity);
797 				p.transform = emission_xform * p.transform;
798 			}
799 
800 			if (flags[FLAG_DISABLE_Z]) {
801 				p.velocity.z = 0.0;
802 				p.transform.origin.z = 0.0;
803 			}
804 
805 		} else if (!p.active) {
806 			continue;
807 		} else if (p.time > p.lifetime) {
808 			p.active = false;
809 		} else {
810 
811 			uint32_t alt_seed = p.seed;
812 
813 			p.time += local_delta;
814 			p.custom[1] = p.time / lifetime;
815 
816 			float tex_linear_velocity = 0.0;
817 			if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
818 				tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->interpolate(p.custom[1]);
819 			}
820 
821 			float tex_orbit_velocity = 0.0;
822 			if (flags[FLAG_DISABLE_Z]) {
823 				if (curve_parameters[PARAM_ORBIT_VELOCITY].is_valid()) {
824 					tex_orbit_velocity = curve_parameters[PARAM_ORBIT_VELOCITY]->interpolate(p.custom[1]);
825 				}
826 			}
827 
828 			float tex_angular_velocity = 0.0;
829 			if (curve_parameters[PARAM_ANGULAR_VELOCITY].is_valid()) {
830 				tex_angular_velocity = curve_parameters[PARAM_ANGULAR_VELOCITY]->interpolate(p.custom[1]);
831 			}
832 
833 			float tex_linear_accel = 0.0;
834 			if (curve_parameters[PARAM_LINEAR_ACCEL].is_valid()) {
835 				tex_linear_accel = curve_parameters[PARAM_LINEAR_ACCEL]->interpolate(p.custom[1]);
836 			}
837 
838 			float tex_tangential_accel = 0.0;
839 			if (curve_parameters[PARAM_TANGENTIAL_ACCEL].is_valid()) {
840 				tex_tangential_accel = curve_parameters[PARAM_TANGENTIAL_ACCEL]->interpolate(p.custom[1]);
841 			}
842 
843 			float tex_radial_accel = 0.0;
844 			if (curve_parameters[PARAM_RADIAL_ACCEL].is_valid()) {
845 				tex_radial_accel = curve_parameters[PARAM_RADIAL_ACCEL]->interpolate(p.custom[1]);
846 			}
847 
848 			float tex_damping = 0.0;
849 			if (curve_parameters[PARAM_DAMPING].is_valid()) {
850 				tex_damping = curve_parameters[PARAM_DAMPING]->interpolate(p.custom[1]);
851 			}
852 
853 			float tex_angle = 0.0;
854 			if (curve_parameters[PARAM_ANGLE].is_valid()) {
855 				tex_angle = curve_parameters[PARAM_ANGLE]->interpolate(p.custom[1]);
856 			}
857 			float tex_anim_speed = 0.0;
858 			if (curve_parameters[PARAM_ANIM_SPEED].is_valid()) {
859 				tex_anim_speed = curve_parameters[PARAM_ANIM_SPEED]->interpolate(p.custom[1]);
860 			}
861 
862 			float tex_anim_offset = 0.0;
863 			if (curve_parameters[PARAM_ANIM_OFFSET].is_valid()) {
864 				tex_anim_offset = curve_parameters[PARAM_ANIM_OFFSET]->interpolate(p.custom[1]);
865 			}
866 
867 			Vector3 force = gravity;
868 			Vector3 position = p.transform.origin;
869 			if (flags[FLAG_DISABLE_Z]) {
870 				position.z = 0.0;
871 			}
872 			//apply linear acceleration
873 			force += p.velocity.length() > 0.0 ? p.velocity.normalized() * (parameters[PARAM_LINEAR_ACCEL] + tex_linear_accel) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_LINEAR_ACCEL]) : Vector3();
874 			//apply radial acceleration
875 			Vector3 org = emission_xform.origin;
876 			Vector3 diff = position - org;
877 			force += diff.length() > 0.0 ? diff.normalized() * (parameters[PARAM_RADIAL_ACCEL] + tex_radial_accel) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_RADIAL_ACCEL]) : Vector3();
878 			//apply tangential acceleration;
879 			if (flags[FLAG_DISABLE_Z]) {
880 
881 				Vector2 yx = Vector2(diff.y, diff.x);
882 				Vector2 yx2 = (yx * Vector2(-1.0, 1.0)).normalized();
883 				force += yx.length() > 0.0 ? Vector3(yx2.x, yx2.y, 0.0) * ((parameters[PARAM_TANGENTIAL_ACCEL] + tex_tangential_accel) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_TANGENTIAL_ACCEL])) : Vector3();
884 
885 			} else {
886 				Vector3 crossDiff = diff.normalized().cross(gravity.normalized());
887 				force += crossDiff.length() > 0.0 ? crossDiff.normalized() * ((parameters[PARAM_TANGENTIAL_ACCEL] + tex_tangential_accel) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_TANGENTIAL_ACCEL])) : Vector3();
888 			}
889 			//apply attractor forces
890 			p.velocity += force * local_delta;
891 			//orbit velocity
892 			if (flags[FLAG_DISABLE_Z]) {
893 				float orbit_amount = (parameters[PARAM_ORBIT_VELOCITY] + tex_orbit_velocity) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_ORBIT_VELOCITY]);
894 				if (orbit_amount != 0.0) {
895 					float ang = orbit_amount * local_delta * Math_PI * 2.0;
896 					// Not sure why the ParticlesMaterial code uses a clockwise rotation matrix,
897 					// but we use -ang here to reproduce its behavior.
898 					Transform2D rot = Transform2D(-ang, Vector2());
899 					Vector2 rotv = rot.basis_xform(Vector2(diff.x, diff.y));
900 					p.transform.origin -= Vector3(diff.x, diff.y, 0);
901 					p.transform.origin += Vector3(rotv.x, rotv.y, 0);
902 				}
903 			}
904 			if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
905 				p.velocity = p.velocity.normalized() * tex_linear_velocity;
906 			}
907 			if (parameters[PARAM_DAMPING] + tex_damping > 0.0) {
908 
909 				float v = p.velocity.length();
910 				float damp = (parameters[PARAM_DAMPING] + tex_damping) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_DAMPING]);
911 				v -= damp * local_delta;
912 				if (v < 0.0) {
913 					p.velocity = Vector3();
914 				} else {
915 					p.velocity = p.velocity.normalized() * v;
916 				}
917 			}
918 			float base_angle = (parameters[PARAM_ANGLE] + tex_angle) * Math::lerp(1.0f, p.angle_rand, randomness[PARAM_ANGLE]);
919 			base_angle += p.custom[1] * lifetime * (parameters[PARAM_ANGULAR_VELOCITY] + tex_angular_velocity) * Math::lerp(1.0f, rand_from_seed(alt_seed) * 2.0f - 1.0f, randomness[PARAM_ANGULAR_VELOCITY]);
920 			p.custom[0] = Math::deg2rad(base_angle); //angle
921 			p.custom[2] = (parameters[PARAM_ANIM_OFFSET] + tex_anim_offset) * Math::lerp(1.0f, p.anim_offset_rand, randomness[PARAM_ANIM_OFFSET]) + p.custom[1] * (parameters[PARAM_ANIM_SPEED] + tex_anim_speed) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_ANIM_SPEED]); //angle
922 		}
923 		//apply color
924 		//apply hue rotation
925 
926 		float tex_scale = 1.0;
927 		if (curve_parameters[PARAM_SCALE].is_valid()) {
928 			tex_scale = curve_parameters[PARAM_SCALE]->interpolate(p.custom[1]);
929 		}
930 
931 		float tex_hue_variation = 0.0;
932 		if (curve_parameters[PARAM_HUE_VARIATION].is_valid()) {
933 			tex_hue_variation = curve_parameters[PARAM_HUE_VARIATION]->interpolate(p.custom[1]);
934 		}
935 
936 		float hue_rot_angle = (parameters[PARAM_HUE_VARIATION] + tex_hue_variation) * Math_PI * 2.0 * Math::lerp(1.0f, p.hue_rot_rand * 2.0f - 1.0f, randomness[PARAM_HUE_VARIATION]);
937 		float hue_rot_c = Math::cos(hue_rot_angle);
938 		float hue_rot_s = Math::sin(hue_rot_angle);
939 
940 		Basis hue_rot_mat;
941 		{
942 			Basis mat1(0.299, 0.587, 0.114, 0.299, 0.587, 0.114, 0.299, 0.587, 0.114);
943 			Basis mat2(0.701, -0.587, -0.114, -0.299, 0.413, -0.114, -0.300, -0.588, 0.886);
944 			Basis mat3(0.168, 0.330, -0.497, -0.328, 0.035, 0.292, 1.250, -1.050, -0.203);
945 
946 			for (int j = 0; j < 3; j++) {
947 				hue_rot_mat[j] = mat1[j] + mat2[j] * hue_rot_c + mat3[j] * hue_rot_s;
948 			}
949 		}
950 
951 		if (color_ramp.is_valid()) {
952 			p.color = color_ramp->get_color_at_offset(p.custom[1]) * color;
953 		} else {
954 			p.color = color;
955 		}
956 
957 		Vector3 color_rgb = hue_rot_mat.xform_inv(Vector3(p.color.r, p.color.g, p.color.b));
958 		p.color.r = color_rgb.x;
959 		p.color.g = color_rgb.y;
960 		p.color.b = color_rgb.z;
961 
962 		p.color *= p.base_color;
963 
964 		if (flags[FLAG_DISABLE_Z]) {
965 
966 			if (flags[FLAG_ALIGN_Y_TO_VELOCITY]) {
967 				if (p.velocity.length() > 0.0) {
968 					p.transform.basis.set_axis(1, p.velocity.normalized());
969 				} else {
970 					p.transform.basis.set_axis(1, p.transform.basis.get_axis(1));
971 				}
972 				p.transform.basis.set_axis(0, p.transform.basis.get_axis(1).cross(p.transform.basis.get_axis(2)).normalized());
973 				p.transform.basis.set_axis(2, Vector3(0, 0, 1));
974 
975 			} else {
976 				p.transform.basis.set_axis(0, Vector3(Math::cos(p.custom[0]), -Math::sin(p.custom[0]), 0.0));
977 				p.transform.basis.set_axis(1, Vector3(Math::sin(p.custom[0]), Math::cos(p.custom[0]), 0.0));
978 				p.transform.basis.set_axis(2, Vector3(0, 0, 1));
979 			}
980 
981 		} else {
982 			//orient particle Y towards velocity
983 			if (flags[FLAG_ALIGN_Y_TO_VELOCITY]) {
984 				if (p.velocity.length() > 0.0) {
985 					p.transform.basis.set_axis(1, p.velocity.normalized());
986 				} else {
987 					p.transform.basis.set_axis(1, p.transform.basis.get_axis(1).normalized());
988 				}
989 				if (p.transform.basis.get_axis(1) == p.transform.basis.get_axis(0)) {
990 					p.transform.basis.set_axis(0, p.transform.basis.get_axis(1).cross(p.transform.basis.get_axis(2)).normalized());
991 					p.transform.basis.set_axis(2, p.transform.basis.get_axis(0).cross(p.transform.basis.get_axis(1)).normalized());
992 				} else {
993 					p.transform.basis.set_axis(2, p.transform.basis.get_axis(0).cross(p.transform.basis.get_axis(1)).normalized());
994 					p.transform.basis.set_axis(0, p.transform.basis.get_axis(1).cross(p.transform.basis.get_axis(2)).normalized());
995 				}
996 			} else {
997 				p.transform.basis.orthonormalize();
998 			}
999 
1000 			//turn particle by rotation in Y
1001 			if (flags[FLAG_ROTATE_Y]) {
1002 				Basis rot_y(Vector3(0, 1, 0), p.custom[0]);
1003 				p.transform.basis = p.transform.basis * rot_y;
1004 			}
1005 		}
1006 
1007 		//scale by scale
1008 		float base_scale = tex_scale * Math::lerp(parameters[PARAM_SCALE], 1.0f, p.scale_rand * randomness[PARAM_SCALE]);
1009 		if (base_scale < 0.000001) base_scale = 0.000001;
1010 
1011 		p.transform.basis.scale(Vector3(1, 1, 1) * base_scale);
1012 
1013 		if (flags[FLAG_DISABLE_Z]) {
1014 			p.velocity.z = 0.0;
1015 			p.transform.origin.z = 0.0;
1016 		}
1017 
1018 		p.transform.origin += p.velocity * local_delta;
1019 	}
1020 }
1021 
_update_particle_data_buffer()1022 void CPUParticles::_update_particle_data_buffer() {
1023 #ifndef NO_THREADS
1024 	update_mutex->lock();
1025 #endif
1026 
1027 	{
1028 
1029 		int pc = particles.size();
1030 
1031 		PoolVector<int>::Write ow;
1032 		int *order = NULL;
1033 
1034 		PoolVector<float>::Write w = particle_data.write();
1035 		PoolVector<Particle>::Read r = particles.read();
1036 		float *ptr = w.ptr();
1037 
1038 		if (draw_order != DRAW_ORDER_INDEX) {
1039 			ow = particle_order.write();
1040 			order = ow.ptr();
1041 
1042 			for (int i = 0; i < pc; i++) {
1043 				order[i] = i;
1044 			}
1045 			if (draw_order == DRAW_ORDER_LIFETIME) {
1046 				SortArray<int, SortLifetime> sorter;
1047 				sorter.compare.particles = r.ptr();
1048 				sorter.sort(order, pc);
1049 			} else if (draw_order == DRAW_ORDER_VIEW_DEPTH) {
1050 				Camera *c = get_viewport()->get_camera();
1051 				if (c) {
1052 					Vector3 dir = c->get_global_transform().basis.get_axis(2); //far away to close
1053 
1054 					if (local_coords) {
1055 
1056 						// will look different from Particles in editor as this is based on the camera in the scenetree
1057 						// and not the editor camera
1058 						dir = inv_emission_transform.xform(dir).normalized();
1059 					} else {
1060 						dir = dir.normalized();
1061 					}
1062 
1063 					SortArray<int, SortAxis> sorter;
1064 					sorter.compare.particles = r.ptr();
1065 					sorter.compare.axis = dir;
1066 					sorter.sort(order, pc);
1067 				}
1068 			}
1069 		}
1070 
1071 		for (int i = 0; i < pc; i++) {
1072 
1073 			int idx = order ? order[i] : i;
1074 
1075 			Transform t = r[idx].transform;
1076 
1077 			if (!local_coords) {
1078 				t = inv_emission_transform * t;
1079 			}
1080 
1081 			if (r[idx].active) {
1082 				ptr[0] = t.basis.elements[0][0];
1083 				ptr[1] = t.basis.elements[0][1];
1084 				ptr[2] = t.basis.elements[0][2];
1085 				ptr[3] = t.origin.x;
1086 				ptr[4] = t.basis.elements[1][0];
1087 				ptr[5] = t.basis.elements[1][1];
1088 				ptr[6] = t.basis.elements[1][2];
1089 				ptr[7] = t.origin.y;
1090 				ptr[8] = t.basis.elements[2][0];
1091 				ptr[9] = t.basis.elements[2][1];
1092 				ptr[10] = t.basis.elements[2][2];
1093 				ptr[11] = t.origin.z;
1094 			} else {
1095 				zeromem(ptr, sizeof(float) * 12);
1096 			}
1097 
1098 			Color c = r[idx].color;
1099 			uint8_t *data8 = (uint8_t *)&ptr[12];
1100 			data8[0] = CLAMP(c.r * 255.0, 0, 255);
1101 			data8[1] = CLAMP(c.g * 255.0, 0, 255);
1102 			data8[2] = CLAMP(c.b * 255.0, 0, 255);
1103 			data8[3] = CLAMP(c.a * 255.0, 0, 255);
1104 
1105 			ptr[13] = r[idx].custom[0];
1106 			ptr[14] = r[idx].custom[1];
1107 			ptr[15] = r[idx].custom[2];
1108 			ptr[16] = r[idx].custom[3];
1109 
1110 			ptr += 17;
1111 		}
1112 
1113 		can_update = true;
1114 	}
1115 
1116 #ifndef NO_THREADS
1117 	update_mutex->unlock();
1118 #endif
1119 }
1120 
_set_redraw(bool p_redraw)1121 void CPUParticles::_set_redraw(bool p_redraw) {
1122 	if (redraw == p_redraw)
1123 		return;
1124 	redraw = p_redraw;
1125 #ifndef NO_THREADS
1126 	update_mutex->lock();
1127 #endif
1128 	if (redraw) {
1129 		VS::get_singleton()->connect("frame_pre_draw", this, "_update_render_thread");
1130 		VS::get_singleton()->instance_geometry_set_flag(get_instance(), VS::INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE, true);
1131 		VS::get_singleton()->multimesh_set_visible_instances(multimesh, -1);
1132 	} else {
1133 		if (VS::get_singleton()->is_connected("frame_pre_draw", this, "_update_render_thread")) {
1134 			VS::get_singleton()->disconnect("frame_pre_draw", this, "_update_render_thread");
1135 		}
1136 		VS::get_singleton()->instance_geometry_set_flag(get_instance(), VS::INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE, false);
1137 		VS::get_singleton()->multimesh_set_visible_instances(multimesh, 0);
1138 	}
1139 #ifndef NO_THREADS
1140 	update_mutex->unlock();
1141 #endif
1142 }
1143 
_update_render_thread()1144 void CPUParticles::_update_render_thread() {
1145 
1146 #ifndef NO_THREADS
1147 	update_mutex->lock();
1148 #endif
1149 	if (can_update) {
1150 		VS::get_singleton()->multimesh_set_as_bulk_array(multimesh, particle_data);
1151 		can_update = false; //wait for next time
1152 	}
1153 
1154 #ifndef NO_THREADS
1155 	update_mutex->unlock();
1156 #endif
1157 }
1158 
_notification(int p_what)1159 void CPUParticles::_notification(int p_what) {
1160 
1161 	if (p_what == NOTIFICATION_ENTER_TREE) {
1162 		set_process_internal(emitting);
1163 
1164 		// first update before rendering to avoid one frame delay after emitting starts
1165 		if (emitting && (time == 0))
1166 			_update_internal();
1167 	}
1168 
1169 	if (p_what == NOTIFICATION_EXIT_TREE) {
1170 		_set_redraw(false);
1171 	}
1172 
1173 	if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
1174 		// first update before rendering to avoid one frame delay after emitting starts
1175 		if (emitting && (time == 0))
1176 			_update_internal();
1177 	}
1178 
1179 	if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
1180 		_update_internal();
1181 	}
1182 
1183 	if (p_what == NOTIFICATION_TRANSFORM_CHANGED) {
1184 
1185 		inv_emission_transform = get_global_transform().affine_inverse();
1186 
1187 		if (!local_coords) {
1188 
1189 			int pc = particles.size();
1190 
1191 			PoolVector<float>::Write w = particle_data.write();
1192 			PoolVector<Particle>::Read r = particles.read();
1193 			float *ptr = w.ptr();
1194 
1195 			for (int i = 0; i < pc; i++) {
1196 
1197 				Transform t = inv_emission_transform * r[i].transform;
1198 
1199 				if (r[i].active) {
1200 					ptr[0] = t.basis.elements[0][0];
1201 					ptr[1] = t.basis.elements[0][1];
1202 					ptr[2] = t.basis.elements[0][2];
1203 					ptr[3] = t.origin.x;
1204 					ptr[4] = t.basis.elements[1][0];
1205 					ptr[5] = t.basis.elements[1][1];
1206 					ptr[6] = t.basis.elements[1][2];
1207 					ptr[7] = t.origin.y;
1208 					ptr[8] = t.basis.elements[2][0];
1209 					ptr[9] = t.basis.elements[2][1];
1210 					ptr[10] = t.basis.elements[2][2];
1211 					ptr[11] = t.origin.z;
1212 				} else {
1213 					zeromem(ptr, sizeof(float) * 12);
1214 				}
1215 
1216 				ptr += 17;
1217 			}
1218 
1219 			can_update = true;
1220 		}
1221 	}
1222 }
1223 
convert_from_particles(Node * p_particles)1224 void CPUParticles::convert_from_particles(Node *p_particles) {
1225 
1226 	Particles *particles = Object::cast_to<Particles>(p_particles);
1227 	ERR_FAIL_COND_MSG(!particles, "Only Particles nodes can be converted to CPUParticles.");
1228 
1229 	set_emitting(particles->is_emitting());
1230 	set_amount(particles->get_amount());
1231 	set_lifetime(particles->get_lifetime());
1232 	set_one_shot(particles->get_one_shot());
1233 	set_pre_process_time(particles->get_pre_process_time());
1234 	set_explosiveness_ratio(particles->get_explosiveness_ratio());
1235 	set_randomness_ratio(particles->get_randomness_ratio());
1236 	set_use_local_coordinates(particles->get_use_local_coordinates());
1237 	set_fixed_fps(particles->get_fixed_fps());
1238 	set_fractional_delta(particles->get_fractional_delta());
1239 	set_speed_scale(particles->get_speed_scale());
1240 	set_draw_order(DrawOrder(particles->get_draw_order()));
1241 	set_mesh(particles->get_draw_pass_mesh(0));
1242 
1243 	Ref<ParticlesMaterial> material = particles->get_process_material();
1244 	if (material.is_null())
1245 		return;
1246 
1247 	set_direction(material->get_direction());
1248 	set_spread(material->get_spread());
1249 	set_flatness(material->get_flatness());
1250 
1251 	set_color(material->get_color());
1252 
1253 	Ref<GradientTexture> gt = material->get_color_ramp();
1254 	if (gt.is_valid()) {
1255 		set_color_ramp(gt->get_gradient());
1256 	}
1257 
1258 	set_particle_flag(FLAG_ALIGN_Y_TO_VELOCITY, material->get_flag(ParticlesMaterial::FLAG_ALIGN_Y_TO_VELOCITY));
1259 	set_particle_flag(FLAG_ROTATE_Y, material->get_flag(ParticlesMaterial::FLAG_ROTATE_Y));
1260 	set_particle_flag(FLAG_DISABLE_Z, material->get_flag(ParticlesMaterial::FLAG_DISABLE_Z));
1261 
1262 	set_emission_shape(EmissionShape(material->get_emission_shape()));
1263 	set_emission_sphere_radius(material->get_emission_sphere_radius());
1264 	set_emission_box_extents(material->get_emission_box_extents());
1265 
1266 	set_gravity(material->get_gravity());
1267 	set_lifetime_randomness(material->get_lifetime_randomness());
1268 
1269 #define CONVERT_PARAM(m_param)                                                            \
1270 	set_param(m_param, material->get_param(ParticlesMaterial::m_param));                  \
1271 	{                                                                                     \
1272 		Ref<CurveTexture> ctex = material->get_param_texture(ParticlesMaterial::m_param); \
1273 		if (ctex.is_valid()) set_param_curve(m_param, ctex->get_curve());                 \
1274 	}                                                                                     \
1275 	set_param_randomness(m_param, material->get_param_randomness(ParticlesMaterial::m_param));
1276 
1277 	CONVERT_PARAM(PARAM_INITIAL_LINEAR_VELOCITY);
1278 	CONVERT_PARAM(PARAM_ANGULAR_VELOCITY);
1279 	CONVERT_PARAM(PARAM_ORBIT_VELOCITY);
1280 	CONVERT_PARAM(PARAM_LINEAR_ACCEL);
1281 	CONVERT_PARAM(PARAM_RADIAL_ACCEL);
1282 	CONVERT_PARAM(PARAM_TANGENTIAL_ACCEL);
1283 	CONVERT_PARAM(PARAM_DAMPING);
1284 	CONVERT_PARAM(PARAM_ANGLE);
1285 	CONVERT_PARAM(PARAM_SCALE);
1286 	CONVERT_PARAM(PARAM_HUE_VARIATION);
1287 	CONVERT_PARAM(PARAM_ANIM_SPEED);
1288 	CONVERT_PARAM(PARAM_ANIM_OFFSET);
1289 
1290 #undef CONVERT_PARAM
1291 }
1292 
_bind_methods()1293 void CPUParticles::_bind_methods() {
1294 
1295 	ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &CPUParticles::set_emitting);
1296 	ClassDB::bind_method(D_METHOD("set_amount", "amount"), &CPUParticles::set_amount);
1297 	ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &CPUParticles::set_lifetime);
1298 	ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &CPUParticles::set_one_shot);
1299 	ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &CPUParticles::set_pre_process_time);
1300 	ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &CPUParticles::set_explosiveness_ratio);
1301 	ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &CPUParticles::set_randomness_ratio);
1302 	ClassDB::bind_method(D_METHOD("set_lifetime_randomness", "random"), &CPUParticles::set_lifetime_randomness);
1303 	ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &CPUParticles::set_use_local_coordinates);
1304 	ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &CPUParticles::set_fixed_fps);
1305 	ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &CPUParticles::set_fractional_delta);
1306 	ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &CPUParticles::set_speed_scale);
1307 
1308 	ClassDB::bind_method(D_METHOD("is_emitting"), &CPUParticles::is_emitting);
1309 	ClassDB::bind_method(D_METHOD("get_amount"), &CPUParticles::get_amount);
1310 	ClassDB::bind_method(D_METHOD("get_lifetime"), &CPUParticles::get_lifetime);
1311 	ClassDB::bind_method(D_METHOD("get_one_shot"), &CPUParticles::get_one_shot);
1312 	ClassDB::bind_method(D_METHOD("get_pre_process_time"), &CPUParticles::get_pre_process_time);
1313 	ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &CPUParticles::get_explosiveness_ratio);
1314 	ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &CPUParticles::get_randomness_ratio);
1315 	ClassDB::bind_method(D_METHOD("get_lifetime_randomness"), &CPUParticles::get_lifetime_randomness);
1316 	ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &CPUParticles::get_use_local_coordinates);
1317 	ClassDB::bind_method(D_METHOD("get_fixed_fps"), &CPUParticles::get_fixed_fps);
1318 	ClassDB::bind_method(D_METHOD("get_fractional_delta"), &CPUParticles::get_fractional_delta);
1319 	ClassDB::bind_method(D_METHOD("get_speed_scale"), &CPUParticles::get_speed_scale);
1320 
1321 	ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &CPUParticles::set_draw_order);
1322 
1323 	ClassDB::bind_method(D_METHOD("get_draw_order"), &CPUParticles::get_draw_order);
1324 
1325 	ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &CPUParticles::set_mesh);
1326 	ClassDB::bind_method(D_METHOD("get_mesh"), &CPUParticles::get_mesh);
1327 
1328 	ClassDB::bind_method(D_METHOD("restart"), &CPUParticles::restart);
1329 
1330 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting"), "set_emitting", "is_emitting");
1331 	ADD_PROPERTY(PropertyInfo(Variant::INT, "amount", PROPERTY_HINT_EXP_RANGE, "1,1000000,1"), "set_amount", "get_amount");
1332 	ADD_GROUP("Time", "");
1333 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "lifetime", PROPERTY_HINT_EXP_RANGE, "0.01,600.0,0.01,or_greater"), "set_lifetime", "get_lifetime");
1334 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
1335 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "preprocess", PROPERTY_HINT_EXP_RANGE, "0.00,600.0,0.01"), "set_pre_process_time", "get_pre_process_time");
1336 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
1337 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
1338 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
1339 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "lifetime_randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_lifetime_randomness", "get_lifetime_randomness");
1340 	ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1"), "set_fixed_fps", "get_fixed_fps");
1341 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
1342 	ADD_GROUP("Drawing", "");
1343 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
1344 	ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime,View Depth"), "set_draw_order", "get_draw_order");
1345 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
1346 
1347 	BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
1348 	BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
1349 	BIND_ENUM_CONSTANT(DRAW_ORDER_VIEW_DEPTH);
1350 
1351 	////////////////////////////////
1352 
1353 	ClassDB::bind_method(D_METHOD("set_direction", "direction"), &CPUParticles::set_direction);
1354 	ClassDB::bind_method(D_METHOD("get_direction"), &CPUParticles::get_direction);
1355 
1356 	ClassDB::bind_method(D_METHOD("set_spread", "degrees"), &CPUParticles::set_spread);
1357 	ClassDB::bind_method(D_METHOD("get_spread"), &CPUParticles::get_spread);
1358 
1359 	ClassDB::bind_method(D_METHOD("set_flatness", "amount"), &CPUParticles::set_flatness);
1360 	ClassDB::bind_method(D_METHOD("get_flatness"), &CPUParticles::get_flatness);
1361 
1362 	ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &CPUParticles::set_param);
1363 	ClassDB::bind_method(D_METHOD("get_param", "param"), &CPUParticles::get_param);
1364 
1365 	ClassDB::bind_method(D_METHOD("set_param_randomness", "param", "randomness"), &CPUParticles::set_param_randomness);
1366 	ClassDB::bind_method(D_METHOD("get_param_randomness", "param"), &CPUParticles::get_param_randomness);
1367 
1368 	ClassDB::bind_method(D_METHOD("set_param_curve", "param", "curve"), &CPUParticles::set_param_curve);
1369 	ClassDB::bind_method(D_METHOD("get_param_curve", "param"), &CPUParticles::get_param_curve);
1370 
1371 	ClassDB::bind_method(D_METHOD("set_color", "color"), &CPUParticles::set_color);
1372 	ClassDB::bind_method(D_METHOD("get_color"), &CPUParticles::get_color);
1373 
1374 	ClassDB::bind_method(D_METHOD("set_color_ramp", "ramp"), &CPUParticles::set_color_ramp);
1375 	ClassDB::bind_method(D_METHOD("get_color_ramp"), &CPUParticles::get_color_ramp);
1376 
1377 	ClassDB::bind_method(D_METHOD("set_particle_flag", "flag", "enable"), &CPUParticles::set_particle_flag);
1378 	ClassDB::bind_method(D_METHOD("get_particle_flag", "flag"), &CPUParticles::get_particle_flag);
1379 
1380 	ClassDB::bind_method(D_METHOD("set_emission_shape", "shape"), &CPUParticles::set_emission_shape);
1381 	ClassDB::bind_method(D_METHOD("get_emission_shape"), &CPUParticles::get_emission_shape);
1382 
1383 	ClassDB::bind_method(D_METHOD("set_emission_sphere_radius", "radius"), &CPUParticles::set_emission_sphere_radius);
1384 	ClassDB::bind_method(D_METHOD("get_emission_sphere_radius"), &CPUParticles::get_emission_sphere_radius);
1385 
1386 	ClassDB::bind_method(D_METHOD("set_emission_box_extents", "extents"), &CPUParticles::set_emission_box_extents);
1387 	ClassDB::bind_method(D_METHOD("get_emission_box_extents"), &CPUParticles::get_emission_box_extents);
1388 
1389 	ClassDB::bind_method(D_METHOD("set_emission_points", "array"), &CPUParticles::set_emission_points);
1390 	ClassDB::bind_method(D_METHOD("get_emission_points"), &CPUParticles::get_emission_points);
1391 
1392 	ClassDB::bind_method(D_METHOD("set_emission_normals", "array"), &CPUParticles::set_emission_normals);
1393 	ClassDB::bind_method(D_METHOD("get_emission_normals"), &CPUParticles::get_emission_normals);
1394 
1395 	ClassDB::bind_method(D_METHOD("set_emission_colors", "array"), &CPUParticles::set_emission_colors);
1396 	ClassDB::bind_method(D_METHOD("get_emission_colors"), &CPUParticles::get_emission_colors);
1397 
1398 	ClassDB::bind_method(D_METHOD("get_gravity"), &CPUParticles::get_gravity);
1399 	ClassDB::bind_method(D_METHOD("set_gravity", "accel_vec"), &CPUParticles::set_gravity);
1400 
1401 	ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &CPUParticles::convert_from_particles);
1402 
1403 	ClassDB::bind_method(D_METHOD("_update_render_thread"), &CPUParticles::_update_render_thread);
1404 
1405 	ADD_GROUP("Emission Shape", "emission_");
1406 	ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_shape", PROPERTY_HINT_ENUM, "Point,Sphere,Box,Points,Directed Points"), "set_emission_shape", "get_emission_shape");
1407 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "emission_sphere_radius", PROPERTY_HINT_RANGE, "0.01,128,0.01"), "set_emission_sphere_radius", "get_emission_sphere_radius");
1408 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "emission_box_extents"), "set_emission_box_extents", "get_emission_box_extents");
1409 	ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR3_ARRAY, "emission_points"), "set_emission_points", "get_emission_points");
1410 	ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR3_ARRAY, "emission_normals"), "set_emission_normals", "get_emission_normals");
1411 	ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "emission_colors"), "set_emission_colors", "get_emission_colors");
1412 	ADD_GROUP("Flags", "flag_");
1413 	ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flag_align_y"), "set_particle_flag", "get_particle_flag", FLAG_ALIGN_Y_TO_VELOCITY);
1414 	ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flag_rotate_y"), "set_particle_flag", "get_particle_flag", FLAG_ROTATE_Y);
1415 	ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flag_disable_z"), "set_particle_flag", "get_particle_flag", FLAG_DISABLE_Z);
1416 	ADD_GROUP("Direction", "");
1417 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "direction"), "set_direction", "get_direction");
1418 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "spread", PROPERTY_HINT_RANGE, "0,180,0.01"), "set_spread", "get_spread");
1419 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "flatness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_flatness", "get_flatness");
1420 	ADD_GROUP("Gravity", "");
1421 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "gravity"), "set_gravity", "get_gravity");
1422 	ADD_GROUP("Initial Velocity", "initial_");
1423 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "initial_velocity", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param", "get_param", PARAM_INITIAL_LINEAR_VELOCITY);
1424 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "initial_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_INITIAL_LINEAR_VELOCITY);
1425 	ADD_GROUP("Angular Velocity", "angular_");
1426 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_velocity", PROPERTY_HINT_RANGE, "-720,720,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_ANGULAR_VELOCITY);
1427 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANGULAR_VELOCITY);
1428 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angular_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGULAR_VELOCITY);
1429 	ADD_GROUP("Orbit Velocity", "orbit_");
1430 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "orbit_velocity", PROPERTY_HINT_RANGE, "-1000,1000,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_ORBIT_VELOCITY);
1431 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "orbit_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ORBIT_VELOCITY);
1432 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "orbit_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ORBIT_VELOCITY);
1433 	ADD_GROUP("Linear Accel", "linear_");
1434 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_accel", PROPERTY_HINT_RANGE, "-100,100,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_LINEAR_ACCEL);
1435 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_LINEAR_ACCEL);
1436 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "linear_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_LINEAR_ACCEL);
1437 	ADD_GROUP("Radial Accel", "radial_");
1438 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "radial_accel", PROPERTY_HINT_RANGE, "-100,100,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_RADIAL_ACCEL);
1439 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "radial_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_RADIAL_ACCEL);
1440 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "radial_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_RADIAL_ACCEL);
1441 	ADD_GROUP("Tangential Accel", "tangential_");
1442 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "tangential_accel", PROPERTY_HINT_RANGE, "-100,100,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_TANGENTIAL_ACCEL);
1443 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "tangential_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_TANGENTIAL_ACCEL);
1444 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "tangential_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_TANGENTIAL_ACCEL);
1445 	ADD_GROUP("Damping", "");
1446 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "damping", PROPERTY_HINT_RANGE, "0,100,0.01"), "set_param", "get_param", PARAM_DAMPING);
1447 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "damping_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_DAMPING);
1448 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "damping_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_DAMPING);
1449 	ADD_GROUP("Angle", "");
1450 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angle", PROPERTY_HINT_RANGE, "-720,720,0.1,or_lesser,or_greater"), "set_param", "get_param", PARAM_ANGLE);
1451 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angle_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANGLE);
1452 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angle_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGLE);
1453 	ADD_GROUP("Scale", "");
1454 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale_amount", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param", "get_param", PARAM_SCALE);
1455 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale_amount_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_SCALE);
1456 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_amount_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_SCALE);
1457 	ADD_GROUP("Color", "");
1458 	ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
1459 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");
1460 
1461 	ADD_GROUP("Hue Variation", "hue_");
1462 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "hue_variation", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param", "get_param", PARAM_HUE_VARIATION);
1463 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "hue_variation_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_HUE_VARIATION);
1464 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "hue_variation_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_HUE_VARIATION);
1465 	ADD_GROUP("Animation", "anim_");
1466 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_speed", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater"), "set_param", "get_param", PARAM_ANIM_SPEED);
1467 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_speed_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANIM_SPEED);
1468 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_speed_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_SPEED);
1469 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_offset", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param", "get_param", PARAM_ANIM_OFFSET);
1470 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_offset_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANIM_OFFSET);
1471 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_offset_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_OFFSET);
1472 
1473 	BIND_ENUM_CONSTANT(PARAM_INITIAL_LINEAR_VELOCITY);
1474 	BIND_ENUM_CONSTANT(PARAM_ANGULAR_VELOCITY);
1475 	BIND_ENUM_CONSTANT(PARAM_ORBIT_VELOCITY);
1476 	BIND_ENUM_CONSTANT(PARAM_LINEAR_ACCEL);
1477 	BIND_ENUM_CONSTANT(PARAM_RADIAL_ACCEL);
1478 	BIND_ENUM_CONSTANT(PARAM_TANGENTIAL_ACCEL);
1479 	BIND_ENUM_CONSTANT(PARAM_DAMPING);
1480 	BIND_ENUM_CONSTANT(PARAM_ANGLE);
1481 	BIND_ENUM_CONSTANT(PARAM_SCALE);
1482 	BIND_ENUM_CONSTANT(PARAM_HUE_VARIATION);
1483 	BIND_ENUM_CONSTANT(PARAM_ANIM_SPEED);
1484 	BIND_ENUM_CONSTANT(PARAM_ANIM_OFFSET);
1485 	BIND_ENUM_CONSTANT(PARAM_MAX);
1486 
1487 	BIND_ENUM_CONSTANT(FLAG_ALIGN_Y_TO_VELOCITY);
1488 	BIND_ENUM_CONSTANT(FLAG_ROTATE_Y);
1489 	BIND_ENUM_CONSTANT(FLAG_DISABLE_Z);
1490 	BIND_ENUM_CONSTANT(FLAG_MAX);
1491 
1492 	BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINT);
1493 	BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE);
1494 	BIND_ENUM_CONSTANT(EMISSION_SHAPE_BOX);
1495 	BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINTS);
1496 	BIND_ENUM_CONSTANT(EMISSION_SHAPE_DIRECTED_POINTS);
1497 	BIND_ENUM_CONSTANT(EMISSION_SHAPE_MAX);
1498 }
1499 
CPUParticles()1500 CPUParticles::CPUParticles() {
1501 
1502 	time = 0;
1503 	inactive_time = 0;
1504 	frame_remainder = 0;
1505 	cycle = 0;
1506 	redraw = false;
1507 	emitting = false;
1508 
1509 	set_notify_transform(true);
1510 
1511 	multimesh = VisualServer::get_singleton()->multimesh_create();
1512 	VisualServer::get_singleton()->multimesh_set_visible_instances(multimesh, 0);
1513 	set_base(multimesh);
1514 
1515 	set_emitting(true);
1516 	set_one_shot(false);
1517 	set_amount(8);
1518 	set_lifetime(1);
1519 	set_fixed_fps(0);
1520 	set_fractional_delta(true);
1521 	set_pre_process_time(0);
1522 	set_explosiveness_ratio(0);
1523 	set_randomness_ratio(0);
1524 	set_lifetime_randomness(0);
1525 	set_use_local_coordinates(true);
1526 
1527 	set_draw_order(DRAW_ORDER_INDEX);
1528 	set_speed_scale(1);
1529 
1530 	set_direction(Vector3(1, 0, 0));
1531 	set_spread(45);
1532 	set_flatness(0);
1533 	set_param(PARAM_INITIAL_LINEAR_VELOCITY, 0);
1534 	set_param(PARAM_ANGULAR_VELOCITY, 0);
1535 	set_param(PARAM_ORBIT_VELOCITY, 0);
1536 	set_param(PARAM_LINEAR_ACCEL, 0);
1537 	set_param(PARAM_RADIAL_ACCEL, 0);
1538 	set_param(PARAM_TANGENTIAL_ACCEL, 0);
1539 	set_param(PARAM_DAMPING, 0);
1540 	set_param(PARAM_ANGLE, 0);
1541 	set_param(PARAM_SCALE, 1);
1542 	set_param(PARAM_HUE_VARIATION, 0);
1543 	set_param(PARAM_ANIM_SPEED, 0);
1544 	set_param(PARAM_ANIM_OFFSET, 0);
1545 	set_emission_shape(EMISSION_SHAPE_POINT);
1546 	set_emission_sphere_radius(1);
1547 	set_emission_box_extents(Vector3(1, 1, 1));
1548 
1549 	set_gravity(Vector3(0, -9.8, 0));
1550 
1551 	for (int i = 0; i < PARAM_MAX; i++) {
1552 		set_param_randomness(Parameter(i), 0);
1553 	}
1554 
1555 	for (int i = 0; i < FLAG_MAX; i++) {
1556 		flags[i] = false;
1557 	}
1558 
1559 	can_update = false;
1560 
1561 	set_color(Color(1, 1, 1, 1));
1562 
1563 #ifndef NO_THREADS
1564 	update_mutex = Mutex::create();
1565 #endif
1566 }
1567 
~CPUParticles()1568 CPUParticles::~CPUParticles() {
1569 	VS::get_singleton()->free(multimesh);
1570 
1571 #ifndef NO_THREADS
1572 	memdelete(update_mutex);
1573 #endif
1574 }
1575