cpu_particles_2d.cpp 53 KB

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