cpu_particles_2d.cpp 56 KB

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