cpu_particles_2d.cpp 54 KB

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