cpu_particles_2d.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. /**************************************************************************/
  2. /* cpu_particles_2d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #include "cpu_particles_2d.h"
  31. #include "core/core_string_names.h"
  32. #include "scene/2d/gpu_particles_2d.h"
  33. #include "scene/resources/particle_process_material.h"
  34. void CPUParticles2D::set_emitting(bool p_emitting) {
  35. if (emitting == p_emitting) {
  36. return;
  37. }
  38. emitting = p_emitting;
  39. if (emitting) {
  40. set_process_internal(true);
  41. }
  42. }
  43. void CPUParticles2D::set_amount(int p_amount) {
  44. ERR_FAIL_COND_MSG(p_amount < 1, "Amount of particles must be greater than 0.");
  45. particles.resize(p_amount);
  46. {
  47. Particle *w = particles.ptrw();
  48. for (int i = 0; i < p_amount; i++) {
  49. w[i].active = false;
  50. }
  51. }
  52. particle_data.resize((8 + 4 + 4) * p_amount);
  53. RS::get_singleton()->multimesh_allocate_data(multimesh, p_amount, RS::MULTIMESH_TRANSFORM_2D, true, true);
  54. particle_order.resize(p_amount);
  55. }
  56. void CPUParticles2D::set_lifetime(double p_lifetime) {
  57. ERR_FAIL_COND_MSG(p_lifetime <= 0, "Particles lifetime must be greater than 0.");
  58. lifetime = p_lifetime;
  59. }
  60. void CPUParticles2D::set_one_shot(bool p_one_shot) {
  61. one_shot = p_one_shot;
  62. }
  63. void CPUParticles2D::set_pre_process_time(double p_time) {
  64. pre_process_time = p_time;
  65. }
  66. void CPUParticles2D::set_explosiveness_ratio(real_t p_ratio) {
  67. explosiveness_ratio = p_ratio;
  68. }
  69. void CPUParticles2D::set_randomness_ratio(real_t p_ratio) {
  70. randomness_ratio = p_ratio;
  71. }
  72. void CPUParticles2D::set_lifetime_randomness(double p_random) {
  73. lifetime_randomness = p_random;
  74. }
  75. void CPUParticles2D::set_use_local_coordinates(bool p_enable) {
  76. local_coords = p_enable;
  77. set_notify_transform(!p_enable);
  78. }
  79. void CPUParticles2D::set_speed_scale(double p_scale) {
  80. speed_scale = p_scale;
  81. }
  82. bool CPUParticles2D::is_emitting() const {
  83. return emitting;
  84. }
  85. int CPUParticles2D::get_amount() const {
  86. return particles.size();
  87. }
  88. double CPUParticles2D::get_lifetime() const {
  89. return lifetime;
  90. }
  91. bool CPUParticles2D::get_one_shot() const {
  92. return one_shot;
  93. }
  94. double CPUParticles2D::get_pre_process_time() const {
  95. return pre_process_time;
  96. }
  97. real_t CPUParticles2D::get_explosiveness_ratio() const {
  98. return explosiveness_ratio;
  99. }
  100. real_t CPUParticles2D::get_randomness_ratio() const {
  101. return randomness_ratio;
  102. }
  103. double CPUParticles2D::get_lifetime_randomness() const {
  104. return lifetime_randomness;
  105. }
  106. bool CPUParticles2D::get_use_local_coordinates() const {
  107. return local_coords;
  108. }
  109. double CPUParticles2D::get_speed_scale() const {
  110. return speed_scale;
  111. }
  112. void CPUParticles2D::set_draw_order(DrawOrder p_order) {
  113. draw_order = p_order;
  114. }
  115. CPUParticles2D::DrawOrder CPUParticles2D::get_draw_order() const {
  116. return draw_order;
  117. }
  118. void CPUParticles2D::_update_mesh_texture() {
  119. Size2 tex_size;
  120. if (texture.is_valid()) {
  121. tex_size = texture->get_size();
  122. } else {
  123. tex_size = Size2(1, 1);
  124. }
  125. Vector<Vector2> vertices = {
  126. -tex_size * 0.5,
  127. -tex_size * 0.5 + Vector2(tex_size.x, 0),
  128. -tex_size * 0.5 + tex_size,
  129. -tex_size * 0.5 + Vector2(0, tex_size.y)
  130. };
  131. Vector<Vector2> uvs;
  132. AtlasTexture *atlas_texure = Object::cast_to<AtlasTexture>(*texture);
  133. if (atlas_texure && atlas_texure->get_atlas().is_valid()) {
  134. Rect2 region_rect = atlas_texure->get_region();
  135. Size2 atlas_size = atlas_texure->get_atlas()->get_size();
  136. uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, region_rect.position.y / atlas_size.y));
  137. uvs.push_back(Vector2((region_rect.position.x + region_rect.size.x) / atlas_size.x, region_rect.position.y / atlas_size.y));
  138. 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));
  139. uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, (region_rect.position.y + region_rect.size.y) / atlas_size.y));
  140. } else {
  141. uvs.push_back(Vector2(0, 0));
  142. uvs.push_back(Vector2(1, 0));
  143. uvs.push_back(Vector2(1, 1));
  144. uvs.push_back(Vector2(0, 1));
  145. }
  146. Vector<Color> colors = {
  147. Color(1, 1, 1, 1),
  148. Color(1, 1, 1, 1),
  149. Color(1, 1, 1, 1),
  150. Color(1, 1, 1, 1)
  151. };
  152. Vector<int> indices = { 0, 1, 2, 2, 3, 0 };
  153. Array arr;
  154. arr.resize(RS::ARRAY_MAX);
  155. arr[RS::ARRAY_VERTEX] = vertices;
  156. arr[RS::ARRAY_TEX_UV] = uvs;
  157. arr[RS::ARRAY_COLOR] = colors;
  158. arr[RS::ARRAY_INDEX] = indices;
  159. RS::get_singleton()->mesh_clear(mesh);
  160. RS::get_singleton()->mesh_add_surface_from_arrays(mesh, RS::PRIMITIVE_TRIANGLES, arr);
  161. }
  162. void CPUParticles2D::set_texture(const Ref<Texture2D> &p_texture) {
  163. if (p_texture == texture) {
  164. return;
  165. }
  166. if (texture.is_valid()) {
  167. texture->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CPUParticles2D::_texture_changed));
  168. }
  169. texture = p_texture;
  170. if (texture.is_valid()) {
  171. texture->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CPUParticles2D::_texture_changed));
  172. }
  173. queue_redraw();
  174. _update_mesh_texture();
  175. }
  176. void CPUParticles2D::_texture_changed() {
  177. if (texture.is_valid()) {
  178. queue_redraw();
  179. _update_mesh_texture();
  180. }
  181. }
  182. Ref<Texture2D> CPUParticles2D::get_texture() const {
  183. return texture;
  184. }
  185. void CPUParticles2D::set_fixed_fps(int p_count) {
  186. fixed_fps = p_count;
  187. }
  188. int CPUParticles2D::get_fixed_fps() const {
  189. return fixed_fps;
  190. }
  191. void CPUParticles2D::set_fractional_delta(bool p_enable) {
  192. fractional_delta = p_enable;
  193. }
  194. bool CPUParticles2D::get_fractional_delta() const {
  195. return fractional_delta;
  196. }
  197. PackedStringArray CPUParticles2D::get_configuration_warnings() const {
  198. PackedStringArray warnings = Node2D::get_configuration_warnings();
  199. CanvasItemMaterial *mat = Object::cast_to<CanvasItemMaterial>(get_material().ptr());
  200. if (get_material().is_null() || (mat && !mat->get_particles_animation())) {
  201. if (get_param_max(PARAM_ANIM_SPEED) != 0.0 || get_param_max(PARAM_ANIM_OFFSET) != 0.0 ||
  202. get_param_curve(PARAM_ANIM_SPEED).is_valid() || get_param_curve(PARAM_ANIM_OFFSET).is_valid()) {
  203. warnings.push_back(RTR("CPUParticles2D animation requires the usage of a CanvasItemMaterial with \"Particles Animation\" enabled."));
  204. }
  205. }
  206. return warnings;
  207. }
  208. void CPUParticles2D::restart() {
  209. time = 0;
  210. inactive_time = 0;
  211. frame_remainder = 0;
  212. cycle = 0;
  213. emitting = false;
  214. {
  215. int pc = particles.size();
  216. Particle *w = particles.ptrw();
  217. for (int i = 0; i < pc; i++) {
  218. w[i].active = false;
  219. }
  220. }
  221. set_emitting(true);
  222. }
  223. void CPUParticles2D::set_direction(Vector2 p_direction) {
  224. direction = p_direction;
  225. }
  226. Vector2 CPUParticles2D::get_direction() const {
  227. return direction;
  228. }
  229. void CPUParticles2D::set_spread(real_t p_spread) {
  230. spread = p_spread;
  231. }
  232. real_t CPUParticles2D::get_spread() const {
  233. return spread;
  234. }
  235. void CPUParticles2D::set_param_min(Parameter p_param, real_t p_value) {
  236. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  237. parameters_min[p_param] = p_value;
  238. if (parameters_min[p_param] > parameters_max[p_param]) {
  239. set_param_max(p_param, p_value);
  240. }
  241. }
  242. real_t CPUParticles2D::get_param_min(Parameter p_param) const {
  243. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  244. return parameters_min[p_param];
  245. }
  246. void CPUParticles2D::set_param_max(Parameter p_param, real_t p_value) {
  247. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  248. parameters_max[p_param] = p_value;
  249. if (parameters_min[p_param] > parameters_max[p_param]) {
  250. set_param_min(p_param, p_value);
  251. }
  252. update_configuration_warnings();
  253. }
  254. real_t CPUParticles2D::get_param_max(Parameter p_param) const {
  255. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  256. return parameters_max[p_param];
  257. }
  258. static void _adjust_curve_range(const Ref<Curve> &p_curve, real_t p_min, real_t p_max) {
  259. Ref<Curve> curve = p_curve;
  260. if (!curve.is_valid()) {
  261. return;
  262. }
  263. curve->ensure_default_setup(p_min, p_max);
  264. }
  265. void CPUParticles2D::set_param_curve(Parameter p_param, const Ref<Curve> &p_curve) {
  266. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  267. curve_parameters[p_param] = p_curve;
  268. switch (p_param) {
  269. case PARAM_INITIAL_LINEAR_VELOCITY: {
  270. //do none for this one
  271. } break;
  272. case PARAM_ANGULAR_VELOCITY: {
  273. _adjust_curve_range(p_curve, -360, 360);
  274. } break;
  275. case PARAM_ORBIT_VELOCITY: {
  276. _adjust_curve_range(p_curve, -500, 500);
  277. } break;
  278. case PARAM_LINEAR_ACCEL: {
  279. _adjust_curve_range(p_curve, -200, 200);
  280. } break;
  281. case PARAM_RADIAL_ACCEL: {
  282. _adjust_curve_range(p_curve, -200, 200);
  283. } break;
  284. case PARAM_TANGENTIAL_ACCEL: {
  285. _adjust_curve_range(p_curve, -200, 200);
  286. } break;
  287. case PARAM_DAMPING: {
  288. _adjust_curve_range(p_curve, 0, 100);
  289. } break;
  290. case PARAM_ANGLE: {
  291. _adjust_curve_range(p_curve, -360, 360);
  292. } break;
  293. case PARAM_SCALE: {
  294. } break;
  295. case PARAM_HUE_VARIATION: {
  296. _adjust_curve_range(p_curve, -1, 1);
  297. } break;
  298. case PARAM_ANIM_SPEED: {
  299. _adjust_curve_range(p_curve, 0, 200);
  300. } break;
  301. case PARAM_ANIM_OFFSET: {
  302. } break;
  303. default: {
  304. }
  305. }
  306. update_configuration_warnings();
  307. }
  308. Ref<Curve> CPUParticles2D::get_param_curve(Parameter p_param) const {
  309. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, Ref<Curve>());
  310. return curve_parameters[p_param];
  311. }
  312. void CPUParticles2D::set_color(const Color &p_color) {
  313. color = p_color;
  314. }
  315. Color CPUParticles2D::get_color() const {
  316. return color;
  317. }
  318. void CPUParticles2D::set_color_ramp(const Ref<Gradient> &p_ramp) {
  319. color_ramp = p_ramp;
  320. }
  321. Ref<Gradient> CPUParticles2D::get_color_ramp() const {
  322. return color_ramp;
  323. }
  324. void CPUParticles2D::set_color_initial_ramp(const Ref<Gradient> &p_ramp) {
  325. color_initial_ramp = p_ramp;
  326. }
  327. Ref<Gradient> CPUParticles2D::get_color_initial_ramp() const {
  328. return color_initial_ramp;
  329. }
  330. void CPUParticles2D::set_particle_flag(ParticleFlags p_particle_flag, bool p_enable) {
  331. ERR_FAIL_INDEX(p_particle_flag, PARTICLE_FLAG_MAX);
  332. particle_flags[p_particle_flag] = p_enable;
  333. }
  334. bool CPUParticles2D::get_particle_flag(ParticleFlags p_particle_flag) const {
  335. ERR_FAIL_INDEX_V(p_particle_flag, PARTICLE_FLAG_MAX, false);
  336. return particle_flags[p_particle_flag];
  337. }
  338. void CPUParticles2D::set_emission_shape(EmissionShape p_shape) {
  339. ERR_FAIL_INDEX(p_shape, EMISSION_SHAPE_MAX);
  340. emission_shape = p_shape;
  341. notify_property_list_changed();
  342. }
  343. void CPUParticles2D::set_emission_sphere_radius(real_t p_radius) {
  344. emission_sphere_radius = p_radius;
  345. }
  346. void CPUParticles2D::set_emission_rect_extents(Vector2 p_extents) {
  347. emission_rect_extents = p_extents;
  348. }
  349. void CPUParticles2D::set_emission_points(const Vector<Vector2> &p_points) {
  350. emission_points = p_points;
  351. }
  352. void CPUParticles2D::set_emission_normals(const Vector<Vector2> &p_normals) {
  353. emission_normals = p_normals;
  354. }
  355. void CPUParticles2D::set_emission_colors(const Vector<Color> &p_colors) {
  356. emission_colors = p_colors;
  357. }
  358. real_t CPUParticles2D::get_emission_sphere_radius() const {
  359. return emission_sphere_radius;
  360. }
  361. Vector2 CPUParticles2D::get_emission_rect_extents() const {
  362. return emission_rect_extents;
  363. }
  364. Vector<Vector2> CPUParticles2D::get_emission_points() const {
  365. return emission_points;
  366. }
  367. Vector<Vector2> CPUParticles2D::get_emission_normals() const {
  368. return emission_normals;
  369. }
  370. Vector<Color> CPUParticles2D::get_emission_colors() const {
  371. return emission_colors;
  372. }
  373. CPUParticles2D::EmissionShape CPUParticles2D::get_emission_shape() const {
  374. return emission_shape;
  375. }
  376. void CPUParticles2D::set_gravity(const Vector2 &p_gravity) {
  377. gravity = p_gravity;
  378. }
  379. Vector2 CPUParticles2D::get_gravity() const {
  380. return gravity;
  381. }
  382. void CPUParticles2D::set_scale_curve_x(Ref<Curve> p_scale_curve) {
  383. scale_curve_x = p_scale_curve;
  384. }
  385. void CPUParticles2D::set_scale_curve_y(Ref<Curve> p_scale_curve) {
  386. scale_curve_y = p_scale_curve;
  387. }
  388. void CPUParticles2D::set_split_scale(bool p_split_scale) {
  389. split_scale = p_split_scale;
  390. notify_property_list_changed();
  391. }
  392. Ref<Curve> CPUParticles2D::get_scale_curve_x() const {
  393. return scale_curve_x;
  394. }
  395. Ref<Curve> CPUParticles2D::get_scale_curve_y() const {
  396. return scale_curve_y;
  397. }
  398. bool CPUParticles2D::get_split_scale() {
  399. return split_scale;
  400. }
  401. void CPUParticles2D::_validate_property(PropertyInfo &p_property) const {
  402. if (p_property.name == "emission_sphere_radius" && (emission_shape != EMISSION_SHAPE_SPHERE && emission_shape != EMISSION_SHAPE_SPHERE_SURFACE)) {
  403. p_property.usage = PROPERTY_USAGE_NONE;
  404. }
  405. if (p_property.name == "emission_rect_extents" && emission_shape != EMISSION_SHAPE_RECTANGLE) {
  406. p_property.usage = PROPERTY_USAGE_NONE;
  407. }
  408. if ((p_property.name == "emission_point_texture" || p_property.name == "emission_color_texture") && (emission_shape < EMISSION_SHAPE_POINTS)) {
  409. p_property.usage = PROPERTY_USAGE_NONE;
  410. }
  411. if (p_property.name == "emission_normals" && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
  412. p_property.usage = PROPERTY_USAGE_NONE;
  413. }
  414. if (p_property.name == "emission_points" && emission_shape != EMISSION_SHAPE_POINTS && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
  415. p_property.usage = PROPERTY_USAGE_NONE;
  416. }
  417. if (p_property.name == "emission_colors" && emission_shape != EMISSION_SHAPE_POINTS && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
  418. p_property.usage = PROPERTY_USAGE_NONE;
  419. }
  420. if (p_property.name.begins_with("scale_curve_") && !split_scale) {
  421. p_property.usage = PROPERTY_USAGE_NONE;
  422. }
  423. }
  424. static uint32_t idhash(uint32_t x) {
  425. x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
  426. x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
  427. x = (x >> uint32_t(16)) ^ x;
  428. return x;
  429. }
  430. static real_t rand_from_seed(uint32_t &seed) {
  431. int k;
  432. int s = int(seed);
  433. if (s == 0) {
  434. s = 305420679;
  435. }
  436. k = s / 127773;
  437. s = 16807 * (s - k * 127773) - 2836 * k;
  438. if (s < 0) {
  439. s += 2147483647;
  440. }
  441. seed = uint32_t(s);
  442. return (seed % uint32_t(65536)) / 65535.0;
  443. }
  444. void CPUParticles2D::_update_internal() {
  445. if (particles.size() == 0 || !is_visible_in_tree()) {
  446. _set_do_redraw(false);
  447. return;
  448. }
  449. double delta = get_process_delta_time();
  450. if (emitting) {
  451. inactive_time = 0;
  452. } else {
  453. inactive_time += delta;
  454. if (inactive_time > lifetime * 1.2) {
  455. set_process_internal(false);
  456. _set_do_redraw(false);
  457. //reset variables
  458. time = 0;
  459. inactive_time = 0;
  460. frame_remainder = 0;
  461. cycle = 0;
  462. return;
  463. }
  464. }
  465. _set_do_redraw(true);
  466. if (time == 0 && pre_process_time > 0.0) {
  467. double frame_time;
  468. if (fixed_fps > 0) {
  469. frame_time = 1.0 / fixed_fps;
  470. } else {
  471. frame_time = 1.0 / 30.0;
  472. }
  473. double todo = pre_process_time;
  474. while (todo >= 0) {
  475. _particles_process(frame_time);
  476. todo -= frame_time;
  477. }
  478. }
  479. if (fixed_fps > 0) {
  480. double frame_time = 1.0 / fixed_fps;
  481. double decr = frame_time;
  482. double ldelta = delta;
  483. if (ldelta > 0.1) { //avoid recursive stalls if fps goes below 10
  484. ldelta = 0.1;
  485. } else if (ldelta <= 0.0) { //unlikely but..
  486. ldelta = 0.001;
  487. }
  488. double todo = frame_remainder + ldelta;
  489. while (todo >= frame_time) {
  490. _particles_process(frame_time);
  491. todo -= decr;
  492. }
  493. frame_remainder = todo;
  494. } else {
  495. _particles_process(delta);
  496. }
  497. _update_particle_data_buffer();
  498. }
  499. void CPUParticles2D::_particles_process(double p_delta) {
  500. p_delta *= speed_scale;
  501. int pcount = particles.size();
  502. Particle *w = particles.ptrw();
  503. Particle *parray = w;
  504. double prev_time = time;
  505. time += p_delta;
  506. if (time > lifetime) {
  507. time = Math::fmod(time, lifetime);
  508. cycle++;
  509. if (one_shot && cycle > 0) {
  510. set_emitting(false);
  511. notify_property_list_changed();
  512. }
  513. }
  514. Transform2D emission_xform;
  515. Transform2D velocity_xform;
  516. if (!local_coords) {
  517. emission_xform = get_global_transform();
  518. velocity_xform = emission_xform;
  519. velocity_xform[2] = Vector2();
  520. }
  521. double system_phase = time / lifetime;
  522. for (int i = 0; i < pcount; i++) {
  523. Particle &p = parray[i];
  524. if (!emitting && !p.active) {
  525. continue;
  526. }
  527. double local_delta = p_delta;
  528. // The phase is a ratio between 0 (birth) and 1 (end of life) for each particle.
  529. // While we use time in tests later on, for randomness we use the phase as done in the
  530. // original shader code, and we later multiply by lifetime to get the time.
  531. double restart_phase = double(i) / double(pcount);
  532. if (randomness_ratio > 0.0) {
  533. uint32_t seed = cycle;
  534. if (restart_phase >= system_phase) {
  535. seed -= uint32_t(1);
  536. }
  537. seed *= uint32_t(pcount);
  538. seed += uint32_t(i);
  539. double random = double(idhash(seed) % uint32_t(65536)) / 65536.0;
  540. restart_phase += randomness_ratio * random * 1.0 / double(pcount);
  541. }
  542. restart_phase *= (1.0 - explosiveness_ratio);
  543. double restart_time = restart_phase * lifetime;
  544. bool restart = false;
  545. if (time > prev_time) {
  546. // restart_time >= prev_time is used so particles emit in the first frame they are processed
  547. if (restart_time >= prev_time && restart_time < time) {
  548. restart = true;
  549. if (fractional_delta) {
  550. local_delta = time - restart_time;
  551. }
  552. }
  553. } else if (local_delta > 0.0) {
  554. if (restart_time >= prev_time) {
  555. restart = true;
  556. if (fractional_delta) {
  557. local_delta = lifetime - restart_time + time;
  558. }
  559. } else if (restart_time < time) {
  560. restart = true;
  561. if (fractional_delta) {
  562. local_delta = time - restart_time;
  563. }
  564. }
  565. }
  566. if (p.time * (1.0 - explosiveness_ratio) > p.lifetime) {
  567. restart = true;
  568. }
  569. float tv = 0.0;
  570. if (restart) {
  571. if (!emitting) {
  572. p.active = false;
  573. continue;
  574. }
  575. p.active = true;
  576. /*real_t tex_linear_velocity = 0;
  577. if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  578. tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->sample(0);
  579. }*/
  580. real_t tex_angle = 1.0;
  581. if (curve_parameters[PARAM_ANGLE].is_valid()) {
  582. tex_angle = curve_parameters[PARAM_ANGLE]->sample(tv);
  583. }
  584. real_t tex_anim_offset = 1.0;
  585. if (curve_parameters[PARAM_ANGLE].is_valid()) {
  586. tex_anim_offset = curve_parameters[PARAM_ANGLE]->sample(tv);
  587. }
  588. p.seed = Math::rand();
  589. p.angle_rand = Math::randf();
  590. p.scale_rand = Math::randf();
  591. p.hue_rot_rand = Math::randf();
  592. p.anim_offset_rand = Math::randf();
  593. if (color_initial_ramp.is_valid()) {
  594. p.start_color_rand = color_initial_ramp->get_color_at_offset(Math::randf());
  595. } else {
  596. p.start_color_rand = Color(1, 1, 1, 1);
  597. }
  598. real_t angle1_rad = direction.angle() + Math::deg_to_rad((Math::randf() * 2.0 - 1.0) * spread);
  599. Vector2 rot = Vector2(Math::cos(angle1_rad), Math::sin(angle1_rad));
  600. p.velocity = rot * Math::lerp(parameters_min[PARAM_INITIAL_LINEAR_VELOCITY], parameters_max[PARAM_INITIAL_LINEAR_VELOCITY], (real_t)Math::randf());
  601. real_t base_angle = tex_angle * Math::lerp(parameters_min[PARAM_ANGLE], parameters_max[PARAM_ANGLE], p.angle_rand);
  602. p.rotation = Math::deg_to_rad(base_angle);
  603. p.custom[0] = 0.0; // unused
  604. p.custom[1] = 0.0; // phase [0..1]
  605. p.custom[2] = tex_anim_offset * Math::lerp(parameters_min[PARAM_ANIM_OFFSET], parameters_max[PARAM_ANIM_OFFSET], p.anim_offset_rand);
  606. p.custom[3] = 0.0;
  607. p.transform = Transform2D();
  608. p.time = 0;
  609. p.lifetime = lifetime * (1.0 - Math::randf() * lifetime_randomness);
  610. p.base_color = Color(1, 1, 1, 1);
  611. switch (emission_shape) {
  612. case EMISSION_SHAPE_POINT: {
  613. //do none
  614. } break;
  615. case EMISSION_SHAPE_SPHERE: {
  616. real_t t = Math_TAU * Math::randf();
  617. real_t radius = emission_sphere_radius * Math::randf();
  618. p.transform[2] = Vector2(Math::cos(t), Math::sin(t)) * radius;
  619. } break;
  620. case EMISSION_SHAPE_SPHERE_SURFACE: {
  621. real_t s = Math::randf(), t = Math_TAU * Math::randf();
  622. real_t radius = emission_sphere_radius * Math::sqrt(1.0 - s * s);
  623. p.transform[2] = Vector2(Math::cos(t), Math::sin(t)) * radius;
  624. } break;
  625. case EMISSION_SHAPE_RECTANGLE: {
  626. p.transform[2] = Vector2(Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0) * emission_rect_extents;
  627. } break;
  628. case EMISSION_SHAPE_POINTS:
  629. case EMISSION_SHAPE_DIRECTED_POINTS: {
  630. int pc = emission_points.size();
  631. if (pc == 0) {
  632. break;
  633. }
  634. int random_idx = Math::rand() % pc;
  635. p.transform[2] = emission_points.get(random_idx);
  636. if (emission_shape == EMISSION_SHAPE_DIRECTED_POINTS && emission_normals.size() == pc) {
  637. Vector2 normal = emission_normals.get(random_idx);
  638. Transform2D m2;
  639. m2.columns[0] = normal;
  640. m2.columns[1] = normal.orthogonal();
  641. p.velocity = m2.basis_xform(p.velocity);
  642. }
  643. if (emission_colors.size() == pc) {
  644. p.base_color = emission_colors.get(random_idx);
  645. }
  646. } break;
  647. case EMISSION_SHAPE_MAX: { // Max value for validity check.
  648. break;
  649. }
  650. }
  651. if (!local_coords) {
  652. p.velocity = velocity_xform.xform(p.velocity);
  653. p.transform = emission_xform * p.transform;
  654. }
  655. } else if (!p.active) {
  656. continue;
  657. } else if (p.time > p.lifetime) {
  658. p.active = false;
  659. tv = 1.0;
  660. } else {
  661. uint32_t alt_seed = p.seed;
  662. p.time += local_delta;
  663. p.custom[1] = p.time / lifetime;
  664. tv = p.time / p.lifetime;
  665. real_t tex_linear_velocity = 1.0;
  666. if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  667. tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->sample(tv);
  668. }
  669. real_t tex_orbit_velocity = 1.0;
  670. if (curve_parameters[PARAM_ORBIT_VELOCITY].is_valid()) {
  671. tex_orbit_velocity = curve_parameters[PARAM_ORBIT_VELOCITY]->sample(tv);
  672. }
  673. real_t tex_angular_velocity = 1.0;
  674. if (curve_parameters[PARAM_ANGULAR_VELOCITY].is_valid()) {
  675. tex_angular_velocity = curve_parameters[PARAM_ANGULAR_VELOCITY]->sample(tv);
  676. }
  677. real_t tex_linear_accel = 1.0;
  678. if (curve_parameters[PARAM_LINEAR_ACCEL].is_valid()) {
  679. tex_linear_accel = curve_parameters[PARAM_LINEAR_ACCEL]->sample(tv);
  680. }
  681. real_t tex_tangential_accel = 1.0;
  682. if (curve_parameters[PARAM_TANGENTIAL_ACCEL].is_valid()) {
  683. tex_tangential_accel = curve_parameters[PARAM_TANGENTIAL_ACCEL]->sample(tv);
  684. }
  685. real_t tex_radial_accel = 1.0;
  686. if (curve_parameters[PARAM_RADIAL_ACCEL].is_valid()) {
  687. tex_radial_accel = curve_parameters[PARAM_RADIAL_ACCEL]->sample(tv);
  688. }
  689. real_t tex_damping = 1.0;
  690. if (curve_parameters[PARAM_DAMPING].is_valid()) {
  691. tex_damping = curve_parameters[PARAM_DAMPING]->sample(tv);
  692. }
  693. real_t tex_angle = 1.0;
  694. if (curve_parameters[PARAM_ANGLE].is_valid()) {
  695. tex_angle = curve_parameters[PARAM_ANGLE]->sample(tv);
  696. }
  697. real_t tex_anim_speed = 1.0;
  698. if (curve_parameters[PARAM_ANIM_SPEED].is_valid()) {
  699. tex_anim_speed = curve_parameters[PARAM_ANIM_SPEED]->sample(tv);
  700. }
  701. real_t tex_anim_offset = 1.0;
  702. if (curve_parameters[PARAM_ANIM_OFFSET].is_valid()) {
  703. tex_anim_offset = curve_parameters[PARAM_ANIM_OFFSET]->sample(tv);
  704. }
  705. Vector2 force = gravity;
  706. Vector2 pos = p.transform[2];
  707. //apply linear acceleration
  708. force += p.velocity.length() > 0.0 ? p.velocity.normalized() * tex_linear_accel * Math::lerp(parameters_min[PARAM_LINEAR_ACCEL], parameters_max[PARAM_LINEAR_ACCEL], rand_from_seed(alt_seed)) : Vector2();
  709. //apply radial acceleration
  710. Vector2 org = emission_xform[2];
  711. Vector2 diff = pos - org;
  712. force += diff.length() > 0.0 ? diff.normalized() * (tex_radial_accel)*Math::lerp(parameters_min[PARAM_RADIAL_ACCEL], parameters_max[PARAM_RADIAL_ACCEL], rand_from_seed(alt_seed)) : Vector2();
  713. //apply tangential acceleration;
  714. Vector2 yx = Vector2(diff.y, diff.x);
  715. force += yx.length() > 0.0 ? yx.normalized() * (tex_tangential_accel * Math::lerp(parameters_min[PARAM_TANGENTIAL_ACCEL], parameters_max[PARAM_TANGENTIAL_ACCEL], rand_from_seed(alt_seed))) : Vector2();
  716. //apply attractor forces
  717. p.velocity += force * local_delta;
  718. //orbit velocity
  719. real_t orbit_amount = tex_orbit_velocity * Math::lerp(parameters_min[PARAM_ORBIT_VELOCITY], parameters_max[PARAM_ORBIT_VELOCITY], rand_from_seed(alt_seed));
  720. if (orbit_amount != 0.0) {
  721. real_t ang = orbit_amount * local_delta * Math_TAU;
  722. // Not sure why the ParticleProcessMaterial code uses a clockwise rotation matrix,
  723. // but we use -ang here to reproduce its behavior.
  724. Transform2D rot = Transform2D(-ang, Vector2());
  725. p.transform[2] -= diff;
  726. p.transform[2] += rot.basis_xform(diff);
  727. }
  728. if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  729. p.velocity = p.velocity.normalized() * tex_linear_velocity;
  730. }
  731. if (parameters_max[PARAM_DAMPING] + tex_damping > 0.0) {
  732. real_t v = p.velocity.length();
  733. real_t damp = tex_damping * Math::lerp(parameters_min[PARAM_DAMPING], parameters_max[PARAM_DAMPING], rand_from_seed(alt_seed));
  734. v -= damp * local_delta;
  735. if (v < 0.0) {
  736. p.velocity = Vector2();
  737. } else {
  738. p.velocity = p.velocity.normalized() * v;
  739. }
  740. }
  741. real_t base_angle = (tex_angle)*Math::lerp(parameters_min[PARAM_ANGLE], parameters_max[PARAM_ANGLE], p.angle_rand);
  742. base_angle += p.custom[1] * lifetime * tex_angular_velocity * Math::lerp(parameters_min[PARAM_ANGULAR_VELOCITY], parameters_max[PARAM_ANGULAR_VELOCITY], rand_from_seed(alt_seed));
  743. p.rotation = Math::deg_to_rad(base_angle); //angle
  744. p.custom[2] = tex_anim_offset * Math::lerp(parameters_min[PARAM_ANIM_OFFSET], parameters_max[PARAM_ANIM_OFFSET], p.anim_offset_rand) + tv * tex_anim_speed * Math::lerp(parameters_min[PARAM_ANIM_SPEED], parameters_max[PARAM_ANIM_SPEED], rand_from_seed(alt_seed));
  745. }
  746. //apply color
  747. //apply hue rotation
  748. Vector2 tex_scale = Vector2(1.0, 1.0);
  749. if (split_scale) {
  750. if (scale_curve_x.is_valid()) {
  751. tex_scale.x = scale_curve_x->sample(tv);
  752. } else {
  753. tex_scale.x = 1.0;
  754. }
  755. if (scale_curve_y.is_valid()) {
  756. tex_scale.y = scale_curve_y->sample(tv);
  757. } else {
  758. tex_scale.y = 1.0;
  759. }
  760. } else {
  761. if (curve_parameters[PARAM_SCALE].is_valid()) {
  762. real_t tmp_scale = curve_parameters[PARAM_SCALE]->sample(tv);
  763. tex_scale.x = tmp_scale;
  764. tex_scale.y = tmp_scale;
  765. }
  766. }
  767. real_t tex_hue_variation = 0.0;
  768. if (curve_parameters[PARAM_HUE_VARIATION].is_valid()) {
  769. tex_hue_variation = curve_parameters[PARAM_HUE_VARIATION]->sample(tv);
  770. }
  771. real_t hue_rot_angle = (tex_hue_variation)*Math_TAU * Math::lerp(parameters_min[PARAM_HUE_VARIATION], parameters_max[PARAM_HUE_VARIATION], p.hue_rot_rand);
  772. real_t hue_rot_c = Math::cos(hue_rot_angle);
  773. real_t hue_rot_s = Math::sin(hue_rot_angle);
  774. Basis hue_rot_mat;
  775. {
  776. Basis mat1(0.299, 0.587, 0.114, 0.299, 0.587, 0.114, 0.299, 0.587, 0.114);
  777. Basis mat2(0.701, -0.587, -0.114, -0.299, 0.413, -0.114, -0.300, -0.588, 0.886);
  778. Basis mat3(0.168, 0.330, -0.497, -0.328, 0.035, 0.292, 1.250, -1.050, -0.203);
  779. for (int j = 0; j < 3; j++) {
  780. hue_rot_mat[j] = mat1[j] + mat2[j] * hue_rot_c + mat3[j] * hue_rot_s;
  781. }
  782. }
  783. if (color_ramp.is_valid()) {
  784. p.color = color_ramp->get_color_at_offset(tv) * color;
  785. } else {
  786. p.color = color;
  787. }
  788. Vector3 color_rgb = hue_rot_mat.xform_inv(Vector3(p.color.r, p.color.g, p.color.b));
  789. p.color.r = color_rgb.x;
  790. p.color.g = color_rgb.y;
  791. p.color.b = color_rgb.z;
  792. p.color *= p.base_color * p.start_color_rand;
  793. if (particle_flags[PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY]) {
  794. if (p.velocity.length() > 0.0) {
  795. p.transform.columns[1] = p.velocity.normalized();
  796. p.transform.columns[0] = p.transform.columns[1].orthogonal();
  797. }
  798. } else {
  799. p.transform.columns[0] = Vector2(Math::cos(p.rotation), -Math::sin(p.rotation));
  800. p.transform.columns[1] = Vector2(Math::sin(p.rotation), Math::cos(p.rotation));
  801. }
  802. //scale by scale
  803. Vector2 base_scale = tex_scale * Math::lerp(parameters_min[PARAM_SCALE], parameters_max[PARAM_SCALE], p.scale_rand);
  804. if (base_scale.x < 0.00001) {
  805. base_scale.x = 0.00001;
  806. }
  807. if (base_scale.y < 0.00001) {
  808. base_scale.y = 0.00001;
  809. }
  810. p.transform.columns[0] *= base_scale.x;
  811. p.transform.columns[1] *= base_scale.y;
  812. p.transform[2] += p.velocity * local_delta;
  813. }
  814. }
  815. void CPUParticles2D::_update_particle_data_buffer() {
  816. MutexLock lock(update_mutex);
  817. int pc = particles.size();
  818. int *ow;
  819. int *order = nullptr;
  820. float *w = particle_data.ptrw();
  821. const Particle *r = particles.ptr();
  822. float *ptr = w;
  823. if (draw_order != DRAW_ORDER_INDEX) {
  824. ow = particle_order.ptrw();
  825. order = ow;
  826. for (int i = 0; i < pc; i++) {
  827. order[i] = i;
  828. }
  829. if (draw_order == DRAW_ORDER_LIFETIME) {
  830. SortArray<int, SortLifetime> sorter;
  831. sorter.compare.particles = r;
  832. sorter.sort(order, pc);
  833. }
  834. }
  835. for (int i = 0; i < pc; i++) {
  836. int idx = order ? order[i] : i;
  837. Transform2D t = r[idx].transform;
  838. if (!local_coords) {
  839. t = inv_emission_transform * t;
  840. }
  841. if (r[idx].active) {
  842. ptr[0] = t.columns[0][0];
  843. ptr[1] = t.columns[1][0];
  844. ptr[2] = 0;
  845. ptr[3] = t.columns[2][0];
  846. ptr[4] = t.columns[0][1];
  847. ptr[5] = t.columns[1][1];
  848. ptr[6] = 0;
  849. ptr[7] = t.columns[2][1];
  850. } else {
  851. memset(ptr, 0, sizeof(float) * 8);
  852. }
  853. Color c = r[idx].color;
  854. ptr[8] = c.r;
  855. ptr[9] = c.g;
  856. ptr[10] = c.b;
  857. ptr[11] = c.a;
  858. ptr[12] = r[idx].custom[0];
  859. ptr[13] = r[idx].custom[1];
  860. ptr[14] = r[idx].custom[2];
  861. ptr[15] = r[idx].custom[3];
  862. ptr += 16;
  863. }
  864. }
  865. void CPUParticles2D::_set_do_redraw(bool p_do_redraw) {
  866. if (do_redraw == p_do_redraw) {
  867. return;
  868. }
  869. do_redraw = p_do_redraw;
  870. {
  871. MutexLock lock(update_mutex);
  872. if (do_redraw) {
  873. RS::get_singleton()->connect("frame_pre_draw", callable_mp(this, &CPUParticles2D::_update_render_thread));
  874. RS::get_singleton()->canvas_item_set_update_when_visible(get_canvas_item(), true);
  875. RS::get_singleton()->multimesh_set_visible_instances(multimesh, -1);
  876. } else {
  877. if (RS::get_singleton()->is_connected("frame_pre_draw", callable_mp(this, &CPUParticles2D::_update_render_thread))) {
  878. RS::get_singleton()->disconnect("frame_pre_draw", callable_mp(this, &CPUParticles2D::_update_render_thread));
  879. }
  880. RS::get_singleton()->canvas_item_set_update_when_visible(get_canvas_item(), false);
  881. RS::get_singleton()->multimesh_set_visible_instances(multimesh, 0);
  882. }
  883. }
  884. queue_redraw(); // redraw to update render list
  885. }
  886. void CPUParticles2D::_update_render_thread() {
  887. MutexLock lock(update_mutex);
  888. RS::get_singleton()->multimesh_set_buffer(multimesh, particle_data);
  889. }
  890. void CPUParticles2D::_notification(int p_what) {
  891. switch (p_what) {
  892. case NOTIFICATION_ENTER_TREE: {
  893. set_process_internal(emitting);
  894. } break;
  895. case NOTIFICATION_EXIT_TREE: {
  896. _set_do_redraw(false);
  897. } break;
  898. case NOTIFICATION_DRAW: {
  899. // first update before rendering to avoid one frame delay after emitting starts
  900. if (emitting && (time == 0)) {
  901. _update_internal();
  902. }
  903. if (!do_redraw) {
  904. return; // don't add to render list
  905. }
  906. RID texrid;
  907. if (texture.is_valid()) {
  908. texrid = texture->get_rid();
  909. }
  910. RS::get_singleton()->canvas_item_add_multimesh(get_canvas_item(), multimesh, texrid);
  911. } break;
  912. case NOTIFICATION_INTERNAL_PROCESS: {
  913. _update_internal();
  914. } break;
  915. case NOTIFICATION_TRANSFORM_CHANGED: {
  916. inv_emission_transform = get_global_transform().affine_inverse();
  917. if (!local_coords) {
  918. int pc = particles.size();
  919. float *w = particle_data.ptrw();
  920. const Particle *r = particles.ptr();
  921. float *ptr = w;
  922. for (int i = 0; i < pc; i++) {
  923. Transform2D t = inv_emission_transform * r[i].transform;
  924. if (r[i].active) {
  925. ptr[0] = t.columns[0][0];
  926. ptr[1] = t.columns[1][0];
  927. ptr[2] = 0;
  928. ptr[3] = t.columns[2][0];
  929. ptr[4] = t.columns[0][1];
  930. ptr[5] = t.columns[1][1];
  931. ptr[6] = 0;
  932. ptr[7] = t.columns[2][1];
  933. } else {
  934. memset(ptr, 0, sizeof(float) * 8);
  935. }
  936. ptr += 16;
  937. }
  938. }
  939. } break;
  940. }
  941. }
  942. void CPUParticles2D::convert_from_particles(Node *p_particles) {
  943. GPUParticles2D *gpu_particles = Object::cast_to<GPUParticles2D>(p_particles);
  944. ERR_FAIL_COND_MSG(!gpu_particles, "Only GPUParticles2D nodes can be converted to CPUParticles2D.");
  945. set_emitting(gpu_particles->is_emitting());
  946. set_amount(gpu_particles->get_amount());
  947. set_lifetime(gpu_particles->get_lifetime());
  948. set_one_shot(gpu_particles->get_one_shot());
  949. set_pre_process_time(gpu_particles->get_pre_process_time());
  950. set_explosiveness_ratio(gpu_particles->get_explosiveness_ratio());
  951. set_randomness_ratio(gpu_particles->get_randomness_ratio());
  952. set_use_local_coordinates(gpu_particles->get_use_local_coordinates());
  953. set_fixed_fps(gpu_particles->get_fixed_fps());
  954. set_fractional_delta(gpu_particles->get_fractional_delta());
  955. set_speed_scale(gpu_particles->get_speed_scale());
  956. set_draw_order(DrawOrder(gpu_particles->get_draw_order()));
  957. set_texture(gpu_particles->get_texture());
  958. Ref<Material> mat = gpu_particles->get_material();
  959. if (mat.is_valid()) {
  960. set_material(mat);
  961. }
  962. Ref<ParticleProcessMaterial> proc_mat = gpu_particles->get_process_material();
  963. if (proc_mat.is_null()) {
  964. return;
  965. }
  966. Vector3 dir = proc_mat->get_direction();
  967. set_direction(Vector2(dir.x, dir.y));
  968. set_spread(proc_mat->get_spread());
  969. set_color(proc_mat->get_color());
  970. Ref<GradientTexture1D> gt = proc_mat->get_color_ramp();
  971. if (gt.is_valid()) {
  972. set_color_ramp(gt->get_gradient());
  973. }
  974. Ref<GradientTexture1D> gti = proc_mat->get_color_initial_ramp();
  975. if (gti.is_valid()) {
  976. set_color_initial_ramp(gti->get_gradient());
  977. }
  978. set_particle_flag(PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY, proc_mat->get_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY));
  979. set_emission_shape(EmissionShape(proc_mat->get_emission_shape()));
  980. set_emission_sphere_radius(proc_mat->get_emission_sphere_radius());
  981. Vector2 rect_extents = Vector2(proc_mat->get_emission_box_extents().x, proc_mat->get_emission_box_extents().y);
  982. set_emission_rect_extents(rect_extents);
  983. Ref<CurveXYZTexture> scale3D = proc_mat->get_param_texture(ParticleProcessMaterial::PARAM_SCALE);
  984. if (scale3D.is_valid()) {
  985. split_scale = true;
  986. scale_curve_x = scale3D->get_curve_x();
  987. scale_curve_y = scale3D->get_curve_y();
  988. }
  989. set_gravity(Vector2(proc_mat->get_gravity().x, proc_mat->get_gravity().y));
  990. set_lifetime_randomness(proc_mat->get_lifetime_randomness());
  991. #define CONVERT_PARAM(m_param) \
  992. set_param_min(m_param, proc_mat->get_param_min(ParticleProcessMaterial::m_param)); \
  993. { \
  994. Ref<CurveTexture> ctex = proc_mat->get_param_texture(ParticleProcessMaterial::m_param); \
  995. if (ctex.is_valid()) \
  996. set_param_curve(m_param, ctex->get_curve()); \
  997. } \
  998. set_param_max(m_param, proc_mat->get_param_max(ParticleProcessMaterial::m_param));
  999. CONVERT_PARAM(PARAM_INITIAL_LINEAR_VELOCITY);
  1000. CONVERT_PARAM(PARAM_ANGULAR_VELOCITY);
  1001. CONVERT_PARAM(PARAM_ORBIT_VELOCITY);
  1002. CONVERT_PARAM(PARAM_LINEAR_ACCEL);
  1003. CONVERT_PARAM(PARAM_RADIAL_ACCEL);
  1004. CONVERT_PARAM(PARAM_TANGENTIAL_ACCEL);
  1005. CONVERT_PARAM(PARAM_DAMPING);
  1006. CONVERT_PARAM(PARAM_ANGLE);
  1007. CONVERT_PARAM(PARAM_SCALE);
  1008. CONVERT_PARAM(PARAM_HUE_VARIATION);
  1009. CONVERT_PARAM(PARAM_ANIM_SPEED);
  1010. CONVERT_PARAM(PARAM_ANIM_OFFSET);
  1011. #undef CONVERT_PARAM
  1012. }
  1013. void CPUParticles2D::_bind_methods() {
  1014. ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &CPUParticles2D::set_emitting);
  1015. ClassDB::bind_method(D_METHOD("set_amount", "amount"), &CPUParticles2D::set_amount);
  1016. ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &CPUParticles2D::set_lifetime);
  1017. ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &CPUParticles2D::set_one_shot);
  1018. ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &CPUParticles2D::set_pre_process_time);
  1019. ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &CPUParticles2D::set_explosiveness_ratio);
  1020. ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &CPUParticles2D::set_randomness_ratio);
  1021. ClassDB::bind_method(D_METHOD("set_lifetime_randomness", "random"), &CPUParticles2D::set_lifetime_randomness);
  1022. ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &CPUParticles2D::set_use_local_coordinates);
  1023. ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &CPUParticles2D::set_fixed_fps);
  1024. ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &CPUParticles2D::set_fractional_delta);
  1025. ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &CPUParticles2D::set_speed_scale);
  1026. ClassDB::bind_method(D_METHOD("is_emitting"), &CPUParticles2D::is_emitting);
  1027. ClassDB::bind_method(D_METHOD("get_amount"), &CPUParticles2D::get_amount);
  1028. ClassDB::bind_method(D_METHOD("get_lifetime"), &CPUParticles2D::get_lifetime);
  1029. ClassDB::bind_method(D_METHOD("get_one_shot"), &CPUParticles2D::get_one_shot);
  1030. ClassDB::bind_method(D_METHOD("get_pre_process_time"), &CPUParticles2D::get_pre_process_time);
  1031. ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &CPUParticles2D::get_explosiveness_ratio);
  1032. ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &CPUParticles2D::get_randomness_ratio);
  1033. ClassDB::bind_method(D_METHOD("get_lifetime_randomness"), &CPUParticles2D::get_lifetime_randomness);
  1034. ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &CPUParticles2D::get_use_local_coordinates);
  1035. ClassDB::bind_method(D_METHOD("get_fixed_fps"), &CPUParticles2D::get_fixed_fps);
  1036. ClassDB::bind_method(D_METHOD("get_fractional_delta"), &CPUParticles2D::get_fractional_delta);
  1037. ClassDB::bind_method(D_METHOD("get_speed_scale"), &CPUParticles2D::get_speed_scale);
  1038. ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &CPUParticles2D::set_draw_order);
  1039. ClassDB::bind_method(D_METHOD("get_draw_order"), &CPUParticles2D::get_draw_order);
  1040. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &CPUParticles2D::set_texture);
  1041. ClassDB::bind_method(D_METHOD("get_texture"), &CPUParticles2D::get_texture);
  1042. ClassDB::bind_method(D_METHOD("restart"), &CPUParticles2D::restart);
  1043. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting"), "set_emitting", "is_emitting");
  1044. ADD_PROPERTY(PropertyInfo(Variant::INT, "amount", PROPERTY_HINT_RANGE, "1,1000000,1,exp"), "set_amount", "get_amount");
  1045. ADD_GROUP("Time", "");
  1046. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime", PROPERTY_HINT_RANGE, "0.01,600.0,0.01,or_greater,suffix:s"), "set_lifetime", "get_lifetime");
  1047. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
  1048. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "preprocess", PROPERTY_HINT_RANGE, "0.00,600.0,0.01,suffix:s"), "set_pre_process_time", "get_pre_process_time");
  1049. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
  1050. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
  1051. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
  1052. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime_randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_lifetime_randomness", "get_lifetime_randomness");
  1053. ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1,suffix:FPS"), "set_fixed_fps", "get_fixed_fps");
  1054. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
  1055. ADD_GROUP("Drawing", "");
  1056. // No visibility_rect property contrarily to Particles2D, it's updated automatically.
  1057. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
  1058. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime"), "set_draw_order", "get_draw_order");
  1059. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  1060. BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
  1061. BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
  1062. ////////////////////////////////
  1063. ClassDB::bind_method(D_METHOD("set_direction", "direction"), &CPUParticles2D::set_direction);
  1064. ClassDB::bind_method(D_METHOD("get_direction"), &CPUParticles2D::get_direction);
  1065. ClassDB::bind_method(D_METHOD("set_spread", "spread"), &CPUParticles2D::set_spread);
  1066. ClassDB::bind_method(D_METHOD("get_spread"), &CPUParticles2D::get_spread);
  1067. ClassDB::bind_method(D_METHOD("set_param_min", "param", "value"), &CPUParticles2D::set_param_min);
  1068. ClassDB::bind_method(D_METHOD("get_param_min", "param"), &CPUParticles2D::get_param_min);
  1069. ClassDB::bind_method(D_METHOD("set_param_max", "param", "value"), &CPUParticles2D::set_param_max);
  1070. ClassDB::bind_method(D_METHOD("get_param_max", "param"), &CPUParticles2D::get_param_max);
  1071. ClassDB::bind_method(D_METHOD("set_param_curve", "param", "curve"), &CPUParticles2D::set_param_curve);
  1072. ClassDB::bind_method(D_METHOD("get_param_curve", "param"), &CPUParticles2D::get_param_curve);
  1073. ClassDB::bind_method(D_METHOD("set_color", "color"), &CPUParticles2D::set_color);
  1074. ClassDB::bind_method(D_METHOD("get_color"), &CPUParticles2D::get_color);
  1075. ClassDB::bind_method(D_METHOD("set_color_ramp", "ramp"), &CPUParticles2D::set_color_ramp);
  1076. ClassDB::bind_method(D_METHOD("get_color_ramp"), &CPUParticles2D::get_color_ramp);
  1077. ClassDB::bind_method(D_METHOD("set_color_initial_ramp", "ramp"), &CPUParticles2D::set_color_initial_ramp);
  1078. ClassDB::bind_method(D_METHOD("get_color_initial_ramp"), &CPUParticles2D::get_color_initial_ramp);
  1079. ClassDB::bind_method(D_METHOD("set_particle_flag", "particle_flag", "enable"), &CPUParticles2D::set_particle_flag);
  1080. ClassDB::bind_method(D_METHOD("get_particle_flag", "particle_flag"), &CPUParticles2D::get_particle_flag);
  1081. ClassDB::bind_method(D_METHOD("set_emission_shape", "shape"), &CPUParticles2D::set_emission_shape);
  1082. ClassDB::bind_method(D_METHOD("get_emission_shape"), &CPUParticles2D::get_emission_shape);
  1083. ClassDB::bind_method(D_METHOD("set_emission_sphere_radius", "radius"), &CPUParticles2D::set_emission_sphere_radius);
  1084. ClassDB::bind_method(D_METHOD("get_emission_sphere_radius"), &CPUParticles2D::get_emission_sphere_radius);
  1085. ClassDB::bind_method(D_METHOD("set_emission_rect_extents", "extents"), &CPUParticles2D::set_emission_rect_extents);
  1086. ClassDB::bind_method(D_METHOD("get_emission_rect_extents"), &CPUParticles2D::get_emission_rect_extents);
  1087. ClassDB::bind_method(D_METHOD("set_emission_points", "array"), &CPUParticles2D::set_emission_points);
  1088. ClassDB::bind_method(D_METHOD("get_emission_points"), &CPUParticles2D::get_emission_points);
  1089. ClassDB::bind_method(D_METHOD("set_emission_normals", "array"), &CPUParticles2D::set_emission_normals);
  1090. ClassDB::bind_method(D_METHOD("get_emission_normals"), &CPUParticles2D::get_emission_normals);
  1091. ClassDB::bind_method(D_METHOD("set_emission_colors", "array"), &CPUParticles2D::set_emission_colors);
  1092. ClassDB::bind_method(D_METHOD("get_emission_colors"), &CPUParticles2D::get_emission_colors);
  1093. ClassDB::bind_method(D_METHOD("get_gravity"), &CPUParticles2D::get_gravity);
  1094. ClassDB::bind_method(D_METHOD("set_gravity", "accel_vec"), &CPUParticles2D::set_gravity);
  1095. ClassDB::bind_method(D_METHOD("get_split_scale"), &CPUParticles2D::get_split_scale);
  1096. ClassDB::bind_method(D_METHOD("set_split_scale", "split_scale"), &CPUParticles2D::set_split_scale);
  1097. ClassDB::bind_method(D_METHOD("get_scale_curve_x"), &CPUParticles2D::get_scale_curve_x);
  1098. ClassDB::bind_method(D_METHOD("set_scale_curve_x", "scale_curve"), &CPUParticles2D::set_scale_curve_x);
  1099. ClassDB::bind_method(D_METHOD("get_scale_curve_y"), &CPUParticles2D::get_scale_curve_y);
  1100. ClassDB::bind_method(D_METHOD("set_scale_curve_y", "scale_curve"), &CPUParticles2D::set_scale_curve_y);
  1101. ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &CPUParticles2D::convert_from_particles);
  1102. ADD_GROUP("Emission Shape", "emission_");
  1103. ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_shape", PROPERTY_HINT_ENUM, "Point,Sphere,Sphere Surface,Rectangle,Points,Directed Points", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_emission_shape", "get_emission_shape");
  1104. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_sphere_radius", PROPERTY_HINT_RANGE, "0.01,128,0.01,suffix:px"), "set_emission_sphere_radius", "get_emission_sphere_radius");
  1105. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "emission_rect_extents", PROPERTY_HINT_NONE, "suffix:px"), "set_emission_rect_extents", "get_emission_rect_extents");
  1106. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "emission_points"), "set_emission_points", "get_emission_points");
  1107. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "emission_normals"), "set_emission_normals", "get_emission_normals");
  1108. ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "emission_colors"), "set_emission_colors", "get_emission_colors");
  1109. ADD_GROUP("Particle Flags", "particle_flag_");
  1110. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_align_y"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY);
  1111. ADD_GROUP("Direction", "");
  1112. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "direction"), "set_direction", "get_direction");
  1113. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "spread", PROPERTY_HINT_RANGE, "0,180,0.01"), "set_spread", "get_spread");
  1114. ADD_GROUP("Gravity", "");
  1115. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "gravity", PROPERTY_HINT_NONE, U"suffix:px/s\u00B2"), "set_gravity", "get_gravity");
  1116. ADD_GROUP("Initial Velocity", "initial_");
  1117. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "initial_velocity_min", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater,suffix:px/s"), "set_param_min", "get_param_min", PARAM_INITIAL_LINEAR_VELOCITY);
  1118. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "initial_velocity_max", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater,suffix:px/s"), "set_param_max", "get_param_max", PARAM_INITIAL_LINEAR_VELOCITY);
  1119. ADD_GROUP("Angular Velocity", "angular_");
  1120. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angular_velocity_min", PROPERTY_HINT_RANGE, "-720,720,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_ANGULAR_VELOCITY);
  1121. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angular_velocity_max", PROPERTY_HINT_RANGE, "-720,720,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_ANGULAR_VELOCITY);
  1122. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angular_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGULAR_VELOCITY);
  1123. ADD_GROUP("Orbit Velocity", "orbit_");
  1124. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "orbit_velocity_min", PROPERTY_HINT_RANGE, "-1000,1000,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_ORBIT_VELOCITY);
  1125. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "orbit_velocity_max", PROPERTY_HINT_RANGE, "-1000,1000,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_ORBIT_VELOCITY);
  1126. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "orbit_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ORBIT_VELOCITY);
  1127. ADD_GROUP("Linear Accel", "linear_");
  1128. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "linear_accel_min", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_LINEAR_ACCEL);
  1129. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "linear_accel_max", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_LINEAR_ACCEL);
  1130. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "linear_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_LINEAR_ACCEL);
  1131. ADD_GROUP("Radial Accel", "radial_");
  1132. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "radial_accel_min", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_RADIAL_ACCEL);
  1133. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "radial_accel_max", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_RADIAL_ACCEL);
  1134. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "radial_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_RADIAL_ACCEL);
  1135. ADD_GROUP("Tangential Accel", "tangential_");
  1136. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "tangential_accel_min", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_TANGENTIAL_ACCEL);
  1137. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "tangential_accel_max", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_TANGENTIAL_ACCEL);
  1138. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "tangential_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_TANGENTIAL_ACCEL);
  1139. ADD_GROUP("Damping", "");
  1140. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "damping_min", PROPERTY_HINT_RANGE, "0,100,0.01,or_greater"), "set_param_min", "get_param_min", PARAM_DAMPING);
  1141. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "damping_max", PROPERTY_HINT_RANGE, "0,100,0.01,or_greater"), "set_param_max", "get_param_max", PARAM_DAMPING);
  1142. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "damping_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_DAMPING);
  1143. ADD_GROUP("Angle", "");
  1144. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angle_min", PROPERTY_HINT_RANGE, "-720,720,0.1,or_less,or_greater,degrees"), "set_param_min", "get_param_min", PARAM_ANGLE);
  1145. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angle_max", PROPERTY_HINT_RANGE, "-720,720,0.1,or_less,or_greater,degrees"), "set_param_max", "get_param_max", PARAM_ANGLE);
  1146. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angle_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGLE);
  1147. ADD_GROUP("Scale", "");
  1148. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "scale_amount_min", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param_min", "get_param_min", PARAM_SCALE);
  1149. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "scale_amount_max", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param_max", "get_param_max", PARAM_SCALE);
  1150. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_amount_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_SCALE);
  1151. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "split_scale"), "set_split_scale", "get_split_scale");
  1152. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scale_curve_x", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_scale_curve_x", "get_scale_curve_x");
  1153. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scale_curve_y", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_scale_curve_y", "get_scale_curve_y");
  1154. ADD_GROUP("Color", "");
  1155. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  1156. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");
  1157. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_initial_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_initial_ramp", "get_color_initial_ramp");
  1158. ADD_GROUP("Hue Variation", "hue_");
  1159. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "hue_variation_min", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param_min", "get_param_min", PARAM_HUE_VARIATION);
  1160. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "hue_variation_max", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param_max", "get_param_max", PARAM_HUE_VARIATION);
  1161. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "hue_variation_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_HUE_VARIATION);
  1162. ADD_GROUP("Animation", "anim_");
  1163. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_speed_min", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater,or_less"), "set_param_min", "get_param_min", PARAM_ANIM_SPEED);
  1164. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_speed_max", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater,or_less"), "set_param_max", "get_param_max", PARAM_ANIM_SPEED);
  1165. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_speed_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_SPEED);
  1166. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_offset_min", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_param_min", "get_param_min", PARAM_ANIM_OFFSET);
  1167. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_offset_max", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_param_max", "get_param_max", PARAM_ANIM_OFFSET);
  1168. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_offset_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_OFFSET);
  1169. BIND_ENUM_CONSTANT(PARAM_INITIAL_LINEAR_VELOCITY);
  1170. BIND_ENUM_CONSTANT(PARAM_ANGULAR_VELOCITY);
  1171. BIND_ENUM_CONSTANT(PARAM_ORBIT_VELOCITY);
  1172. BIND_ENUM_CONSTANT(PARAM_LINEAR_ACCEL);
  1173. BIND_ENUM_CONSTANT(PARAM_RADIAL_ACCEL);
  1174. BIND_ENUM_CONSTANT(PARAM_TANGENTIAL_ACCEL);
  1175. BIND_ENUM_CONSTANT(PARAM_DAMPING);
  1176. BIND_ENUM_CONSTANT(PARAM_ANGLE);
  1177. BIND_ENUM_CONSTANT(PARAM_SCALE);
  1178. BIND_ENUM_CONSTANT(PARAM_HUE_VARIATION);
  1179. BIND_ENUM_CONSTANT(PARAM_ANIM_SPEED);
  1180. BIND_ENUM_CONSTANT(PARAM_ANIM_OFFSET);
  1181. BIND_ENUM_CONSTANT(PARAM_MAX);
  1182. BIND_ENUM_CONSTANT(PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY);
  1183. BIND_ENUM_CONSTANT(PARTICLE_FLAG_ROTATE_Y); // Unused, but exposed for consistency with 3D.
  1184. BIND_ENUM_CONSTANT(PARTICLE_FLAG_DISABLE_Z); // Unused, but exposed for consistency with 3D.
  1185. BIND_ENUM_CONSTANT(PARTICLE_FLAG_MAX);
  1186. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINT);
  1187. BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE);
  1188. BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE_SURFACE);
  1189. BIND_ENUM_CONSTANT(EMISSION_SHAPE_RECTANGLE);
  1190. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINTS);
  1191. BIND_ENUM_CONSTANT(EMISSION_SHAPE_DIRECTED_POINTS);
  1192. BIND_ENUM_CONSTANT(EMISSION_SHAPE_MAX);
  1193. }
  1194. CPUParticles2D::CPUParticles2D() {
  1195. mesh = RenderingServer::get_singleton()->mesh_create();
  1196. multimesh = RenderingServer::get_singleton()->multimesh_create();
  1197. RenderingServer::get_singleton()->multimesh_set_mesh(multimesh, mesh);
  1198. set_emitting(true);
  1199. set_amount(8);
  1200. set_use_local_coordinates(false);
  1201. set_param_min(PARAM_INITIAL_LINEAR_VELOCITY, 0);
  1202. set_param_min(PARAM_ANGULAR_VELOCITY, 0);
  1203. set_param_min(PARAM_ORBIT_VELOCITY, 0);
  1204. set_param_min(PARAM_LINEAR_ACCEL, 0);
  1205. set_param_min(PARAM_RADIAL_ACCEL, 0);
  1206. set_param_min(PARAM_TANGENTIAL_ACCEL, 0);
  1207. set_param_min(PARAM_DAMPING, 0);
  1208. set_param_min(PARAM_ANGLE, 0);
  1209. set_param_min(PARAM_SCALE, 1);
  1210. set_param_min(PARAM_HUE_VARIATION, 0);
  1211. set_param_min(PARAM_ANIM_SPEED, 0);
  1212. set_param_min(PARAM_ANIM_OFFSET, 0);
  1213. set_param_max(PARAM_INITIAL_LINEAR_VELOCITY, 0);
  1214. set_param_max(PARAM_ANGULAR_VELOCITY, 0);
  1215. set_param_max(PARAM_ORBIT_VELOCITY, 0);
  1216. set_param_max(PARAM_LINEAR_ACCEL, 0);
  1217. set_param_max(PARAM_RADIAL_ACCEL, 0);
  1218. set_param_max(PARAM_TANGENTIAL_ACCEL, 0);
  1219. set_param_max(PARAM_DAMPING, 0);
  1220. set_param_max(PARAM_ANGLE, 0);
  1221. set_param_max(PARAM_SCALE, 1);
  1222. set_param_max(PARAM_HUE_VARIATION, 0);
  1223. set_param_max(PARAM_ANIM_SPEED, 0);
  1224. set_param_max(PARAM_ANIM_OFFSET, 0);
  1225. for (int i = 0; i < PARTICLE_FLAG_MAX; i++) {
  1226. particle_flags[i] = false;
  1227. }
  1228. set_color(Color(1, 1, 1, 1));
  1229. _update_mesh_texture();
  1230. }
  1231. CPUParticles2D::~CPUParticles2D() {
  1232. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1233. RS::get_singleton()->free(multimesh);
  1234. RS::get_singleton()->free(mesh);
  1235. }