cpu_particles_2d.cpp 62 KB

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