1 /*************************************************************************/
2 /*  cpu_particles_2d.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_2d.h"
32 #include "core/core_string_names.h"
33 #include "scene/2d/canvas_item.h"
34 #include "scene/2d/particles_2d.h"
35 #include "scene/resources/particles_material.h"
36 #include "servers/visual_server.h"
37 
set_emitting(bool p_emitting)38 void CPUParticles2D::set_emitting(bool p_emitting) {
39 
40 	if (emitting == p_emitting)
41 		return;
42 
43 	emitting = p_emitting;
44 	if (emitting)
45 		set_process_internal(true);
46 }
47 
set_amount(int p_amount)48 void CPUParticles2D::set_amount(int p_amount) {
49 
50 	ERR_FAIL_COND_MSG(p_amount < 1, "Amount of particles must be greater than 0.");
51 
52 	particles.resize(p_amount);
53 	{
54 		PoolVector<Particle>::Write w = particles.write();
55 
56 		// each particle must be set to false
57 		// zeroing the data also prevents uninitialized memory being sent to GPU
58 		zeromem(static_cast<void *>(&w[0]), p_amount * sizeof(Particle));
59 		// cast to prevent compiler warning .. note this relies on Particle not containing any complex types.
60 		// an alternative is to use some zero method per item but the generated code will be far less efficient.
61 	}
62 
63 	particle_data.resize((8 + 4 + 1) * p_amount);
64 	VS::get_singleton()->multimesh_allocate(multimesh, p_amount, VS::MULTIMESH_TRANSFORM_2D, VS::MULTIMESH_COLOR_8BIT, VS::MULTIMESH_CUSTOM_DATA_FLOAT);
65 
66 	particle_order.resize(p_amount);
67 }
set_lifetime(float p_lifetime)68 void CPUParticles2D::set_lifetime(float p_lifetime) {
69 
70 	ERR_FAIL_COND_MSG(p_lifetime <= 0, "Particles lifetime must be greater than 0.");
71 	lifetime = p_lifetime;
72 }
73 
set_one_shot(bool p_one_shot)74 void CPUParticles2D::set_one_shot(bool p_one_shot) {
75 
76 	one_shot = p_one_shot;
77 }
78 
set_pre_process_time(float p_time)79 void CPUParticles2D::set_pre_process_time(float p_time) {
80 
81 	pre_process_time = p_time;
82 }
set_explosiveness_ratio(float p_ratio)83 void CPUParticles2D::set_explosiveness_ratio(float p_ratio) {
84 
85 	explosiveness_ratio = p_ratio;
86 }
set_randomness_ratio(float p_ratio)87 void CPUParticles2D::set_randomness_ratio(float p_ratio) {
88 
89 	randomness_ratio = p_ratio;
90 }
set_lifetime_randomness(float p_random)91 void CPUParticles2D::set_lifetime_randomness(float p_random) {
92 
93 	lifetime_randomness = p_random;
94 }
set_use_local_coordinates(bool p_enable)95 void CPUParticles2D::set_use_local_coordinates(bool p_enable) {
96 
97 	local_coords = p_enable;
98 	set_notify_transform(!p_enable);
99 }
100 
set_speed_scale(float p_scale)101 void CPUParticles2D::set_speed_scale(float p_scale) {
102 
103 	speed_scale = p_scale;
104 }
105 
is_emitting() const106 bool CPUParticles2D::is_emitting() const {
107 
108 	return emitting;
109 }
get_amount() const110 int CPUParticles2D::get_amount() const {
111 
112 	return particles.size();
113 }
get_lifetime() const114 float CPUParticles2D::get_lifetime() const {
115 
116 	return lifetime;
117 }
get_one_shot() const118 bool CPUParticles2D::get_one_shot() const {
119 
120 	return one_shot;
121 }
122 
get_pre_process_time() const123 float CPUParticles2D::get_pre_process_time() const {
124 
125 	return pre_process_time;
126 }
get_explosiveness_ratio() const127 float CPUParticles2D::get_explosiveness_ratio() const {
128 
129 	return explosiveness_ratio;
130 }
get_randomness_ratio() const131 float CPUParticles2D::get_randomness_ratio() const {
132 
133 	return randomness_ratio;
134 }
get_lifetime_randomness() const135 float CPUParticles2D::get_lifetime_randomness() const {
136 
137 	return lifetime_randomness;
138 }
139 
get_use_local_coordinates() const140 bool CPUParticles2D::get_use_local_coordinates() const {
141 
142 	return local_coords;
143 }
144 
get_speed_scale() const145 float CPUParticles2D::get_speed_scale() const {
146 
147 	return speed_scale;
148 }
149 
set_draw_order(DrawOrder p_order)150 void CPUParticles2D::set_draw_order(DrawOrder p_order) {
151 
152 	draw_order = p_order;
153 }
154 
get_draw_order() const155 CPUParticles2D::DrawOrder CPUParticles2D::get_draw_order() const {
156 
157 	return draw_order;
158 }
159 
_update_mesh_texture()160 void CPUParticles2D::_update_mesh_texture() {
161 
162 	Size2 tex_size;
163 	if (texture.is_valid()) {
164 		tex_size = texture->get_size();
165 	} else {
166 		tex_size = Size2(1, 1);
167 	}
168 	PoolVector<Vector2> vertices;
169 	vertices.push_back(-tex_size * 0.5);
170 	vertices.push_back(-tex_size * 0.5 + Vector2(tex_size.x, 0));
171 	vertices.push_back(-tex_size * 0.5 + Vector2(tex_size.x, tex_size.y));
172 	vertices.push_back(-tex_size * 0.5 + Vector2(0, tex_size.y));
173 	PoolVector<Vector2> uvs;
174 	AtlasTexture *atlas_texure = Object::cast_to<AtlasTexture>(*texture);
175 	if (atlas_texure && atlas_texure->get_atlas().is_valid()) {
176 		Rect2 region_rect = atlas_texure->get_region();
177 		Size2 atlas_size = atlas_texure->get_atlas()->get_size();
178 		uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, region_rect.position.y / atlas_size.y));
179 		uvs.push_back(Vector2((region_rect.position.x + region_rect.size.x) / atlas_size.x, region_rect.position.y / atlas_size.y));
180 		uvs.push_back(Vector2((region_rect.position.x + region_rect.size.x) / atlas_size.x, (region_rect.position.y + region_rect.size.y) / atlas_size.y));
181 		uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, (region_rect.position.y + region_rect.size.y) / atlas_size.y));
182 	} else {
183 		uvs.push_back(Vector2(0, 0));
184 		uvs.push_back(Vector2(1, 0));
185 		uvs.push_back(Vector2(1, 1));
186 		uvs.push_back(Vector2(0, 1));
187 	}
188 	PoolVector<Color> colors;
189 	colors.push_back(Color(1, 1, 1, 1));
190 	colors.push_back(Color(1, 1, 1, 1));
191 	colors.push_back(Color(1, 1, 1, 1));
192 	colors.push_back(Color(1, 1, 1, 1));
193 	PoolVector<int> indices;
194 	indices.push_back(0);
195 	indices.push_back(1);
196 	indices.push_back(2);
197 	indices.push_back(2);
198 	indices.push_back(3);
199 	indices.push_back(0);
200 
201 	Array arr;
202 	arr.resize(VS::ARRAY_MAX);
203 	arr[VS::ARRAY_VERTEX] = vertices;
204 	arr[VS::ARRAY_TEX_UV] = uvs;
205 	arr[VS::ARRAY_COLOR] = colors;
206 	arr[VS::ARRAY_INDEX] = indices;
207 
208 	VS::get_singleton()->mesh_clear(mesh);
209 	VS::get_singleton()->mesh_add_surface_from_arrays(mesh, VS::PRIMITIVE_TRIANGLES, arr);
210 }
211 
set_texture(const Ref<Texture> & p_texture)212 void CPUParticles2D::set_texture(const Ref<Texture> &p_texture) {
213 	if (p_texture == texture)
214 		return;
215 
216 	if (texture.is_valid())
217 		texture->disconnect(CoreStringNames::get_singleton()->changed, this, "_texture_changed");
218 
219 	texture = p_texture;
220 
221 	if (texture.is_valid())
222 		texture->connect(CoreStringNames::get_singleton()->changed, this, "_texture_changed");
223 
224 	update();
225 	_update_mesh_texture();
226 }
227 
_texture_changed()228 void CPUParticles2D::_texture_changed() {
229 
230 	if (texture.is_valid()) {
231 		update();
232 		_update_mesh_texture();
233 	}
234 }
235 
get_texture() const236 Ref<Texture> CPUParticles2D::get_texture() const {
237 
238 	return texture;
239 }
240 
set_normalmap(const Ref<Texture> & p_normalmap)241 void CPUParticles2D::set_normalmap(const Ref<Texture> &p_normalmap) {
242 
243 	normalmap = p_normalmap;
244 	update();
245 }
246 
get_normalmap() const247 Ref<Texture> CPUParticles2D::get_normalmap() const {
248 
249 	return normalmap;
250 }
251 
set_fixed_fps(int p_count)252 void CPUParticles2D::set_fixed_fps(int p_count) {
253 	fixed_fps = p_count;
254 }
255 
get_fixed_fps() const256 int CPUParticles2D::get_fixed_fps() const {
257 	return fixed_fps;
258 }
259 
set_fractional_delta(bool p_enable)260 void CPUParticles2D::set_fractional_delta(bool p_enable) {
261 	fractional_delta = p_enable;
262 }
263 
get_fractional_delta() const264 bool CPUParticles2D::get_fractional_delta() const {
265 	return fractional_delta;
266 }
267 
get_configuration_warning() const268 String CPUParticles2D::get_configuration_warning() const {
269 
270 	String warnings;
271 
272 	CanvasItemMaterial *mat = Object::cast_to<CanvasItemMaterial>(get_material().ptr());
273 
274 	if (get_material().is_null() || (mat && !mat->get_particles_animation())) {
275 		if (get_param(PARAM_ANIM_SPEED) != 0.0 || get_param(PARAM_ANIM_OFFSET) != 0.0 ||
276 				get_param_curve(PARAM_ANIM_SPEED).is_valid() || get_param_curve(PARAM_ANIM_OFFSET).is_valid()) {
277 			if (warnings != String())
278 				warnings += "\n";
279 			warnings += "- " + TTR("CPUParticles2D animation requires the usage of a CanvasItemMaterial with \"Particles Animation\" enabled.");
280 		}
281 	}
282 
283 	return warnings;
284 }
285 
restart()286 void CPUParticles2D::restart() {
287 
288 	time = 0;
289 	inactive_time = 0;
290 	frame_remainder = 0;
291 	cycle = 0;
292 	emitting = false;
293 
294 	{
295 		int pc = particles.size();
296 		PoolVector<Particle>::Write w = particles.write();
297 
298 		for (int i = 0; i < pc; i++) {
299 			w[i].active = false;
300 		}
301 	}
302 
303 	set_emitting(true);
304 }
305 
set_direction(Vector2 p_direction)306 void CPUParticles2D::set_direction(Vector2 p_direction) {
307 
308 	direction = p_direction;
309 }
310 
get_direction() const311 Vector2 CPUParticles2D::get_direction() const {
312 
313 	return direction;
314 }
315 
set_spread(float p_spread)316 void CPUParticles2D::set_spread(float p_spread) {
317 
318 	spread = p_spread;
319 }
320 
get_spread() const321 float CPUParticles2D::get_spread() const {
322 
323 	return spread;
324 }
325 
set_param(Parameter p_param,float p_value)326 void CPUParticles2D::set_param(Parameter p_param, float p_value) {
327 
328 	ERR_FAIL_INDEX(p_param, PARAM_MAX);
329 
330 	parameters[p_param] = p_value;
331 }
get_param(Parameter p_param) const332 float CPUParticles2D::get_param(Parameter p_param) const {
333 
334 	ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
335 
336 	return parameters[p_param];
337 }
338 
set_param_randomness(Parameter p_param,float p_value)339 void CPUParticles2D::set_param_randomness(Parameter p_param, float p_value) {
340 
341 	ERR_FAIL_INDEX(p_param, PARAM_MAX);
342 
343 	randomness[p_param] = p_value;
344 }
get_param_randomness(Parameter p_param) const345 float CPUParticles2D::get_param_randomness(Parameter p_param) const {
346 
347 	ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
348 
349 	return randomness[p_param];
350 }
351 
_adjust_curve_range(const Ref<Curve> & p_curve,float p_min,float p_max)352 static void _adjust_curve_range(const Ref<Curve> &p_curve, float p_min, float p_max) {
353 
354 	Ref<Curve> curve = p_curve;
355 	if (!curve.is_valid())
356 		return;
357 
358 	curve->ensure_default_setup(p_min, p_max);
359 }
360 
set_param_curve(Parameter p_param,const Ref<Curve> & p_curve)361 void CPUParticles2D::set_param_curve(Parameter p_param, const Ref<Curve> &p_curve) {
362 
363 	ERR_FAIL_INDEX(p_param, PARAM_MAX);
364 
365 	curve_parameters[p_param] = p_curve;
366 
367 	switch (p_param) {
368 		case PARAM_INITIAL_LINEAR_VELOCITY: {
369 			//do none for this one
370 		} break;
371 		case PARAM_ANGULAR_VELOCITY: {
372 			_adjust_curve_range(p_curve, -360, 360);
373 		} break;
374 		case PARAM_ORBIT_VELOCITY: {
375 			_adjust_curve_range(p_curve, -500, 500);
376 		} break;
377 		case PARAM_LINEAR_ACCEL: {
378 			_adjust_curve_range(p_curve, -200, 200);
379 		} break;
380 		case PARAM_RADIAL_ACCEL: {
381 			_adjust_curve_range(p_curve, -200, 200);
382 		} break;
383 		case PARAM_TANGENTIAL_ACCEL: {
384 			_adjust_curve_range(p_curve, -200, 200);
385 		} break;
386 		case PARAM_DAMPING: {
387 			_adjust_curve_range(p_curve, 0, 100);
388 		} break;
389 		case PARAM_ANGLE: {
390 			_adjust_curve_range(p_curve, -360, 360);
391 		} break;
392 		case PARAM_SCALE: {
393 
394 		} break;
395 		case PARAM_HUE_VARIATION: {
396 			_adjust_curve_range(p_curve, -1, 1);
397 		} break;
398 		case PARAM_ANIM_SPEED: {
399 			_adjust_curve_range(p_curve, 0, 200);
400 		} break;
401 		case PARAM_ANIM_OFFSET: {
402 		} break;
403 		default: {
404 		}
405 	}
406 }
get_param_curve(Parameter p_param) const407 Ref<Curve> CPUParticles2D::get_param_curve(Parameter p_param) const {
408 
409 	ERR_FAIL_INDEX_V(p_param, PARAM_MAX, Ref<Curve>());
410 
411 	return curve_parameters[p_param];
412 }
413 
set_color(const Color & p_color)414 void CPUParticles2D::set_color(const Color &p_color) {
415 
416 	color = p_color;
417 }
418 
get_color() const419 Color CPUParticles2D::get_color() const {
420 
421 	return color;
422 }
423 
set_color_ramp(const Ref<Gradient> & p_ramp)424 void CPUParticles2D::set_color_ramp(const Ref<Gradient> &p_ramp) {
425 
426 	color_ramp = p_ramp;
427 }
428 
get_color_ramp() const429 Ref<Gradient> CPUParticles2D::get_color_ramp() const {
430 
431 	return color_ramp;
432 }
433 
set_particle_flag(Flags p_flag,bool p_enable)434 void CPUParticles2D::set_particle_flag(Flags p_flag, bool p_enable) {
435 	ERR_FAIL_INDEX(p_flag, FLAG_MAX);
436 	flags[p_flag] = p_enable;
437 }
438 
get_particle_flag(Flags p_flag) const439 bool CPUParticles2D::get_particle_flag(Flags p_flag) const {
440 	ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
441 	return flags[p_flag];
442 }
443 
set_emission_shape(EmissionShape p_shape)444 void CPUParticles2D::set_emission_shape(EmissionShape p_shape) {
445 	ERR_FAIL_INDEX(p_shape, EMISSION_SHAPE_MAX);
446 	emission_shape = p_shape;
447 	_change_notify();
448 }
449 
set_emission_sphere_radius(float p_radius)450 void CPUParticles2D::set_emission_sphere_radius(float p_radius) {
451 
452 	emission_sphere_radius = p_radius;
453 }
454 
set_emission_rect_extents(Vector2 p_extents)455 void CPUParticles2D::set_emission_rect_extents(Vector2 p_extents) {
456 
457 	emission_rect_extents = p_extents;
458 }
459 
set_emission_points(const PoolVector<Vector2> & p_points)460 void CPUParticles2D::set_emission_points(const PoolVector<Vector2> &p_points) {
461 
462 	emission_points = p_points;
463 }
464 
set_emission_normals(const PoolVector<Vector2> & p_normals)465 void CPUParticles2D::set_emission_normals(const PoolVector<Vector2> &p_normals) {
466 
467 	emission_normals = p_normals;
468 }
469 
set_emission_colors(const PoolVector<Color> & p_colors)470 void CPUParticles2D::set_emission_colors(const PoolVector<Color> &p_colors) {
471 
472 	emission_colors = p_colors;
473 }
474 
get_emission_sphere_radius() const475 float CPUParticles2D::get_emission_sphere_radius() const {
476 
477 	return emission_sphere_radius;
478 }
get_emission_rect_extents() const479 Vector2 CPUParticles2D::get_emission_rect_extents() const {
480 
481 	return emission_rect_extents;
482 }
get_emission_points() const483 PoolVector<Vector2> CPUParticles2D::get_emission_points() const {
484 
485 	return emission_points;
486 }
get_emission_normals() const487 PoolVector<Vector2> CPUParticles2D::get_emission_normals() const {
488 
489 	return emission_normals;
490 }
491 
get_emission_colors() const492 PoolVector<Color> CPUParticles2D::get_emission_colors() const {
493 
494 	return emission_colors;
495 }
496 
get_emission_shape() const497 CPUParticles2D::EmissionShape CPUParticles2D::get_emission_shape() const {
498 	return emission_shape;
499 }
set_gravity(const Vector2 & p_gravity)500 void CPUParticles2D::set_gravity(const Vector2 &p_gravity) {
501 
502 	gravity = p_gravity;
503 }
504 
get_gravity() const505 Vector2 CPUParticles2D::get_gravity() const {
506 
507 	return gravity;
508 }
509 
_validate_property(PropertyInfo & property) const510 void CPUParticles2D::_validate_property(PropertyInfo &property) const {
511 
512 	if (property.name == "color" && color_ramp.is_valid()) {
513 		property.usage = 0;
514 	}
515 
516 	if (property.name == "emission_sphere_radius" && emission_shape != EMISSION_SHAPE_SPHERE) {
517 		property.usage = 0;
518 	}
519 
520 	if (property.name == "emission_rect_extents" && emission_shape != EMISSION_SHAPE_RECTANGLE) {
521 		property.usage = 0;
522 	}
523 
524 	if ((property.name == "emission_point_texture" || property.name == "emission_color_texture") && (emission_shape < EMISSION_SHAPE_POINTS)) {
525 		property.usage = 0;
526 	}
527 
528 	if (property.name == "emission_normals" && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
529 		property.usage = 0;
530 	}
531 
532 	if (property.name == "emission_points" && emission_shape != EMISSION_SHAPE_POINTS && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
533 		property.usage = 0;
534 	}
535 
536 	if (property.name == "emission_colors" && emission_shape != EMISSION_SHAPE_POINTS && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
537 		property.usage = 0;
538 	}
539 }
540 
idhash(uint32_t x)541 static uint32_t idhash(uint32_t x) {
542 
543 	x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
544 	x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
545 	x = (x >> uint32_t(16)) ^ x;
546 	return x;
547 }
548 
rand_from_seed(uint32_t & seed)549 static float rand_from_seed(uint32_t &seed) {
550 	int k;
551 	int s = int(seed);
552 	if (s == 0)
553 		s = 305420679;
554 	k = s / 127773;
555 	s = 16807 * (s - k * 127773) - 2836 * k;
556 	if (s < 0)
557 		s += 2147483647;
558 	seed = uint32_t(s);
559 	return float(seed % uint32_t(65536)) / 65535.0;
560 }
561 
_update_internal()562 void CPUParticles2D::_update_internal() {
563 
564 	if (particles.size() == 0 || !is_visible_in_tree()) {
565 		_set_redraw(false);
566 		return;
567 	}
568 
569 	float delta = get_process_delta_time();
570 	if (emitting) {
571 		inactive_time = 0;
572 	} else {
573 		inactive_time += delta;
574 		if (inactive_time > lifetime * 1.2) {
575 			set_process_internal(false);
576 			_set_redraw(false);
577 
578 			//reset variables
579 			time = 0;
580 			inactive_time = 0;
581 			frame_remainder = 0;
582 			cycle = 0;
583 			return;
584 		}
585 	}
586 	_set_redraw(true);
587 
588 	if (time == 0 && pre_process_time > 0.0) {
589 
590 		float frame_time;
591 		if (fixed_fps > 0)
592 			frame_time = 1.0 / fixed_fps;
593 		else
594 			frame_time = 1.0 / 30.0;
595 
596 		float todo = pre_process_time;
597 
598 		while (todo >= 0) {
599 			_particles_process(frame_time);
600 			todo -= frame_time;
601 		}
602 	}
603 
604 	if (fixed_fps > 0) {
605 		float frame_time = 1.0 / fixed_fps;
606 		float decr = frame_time;
607 
608 		float ldelta = delta;
609 		if (ldelta > 0.1) { //avoid recursive stalls if fps goes below 10
610 			ldelta = 0.1;
611 		} else if (ldelta <= 0.0) { //unlikely but..
612 			ldelta = 0.001;
613 		}
614 		float todo = frame_remainder + ldelta;
615 
616 		while (todo >= frame_time) {
617 			_particles_process(frame_time);
618 			todo -= decr;
619 		}
620 
621 		frame_remainder = todo;
622 
623 	} else {
624 		_particles_process(delta);
625 	}
626 
627 	_update_particle_data_buffer();
628 }
629 
_particles_process(float p_delta)630 void CPUParticles2D::_particles_process(float p_delta) {
631 
632 	p_delta *= speed_scale;
633 
634 	int pcount = particles.size();
635 	PoolVector<Particle>::Write w = particles.write();
636 
637 	Particle *parray = w.ptr();
638 
639 	float prev_time = time;
640 	time += p_delta;
641 	if (time > lifetime) {
642 		time = Math::fmod(time, lifetime);
643 		cycle++;
644 		if (one_shot && cycle > 0) {
645 			set_emitting(false);
646 			_change_notify();
647 		}
648 	}
649 
650 	Transform2D emission_xform;
651 	Transform2D velocity_xform;
652 	if (!local_coords) {
653 		emission_xform = get_global_transform();
654 		velocity_xform = emission_xform;
655 		velocity_xform[2] = Vector2();
656 	}
657 
658 	float system_phase = time / lifetime;
659 
660 	for (int i = 0; i < pcount; i++) {
661 
662 		Particle &p = parray[i];
663 
664 		if (!emitting && !p.active)
665 			continue;
666 
667 		float local_delta = p_delta;
668 
669 		// The phase is a ratio between 0 (birth) and 1 (end of life) for each particle.
670 		// While we use time in tests later on, for randomness we use the phase as done in the
671 		// original shader code, and we later multiply by lifetime to get the time.
672 		float restart_phase = float(i) / float(pcount);
673 
674 		if (randomness_ratio > 0.0) {
675 			uint32_t seed = cycle;
676 			if (restart_phase >= system_phase) {
677 				seed -= uint32_t(1);
678 			}
679 			seed *= uint32_t(pcount);
680 			seed += uint32_t(i);
681 			float random = float(idhash(seed) % uint32_t(65536)) / 65536.0;
682 			restart_phase += randomness_ratio * random * 1.0 / float(pcount);
683 		}
684 
685 		restart_phase *= (1.0 - explosiveness_ratio);
686 		float restart_time = restart_phase * lifetime;
687 		bool restart = false;
688 
689 		if (time > prev_time) {
690 			// restart_time >= prev_time is used so particles emit in the first frame they are processed
691 
692 			if (restart_time >= prev_time && restart_time < time) {
693 				restart = true;
694 				if (fractional_delta) {
695 					local_delta = time - restart_time;
696 				}
697 			}
698 
699 		} else if (local_delta > 0.0) {
700 			if (restart_time >= prev_time) {
701 				restart = true;
702 				if (fractional_delta) {
703 					local_delta = lifetime - restart_time + time;
704 				}
705 
706 			} else if (restart_time < time) {
707 				restart = true;
708 				if (fractional_delta) {
709 					local_delta = time - restart_time;
710 				}
711 			}
712 		}
713 
714 		if (p.time * (1.0 - explosiveness_ratio) > p.lifetime) {
715 			restart = true;
716 		}
717 
718 		if (restart) {
719 
720 			if (!emitting) {
721 				p.active = false;
722 				continue;
723 			}
724 			p.active = true;
725 
726 			/*float tex_linear_velocity = 0;
727 			if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
728 				tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->interpolate(0);
729 			}*/
730 
731 			float tex_angle = 0.0;
732 			if (curve_parameters[PARAM_ANGLE].is_valid()) {
733 				tex_angle = curve_parameters[PARAM_ANGLE]->interpolate(0);
734 			}
735 
736 			float tex_anim_offset = 0.0;
737 			if (curve_parameters[PARAM_ANGLE].is_valid()) {
738 				tex_anim_offset = curve_parameters[PARAM_ANGLE]->interpolate(0);
739 			}
740 
741 			p.seed = Math::rand();
742 
743 			p.angle_rand = Math::randf();
744 			p.scale_rand = Math::randf();
745 			p.hue_rot_rand = Math::randf();
746 			p.anim_offset_rand = Math::randf();
747 
748 			float angle1_rad = Math::atan2(direction.y, direction.x) + (Math::randf() * 2.0 - 1.0) * Math_PI * spread / 180.0;
749 			Vector2 rot = Vector2(Math::cos(angle1_rad), Math::sin(angle1_rad));
750 			p.velocity = rot * parameters[PARAM_INITIAL_LINEAR_VELOCITY] * Math::lerp(1.0f, float(Math::randf()), randomness[PARAM_INITIAL_LINEAR_VELOCITY]);
751 
752 			float base_angle = (parameters[PARAM_ANGLE] + tex_angle) * Math::lerp(1.0f, p.angle_rand, randomness[PARAM_ANGLE]);
753 			p.rotation = Math::deg2rad(base_angle);
754 
755 			p.custom[0] = 0.0; // unused
756 			p.custom[1] = 0.0; // phase [0..1]
757 			p.custom[2] = (parameters[PARAM_ANIM_OFFSET] + tex_anim_offset) * Math::lerp(1.0f, p.anim_offset_rand, randomness[PARAM_ANIM_OFFSET]); //animation phase [0..1]
758 			p.custom[3] = 0.0;
759 			p.transform = Transform2D();
760 			p.time = 0;
761 			p.lifetime = lifetime * (1.0 - Math::randf() * lifetime_randomness);
762 			p.base_color = Color(1, 1, 1, 1);
763 
764 			switch (emission_shape) {
765 				case EMISSION_SHAPE_POINT: {
766 					//do none
767 				} break;
768 				case EMISSION_SHAPE_SPHERE: {
769 					float s = Math::randf(), t = 2.0 * Math_PI * Math::randf();
770 					float radius = emission_sphere_radius * Math::sqrt(1.0 - s * s);
771 					p.transform[2] = Vector2(Math::cos(t), Math::sin(t)) * radius;
772 				} break;
773 				case EMISSION_SHAPE_RECTANGLE: {
774 					p.transform[2] = Vector2(Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0) * emission_rect_extents;
775 				} break;
776 				case EMISSION_SHAPE_POINTS:
777 				case EMISSION_SHAPE_DIRECTED_POINTS: {
778 
779 					int pc = emission_points.size();
780 					if (pc == 0)
781 						break;
782 
783 					int random_idx = Math::rand() % pc;
784 
785 					p.transform[2] = emission_points.get(random_idx);
786 
787 					if (emission_shape == EMISSION_SHAPE_DIRECTED_POINTS && emission_normals.size() == pc) {
788 						Vector2 normal = emission_normals.get(random_idx);
789 						Transform2D m2;
790 						m2.set_axis(0, normal);
791 						m2.set_axis(1, normal.tangent());
792 						p.velocity = m2.basis_xform(p.velocity);
793 					}
794 
795 					if (emission_colors.size() == pc) {
796 						p.base_color = emission_colors.get(random_idx);
797 					}
798 				} break;
799 				case EMISSION_SHAPE_MAX: { // Max value for validity check.
800 					break;
801 				}
802 			}
803 
804 			if (!local_coords) {
805 				p.velocity = velocity_xform.xform(p.velocity);
806 				p.transform = emission_xform * p.transform;
807 			}
808 
809 		} else if (!p.active) {
810 			continue;
811 		} else if (p.time > p.lifetime) {
812 			p.active = false;
813 		} else {
814 
815 			uint32_t alt_seed = p.seed;
816 
817 			p.time += local_delta;
818 			p.custom[1] = p.time / lifetime;
819 
820 			float tex_linear_velocity = 0.0;
821 			if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
822 				tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->interpolate(p.custom[1]);
823 			}
824 
825 			float tex_orbit_velocity = 0.0;
826 			if (curve_parameters[PARAM_ORBIT_VELOCITY].is_valid()) {
827 				tex_orbit_velocity = curve_parameters[PARAM_ORBIT_VELOCITY]->interpolate(p.custom[1]);
828 			}
829 
830 			float tex_angular_velocity = 0.0;
831 			if (curve_parameters[PARAM_ANGULAR_VELOCITY].is_valid()) {
832 				tex_angular_velocity = curve_parameters[PARAM_ANGULAR_VELOCITY]->interpolate(p.custom[1]);
833 			}
834 
835 			float tex_linear_accel = 0.0;
836 			if (curve_parameters[PARAM_LINEAR_ACCEL].is_valid()) {
837 				tex_linear_accel = curve_parameters[PARAM_LINEAR_ACCEL]->interpolate(p.custom[1]);
838 			}
839 
840 			float tex_tangential_accel = 0.0;
841 			if (curve_parameters[PARAM_TANGENTIAL_ACCEL].is_valid()) {
842 				tex_tangential_accel = curve_parameters[PARAM_TANGENTIAL_ACCEL]->interpolate(p.custom[1]);
843 			}
844 
845 			float tex_radial_accel = 0.0;
846 			if (curve_parameters[PARAM_RADIAL_ACCEL].is_valid()) {
847 				tex_radial_accel = curve_parameters[PARAM_RADIAL_ACCEL]->interpolate(p.custom[1]);
848 			}
849 
850 			float tex_damping = 0.0;
851 			if (curve_parameters[PARAM_DAMPING].is_valid()) {
852 				tex_damping = curve_parameters[PARAM_DAMPING]->interpolate(p.custom[1]);
853 			}
854 
855 			float tex_angle = 0.0;
856 			if (curve_parameters[PARAM_ANGLE].is_valid()) {
857 				tex_angle = curve_parameters[PARAM_ANGLE]->interpolate(p.custom[1]);
858 			}
859 			float tex_anim_speed = 0.0;
860 			if (curve_parameters[PARAM_ANIM_SPEED].is_valid()) {
861 				tex_anim_speed = curve_parameters[PARAM_ANIM_SPEED]->interpolate(p.custom[1]);
862 			}
863 
864 			float tex_anim_offset = 0.0;
865 			if (curve_parameters[PARAM_ANIM_OFFSET].is_valid()) {
866 				tex_anim_offset = curve_parameters[PARAM_ANIM_OFFSET]->interpolate(p.custom[1]);
867 			}
868 
869 			Vector2 force = gravity;
870 			Vector2 pos = p.transform[2];
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]) : Vector2();
874 			//apply radial acceleration
875 			Vector2 org = emission_xform[2];
876 			Vector2 diff = pos - 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]) : Vector2();
878 			//apply tangential acceleration;
879 			Vector2 yx = Vector2(diff.y, diff.x);
880 			force += yx.length() > 0.0 ? (yx * Vector2(-1.0, 1.0)).normalized() * ((parameters[PARAM_TANGENTIAL_ACCEL] + tex_tangential_accel) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_TANGENTIAL_ACCEL])) : Vector2();
881 			//apply attractor forces
882 			p.velocity += force * local_delta;
883 			//orbit velocity
884 			float orbit_amount = (parameters[PARAM_ORBIT_VELOCITY] + tex_orbit_velocity) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_ORBIT_VELOCITY]);
885 			if (orbit_amount != 0.0) {
886 				float ang = orbit_amount * local_delta * Math_PI * 2.0;
887 				// Not sure why the ParticlesMaterial code uses a clockwise rotation matrix,
888 				// but we use -ang here to reproduce its behavior.
889 				Transform2D rot = Transform2D(-ang, Vector2());
890 				p.transform[2] -= diff;
891 				p.transform[2] += rot.basis_xform(diff);
892 			}
893 			if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
894 				p.velocity = p.velocity.normalized() * tex_linear_velocity;
895 			}
896 
897 			if (parameters[PARAM_DAMPING] + tex_damping > 0.0) {
898 
899 				float v = p.velocity.length();
900 				float damp = (parameters[PARAM_DAMPING] + tex_damping) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_DAMPING]);
901 				v -= damp * local_delta;
902 				if (v < 0.0) {
903 					p.velocity = Vector2();
904 				} else {
905 					p.velocity = p.velocity.normalized() * v;
906 				}
907 			}
908 			float base_angle = (parameters[PARAM_ANGLE] + tex_angle) * Math::lerp(1.0f, p.angle_rand, randomness[PARAM_ANGLE]);
909 			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]);
910 			p.rotation = Math::deg2rad(base_angle); //angle
911 			float animation_phase = (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]);
912 			p.custom[2] = animation_phase;
913 		}
914 		//apply color
915 		//apply hue rotation
916 
917 		float tex_scale = 1.0;
918 		if (curve_parameters[PARAM_SCALE].is_valid()) {
919 			tex_scale = curve_parameters[PARAM_SCALE]->interpolate(p.custom[1]);
920 		}
921 
922 		float tex_hue_variation = 0.0;
923 		if (curve_parameters[PARAM_HUE_VARIATION].is_valid()) {
924 			tex_hue_variation = curve_parameters[PARAM_HUE_VARIATION]->interpolate(p.custom[1]);
925 		}
926 
927 		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]);
928 		float hue_rot_c = Math::cos(hue_rot_angle);
929 		float hue_rot_s = Math::sin(hue_rot_angle);
930 
931 		Basis hue_rot_mat;
932 		{
933 			Basis mat1(0.299, 0.587, 0.114, 0.299, 0.587, 0.114, 0.299, 0.587, 0.114);
934 			Basis mat2(0.701, -0.587, -0.114, -0.299, 0.413, -0.114, -0.300, -0.588, 0.886);
935 			Basis mat3(0.168, 0.330, -0.497, -0.328, 0.035, 0.292, 1.250, -1.050, -0.203);
936 
937 			for (int j = 0; j < 3; j++) {
938 				hue_rot_mat[j] = mat1[j] + mat2[j] * hue_rot_c + mat3[j] * hue_rot_s;
939 			}
940 		}
941 
942 		if (color_ramp.is_valid()) {
943 			p.color = color_ramp->get_color_at_offset(p.custom[1]) * color;
944 		} else {
945 			p.color = color;
946 		}
947 
948 		Vector3 color_rgb = hue_rot_mat.xform_inv(Vector3(p.color.r, p.color.g, p.color.b));
949 		p.color.r = color_rgb.x;
950 		p.color.g = color_rgb.y;
951 		p.color.b = color_rgb.z;
952 
953 		p.color *= p.base_color;
954 
955 		if (flags[FLAG_ALIGN_Y_TO_VELOCITY]) {
956 			if (p.velocity.length() > 0.0) {
957 
958 				p.transform.elements[1] = p.velocity.normalized();
959 				p.transform.elements[0] = p.transform.elements[1].tangent();
960 			}
961 
962 		} else {
963 			p.transform.elements[0] = Vector2(Math::cos(p.rotation), -Math::sin(p.rotation));
964 			p.transform.elements[1] = Vector2(Math::sin(p.rotation), Math::cos(p.rotation));
965 		}
966 
967 		//scale by scale
968 		float base_scale = tex_scale * Math::lerp(parameters[PARAM_SCALE], 1.0f, p.scale_rand * randomness[PARAM_SCALE]);
969 		if (base_scale < 0.000001) base_scale = 0.000001;
970 
971 		p.transform.elements[0] *= base_scale;
972 		p.transform.elements[1] *= base_scale;
973 
974 		p.transform[2] += p.velocity * local_delta;
975 	}
976 }
977 
_update_particle_data_buffer()978 void CPUParticles2D::_update_particle_data_buffer() {
979 #ifndef NO_THREADS
980 	update_mutex->lock();
981 #endif
982 
983 	{
984 
985 		int pc = particles.size();
986 
987 		PoolVector<int>::Write ow;
988 		int *order = NULL;
989 
990 		PoolVector<float>::Write w = particle_data.write();
991 		PoolVector<Particle>::Read r = particles.read();
992 		float *ptr = w.ptr();
993 
994 		if (draw_order != DRAW_ORDER_INDEX) {
995 			ow = particle_order.write();
996 			order = ow.ptr();
997 
998 			for (int i = 0; i < pc; i++) {
999 				order[i] = i;
1000 			}
1001 			if (draw_order == DRAW_ORDER_LIFETIME) {
1002 				SortArray<int, SortLifetime> sorter;
1003 				sorter.compare.particles = r.ptr();
1004 				sorter.sort(order, pc);
1005 			}
1006 		}
1007 
1008 		for (int i = 0; i < pc; i++) {
1009 
1010 			int idx = order ? order[i] : i;
1011 
1012 			Transform2D t = r[idx].transform;
1013 
1014 			if (!local_coords) {
1015 				t = inv_emission_transform * t;
1016 			}
1017 
1018 			if (r[idx].active) {
1019 
1020 				ptr[0] = t.elements[0][0];
1021 				ptr[1] = t.elements[1][0];
1022 				ptr[2] = 0;
1023 				ptr[3] = t.elements[2][0];
1024 				ptr[4] = t.elements[0][1];
1025 				ptr[5] = t.elements[1][1];
1026 				ptr[6] = 0;
1027 				ptr[7] = t.elements[2][1];
1028 
1029 				Color c = r[idx].color;
1030 				uint8_t *data8 = (uint8_t *)&ptr[8];
1031 				data8[0] = CLAMP(c.r * 255.0, 0, 255);
1032 				data8[1] = CLAMP(c.g * 255.0, 0, 255);
1033 				data8[2] = CLAMP(c.b * 255.0, 0, 255);
1034 				data8[3] = CLAMP(c.a * 255.0, 0, 255);
1035 
1036 				ptr[9] = r[idx].custom[0];
1037 				ptr[10] = r[idx].custom[1];
1038 				ptr[11] = r[idx].custom[2];
1039 				ptr[12] = r[idx].custom[3];
1040 
1041 			} else {
1042 				zeromem(ptr, sizeof(float) * 13);
1043 			}
1044 
1045 			ptr += 13;
1046 		}
1047 	}
1048 
1049 #ifndef NO_THREADS
1050 	update_mutex->unlock();
1051 #endif
1052 }
1053 
_set_redraw(bool p_redraw)1054 void CPUParticles2D::_set_redraw(bool p_redraw) {
1055 	if (redraw == p_redraw)
1056 		return;
1057 	redraw = p_redraw;
1058 #ifndef NO_THREADS
1059 	update_mutex->lock();
1060 #endif
1061 	if (redraw) {
1062 		VS::get_singleton()->connect("frame_pre_draw", this, "_update_render_thread");
1063 		VS::get_singleton()->canvas_item_set_update_when_visible(get_canvas_item(), true);
1064 
1065 		VS::get_singleton()->multimesh_set_visible_instances(multimesh, -1);
1066 	} else {
1067 		if (VS::get_singleton()->is_connected("frame_pre_draw", this, "_update_render_thread")) {
1068 			VS::get_singleton()->disconnect("frame_pre_draw", this, "_update_render_thread");
1069 		}
1070 		VS::get_singleton()->canvas_item_set_update_when_visible(get_canvas_item(), false);
1071 
1072 		VS::get_singleton()->multimesh_set_visible_instances(multimesh, 0);
1073 	}
1074 #ifndef NO_THREADS
1075 	update_mutex->unlock();
1076 #endif
1077 	update(); // redraw to update render list
1078 }
1079 
_update_render_thread()1080 void CPUParticles2D::_update_render_thread() {
1081 
1082 #ifndef NO_THREADS
1083 	update_mutex->lock();
1084 #endif
1085 
1086 	VS::get_singleton()->multimesh_set_as_bulk_array(multimesh, particle_data);
1087 
1088 #ifndef NO_THREADS
1089 	update_mutex->unlock();
1090 #endif
1091 }
1092 
_notification(int p_what)1093 void CPUParticles2D::_notification(int p_what) {
1094 
1095 	if (p_what == NOTIFICATION_ENTER_TREE) {
1096 		set_process_internal(emitting);
1097 	}
1098 
1099 	if (p_what == NOTIFICATION_EXIT_TREE) {
1100 		_set_redraw(false);
1101 	}
1102 
1103 	if (p_what == NOTIFICATION_DRAW) {
1104 		// first update before rendering to avoid one frame delay after emitting starts
1105 		if (emitting && (time == 0))
1106 			_update_internal();
1107 
1108 		if (!redraw)
1109 			return; // don't add to render list
1110 
1111 		RID texrid;
1112 		if (texture.is_valid()) {
1113 			texrid = texture->get_rid();
1114 		}
1115 
1116 		RID normrid;
1117 		if (normalmap.is_valid()) {
1118 			normrid = normalmap->get_rid();
1119 		}
1120 
1121 		VS::get_singleton()->canvas_item_add_multimesh(get_canvas_item(), multimesh, texrid, normrid);
1122 	}
1123 
1124 	if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
1125 		_update_internal();
1126 	}
1127 
1128 	if (p_what == NOTIFICATION_TRANSFORM_CHANGED) {
1129 
1130 		inv_emission_transform = get_global_transform().affine_inverse();
1131 
1132 		if (!local_coords) {
1133 
1134 			int pc = particles.size();
1135 
1136 			PoolVector<float>::Write w = particle_data.write();
1137 			PoolVector<Particle>::Read r = particles.read();
1138 			float *ptr = w.ptr();
1139 
1140 			for (int i = 0; i < pc; i++) {
1141 
1142 				Transform2D t = inv_emission_transform * r[i].transform;
1143 
1144 				if (r[i].active) {
1145 
1146 					ptr[0] = t.elements[0][0];
1147 					ptr[1] = t.elements[1][0];
1148 					ptr[2] = 0;
1149 					ptr[3] = t.elements[2][0];
1150 					ptr[4] = t.elements[0][1];
1151 					ptr[5] = t.elements[1][1];
1152 					ptr[6] = 0;
1153 					ptr[7] = t.elements[2][1];
1154 
1155 				} else {
1156 					zeromem(ptr, sizeof(float) * 8);
1157 				}
1158 
1159 				ptr += 13;
1160 			}
1161 		}
1162 	}
1163 }
1164 
convert_from_particles(Node * p_particles)1165 void CPUParticles2D::convert_from_particles(Node *p_particles) {
1166 
1167 	Particles2D *particles = Object::cast_to<Particles2D>(p_particles);
1168 	ERR_FAIL_COND_MSG(!particles, "Only Particles2D nodes can be converted to CPUParticles2D.");
1169 
1170 	set_emitting(particles->is_emitting());
1171 	set_amount(particles->get_amount());
1172 	set_lifetime(particles->get_lifetime());
1173 	set_one_shot(particles->get_one_shot());
1174 	set_pre_process_time(particles->get_pre_process_time());
1175 	set_explosiveness_ratio(particles->get_explosiveness_ratio());
1176 	set_randomness_ratio(particles->get_randomness_ratio());
1177 	set_use_local_coordinates(particles->get_use_local_coordinates());
1178 	set_fixed_fps(particles->get_fixed_fps());
1179 	set_fractional_delta(particles->get_fractional_delta());
1180 	set_speed_scale(particles->get_speed_scale());
1181 	set_draw_order(DrawOrder(particles->get_draw_order()));
1182 	set_texture(particles->get_texture());
1183 
1184 	Ref<Material> mat = particles->get_material();
1185 	if (mat.is_valid()) {
1186 		set_material(mat);
1187 	}
1188 
1189 	Ref<ParticlesMaterial> material = particles->get_process_material();
1190 	if (material.is_null())
1191 		return;
1192 
1193 	Vector3 dir = material->get_direction();
1194 	set_direction(Vector2(dir.x, dir.y));
1195 	set_spread(material->get_spread());
1196 
1197 	set_color(material->get_color());
1198 
1199 	Ref<GradientTexture> gt = material->get_color_ramp();
1200 	if (gt.is_valid()) {
1201 		set_color_ramp(gt->get_gradient());
1202 	}
1203 
1204 	set_particle_flag(FLAG_ALIGN_Y_TO_VELOCITY, material->get_flag(ParticlesMaterial::FLAG_ALIGN_Y_TO_VELOCITY));
1205 
1206 	set_emission_shape(EmissionShape(material->get_emission_shape()));
1207 	set_emission_sphere_radius(material->get_emission_sphere_radius());
1208 	Vector2 rect_extents = Vector2(material->get_emission_box_extents().x, material->get_emission_box_extents().y);
1209 	set_emission_rect_extents(rect_extents);
1210 
1211 	Vector2 gravity = Vector2(material->get_gravity().x, material->get_gravity().y);
1212 	set_gravity(gravity);
1213 	set_lifetime_randomness(material->get_lifetime_randomness());
1214 
1215 #define CONVERT_PARAM(m_param)                                                            \
1216 	set_param(m_param, material->get_param(ParticlesMaterial::m_param));                  \
1217 	{                                                                                     \
1218 		Ref<CurveTexture> ctex = material->get_param_texture(ParticlesMaterial::m_param); \
1219 		if (ctex.is_valid()) set_param_curve(m_param, ctex->get_curve());                 \
1220 	}                                                                                     \
1221 	set_param_randomness(m_param, material->get_param_randomness(ParticlesMaterial::m_param));
1222 
1223 	CONVERT_PARAM(PARAM_INITIAL_LINEAR_VELOCITY);
1224 	CONVERT_PARAM(PARAM_ANGULAR_VELOCITY);
1225 	CONVERT_PARAM(PARAM_ORBIT_VELOCITY);
1226 	CONVERT_PARAM(PARAM_LINEAR_ACCEL);
1227 	CONVERT_PARAM(PARAM_RADIAL_ACCEL);
1228 	CONVERT_PARAM(PARAM_TANGENTIAL_ACCEL);
1229 	CONVERT_PARAM(PARAM_DAMPING);
1230 	CONVERT_PARAM(PARAM_ANGLE);
1231 	CONVERT_PARAM(PARAM_SCALE);
1232 	CONVERT_PARAM(PARAM_HUE_VARIATION);
1233 	CONVERT_PARAM(PARAM_ANIM_SPEED);
1234 	CONVERT_PARAM(PARAM_ANIM_OFFSET);
1235 
1236 #undef CONVERT_PARAM
1237 }
1238 
_bind_methods()1239 void CPUParticles2D::_bind_methods() {
1240 
1241 	ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &CPUParticles2D::set_emitting);
1242 	ClassDB::bind_method(D_METHOD("set_amount", "amount"), &CPUParticles2D::set_amount);
1243 	ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &CPUParticles2D::set_lifetime);
1244 	ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &CPUParticles2D::set_one_shot);
1245 	ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &CPUParticles2D::set_pre_process_time);
1246 	ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &CPUParticles2D::set_explosiveness_ratio);
1247 	ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &CPUParticles2D::set_randomness_ratio);
1248 	ClassDB::bind_method(D_METHOD("set_lifetime_randomness", "random"), &CPUParticles2D::set_lifetime_randomness);
1249 	ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &CPUParticles2D::set_use_local_coordinates);
1250 	ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &CPUParticles2D::set_fixed_fps);
1251 	ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &CPUParticles2D::set_fractional_delta);
1252 	ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &CPUParticles2D::set_speed_scale);
1253 
1254 	ClassDB::bind_method(D_METHOD("is_emitting"), &CPUParticles2D::is_emitting);
1255 	ClassDB::bind_method(D_METHOD("get_amount"), &CPUParticles2D::get_amount);
1256 	ClassDB::bind_method(D_METHOD("get_lifetime"), &CPUParticles2D::get_lifetime);
1257 	ClassDB::bind_method(D_METHOD("get_one_shot"), &CPUParticles2D::get_one_shot);
1258 	ClassDB::bind_method(D_METHOD("get_pre_process_time"), &CPUParticles2D::get_pre_process_time);
1259 	ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &CPUParticles2D::get_explosiveness_ratio);
1260 	ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &CPUParticles2D::get_randomness_ratio);
1261 	ClassDB::bind_method(D_METHOD("get_lifetime_randomness"), &CPUParticles2D::get_lifetime_randomness);
1262 	ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &CPUParticles2D::get_use_local_coordinates);
1263 	ClassDB::bind_method(D_METHOD("get_fixed_fps"), &CPUParticles2D::get_fixed_fps);
1264 	ClassDB::bind_method(D_METHOD("get_fractional_delta"), &CPUParticles2D::get_fractional_delta);
1265 	ClassDB::bind_method(D_METHOD("get_speed_scale"), &CPUParticles2D::get_speed_scale);
1266 
1267 	ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &CPUParticles2D::set_draw_order);
1268 
1269 	ClassDB::bind_method(D_METHOD("get_draw_order"), &CPUParticles2D::get_draw_order);
1270 
1271 	ClassDB::bind_method(D_METHOD("set_texture", "texture"), &CPUParticles2D::set_texture);
1272 	ClassDB::bind_method(D_METHOD("get_texture"), &CPUParticles2D::get_texture);
1273 
1274 	ClassDB::bind_method(D_METHOD("set_normalmap", "normalmap"), &CPUParticles2D::set_normalmap);
1275 	ClassDB::bind_method(D_METHOD("get_normalmap"), &CPUParticles2D::get_normalmap);
1276 
1277 	ClassDB::bind_method(D_METHOD("restart"), &CPUParticles2D::restart);
1278 
1279 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting"), "set_emitting", "is_emitting");
1280 	ADD_PROPERTY(PropertyInfo(Variant::INT, "amount", PROPERTY_HINT_EXP_RANGE, "1,1000000,1"), "set_amount", "get_amount");
1281 	ADD_GROUP("Time", "");
1282 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "lifetime", PROPERTY_HINT_RANGE, "0.01,600.0,0.01,or_greater"), "set_lifetime", "get_lifetime");
1283 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
1284 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "preprocess", PROPERTY_HINT_RANGE, "0.00,600.0,0.01"), "set_pre_process_time", "get_pre_process_time");
1285 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
1286 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
1287 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
1288 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "lifetime_randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_lifetime_randomness", "get_lifetime_randomness");
1289 	ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1"), "set_fixed_fps", "get_fixed_fps");
1290 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
1291 	ADD_GROUP("Drawing", "");
1292 	// No visibility_rect property contrarily to Particles2D, it's updated automatically.
1293 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
1294 	ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime"), "set_draw_order", "get_draw_order");
1295 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
1296 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "normalmap", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_normalmap", "get_normalmap");
1297 
1298 	BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
1299 	BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
1300 
1301 	////////////////////////////////
1302 
1303 	ClassDB::bind_method(D_METHOD("set_direction", "direction"), &CPUParticles2D::set_direction);
1304 	ClassDB::bind_method(D_METHOD("get_direction"), &CPUParticles2D::get_direction);
1305 
1306 	ClassDB::bind_method(D_METHOD("set_spread", "degrees"), &CPUParticles2D::set_spread);
1307 	ClassDB::bind_method(D_METHOD("get_spread"), &CPUParticles2D::get_spread);
1308 
1309 	ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &CPUParticles2D::set_param);
1310 	ClassDB::bind_method(D_METHOD("get_param", "param"), &CPUParticles2D::get_param);
1311 
1312 	ClassDB::bind_method(D_METHOD("set_param_randomness", "param", "randomness"), &CPUParticles2D::set_param_randomness);
1313 	ClassDB::bind_method(D_METHOD("get_param_randomness", "param"), &CPUParticles2D::get_param_randomness);
1314 
1315 	ClassDB::bind_method(D_METHOD("set_param_curve", "param", "curve"), &CPUParticles2D::set_param_curve);
1316 	ClassDB::bind_method(D_METHOD("get_param_curve", "param"), &CPUParticles2D::get_param_curve);
1317 
1318 	ClassDB::bind_method(D_METHOD("set_color", "color"), &CPUParticles2D::set_color);
1319 	ClassDB::bind_method(D_METHOD("get_color"), &CPUParticles2D::get_color);
1320 
1321 	ClassDB::bind_method(D_METHOD("set_color_ramp", "ramp"), &CPUParticles2D::set_color_ramp);
1322 	ClassDB::bind_method(D_METHOD("get_color_ramp"), &CPUParticles2D::get_color_ramp);
1323 
1324 	ClassDB::bind_method(D_METHOD("set_particle_flag", "flag", "enable"), &CPUParticles2D::set_particle_flag);
1325 	ClassDB::bind_method(D_METHOD("get_particle_flag", "flag"), &CPUParticles2D::get_particle_flag);
1326 
1327 	ClassDB::bind_method(D_METHOD("set_emission_shape", "shape"), &CPUParticles2D::set_emission_shape);
1328 	ClassDB::bind_method(D_METHOD("get_emission_shape"), &CPUParticles2D::get_emission_shape);
1329 
1330 	ClassDB::bind_method(D_METHOD("set_emission_sphere_radius", "radius"), &CPUParticles2D::set_emission_sphere_radius);
1331 	ClassDB::bind_method(D_METHOD("get_emission_sphere_radius"), &CPUParticles2D::get_emission_sphere_radius);
1332 
1333 	ClassDB::bind_method(D_METHOD("set_emission_rect_extents", "extents"), &CPUParticles2D::set_emission_rect_extents);
1334 	ClassDB::bind_method(D_METHOD("get_emission_rect_extents"), &CPUParticles2D::get_emission_rect_extents);
1335 
1336 	ClassDB::bind_method(D_METHOD("set_emission_points", "array"), &CPUParticles2D::set_emission_points);
1337 	ClassDB::bind_method(D_METHOD("get_emission_points"), &CPUParticles2D::get_emission_points);
1338 
1339 	ClassDB::bind_method(D_METHOD("set_emission_normals", "array"), &CPUParticles2D::set_emission_normals);
1340 	ClassDB::bind_method(D_METHOD("get_emission_normals"), &CPUParticles2D::get_emission_normals);
1341 
1342 	ClassDB::bind_method(D_METHOD("set_emission_colors", "array"), &CPUParticles2D::set_emission_colors);
1343 	ClassDB::bind_method(D_METHOD("get_emission_colors"), &CPUParticles2D::get_emission_colors);
1344 
1345 	ClassDB::bind_method(D_METHOD("get_gravity"), &CPUParticles2D::get_gravity);
1346 	ClassDB::bind_method(D_METHOD("set_gravity", "accel_vec"), &CPUParticles2D::set_gravity);
1347 
1348 	ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &CPUParticles2D::convert_from_particles);
1349 
1350 	ClassDB::bind_method(D_METHOD("_update_render_thread"), &CPUParticles2D::_update_render_thread);
1351 	ClassDB::bind_method(D_METHOD("_texture_changed"), &CPUParticles2D::_texture_changed);
1352 
1353 	ADD_GROUP("Emission Shape", "emission_");
1354 	ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_shape", PROPERTY_HINT_ENUM, "Point,Sphere,Box,Points,Directed Points"), "set_emission_shape", "get_emission_shape");
1355 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "emission_sphere_radius", PROPERTY_HINT_RANGE, "0.01,128,0.01"), "set_emission_sphere_radius", "get_emission_sphere_radius");
1356 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "emission_rect_extents"), "set_emission_rect_extents", "get_emission_rect_extents");
1357 	ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "emission_points"), "set_emission_points", "get_emission_points");
1358 	ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "emission_normals"), "set_emission_normals", "get_emission_normals");
1359 	ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "emission_colors"), "set_emission_colors", "get_emission_colors");
1360 	ADD_GROUP("Flags", "flag_");
1361 	ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flag_align_y"), "set_particle_flag", "get_particle_flag", FLAG_ALIGN_Y_TO_VELOCITY);
1362 	ADD_GROUP("Direction", "");
1363 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "direction"), "set_direction", "get_direction");
1364 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "spread", PROPERTY_HINT_RANGE, "0,180,0.01"), "set_spread", "get_spread");
1365 	ADD_GROUP("Gravity", "");
1366 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "gravity"), "set_gravity", "get_gravity");
1367 	ADD_GROUP("Initial Velocity", "initial_");
1368 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "initial_velocity", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param", "get_param", PARAM_INITIAL_LINEAR_VELOCITY);
1369 	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);
1370 	ADD_GROUP("Angular Velocity", "angular_");
1371 	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);
1372 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANGULAR_VELOCITY);
1373 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angular_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGULAR_VELOCITY);
1374 	ADD_GROUP("Orbit Velocity", "orbit_");
1375 	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);
1376 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "orbit_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ORBIT_VELOCITY);
1377 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "orbit_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ORBIT_VELOCITY);
1378 	ADD_GROUP("Linear Accel", "linear_");
1379 	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);
1380 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_LINEAR_ACCEL);
1381 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "linear_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_LINEAR_ACCEL);
1382 	ADD_GROUP("Radial Accel", "radial_");
1383 	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);
1384 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "radial_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_RADIAL_ACCEL);
1385 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "radial_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_RADIAL_ACCEL);
1386 	ADD_GROUP("Tangential Accel", "tangential_");
1387 	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);
1388 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "tangential_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_TANGENTIAL_ACCEL);
1389 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "tangential_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_TANGENTIAL_ACCEL);
1390 	ADD_GROUP("Damping", "");
1391 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "damping", PROPERTY_HINT_RANGE, "0,100,0.01"), "set_param", "get_param", PARAM_DAMPING);
1392 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "damping_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_DAMPING);
1393 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "damping_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_DAMPING);
1394 	ADD_GROUP("Angle", "");
1395 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angle", PROPERTY_HINT_RANGE, "-720,720,0.1,or_lesser,or_greater"), "set_param", "get_param", PARAM_ANGLE);
1396 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angle_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANGLE);
1397 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angle_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGLE);
1398 	ADD_GROUP("Scale", "");
1399 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale_amount", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param", "get_param", PARAM_SCALE);
1400 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale_amount_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_SCALE);
1401 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_amount_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_SCALE);
1402 	ADD_GROUP("Color", "");
1403 	ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
1404 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");
1405 
1406 	ADD_GROUP("Hue Variation", "hue_");
1407 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "hue_variation", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param", "get_param", PARAM_HUE_VARIATION);
1408 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "hue_variation_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_HUE_VARIATION);
1409 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "hue_variation_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_HUE_VARIATION);
1410 	ADD_GROUP("Animation", "anim_");
1411 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_speed", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater"), "set_param", "get_param", PARAM_ANIM_SPEED);
1412 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_speed_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANIM_SPEED);
1413 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_speed_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_SPEED);
1414 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_offset", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param", "get_param", PARAM_ANIM_OFFSET);
1415 	ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_offset_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANIM_OFFSET);
1416 	ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_offset_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_OFFSET);
1417 
1418 	BIND_ENUM_CONSTANT(PARAM_INITIAL_LINEAR_VELOCITY);
1419 	BIND_ENUM_CONSTANT(PARAM_ANGULAR_VELOCITY);
1420 	BIND_ENUM_CONSTANT(PARAM_ORBIT_VELOCITY);
1421 	BIND_ENUM_CONSTANT(PARAM_LINEAR_ACCEL);
1422 	BIND_ENUM_CONSTANT(PARAM_RADIAL_ACCEL);
1423 	BIND_ENUM_CONSTANT(PARAM_TANGENTIAL_ACCEL);
1424 	BIND_ENUM_CONSTANT(PARAM_DAMPING);
1425 	BIND_ENUM_CONSTANT(PARAM_ANGLE);
1426 	BIND_ENUM_CONSTANT(PARAM_SCALE);
1427 	BIND_ENUM_CONSTANT(PARAM_HUE_VARIATION);
1428 	BIND_ENUM_CONSTANT(PARAM_ANIM_SPEED);
1429 	BIND_ENUM_CONSTANT(PARAM_ANIM_OFFSET);
1430 	BIND_ENUM_CONSTANT(PARAM_MAX);
1431 
1432 	BIND_ENUM_CONSTANT(FLAG_ALIGN_Y_TO_VELOCITY);
1433 	BIND_ENUM_CONSTANT(FLAG_ROTATE_Y); // Unused, but exposed for consistency with 3D.
1434 	BIND_ENUM_CONSTANT(FLAG_DISABLE_Z); // Unused, but exposed for consistency with 3D.
1435 	BIND_ENUM_CONSTANT(FLAG_MAX);
1436 
1437 	BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINT);
1438 	BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE);
1439 	BIND_ENUM_CONSTANT(EMISSION_SHAPE_RECTANGLE);
1440 	BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINTS);
1441 	BIND_ENUM_CONSTANT(EMISSION_SHAPE_DIRECTED_POINTS);
1442 	BIND_ENUM_CONSTANT(EMISSION_SHAPE_MAX);
1443 }
1444 
CPUParticles2D()1445 CPUParticles2D::CPUParticles2D() {
1446 
1447 	time = 0;
1448 	inactive_time = 0;
1449 	frame_remainder = 0;
1450 	cycle = 0;
1451 	redraw = false;
1452 	emitting = false;
1453 
1454 	mesh = VisualServer::get_singleton()->mesh_create();
1455 	multimesh = VisualServer::get_singleton()->multimesh_create();
1456 	VisualServer::get_singleton()->multimesh_set_mesh(multimesh, mesh);
1457 
1458 	set_emitting(true);
1459 	set_one_shot(false);
1460 	set_amount(8);
1461 	set_lifetime(1);
1462 	set_fixed_fps(0);
1463 	set_fractional_delta(true);
1464 	set_pre_process_time(0);
1465 	set_explosiveness_ratio(0);
1466 	set_randomness_ratio(0);
1467 	set_lifetime_randomness(0);
1468 	set_use_local_coordinates(true);
1469 
1470 	set_draw_order(DRAW_ORDER_INDEX);
1471 	set_speed_scale(1);
1472 
1473 	set_direction(Vector2(1, 0));
1474 	set_spread(45);
1475 	set_param(PARAM_INITIAL_LINEAR_VELOCITY, 0);
1476 	set_param(PARAM_ANGULAR_VELOCITY, 0);
1477 	set_param(PARAM_ORBIT_VELOCITY, 0);
1478 	set_param(PARAM_LINEAR_ACCEL, 0);
1479 	set_param(PARAM_RADIAL_ACCEL, 0);
1480 	set_param(PARAM_TANGENTIAL_ACCEL, 0);
1481 	set_param(PARAM_DAMPING, 0);
1482 	set_param(PARAM_ANGLE, 0);
1483 	set_param(PARAM_SCALE, 1);
1484 	set_param(PARAM_HUE_VARIATION, 0);
1485 	set_param(PARAM_ANIM_SPEED, 0);
1486 	set_param(PARAM_ANIM_OFFSET, 0);
1487 	set_emission_shape(EMISSION_SHAPE_POINT);
1488 	set_emission_sphere_radius(1);
1489 	set_emission_rect_extents(Vector2(1, 1));
1490 
1491 	set_gravity(Vector2(0, 98));
1492 
1493 	for (int i = 0; i < PARAM_MAX; i++) {
1494 		set_param_randomness(Parameter(i), 0);
1495 	}
1496 
1497 	for (int i = 0; i < FLAG_MAX; i++) {
1498 		flags[i] = false;
1499 	}
1500 
1501 	set_color(Color(1, 1, 1, 1));
1502 
1503 #ifndef NO_THREADS
1504 	update_mutex = Mutex::create();
1505 #endif
1506 
1507 	_update_mesh_texture();
1508 }
1509 
~CPUParticles2D()1510 CPUParticles2D::~CPUParticles2D() {
1511 	VS::get_singleton()->free(multimesh);
1512 	VS::get_singleton()->free(mesh);
1513 
1514 #ifndef NO_THREADS
1515 	memdelete(update_mutex);
1516 #endif
1517 }
1518