cpu_particles_2d.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  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/canvas_item_material.h"
  34. #include "scene/resources/curve_texture.h"
  35. #include "scene/resources/gradient_texture.h"
  36. #include "scene/resources/particle_process_material.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_texture = Object::cast_to<AtlasTexture>(*texture);
  137. if (atlas_texture && atlas_texture->get_atlas().is_valid()) {
  138. Rect2 region_rect = atlas_texture->get_region();
  139. Size2 atlas_size = atlas_texture->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. PackedStringArray CPUParticles2D::get_configuration_warnings() const {
  202. PackedStringArray 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_null()) {
  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 == "emitting") {
  406. p_property.hint = one_shot ? PROPERTY_HINT_ONESHOT : PROPERTY_HINT_NONE;
  407. }
  408. if (p_property.name == "emission_sphere_radius" && (emission_shape != EMISSION_SHAPE_SPHERE && emission_shape != EMISSION_SHAPE_SPHERE_SURFACE)) {
  409. p_property.usage = PROPERTY_USAGE_NONE;
  410. }
  411. if (p_property.name == "emission_rect_extents" && emission_shape != EMISSION_SHAPE_RECTANGLE) {
  412. p_property.usage = PROPERTY_USAGE_NONE;
  413. }
  414. if ((p_property.name == "emission_point_texture" || p_property.name == "emission_color_texture") && (emission_shape < EMISSION_SHAPE_POINTS)) {
  415. p_property.usage = PROPERTY_USAGE_NONE;
  416. }
  417. if (p_property.name == "emission_normals" && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
  418. p_property.usage = PROPERTY_USAGE_NONE;
  419. }
  420. if (p_property.name == "emission_points" && emission_shape != EMISSION_SHAPE_POINTS && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
  421. p_property.usage = PROPERTY_USAGE_NONE;
  422. }
  423. if (p_property.name == "emission_colors" && emission_shape != EMISSION_SHAPE_POINTS && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
  424. p_property.usage = PROPERTY_USAGE_NONE;
  425. }
  426. if (p_property.name.begins_with("scale_curve_") && !split_scale) {
  427. p_property.usage = PROPERTY_USAGE_NONE;
  428. }
  429. }
  430. static uint32_t idhash(uint32_t x) {
  431. x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
  432. x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
  433. x = (x >> uint32_t(16)) ^ x;
  434. return x;
  435. }
  436. static real_t rand_from_seed(uint32_t &seed) {
  437. int k;
  438. int s = int(seed);
  439. if (s == 0) {
  440. s = 305420679;
  441. }
  442. k = s / 127773;
  443. s = 16807 * (s - k * 127773) - 2836 * k;
  444. if (s < 0) {
  445. s += 2147483647;
  446. }
  447. seed = uint32_t(s);
  448. return (seed % uint32_t(65536)) / 65535.0;
  449. }
  450. void CPUParticles2D::_update_internal() {
  451. if (particles.size() == 0 || !is_visible_in_tree()) {
  452. _set_do_redraw(false);
  453. return;
  454. }
  455. double delta = get_process_delta_time();
  456. if (!active && !emitting) {
  457. set_process_internal(false);
  458. _set_do_redraw(false);
  459. //reset variables
  460. time = 0;
  461. frame_remainder = 0;
  462. cycle = 0;
  463. return;
  464. }
  465. _set_do_redraw(true);
  466. if (time == 0 && pre_process_time > 0.0) {
  467. double frame_time;
  468. if (fixed_fps > 0) {
  469. frame_time = 1.0 / fixed_fps;
  470. } else {
  471. frame_time = 1.0 / 30.0;
  472. }
  473. double todo = pre_process_time;
  474. while (todo >= 0) {
  475. _particles_process(frame_time);
  476. todo -= frame_time;
  477. }
  478. }
  479. if (fixed_fps > 0) {
  480. double frame_time = 1.0 / fixed_fps;
  481. double decr = frame_time;
  482. double ldelta = delta;
  483. if (ldelta > 0.1) { //avoid recursive stalls if fps goes below 10
  484. ldelta = 0.1;
  485. } else if (ldelta <= 0.0) { //unlikely but..
  486. ldelta = 0.001;
  487. }
  488. double todo = frame_remainder + ldelta;
  489. while (todo >= frame_time) {
  490. _particles_process(frame_time);
  491. todo -= decr;
  492. }
  493. frame_remainder = todo;
  494. } else {
  495. _particles_process(delta);
  496. }
  497. _update_particle_data_buffer();
  498. }
  499. void CPUParticles2D::_particles_process(double p_delta) {
  500. p_delta *= speed_scale;
  501. int pcount = particles.size();
  502. Particle *w = particles.ptrw();
  503. Particle *parray = w;
  504. double prev_time = time;
  505. time += p_delta;
  506. if (time > lifetime) {
  507. time = Math::fmod(time, lifetime);
  508. cycle++;
  509. if (one_shot && cycle > 0) {
  510. set_emitting(false);
  511. notify_property_list_changed();
  512. }
  513. }
  514. Transform2D emission_xform;
  515. Transform2D velocity_xform;
  516. if (!local_coords) {
  517. emission_xform = get_global_transform();
  518. velocity_xform = emission_xform;
  519. velocity_xform[2] = Vector2();
  520. }
  521. double system_phase = time / lifetime;
  522. bool should_be_active = false;
  523. for (int i = 0; i < pcount; i++) {
  524. Particle &p = parray[i];
  525. if (!emitting && !p.active) {
  526. continue;
  527. }
  528. double local_delta = p_delta;
  529. // The phase is a ratio between 0 (birth) and 1 (end of life) for each particle.
  530. // While we use time in tests later on, for randomness we use the phase as done in the
  531. // original shader code, and we later multiply by lifetime to get the time.
  532. double restart_phase = double(i) / double(pcount);
  533. if (randomness_ratio > 0.0) {
  534. uint32_t seed = cycle;
  535. if (restart_phase >= system_phase) {
  536. seed -= uint32_t(1);
  537. }
  538. seed *= uint32_t(pcount);
  539. seed += uint32_t(i);
  540. double random = double(idhash(seed) % uint32_t(65536)) / 65536.0;
  541. restart_phase += randomness_ratio * random * 1.0 / double(pcount);
  542. }
  543. restart_phase *= (1.0 - explosiveness_ratio);
  544. double restart_time = restart_phase * lifetime;
  545. bool restart = false;
  546. if (time > prev_time) {
  547. // restart_time >= prev_time is used so particles emit in the first frame they are processed
  548. if (restart_time >= prev_time && restart_time < time) {
  549. restart = true;
  550. if (fractional_delta) {
  551. local_delta = time - restart_time;
  552. }
  553. }
  554. } else if (local_delta > 0.0) {
  555. if (restart_time >= prev_time) {
  556. restart = true;
  557. if (fractional_delta) {
  558. local_delta = lifetime - restart_time + time;
  559. }
  560. } else if (restart_time < time) {
  561. restart = true;
  562. if (fractional_delta) {
  563. local_delta = time - restart_time;
  564. }
  565. }
  566. }
  567. if (p.time * (1.0 - explosiveness_ratio) > p.lifetime) {
  568. restart = true;
  569. }
  570. float tv = 0.0;
  571. if (restart) {
  572. if (!emitting) {
  573. p.active = false;
  574. continue;
  575. }
  576. p.active = true;
  577. /*real_t tex_linear_velocity = 0;
  578. if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  579. tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->sample(0);
  580. }*/
  581. real_t tex_angle = 1.0;
  582. if (curve_parameters[PARAM_ANGLE].is_valid()) {
  583. tex_angle = curve_parameters[PARAM_ANGLE]->sample(tv);
  584. }
  585. real_t tex_anim_offset = 1.0;
  586. if (curve_parameters[PARAM_ANGLE].is_valid()) {
  587. tex_anim_offset = curve_parameters[PARAM_ANGLE]->sample(tv);
  588. }
  589. p.seed = Math::rand();
  590. p.angle_rand = Math::randf();
  591. p.scale_rand = Math::randf();
  592. p.hue_rot_rand = Math::randf();
  593. p.anim_offset_rand = Math::randf();
  594. if (color_initial_ramp.is_valid()) {
  595. p.start_color_rand = color_initial_ramp->get_color_at_offset(Math::randf());
  596. } else {
  597. p.start_color_rand = Color(1, 1, 1, 1);
  598. }
  599. real_t angle1_rad = direction.angle() + Math::deg_to_rad((Math::randf() * 2.0 - 1.0) * spread);
  600. Vector2 rot = Vector2(Math::cos(angle1_rad), Math::sin(angle1_rad));
  601. p.velocity = rot * Math::lerp(parameters_min[PARAM_INITIAL_LINEAR_VELOCITY], parameters_max[PARAM_INITIAL_LINEAR_VELOCITY], (real_t)Math::randf());
  602. real_t base_angle = tex_angle * Math::lerp(parameters_min[PARAM_ANGLE], parameters_max[PARAM_ANGLE], p.angle_rand);
  603. p.rotation = Math::deg_to_rad(base_angle);
  604. p.custom[0] = 0.0; // unused
  605. p.custom[1] = 0.0; // phase [0..1]
  606. p.custom[2] = tex_anim_offset * Math::lerp(parameters_min[PARAM_ANIM_OFFSET], parameters_max[PARAM_ANIM_OFFSET], p.anim_offset_rand);
  607. p.custom[3] = (1.0 - Math::randf() * lifetime_randomness);
  608. p.transform = Transform2D();
  609. p.time = 0;
  610. p.lifetime = lifetime * p.custom[3];
  611. p.base_color = Color(1, 1, 1, 1);
  612. switch (emission_shape) {
  613. case EMISSION_SHAPE_POINT: {
  614. //do none
  615. } break;
  616. case EMISSION_SHAPE_SPHERE: {
  617. real_t t = Math_TAU * Math::randf();
  618. real_t radius = emission_sphere_radius * Math::randf();
  619. p.transform[2] = Vector2(Math::cos(t), Math::sin(t)) * radius;
  620. } break;
  621. case EMISSION_SHAPE_SPHERE_SURFACE: {
  622. real_t s = Math::randf(), t = Math_TAU * Math::randf();
  623. real_t radius = emission_sphere_radius * Math::sqrt(1.0 - s * s);
  624. p.transform[2] = Vector2(Math::cos(t), Math::sin(t)) * radius;
  625. } break;
  626. case EMISSION_SHAPE_RECTANGLE: {
  627. p.transform[2] = Vector2(Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0) * emission_rect_extents;
  628. } break;
  629. case EMISSION_SHAPE_POINTS:
  630. case EMISSION_SHAPE_DIRECTED_POINTS: {
  631. int pc = emission_points.size();
  632. if (pc == 0) {
  633. break;
  634. }
  635. int random_idx = Math::rand() % pc;
  636. p.transform[2] = emission_points.get(random_idx);
  637. if (emission_shape == EMISSION_SHAPE_DIRECTED_POINTS && emission_normals.size() == pc) {
  638. Vector2 normal = emission_normals.get(random_idx);
  639. Transform2D m2;
  640. m2.columns[0] = normal;
  641. m2.columns[1] = normal.orthogonal();
  642. p.velocity = m2.basis_xform(p.velocity);
  643. }
  644. if (emission_colors.size() == pc) {
  645. p.base_color = emission_colors.get(random_idx);
  646. }
  647. } break;
  648. case EMISSION_SHAPE_MAX: { // Max value for validity check.
  649. break;
  650. }
  651. }
  652. if (!local_coords) {
  653. p.velocity = velocity_xform.xform(p.velocity);
  654. p.transform = emission_xform * p.transform;
  655. }
  656. } else if (!p.active) {
  657. continue;
  658. } else if (p.time > p.lifetime) {
  659. p.active = false;
  660. tv = 1.0;
  661. } else {
  662. uint32_t alt_seed = p.seed;
  663. p.time += local_delta;
  664. p.custom[1] = p.time / lifetime;
  665. tv = p.time / p.lifetime;
  666. real_t tex_linear_velocity = 1.0;
  667. if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  668. tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->sample(tv);
  669. }
  670. real_t tex_orbit_velocity = 1.0;
  671. if (curve_parameters[PARAM_ORBIT_VELOCITY].is_valid()) {
  672. tex_orbit_velocity = curve_parameters[PARAM_ORBIT_VELOCITY]->sample(tv);
  673. }
  674. real_t tex_angular_velocity = 1.0;
  675. if (curve_parameters[PARAM_ANGULAR_VELOCITY].is_valid()) {
  676. tex_angular_velocity = curve_parameters[PARAM_ANGULAR_VELOCITY]->sample(tv);
  677. }
  678. real_t tex_linear_accel = 1.0;
  679. if (curve_parameters[PARAM_LINEAR_ACCEL].is_valid()) {
  680. tex_linear_accel = curve_parameters[PARAM_LINEAR_ACCEL]->sample(tv);
  681. }
  682. real_t tex_tangential_accel = 1.0;
  683. if (curve_parameters[PARAM_TANGENTIAL_ACCEL].is_valid()) {
  684. tex_tangential_accel = curve_parameters[PARAM_TANGENTIAL_ACCEL]->sample(tv);
  685. }
  686. real_t tex_radial_accel = 1.0;
  687. if (curve_parameters[PARAM_RADIAL_ACCEL].is_valid()) {
  688. tex_radial_accel = curve_parameters[PARAM_RADIAL_ACCEL]->sample(tv);
  689. }
  690. real_t tex_damping = 1.0;
  691. if (curve_parameters[PARAM_DAMPING].is_valid()) {
  692. tex_damping = curve_parameters[PARAM_DAMPING]->sample(tv);
  693. }
  694. real_t tex_angle = 1.0;
  695. if (curve_parameters[PARAM_ANGLE].is_valid()) {
  696. tex_angle = curve_parameters[PARAM_ANGLE]->sample(tv);
  697. }
  698. real_t tex_anim_speed = 1.0;
  699. if (curve_parameters[PARAM_ANIM_SPEED].is_valid()) {
  700. tex_anim_speed = curve_parameters[PARAM_ANIM_SPEED]->sample(tv);
  701. }
  702. real_t tex_anim_offset = 1.0;
  703. if (curve_parameters[PARAM_ANIM_OFFSET].is_valid()) {
  704. tex_anim_offset = curve_parameters[PARAM_ANIM_OFFSET]->sample(tv);
  705. }
  706. Vector2 force = gravity;
  707. Vector2 pos = p.transform[2];
  708. //apply linear acceleration
  709. 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();
  710. //apply radial acceleration
  711. Vector2 org = emission_xform[2];
  712. Vector2 diff = pos - org;
  713. 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();
  714. //apply tangential acceleration;
  715. Vector2 yx = Vector2(diff.y, diff.x);
  716. 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();
  717. //apply attractor forces
  718. p.velocity += force * local_delta;
  719. //orbit velocity
  720. real_t orbit_amount = tex_orbit_velocity * Math::lerp(parameters_min[PARAM_ORBIT_VELOCITY], parameters_max[PARAM_ORBIT_VELOCITY], rand_from_seed(alt_seed));
  721. if (orbit_amount != 0.0) {
  722. real_t ang = orbit_amount * local_delta * Math_TAU;
  723. // Not sure why the ParticleProcessMaterial code uses a clockwise rotation matrix,
  724. // but we use -ang here to reproduce its behavior.
  725. Transform2D rot = Transform2D(-ang, Vector2());
  726. p.transform[2] -= diff;
  727. p.transform[2] += rot.basis_xform(diff);
  728. }
  729. if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  730. p.velocity = p.velocity.normalized() * tex_linear_velocity;
  731. }
  732. if (parameters_max[PARAM_DAMPING] + tex_damping > 0.0) {
  733. real_t v = p.velocity.length();
  734. real_t damp = tex_damping * Math::lerp(parameters_min[PARAM_DAMPING], parameters_max[PARAM_DAMPING], rand_from_seed(alt_seed));
  735. v -= damp * local_delta;
  736. if (v < 0.0) {
  737. p.velocity = Vector2();
  738. } else {
  739. p.velocity = p.velocity.normalized() * v;
  740. }
  741. }
  742. real_t base_angle = (tex_angle)*Math::lerp(parameters_min[PARAM_ANGLE], parameters_max[PARAM_ANGLE], p.angle_rand);
  743. 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));
  744. p.rotation = Math::deg_to_rad(base_angle); //angle
  745. 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));
  746. }
  747. //apply color
  748. //apply hue rotation
  749. Vector2 tex_scale = Vector2(1.0, 1.0);
  750. if (split_scale) {
  751. if (scale_curve_x.is_valid()) {
  752. tex_scale.x = scale_curve_x->sample(tv);
  753. } else {
  754. tex_scale.x = 1.0;
  755. }
  756. if (scale_curve_y.is_valid()) {
  757. tex_scale.y = scale_curve_y->sample(tv);
  758. } else {
  759. tex_scale.y = 1.0;
  760. }
  761. } else {
  762. if (curve_parameters[PARAM_SCALE].is_valid()) {
  763. real_t tmp_scale = curve_parameters[PARAM_SCALE]->sample(tv);
  764. tex_scale.x = tmp_scale;
  765. tex_scale.y = tmp_scale;
  766. }
  767. }
  768. real_t tex_hue_variation = 0.0;
  769. if (curve_parameters[PARAM_HUE_VARIATION].is_valid()) {
  770. tex_hue_variation = curve_parameters[PARAM_HUE_VARIATION]->sample(tv);
  771. }
  772. 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);
  773. real_t hue_rot_c = Math::cos(hue_rot_angle);
  774. real_t hue_rot_s = Math::sin(hue_rot_angle);
  775. Basis hue_rot_mat;
  776. {
  777. Basis mat1(0.299, 0.587, 0.114, 0.299, 0.587, 0.114, 0.299, 0.587, 0.114);
  778. Basis mat2(0.701, -0.587, -0.114, -0.299, 0.413, -0.114, -0.300, -0.588, 0.886);
  779. Basis mat3(0.168, 0.330, -0.497, -0.328, 0.035, 0.292, 1.250, -1.050, -0.203);
  780. for (int j = 0; j < 3; j++) {
  781. hue_rot_mat[j] = mat1[j] + mat2[j] * hue_rot_c + mat3[j] * hue_rot_s;
  782. }
  783. }
  784. if (color_ramp.is_valid()) {
  785. p.color = color_ramp->get_color_at_offset(tv) * color;
  786. } else {
  787. p.color = color;
  788. }
  789. Vector3 color_rgb = hue_rot_mat.xform_inv(Vector3(p.color.r, p.color.g, p.color.b));
  790. p.color.r = color_rgb.x;
  791. p.color.g = color_rgb.y;
  792. p.color.b = color_rgb.z;
  793. p.color *= p.base_color * p.start_color_rand;
  794. if (particle_flags[PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY]) {
  795. if (p.velocity.length() > 0.0) {
  796. p.transform.columns[1] = p.velocity.normalized();
  797. p.transform.columns[0] = p.transform.columns[1].orthogonal();
  798. }
  799. } else {
  800. p.transform.columns[0] = Vector2(Math::cos(p.rotation), -Math::sin(p.rotation));
  801. p.transform.columns[1] = Vector2(Math::sin(p.rotation), Math::cos(p.rotation));
  802. }
  803. //scale by scale
  804. Vector2 base_scale = tex_scale * Math::lerp(parameters_min[PARAM_SCALE], parameters_max[PARAM_SCALE], p.scale_rand);
  805. if (base_scale.x < 0.00001) {
  806. base_scale.x = 0.00001;
  807. }
  808. if (base_scale.y < 0.00001) {
  809. base_scale.y = 0.00001;
  810. }
  811. p.transform.columns[0] *= base_scale.x;
  812. p.transform.columns[1] *= base_scale.y;
  813. p.transform[2] += p.velocity * local_delta;
  814. should_be_active = true;
  815. }
  816. if (!Math::is_equal_approx(time, 0.0) && active && !should_be_active) {
  817. active = false;
  818. emit_signal(SceneStringName(finished));
  819. }
  820. }
  821. void CPUParticles2D::_update_particle_data_buffer() {
  822. MutexLock lock(update_mutex);
  823. int pc = particles.size();
  824. int *ow;
  825. int *order = nullptr;
  826. float *w = particle_data.ptrw();
  827. const Particle *r = particles.ptr();
  828. float *ptr = w;
  829. if (draw_order != DRAW_ORDER_INDEX) {
  830. ow = particle_order.ptrw();
  831. order = ow;
  832. for (int i = 0; i < pc; i++) {
  833. order[i] = i;
  834. }
  835. if (draw_order == DRAW_ORDER_LIFETIME) {
  836. SortArray<int, SortLifetime> sorter;
  837. sorter.compare.particles = r;
  838. sorter.sort(order, pc);
  839. }
  840. }
  841. for (int i = 0; i < pc; i++) {
  842. int idx = order ? order[i] : i;
  843. Transform2D t = r[idx].transform;
  844. if (!local_coords) {
  845. t = inv_emission_transform * t;
  846. }
  847. if (r[idx].active) {
  848. ptr[0] = t.columns[0][0];
  849. ptr[1] = t.columns[1][0];
  850. ptr[2] = 0;
  851. ptr[3] = t.columns[2][0];
  852. ptr[4] = t.columns[0][1];
  853. ptr[5] = t.columns[1][1];
  854. ptr[6] = 0;
  855. ptr[7] = t.columns[2][1];
  856. } else {
  857. memset(ptr, 0, sizeof(float) * 8);
  858. }
  859. Color c = r[idx].color;
  860. ptr[8] = c.r;
  861. ptr[9] = c.g;
  862. ptr[10] = c.b;
  863. ptr[11] = c.a;
  864. ptr[12] = r[idx].custom[0];
  865. ptr[13] = r[idx].custom[1];
  866. ptr[14] = r[idx].custom[2];
  867. ptr[15] = r[idx].custom[3];
  868. ptr += 16;
  869. }
  870. }
  871. void CPUParticles2D::_set_do_redraw(bool p_do_redraw) {
  872. if (do_redraw == p_do_redraw) {
  873. return;
  874. }
  875. do_redraw = p_do_redraw;
  876. {
  877. MutexLock lock(update_mutex);
  878. if (do_redraw) {
  879. RS::get_singleton()->connect("frame_pre_draw", callable_mp(this, &CPUParticles2D::_update_render_thread));
  880. RS::get_singleton()->canvas_item_set_update_when_visible(get_canvas_item(), true);
  881. RS::get_singleton()->multimesh_set_visible_instances(multimesh, -1);
  882. } else {
  883. if (RS::get_singleton()->is_connected("frame_pre_draw", callable_mp(this, &CPUParticles2D::_update_render_thread))) {
  884. RS::get_singleton()->disconnect("frame_pre_draw", callable_mp(this, &CPUParticles2D::_update_render_thread));
  885. }
  886. RS::get_singleton()->canvas_item_set_update_when_visible(get_canvas_item(), false);
  887. RS::get_singleton()->multimesh_set_visible_instances(multimesh, 0);
  888. }
  889. }
  890. queue_redraw(); // redraw to update render list
  891. }
  892. void CPUParticles2D::_update_render_thread() {
  893. MutexLock lock(update_mutex);
  894. RS::get_singleton()->multimesh_set_buffer(multimesh, particle_data);
  895. }
  896. void CPUParticles2D::_notification(int p_what) {
  897. switch (p_what) {
  898. case NOTIFICATION_ENTER_TREE: {
  899. set_process_internal(emitting);
  900. } break;
  901. case NOTIFICATION_EXIT_TREE: {
  902. _set_do_redraw(false);
  903. } break;
  904. case NOTIFICATION_DRAW: {
  905. // first update before rendering to avoid one frame delay after emitting starts
  906. if (emitting && (time == 0)) {
  907. _update_internal();
  908. }
  909. if (!do_redraw) {
  910. return; // don't add to render list
  911. }
  912. RID texrid;
  913. if (texture.is_valid()) {
  914. texrid = texture->get_rid();
  915. }
  916. RS::get_singleton()->canvas_item_add_multimesh(get_canvas_item(), multimesh, texrid);
  917. } break;
  918. case NOTIFICATION_INTERNAL_PROCESS: {
  919. _update_internal();
  920. } break;
  921. case NOTIFICATION_TRANSFORM_CHANGED: {
  922. inv_emission_transform = get_global_transform().affine_inverse();
  923. if (!local_coords) {
  924. int pc = particles.size();
  925. float *w = particle_data.ptrw();
  926. const Particle *r = particles.ptr();
  927. float *ptr = w;
  928. for (int i = 0; i < pc; i++) {
  929. Transform2D t = inv_emission_transform * r[i].transform;
  930. if (r[i].active) {
  931. ptr[0] = t.columns[0][0];
  932. ptr[1] = t.columns[1][0];
  933. ptr[2] = 0;
  934. ptr[3] = t.columns[2][0];
  935. ptr[4] = t.columns[0][1];
  936. ptr[5] = t.columns[1][1];
  937. ptr[6] = 0;
  938. ptr[7] = t.columns[2][1];
  939. } else {
  940. memset(ptr, 0, sizeof(float) * 8);
  941. }
  942. ptr += 16;
  943. }
  944. }
  945. } break;
  946. }
  947. }
  948. void CPUParticles2D::convert_from_particles(Node *p_particles) {
  949. GPUParticles2D *gpu_particles = Object::cast_to<GPUParticles2D>(p_particles);
  950. ERR_FAIL_NULL_MSG(gpu_particles, "Only GPUParticles2D nodes can be converted to CPUParticles2D.");
  951. set_emitting(gpu_particles->is_emitting());
  952. set_amount(gpu_particles->get_amount());
  953. set_lifetime(gpu_particles->get_lifetime());
  954. set_one_shot(gpu_particles->get_one_shot());
  955. set_pre_process_time(gpu_particles->get_pre_process_time());
  956. set_explosiveness_ratio(gpu_particles->get_explosiveness_ratio());
  957. set_randomness_ratio(gpu_particles->get_randomness_ratio());
  958. set_use_local_coordinates(gpu_particles->get_use_local_coordinates());
  959. set_fixed_fps(gpu_particles->get_fixed_fps());
  960. set_fractional_delta(gpu_particles->get_fractional_delta());
  961. set_speed_scale(gpu_particles->get_speed_scale());
  962. set_draw_order(DrawOrder(gpu_particles->get_draw_order()));
  963. set_texture(gpu_particles->get_texture());
  964. Ref<Material> mat = gpu_particles->get_material();
  965. if (mat.is_valid()) {
  966. set_material(mat);
  967. }
  968. Ref<ParticleProcessMaterial> proc_mat = gpu_particles->get_process_material();
  969. if (proc_mat.is_null()) {
  970. return;
  971. }
  972. Vector3 dir = proc_mat->get_direction();
  973. set_direction(Vector2(dir.x, dir.y));
  974. set_spread(proc_mat->get_spread());
  975. set_color(proc_mat->get_color());
  976. Ref<GradientTexture1D> gt = proc_mat->get_color_ramp();
  977. if (gt.is_valid()) {
  978. set_color_ramp(gt->get_gradient());
  979. }
  980. Ref<GradientTexture1D> gti = proc_mat->get_color_initial_ramp();
  981. if (gti.is_valid()) {
  982. set_color_initial_ramp(gti->get_gradient());
  983. }
  984. set_particle_flag(PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY, proc_mat->get_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY));
  985. set_emission_shape(EmissionShape(proc_mat->get_emission_shape()));
  986. set_emission_sphere_radius(proc_mat->get_emission_sphere_radius());
  987. Vector2 rect_extents = Vector2(proc_mat->get_emission_box_extents().x, proc_mat->get_emission_box_extents().y);
  988. set_emission_rect_extents(rect_extents);
  989. Ref<CurveXYZTexture> scale3D = proc_mat->get_param_texture(ParticleProcessMaterial::PARAM_SCALE);
  990. if (scale3D.is_valid()) {
  991. split_scale = true;
  992. scale_curve_x = scale3D->get_curve_x();
  993. scale_curve_y = scale3D->get_curve_y();
  994. }
  995. set_gravity(Vector2(proc_mat->get_gravity().x, proc_mat->get_gravity().y));
  996. set_lifetime_randomness(proc_mat->get_lifetime_randomness());
  997. #define CONVERT_PARAM(m_param) \
  998. set_param_min(m_param, proc_mat->get_param_min(ParticleProcessMaterial::m_param)); \
  999. { \
  1000. Ref<CurveTexture> ctex = proc_mat->get_param_texture(ParticleProcessMaterial::m_param); \
  1001. if (ctex.is_valid()) \
  1002. set_param_curve(m_param, ctex->get_curve()); \
  1003. } \
  1004. set_param_max(m_param, proc_mat->get_param_max(ParticleProcessMaterial::m_param));
  1005. CONVERT_PARAM(PARAM_INITIAL_LINEAR_VELOCITY);
  1006. CONVERT_PARAM(PARAM_ANGULAR_VELOCITY);
  1007. CONVERT_PARAM(PARAM_ORBIT_VELOCITY);
  1008. CONVERT_PARAM(PARAM_LINEAR_ACCEL);
  1009. CONVERT_PARAM(PARAM_RADIAL_ACCEL);
  1010. CONVERT_PARAM(PARAM_TANGENTIAL_ACCEL);
  1011. CONVERT_PARAM(PARAM_DAMPING);
  1012. CONVERT_PARAM(PARAM_ANGLE);
  1013. CONVERT_PARAM(PARAM_SCALE);
  1014. CONVERT_PARAM(PARAM_HUE_VARIATION);
  1015. CONVERT_PARAM(PARAM_ANIM_SPEED);
  1016. CONVERT_PARAM(PARAM_ANIM_OFFSET);
  1017. #undef CONVERT_PARAM
  1018. }
  1019. void CPUParticles2D::_bind_methods() {
  1020. ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &CPUParticles2D::set_emitting);
  1021. ClassDB::bind_method(D_METHOD("set_amount", "amount"), &CPUParticles2D::set_amount);
  1022. ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &CPUParticles2D::set_lifetime);
  1023. ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &CPUParticles2D::set_one_shot);
  1024. ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &CPUParticles2D::set_pre_process_time);
  1025. ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &CPUParticles2D::set_explosiveness_ratio);
  1026. ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &CPUParticles2D::set_randomness_ratio);
  1027. ClassDB::bind_method(D_METHOD("set_lifetime_randomness", "random"), &CPUParticles2D::set_lifetime_randomness);
  1028. ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &CPUParticles2D::set_use_local_coordinates);
  1029. ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &CPUParticles2D::set_fixed_fps);
  1030. ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &CPUParticles2D::set_fractional_delta);
  1031. ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &CPUParticles2D::set_speed_scale);
  1032. ClassDB::bind_method(D_METHOD("is_emitting"), &CPUParticles2D::is_emitting);
  1033. ClassDB::bind_method(D_METHOD("get_amount"), &CPUParticles2D::get_amount);
  1034. ClassDB::bind_method(D_METHOD("get_lifetime"), &CPUParticles2D::get_lifetime);
  1035. ClassDB::bind_method(D_METHOD("get_one_shot"), &CPUParticles2D::get_one_shot);
  1036. ClassDB::bind_method(D_METHOD("get_pre_process_time"), &CPUParticles2D::get_pre_process_time);
  1037. ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &CPUParticles2D::get_explosiveness_ratio);
  1038. ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &CPUParticles2D::get_randomness_ratio);
  1039. ClassDB::bind_method(D_METHOD("get_lifetime_randomness"), &CPUParticles2D::get_lifetime_randomness);
  1040. ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &CPUParticles2D::get_use_local_coordinates);
  1041. ClassDB::bind_method(D_METHOD("get_fixed_fps"), &CPUParticles2D::get_fixed_fps);
  1042. ClassDB::bind_method(D_METHOD("get_fractional_delta"), &CPUParticles2D::get_fractional_delta);
  1043. ClassDB::bind_method(D_METHOD("get_speed_scale"), &CPUParticles2D::get_speed_scale);
  1044. ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &CPUParticles2D::set_draw_order);
  1045. ClassDB::bind_method(D_METHOD("get_draw_order"), &CPUParticles2D::get_draw_order);
  1046. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &CPUParticles2D::set_texture);
  1047. ClassDB::bind_method(D_METHOD("get_texture"), &CPUParticles2D::get_texture);
  1048. ClassDB::bind_method(D_METHOD("restart"), &CPUParticles2D::restart);
  1049. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting", PROPERTY_HINT_ONESHOT), "set_emitting", "is_emitting");
  1050. 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.
  1051. ADD_GROUP("Time", "");
  1052. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime", PROPERTY_HINT_RANGE, "0.01,600.0,0.01,or_greater,exp,suffix:s"), "set_lifetime", "get_lifetime");
  1053. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
  1054. 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");
  1055. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
  1056. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
  1057. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
  1058. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime_randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_lifetime_randomness", "get_lifetime_randomness");
  1059. ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1,suffix:FPS"), "set_fixed_fps", "get_fixed_fps");
  1060. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
  1061. ADD_GROUP("Drawing", "");
  1062. // No visibility_rect property contrarily to Particles2D, it's updated automatically.
  1063. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
  1064. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime"), "set_draw_order", "get_draw_order");
  1065. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  1066. BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
  1067. BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
  1068. ////////////////////////////////
  1069. ClassDB::bind_method(D_METHOD("set_direction", "direction"), &CPUParticles2D::set_direction);
  1070. ClassDB::bind_method(D_METHOD("get_direction"), &CPUParticles2D::get_direction);
  1071. ClassDB::bind_method(D_METHOD("set_spread", "spread"), &CPUParticles2D::set_spread);
  1072. ClassDB::bind_method(D_METHOD("get_spread"), &CPUParticles2D::get_spread);
  1073. ClassDB::bind_method(D_METHOD("set_param_min", "param", "value"), &CPUParticles2D::set_param_min);
  1074. ClassDB::bind_method(D_METHOD("get_param_min", "param"), &CPUParticles2D::get_param_min);
  1075. ClassDB::bind_method(D_METHOD("set_param_max", "param", "value"), &CPUParticles2D::set_param_max);
  1076. ClassDB::bind_method(D_METHOD("get_param_max", "param"), &CPUParticles2D::get_param_max);
  1077. ClassDB::bind_method(D_METHOD("set_param_curve", "param", "curve"), &CPUParticles2D::set_param_curve);
  1078. ClassDB::bind_method(D_METHOD("get_param_curve", "param"), &CPUParticles2D::get_param_curve);
  1079. ClassDB::bind_method(D_METHOD("set_color", "color"), &CPUParticles2D::set_color);
  1080. ClassDB::bind_method(D_METHOD("get_color"), &CPUParticles2D::get_color);
  1081. ClassDB::bind_method(D_METHOD("set_color_ramp", "ramp"), &CPUParticles2D::set_color_ramp);
  1082. ClassDB::bind_method(D_METHOD("get_color_ramp"), &CPUParticles2D::get_color_ramp);
  1083. ClassDB::bind_method(D_METHOD("set_color_initial_ramp", "ramp"), &CPUParticles2D::set_color_initial_ramp);
  1084. ClassDB::bind_method(D_METHOD("get_color_initial_ramp"), &CPUParticles2D::get_color_initial_ramp);
  1085. ClassDB::bind_method(D_METHOD("set_particle_flag", "particle_flag", "enable"), &CPUParticles2D::set_particle_flag);
  1086. ClassDB::bind_method(D_METHOD("get_particle_flag", "particle_flag"), &CPUParticles2D::get_particle_flag);
  1087. ClassDB::bind_method(D_METHOD("set_emission_shape", "shape"), &CPUParticles2D::set_emission_shape);
  1088. ClassDB::bind_method(D_METHOD("get_emission_shape"), &CPUParticles2D::get_emission_shape);
  1089. ClassDB::bind_method(D_METHOD("set_emission_sphere_radius", "radius"), &CPUParticles2D::set_emission_sphere_radius);
  1090. ClassDB::bind_method(D_METHOD("get_emission_sphere_radius"), &CPUParticles2D::get_emission_sphere_radius);
  1091. ClassDB::bind_method(D_METHOD("set_emission_rect_extents", "extents"), &CPUParticles2D::set_emission_rect_extents);
  1092. ClassDB::bind_method(D_METHOD("get_emission_rect_extents"), &CPUParticles2D::get_emission_rect_extents);
  1093. ClassDB::bind_method(D_METHOD("set_emission_points", "array"), &CPUParticles2D::set_emission_points);
  1094. ClassDB::bind_method(D_METHOD("get_emission_points"), &CPUParticles2D::get_emission_points);
  1095. ClassDB::bind_method(D_METHOD("set_emission_normals", "array"), &CPUParticles2D::set_emission_normals);
  1096. ClassDB::bind_method(D_METHOD("get_emission_normals"), &CPUParticles2D::get_emission_normals);
  1097. ClassDB::bind_method(D_METHOD("set_emission_colors", "array"), &CPUParticles2D::set_emission_colors);
  1098. ClassDB::bind_method(D_METHOD("get_emission_colors"), &CPUParticles2D::get_emission_colors);
  1099. ClassDB::bind_method(D_METHOD("get_gravity"), &CPUParticles2D::get_gravity);
  1100. ClassDB::bind_method(D_METHOD("set_gravity", "accel_vec"), &CPUParticles2D::set_gravity);
  1101. ClassDB::bind_method(D_METHOD("get_split_scale"), &CPUParticles2D::get_split_scale);
  1102. ClassDB::bind_method(D_METHOD("set_split_scale", "split_scale"), &CPUParticles2D::set_split_scale);
  1103. ClassDB::bind_method(D_METHOD("get_scale_curve_x"), &CPUParticles2D::get_scale_curve_x);
  1104. ClassDB::bind_method(D_METHOD("set_scale_curve_x", "scale_curve"), &CPUParticles2D::set_scale_curve_x);
  1105. ClassDB::bind_method(D_METHOD("get_scale_curve_y"), &CPUParticles2D::get_scale_curve_y);
  1106. ClassDB::bind_method(D_METHOD("set_scale_curve_y", "scale_curve"), &CPUParticles2D::set_scale_curve_y);
  1107. ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &CPUParticles2D::convert_from_particles);
  1108. ADD_SIGNAL(MethodInfo("finished"));
  1109. ADD_GROUP("Emission Shape", "emission_");
  1110. 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");
  1111. 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");
  1112. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "emission_rect_extents", PROPERTY_HINT_NONE, "suffix:px"), "set_emission_rect_extents", "get_emission_rect_extents");
  1113. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "emission_points"), "set_emission_points", "get_emission_points");
  1114. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "emission_normals"), "set_emission_normals", "get_emission_normals");
  1115. ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "emission_colors"), "set_emission_colors", "get_emission_colors");
  1116. ADD_GROUP("Particle Flags", "particle_flag_");
  1117. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_align_y"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY);
  1118. ADD_GROUP("Direction", "");
  1119. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "direction"), "set_direction", "get_direction");
  1120. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "spread", PROPERTY_HINT_RANGE, "0,180,0.01"), "set_spread", "get_spread");
  1121. ADD_GROUP("Gravity", "");
  1122. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "gravity", PROPERTY_HINT_NONE, U"suffix:px/s\u00B2"), "set_gravity", "get_gravity");
  1123. ADD_GROUP("Initial Velocity", "initial_");
  1124. 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);
  1125. 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);
  1126. ADD_GROUP("Angular Velocity", "angular_");
  1127. 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);
  1128. 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);
  1129. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angular_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGULAR_VELOCITY);
  1130. ADD_GROUP("Orbit Velocity", "orbit_");
  1131. 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);
  1132. 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);
  1133. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "orbit_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ORBIT_VELOCITY);
  1134. ADD_GROUP("Linear Accel", "linear_");
  1135. 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);
  1136. 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);
  1137. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "linear_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_LINEAR_ACCEL);
  1138. ADD_GROUP("Radial Accel", "radial_");
  1139. 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);
  1140. 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);
  1141. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "radial_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_RADIAL_ACCEL);
  1142. ADD_GROUP("Tangential Accel", "tangential_");
  1143. 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);
  1144. 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);
  1145. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "tangential_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_TANGENTIAL_ACCEL);
  1146. ADD_GROUP("Damping", "");
  1147. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "damping_min", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_param_min", "get_param_min", PARAM_DAMPING);
  1148. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "damping_max", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_param_max", "get_param_max", PARAM_DAMPING);
  1149. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "damping_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_DAMPING);
  1150. ADD_GROUP("Angle", "");
  1151. 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);
  1152. 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);
  1153. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angle_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGLE);
  1154. ADD_GROUP("Scale", "");
  1155. 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);
  1156. 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);
  1157. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_amount_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_SCALE);
  1158. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "split_scale"), "set_split_scale", "get_split_scale");
  1159. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scale_curve_x", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_scale_curve_x", "get_scale_curve_x");
  1160. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scale_curve_y", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_scale_curve_y", "get_scale_curve_y");
  1161. ADD_GROUP("Color", "");
  1162. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  1163. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");
  1164. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_initial_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_initial_ramp", "get_color_initial_ramp");
  1165. ADD_GROUP("Hue Variation", "hue_");
  1166. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "hue_variation_min", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param_min", "get_param_min", PARAM_HUE_VARIATION);
  1167. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "hue_variation_max", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param_max", "get_param_max", PARAM_HUE_VARIATION);
  1168. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "hue_variation_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_HUE_VARIATION);
  1169. ADD_GROUP("Animation", "anim_");
  1170. 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);
  1171. 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);
  1172. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_speed_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_SPEED);
  1173. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_offset_min", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_param_min", "get_param_min", PARAM_ANIM_OFFSET);
  1174. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_offset_max", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_param_max", "get_param_max", PARAM_ANIM_OFFSET);
  1175. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_offset_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_OFFSET);
  1176. BIND_ENUM_CONSTANT(PARAM_INITIAL_LINEAR_VELOCITY);
  1177. BIND_ENUM_CONSTANT(PARAM_ANGULAR_VELOCITY);
  1178. BIND_ENUM_CONSTANT(PARAM_ORBIT_VELOCITY);
  1179. BIND_ENUM_CONSTANT(PARAM_LINEAR_ACCEL);
  1180. BIND_ENUM_CONSTANT(PARAM_RADIAL_ACCEL);
  1181. BIND_ENUM_CONSTANT(PARAM_TANGENTIAL_ACCEL);
  1182. BIND_ENUM_CONSTANT(PARAM_DAMPING);
  1183. BIND_ENUM_CONSTANT(PARAM_ANGLE);
  1184. BIND_ENUM_CONSTANT(PARAM_SCALE);
  1185. BIND_ENUM_CONSTANT(PARAM_HUE_VARIATION);
  1186. BIND_ENUM_CONSTANT(PARAM_ANIM_SPEED);
  1187. BIND_ENUM_CONSTANT(PARAM_ANIM_OFFSET);
  1188. BIND_ENUM_CONSTANT(PARAM_MAX);
  1189. BIND_ENUM_CONSTANT(PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY);
  1190. BIND_ENUM_CONSTANT(PARTICLE_FLAG_ROTATE_Y); // Unused, but exposed for consistency with 3D.
  1191. BIND_ENUM_CONSTANT(PARTICLE_FLAG_DISABLE_Z); // Unused, but exposed for consistency with 3D.
  1192. BIND_ENUM_CONSTANT(PARTICLE_FLAG_MAX);
  1193. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINT);
  1194. BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE);
  1195. BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE_SURFACE);
  1196. BIND_ENUM_CONSTANT(EMISSION_SHAPE_RECTANGLE);
  1197. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINTS);
  1198. BIND_ENUM_CONSTANT(EMISSION_SHAPE_DIRECTED_POINTS);
  1199. BIND_ENUM_CONSTANT(EMISSION_SHAPE_MAX);
  1200. }
  1201. CPUParticles2D::CPUParticles2D() {
  1202. mesh = RenderingServer::get_singleton()->mesh_create();
  1203. multimesh = RenderingServer::get_singleton()->multimesh_create();
  1204. RenderingServer::get_singleton()->multimesh_set_mesh(multimesh, mesh);
  1205. set_emitting(true);
  1206. set_amount(8);
  1207. set_use_local_coordinates(false);
  1208. set_param_min(PARAM_INITIAL_LINEAR_VELOCITY, 0);
  1209. set_param_min(PARAM_ANGULAR_VELOCITY, 0);
  1210. set_param_min(PARAM_ORBIT_VELOCITY, 0);
  1211. set_param_min(PARAM_LINEAR_ACCEL, 0);
  1212. set_param_min(PARAM_RADIAL_ACCEL, 0);
  1213. set_param_min(PARAM_TANGENTIAL_ACCEL, 0);
  1214. set_param_min(PARAM_DAMPING, 0);
  1215. set_param_min(PARAM_ANGLE, 0);
  1216. set_param_min(PARAM_SCALE, 1);
  1217. set_param_min(PARAM_HUE_VARIATION, 0);
  1218. set_param_min(PARAM_ANIM_SPEED, 0);
  1219. set_param_min(PARAM_ANIM_OFFSET, 0);
  1220. set_param_max(PARAM_INITIAL_LINEAR_VELOCITY, 0);
  1221. set_param_max(PARAM_ANGULAR_VELOCITY, 0);
  1222. set_param_max(PARAM_ORBIT_VELOCITY, 0);
  1223. set_param_max(PARAM_LINEAR_ACCEL, 0);
  1224. set_param_max(PARAM_RADIAL_ACCEL, 0);
  1225. set_param_max(PARAM_TANGENTIAL_ACCEL, 0);
  1226. set_param_max(PARAM_DAMPING, 0);
  1227. set_param_max(PARAM_ANGLE, 0);
  1228. set_param_max(PARAM_SCALE, 1);
  1229. set_param_max(PARAM_HUE_VARIATION, 0);
  1230. set_param_max(PARAM_ANIM_SPEED, 0);
  1231. set_param_max(PARAM_ANIM_OFFSET, 0);
  1232. for (int i = 0; i < PARTICLE_FLAG_MAX; i++) {
  1233. particle_flags[i] = false;
  1234. }
  1235. set_color(Color(1, 1, 1, 1));
  1236. _update_mesh_texture();
  1237. }
  1238. CPUParticles2D::~CPUParticles2D() {
  1239. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1240. RS::get_singleton()->free(multimesh);
  1241. RS::get_singleton()->free(mesh);
  1242. }