gpu_particles_2d.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /**************************************************************************/
  2. /* gpu_particles_2d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "gpu_particles_2d.h"
  31. #include "gpu_particles_2d.compat.inc"
  32. #include "scene/2d/cpu_particles_2d.h"
  33. #include "scene/resources/atlas_texture.h"
  34. #include "scene/resources/canvas_item_material.h"
  35. #include "scene/resources/curve_texture.h"
  36. #include "scene/resources/gradient_texture.h"
  37. #include "scene/resources/particle_process_material.h"
  38. void GPUParticles2D::set_emitting(bool p_emitting) {
  39. // Do not return even if `p_emitting == emitting` because `emitting` is just an approximation.
  40. if (p_emitting && p_emitting != emitting && !use_fixed_seed) {
  41. set_seed(Math::rand());
  42. }
  43. if (p_emitting && one_shot) {
  44. if (!active && !emitting) {
  45. // Last cycle ended.
  46. active = true;
  47. time = 0;
  48. signal_canceled = false;
  49. emission_time = lifetime;
  50. active_time = lifetime * (2 - explosiveness_ratio);
  51. } else {
  52. signal_canceled = true;
  53. }
  54. set_process_internal(true);
  55. } else if (!p_emitting) {
  56. if (one_shot) {
  57. set_process_internal(true);
  58. } else {
  59. set_process_internal(false);
  60. }
  61. }
  62. emitting = p_emitting;
  63. RS::get_singleton()->particles_set_emitting(particles, p_emitting);
  64. }
  65. void GPUParticles2D::set_amount(int p_amount) {
  66. ERR_FAIL_COND_MSG(p_amount < 1, "Amount of particles cannot be smaller than 1.");
  67. amount = p_amount;
  68. RS::get_singleton()->particles_set_amount(particles, amount);
  69. }
  70. void GPUParticles2D::set_lifetime(double p_lifetime) {
  71. ERR_FAIL_COND_MSG(p_lifetime <= 0, "Particles lifetime must be greater than 0.");
  72. lifetime = p_lifetime;
  73. RS::get_singleton()->particles_set_lifetime(particles, lifetime);
  74. }
  75. void GPUParticles2D::set_one_shot(bool p_enable) {
  76. one_shot = p_enable;
  77. RS::get_singleton()->particles_set_one_shot(particles, one_shot);
  78. if (is_emitting()) {
  79. set_process_internal(true);
  80. if (!one_shot) {
  81. RenderingServer::get_singleton()->particles_restart(particles);
  82. }
  83. }
  84. if (!one_shot) {
  85. set_process_internal(false);
  86. }
  87. }
  88. void GPUParticles2D::set_pre_process_time(double p_time) {
  89. pre_process_time = p_time;
  90. RS::get_singleton()->particles_set_pre_process_time(particles, pre_process_time);
  91. }
  92. void GPUParticles2D::set_explosiveness_ratio(real_t p_ratio) {
  93. explosiveness_ratio = p_ratio;
  94. RS::get_singleton()->particles_set_explosiveness_ratio(particles, explosiveness_ratio);
  95. }
  96. void GPUParticles2D::set_randomness_ratio(real_t p_ratio) {
  97. randomness_ratio = p_ratio;
  98. RS::get_singleton()->particles_set_randomness_ratio(particles, randomness_ratio);
  99. }
  100. void GPUParticles2D::set_visibility_rect(const Rect2 &p_visibility_rect) {
  101. visibility_rect = p_visibility_rect;
  102. AABB aabb;
  103. aabb.position.x = p_visibility_rect.position.x;
  104. aabb.position.y = p_visibility_rect.position.y;
  105. aabb.size.x = p_visibility_rect.size.x;
  106. aabb.size.y = p_visibility_rect.size.y;
  107. RS::get_singleton()->particles_set_custom_aabb(particles, aabb);
  108. queue_redraw();
  109. }
  110. void GPUParticles2D::set_use_local_coordinates(bool p_enable) {
  111. local_coords = p_enable;
  112. RS::get_singleton()->particles_set_use_local_coordinates(particles, local_coords);
  113. set_notify_transform(!p_enable);
  114. if (!p_enable && is_inside_tree()) {
  115. _update_particle_emission_transform();
  116. }
  117. }
  118. void GPUParticles2D::_update_particle_emission_transform() {
  119. Transform2D xf2d = get_global_transform();
  120. Transform3D xf;
  121. xf.basis.set_column(0, Vector3(xf2d.columns[0].x, xf2d.columns[0].y, 0));
  122. xf.basis.set_column(1, Vector3(xf2d.columns[1].x, xf2d.columns[1].y, 0));
  123. xf.set_origin(Vector3(xf2d.get_origin().x, xf2d.get_origin().y, 0));
  124. RS::get_singleton()->particles_set_emission_transform(particles, xf);
  125. }
  126. void GPUParticles2D::set_process_material(const Ref<Material> &p_material) {
  127. process_material = p_material;
  128. Ref<ParticleProcessMaterial> pm = p_material;
  129. if (pm.is_valid() && !pm->get_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_DISABLE_Z) && pm->get_gravity() == Vector3(0, -9.8, 0)) {
  130. // Likely a new (3D) material, modify it to match 2D space
  131. pm->set_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_DISABLE_Z, true);
  132. pm->set_gravity(Vector3(0, 98, 0));
  133. }
  134. RID material_rid;
  135. if (process_material.is_valid()) {
  136. material_rid = process_material->get_rid();
  137. }
  138. RS::get_singleton()->particles_set_process_material(particles, material_rid);
  139. update_configuration_warnings();
  140. }
  141. void GPUParticles2D::set_trail_enabled(bool p_enabled) {
  142. trail_enabled = p_enabled;
  143. RS::get_singleton()->particles_set_trails(particles, trail_enabled, trail_lifetime);
  144. queue_redraw();
  145. update_configuration_warnings();
  146. RS::get_singleton()->particles_set_transform_align(particles, p_enabled ? RS::PARTICLES_TRANSFORM_ALIGN_Y_TO_VELOCITY : RS::PARTICLES_TRANSFORM_ALIGN_DISABLED);
  147. }
  148. void GPUParticles2D::set_trail_lifetime(double p_seconds) {
  149. ERR_FAIL_COND(p_seconds < 0.01);
  150. trail_lifetime = p_seconds;
  151. RS::get_singleton()->particles_set_trails(particles, trail_enabled, trail_lifetime);
  152. queue_redraw();
  153. }
  154. void GPUParticles2D::set_trail_sections(int p_sections) {
  155. ERR_FAIL_COND(p_sections < 2);
  156. ERR_FAIL_COND(p_sections > 128);
  157. trail_sections = p_sections;
  158. queue_redraw();
  159. }
  160. void GPUParticles2D::set_trail_section_subdivisions(int p_subdivisions) {
  161. ERR_FAIL_COND(p_subdivisions < 1);
  162. ERR_FAIL_COND(p_subdivisions > 1024);
  163. trail_section_subdivisions = p_subdivisions;
  164. queue_redraw();
  165. }
  166. void GPUParticles2D::set_interp_to_end(float p_interp) {
  167. interp_to_end_factor = CLAMP(p_interp, 0.0, 1.0);
  168. RS::get_singleton()->particles_set_interp_to_end(particles, interp_to_end_factor);
  169. }
  170. #ifdef TOOLS_ENABLED
  171. void GPUParticles2D::set_show_visibility_rect(bool p_show_visibility_rect) {
  172. show_visibility_rect = p_show_visibility_rect;
  173. queue_redraw();
  174. }
  175. #endif
  176. bool GPUParticles2D::is_trail_enabled() const {
  177. return trail_enabled;
  178. }
  179. double GPUParticles2D::get_trail_lifetime() const {
  180. return trail_lifetime;
  181. }
  182. void GPUParticles2D::_update_collision_size() {
  183. real_t csize = collision_base_size;
  184. if (texture.is_valid()) {
  185. csize *= (texture->get_width() + texture->get_height()) / 4.0; //half size since its a radius
  186. }
  187. RS::get_singleton()->particles_set_collision_base_size(particles, csize);
  188. }
  189. void GPUParticles2D::set_collision_base_size(real_t p_size) {
  190. collision_base_size = p_size;
  191. _update_collision_size();
  192. }
  193. real_t GPUParticles2D::get_collision_base_size() const {
  194. return collision_base_size;
  195. }
  196. void GPUParticles2D::set_speed_scale(double p_scale) {
  197. speed_scale = p_scale;
  198. RS::get_singleton()->particles_set_speed_scale(particles, p_scale);
  199. }
  200. bool GPUParticles2D::is_emitting() const {
  201. return emitting;
  202. }
  203. int GPUParticles2D::get_amount() const {
  204. return amount;
  205. }
  206. double GPUParticles2D::get_lifetime() const {
  207. return lifetime;
  208. }
  209. int GPUParticles2D::get_trail_sections() const {
  210. return trail_sections;
  211. }
  212. int GPUParticles2D::get_trail_section_subdivisions() const {
  213. return trail_section_subdivisions;
  214. }
  215. bool GPUParticles2D::get_one_shot() const {
  216. return one_shot;
  217. }
  218. double GPUParticles2D::get_pre_process_time() const {
  219. return pre_process_time;
  220. }
  221. real_t GPUParticles2D::get_explosiveness_ratio() const {
  222. return explosiveness_ratio;
  223. }
  224. real_t GPUParticles2D::get_randomness_ratio() const {
  225. return randomness_ratio;
  226. }
  227. Rect2 GPUParticles2D::get_visibility_rect() const {
  228. return visibility_rect;
  229. }
  230. bool GPUParticles2D::get_use_local_coordinates() const {
  231. return local_coords;
  232. }
  233. Ref<Material> GPUParticles2D::get_process_material() const {
  234. return process_material;
  235. }
  236. double GPUParticles2D::get_speed_scale() const {
  237. return speed_scale;
  238. }
  239. void GPUParticles2D::set_draw_order(DrawOrder p_order) {
  240. draw_order = p_order;
  241. RS::get_singleton()->particles_set_draw_order(particles, RS::ParticlesDrawOrder(p_order));
  242. }
  243. GPUParticles2D::DrawOrder GPUParticles2D::get_draw_order() const {
  244. return draw_order;
  245. }
  246. void GPUParticles2D::set_fixed_fps(int p_count) {
  247. fixed_fps = p_count;
  248. RS::get_singleton()->particles_set_fixed_fps(particles, p_count);
  249. }
  250. int GPUParticles2D::get_fixed_fps() const {
  251. return fixed_fps;
  252. }
  253. void GPUParticles2D::set_fractional_delta(bool p_enable) {
  254. fractional_delta = p_enable;
  255. RS::get_singleton()->particles_set_fractional_delta(particles, p_enable);
  256. }
  257. bool GPUParticles2D::get_fractional_delta() const {
  258. return fractional_delta;
  259. }
  260. void GPUParticles2D::set_interpolate(bool p_enable) {
  261. interpolate = p_enable;
  262. RS::get_singleton()->particles_set_interpolate(particles, p_enable);
  263. }
  264. bool GPUParticles2D::get_interpolate() const {
  265. return interpolate;
  266. }
  267. float GPUParticles2D::get_interp_to_end() const {
  268. return interp_to_end_factor;
  269. }
  270. void GPUParticles2D::set_use_fixed_seed(bool p_use_fixed_seed) {
  271. if (p_use_fixed_seed == use_fixed_seed) {
  272. return;
  273. }
  274. use_fixed_seed = p_use_fixed_seed;
  275. notify_property_list_changed();
  276. }
  277. bool GPUParticles2D::get_use_fixed_seed() const {
  278. return use_fixed_seed;
  279. }
  280. void GPUParticles2D::set_seed(uint32_t p_seed) {
  281. seed = p_seed;
  282. RS::get_singleton()->particles_set_seed(particles, p_seed);
  283. }
  284. uint32_t GPUParticles2D::get_seed() const {
  285. return seed;
  286. }
  287. void GPUParticles2D::request_particles_process(real_t p_requested_process_time) {
  288. RS::get_singleton()->particles_request_process_time(particles, p_requested_process_time);
  289. }
  290. PackedStringArray GPUParticles2D::get_configuration_warnings() const {
  291. PackedStringArray warnings = Node2D::get_configuration_warnings();
  292. if (process_material.is_null()) {
  293. warnings.push_back(RTR("A material to process the particles is not assigned, so no behavior is imprinted."));
  294. } else {
  295. CanvasItemMaterial *mat = Object::cast_to<CanvasItemMaterial>(get_material().ptr());
  296. if (get_material().is_null() || (mat && !mat->get_particles_animation())) {
  297. const ParticleProcessMaterial *process = Object::cast_to<ParticleProcessMaterial>(process_material.ptr());
  298. if (process &&
  299. (process->get_param_max(ParticleProcessMaterial::PARAM_ANIM_SPEED) != 0.0 || process->get_param_max(ParticleProcessMaterial::PARAM_ANIM_OFFSET) != 0.0 ||
  300. process->get_param_texture(ParticleProcessMaterial::PARAM_ANIM_SPEED).is_valid() || process->get_param_texture(ParticleProcessMaterial::PARAM_ANIM_OFFSET).is_valid())) {
  301. warnings.push_back(RTR("Particles2D animation requires the usage of a CanvasItemMaterial with \"Particles Animation\" enabled."));
  302. }
  303. }
  304. }
  305. if (trail_enabled && OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  306. warnings.push_back(RTR("Particle trails are only available when using the Forward+ or Mobile renderers."));
  307. }
  308. if (sub_emitter != NodePath() && OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  309. warnings.push_back(RTR("Particle sub-emitters are not available when using the Compatibility renderer."));
  310. }
  311. return warnings;
  312. }
  313. Rect2 GPUParticles2D::capture_rect() const {
  314. AABB aabb = RS::get_singleton()->particles_get_current_aabb(particles);
  315. Rect2 r;
  316. r.position.x = aabb.position.x;
  317. r.position.y = aabb.position.y;
  318. r.size.x = aabb.size.x;
  319. r.size.y = aabb.size.y;
  320. return r;
  321. }
  322. void GPUParticles2D::set_texture(const Ref<Texture2D> &p_texture) {
  323. if (texture.is_valid()) {
  324. texture->disconnect_changed(callable_mp(this, &GPUParticles2D::_texture_changed));
  325. }
  326. texture = p_texture;
  327. if (texture.is_valid()) {
  328. texture->connect_changed(callable_mp(this, &GPUParticles2D::_texture_changed));
  329. }
  330. _update_collision_size();
  331. queue_redraw();
  332. }
  333. Ref<Texture2D> GPUParticles2D::get_texture() const {
  334. return texture;
  335. }
  336. void GPUParticles2D::_validate_property(PropertyInfo &p_property) const {
  337. if (p_property.name == "seed" && !use_fixed_seed) {
  338. p_property.usage = PROPERTY_USAGE_NONE;
  339. }
  340. if (p_property.name == "emitting") {
  341. p_property.hint = one_shot ? PROPERTY_HINT_ONESHOT : PROPERTY_HINT_NONE;
  342. }
  343. }
  344. void GPUParticles2D::emit_particle(const Transform2D &p_transform2d, const Vector2 &p_velocity2d, const Color &p_color, const Color &p_custom, uint32_t p_emit_flags) {
  345. Transform3D emit_transform;
  346. emit_transform.basis.set_column(0, Vector3(p_transform2d.columns[0].x, p_transform2d.columns[0].y, 0));
  347. emit_transform.basis.set_column(1, Vector3(p_transform2d.columns[1].x, p_transform2d.columns[1].y, 0));
  348. emit_transform.set_origin(Vector3(p_transform2d.get_origin().x, p_transform2d.get_origin().y, 0));
  349. Vector3 velocity = Vector3(p_velocity2d.x, p_velocity2d.y, 0);
  350. RS::get_singleton()->particles_emit(particles, emit_transform, velocity, p_color, p_custom, p_emit_flags);
  351. }
  352. void GPUParticles2D::_attach_sub_emitter() {
  353. Node *n = get_node_or_null(sub_emitter);
  354. if (n) {
  355. GPUParticles2D *sen = Object::cast_to<GPUParticles2D>(n);
  356. if (sen && sen != this) {
  357. RS::get_singleton()->particles_set_subemitter(particles, sen->particles);
  358. }
  359. }
  360. }
  361. void GPUParticles2D::_texture_changed() {
  362. // Changes to the texture need to trigger an update to make
  363. // the editor redraw the sprite with the updated texture.
  364. if (texture.is_valid()) {
  365. queue_redraw();
  366. }
  367. }
  368. void GPUParticles2D::set_sub_emitter(const NodePath &p_path) {
  369. if (is_inside_tree()) {
  370. RS::get_singleton()->particles_set_subemitter(particles, RID());
  371. }
  372. sub_emitter = p_path;
  373. if (is_inside_tree() && sub_emitter != NodePath()) {
  374. _attach_sub_emitter();
  375. }
  376. update_configuration_warnings();
  377. }
  378. NodePath GPUParticles2D::get_sub_emitter() const {
  379. return sub_emitter;
  380. }
  381. void GPUParticles2D::set_amount_ratio(float p_ratio) {
  382. amount_ratio = p_ratio;
  383. RenderingServer::get_singleton()->particles_set_amount_ratio(particles, p_ratio);
  384. }
  385. float GPUParticles2D::get_amount_ratio() const {
  386. return amount_ratio;
  387. }
  388. void GPUParticles2D::restart(bool p_keep_seed) {
  389. if (!p_keep_seed && !use_fixed_seed) {
  390. set_seed(Math::rand());
  391. }
  392. RS::get_singleton()->particles_restart(particles);
  393. RS::get_singleton()->particles_set_emitting(particles, true);
  394. emitting = true;
  395. active = true;
  396. signal_canceled = false;
  397. time = 0;
  398. emission_time = lifetime;
  399. active_time = lifetime * (2 - explosiveness_ratio);
  400. if (one_shot) {
  401. set_process_internal(true);
  402. }
  403. }
  404. void GPUParticles2D::convert_from_particles(Node *p_particles) {
  405. CPUParticles2D *cpu_particles = Object::cast_to<CPUParticles2D>(p_particles);
  406. ERR_FAIL_NULL_MSG(cpu_particles, "Only CPUParticles2D nodes can be converted to GPUParticles2D.");
  407. set_emitting(cpu_particles->is_emitting());
  408. set_amount(cpu_particles->get_amount());
  409. set_lifetime(cpu_particles->get_lifetime());
  410. set_one_shot(cpu_particles->get_one_shot());
  411. set_pre_process_time(cpu_particles->get_pre_process_time());
  412. set_explosiveness_ratio(cpu_particles->get_explosiveness_ratio());
  413. set_randomness_ratio(cpu_particles->get_randomness_ratio());
  414. set_use_local_coordinates(cpu_particles->get_use_local_coordinates());
  415. set_fixed_fps(cpu_particles->get_fixed_fps());
  416. set_fractional_delta(cpu_particles->get_fractional_delta());
  417. set_speed_scale(cpu_particles->get_speed_scale());
  418. set_draw_order(DrawOrder(cpu_particles->get_draw_order()));
  419. set_texture(cpu_particles->get_texture());
  420. Ref<Material> mat = cpu_particles->get_material();
  421. if (mat.is_valid()) {
  422. set_material(mat);
  423. }
  424. Ref<ParticleProcessMaterial> proc_mat = memnew(ParticleProcessMaterial);
  425. set_process_material(proc_mat);
  426. Vector2 dir = cpu_particles->get_direction();
  427. proc_mat->set_direction(Vector3(dir.x, dir.y, 0));
  428. proc_mat->set_spread(cpu_particles->get_spread());
  429. proc_mat->set_color(cpu_particles->get_color());
  430. Ref<Gradient> color_grad = cpu_particles->get_color_ramp();
  431. if (color_grad.is_valid()) {
  432. Ref<GradientTexture1D> tex = memnew(GradientTexture1D);
  433. tex->set_gradient(color_grad);
  434. proc_mat->set_color_ramp(tex);
  435. }
  436. Ref<Gradient> color_init_grad = cpu_particles->get_color_initial_ramp();
  437. if (color_init_grad.is_valid()) {
  438. Ref<GradientTexture1D> tex = memnew(GradientTexture1D);
  439. tex->set_gradient(color_init_grad);
  440. proc_mat->set_color_initial_ramp(tex);
  441. }
  442. proc_mat->set_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY, cpu_particles->get_particle_flag(CPUParticles2D::PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY));
  443. proc_mat->set_emission_shape(ParticleProcessMaterial::EmissionShape(cpu_particles->get_emission_shape()));
  444. proc_mat->set_emission_sphere_radius(cpu_particles->get_emission_sphere_radius());
  445. Vector2 rect_extents = cpu_particles->get_emission_rect_extents();
  446. proc_mat->set_emission_box_extents(Vector3(rect_extents.x, rect_extents.y, 0));
  447. if (cpu_particles->get_split_scale()) {
  448. Ref<CurveXYZTexture> scale3D = memnew(CurveXYZTexture);
  449. scale3D->set_curve_x(cpu_particles->get_scale_curve_x());
  450. scale3D->set_curve_y(cpu_particles->get_scale_curve_y());
  451. proc_mat->set_param_texture(ParticleProcessMaterial::PARAM_SCALE, scale3D);
  452. }
  453. Vector2 gravity = cpu_particles->get_gravity();
  454. proc_mat->set_gravity(Vector3(gravity.x, gravity.y, 0));
  455. proc_mat->set_lifetime_randomness(cpu_particles->get_lifetime_randomness());
  456. #define CONVERT_PARAM(m_param) \
  457. proc_mat->set_param_min(ParticleProcessMaterial::m_param, cpu_particles->get_param_min(CPUParticles2D::m_param)); \
  458. { \
  459. Ref<Curve> curve = cpu_particles->get_param_curve(CPUParticles2D::m_param); \
  460. if (curve.is_valid()) { \
  461. Ref<CurveTexture> tex = memnew(CurveTexture); \
  462. tex->set_curve(curve); \
  463. proc_mat->set_param_texture(ParticleProcessMaterial::m_param, tex); \
  464. } \
  465. } \
  466. proc_mat->set_param_max(ParticleProcessMaterial::m_param, cpu_particles->get_param_max(CPUParticles2D::m_param));
  467. CONVERT_PARAM(PARAM_INITIAL_LINEAR_VELOCITY);
  468. CONVERT_PARAM(PARAM_ANGULAR_VELOCITY);
  469. CONVERT_PARAM(PARAM_ORBIT_VELOCITY);
  470. CONVERT_PARAM(PARAM_LINEAR_ACCEL);
  471. CONVERT_PARAM(PARAM_RADIAL_ACCEL);
  472. CONVERT_PARAM(PARAM_TANGENTIAL_ACCEL);
  473. CONVERT_PARAM(PARAM_DAMPING);
  474. CONVERT_PARAM(PARAM_ANGLE);
  475. CONVERT_PARAM(PARAM_SCALE);
  476. CONVERT_PARAM(PARAM_HUE_VARIATION);
  477. CONVERT_PARAM(PARAM_ANIM_SPEED);
  478. CONVERT_PARAM(PARAM_ANIM_OFFSET);
  479. #undef CONVERT_PARAM
  480. }
  481. void GPUParticles2D::_notification(int p_what) {
  482. switch (p_what) {
  483. case NOTIFICATION_DRAW: {
  484. RID texture_rid;
  485. Size2 size;
  486. if (texture.is_valid()) {
  487. texture_rid = texture->get_rid();
  488. size = texture->get_size();
  489. } else {
  490. size = Size2(1, 1);
  491. }
  492. if (trail_enabled) {
  493. RS::get_singleton()->mesh_clear(mesh);
  494. PackedVector2Array points;
  495. PackedVector2Array uvs;
  496. PackedInt32Array bone_indices;
  497. PackedFloat32Array bone_weights;
  498. PackedInt32Array indices;
  499. int total_segments = trail_sections * trail_section_subdivisions;
  500. real_t depth = size.height * trail_sections;
  501. for (int j = 0; j <= total_segments; j++) {
  502. real_t v = j;
  503. v /= total_segments;
  504. real_t y = depth * v;
  505. y = (depth * 0.5) - y;
  506. int bone = j / trail_section_subdivisions;
  507. real_t blend = 1.0 - real_t(j % trail_section_subdivisions) / real_t(trail_section_subdivisions);
  508. real_t s = size.width;
  509. points.push_back(Vector2(-s * 0.5, 0));
  510. points.push_back(Vector2(+s * 0.5, 0));
  511. uvs.push_back(Vector2(0, v));
  512. uvs.push_back(Vector2(1, v));
  513. for (int i = 0; i < 2; i++) {
  514. bone_indices.push_back(bone);
  515. bone_indices.push_back(MIN(trail_sections, bone + 1));
  516. bone_indices.push_back(0);
  517. bone_indices.push_back(0);
  518. bone_weights.push_back(blend);
  519. bone_weights.push_back(1.0 - blend);
  520. bone_weights.push_back(0);
  521. bone_weights.push_back(0);
  522. }
  523. if (j > 0) {
  524. int base = j * 2 - 2;
  525. indices.push_back(base + 0);
  526. indices.push_back(base + 1);
  527. indices.push_back(base + 2);
  528. indices.push_back(base + 1);
  529. indices.push_back(base + 3);
  530. indices.push_back(base + 2);
  531. }
  532. }
  533. Array arr;
  534. arr.resize(RS::ARRAY_MAX);
  535. arr[RS::ARRAY_VERTEX] = points;
  536. arr[RS::ARRAY_TEX_UV] = uvs;
  537. arr[RS::ARRAY_BONES] = bone_indices;
  538. arr[RS::ARRAY_WEIGHTS] = bone_weights;
  539. arr[RS::ARRAY_INDEX] = indices;
  540. RS::get_singleton()->mesh_add_surface_from_arrays(mesh, RS::PRIMITIVE_TRIANGLES, arr, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
  541. Vector<Transform3D> xforms;
  542. for (int i = 0; i <= trail_sections; i++) {
  543. Transform3D xform;
  544. /*
  545. xform.origin.y = depth / 2.0 - size.height * real_t(i);
  546. xform.origin.y = -xform.origin.y; //bind is an inverse transform, so negate y */
  547. xforms.push_back(xform);
  548. }
  549. RS::get_singleton()->particles_set_trail_bind_poses(particles, xforms);
  550. } else {
  551. RS::get_singleton()->mesh_clear(mesh);
  552. Vector<Vector2> points = {
  553. Vector2(-size.x / 2.0, -size.y / 2.0),
  554. Vector2(size.x / 2.0, -size.y / 2.0),
  555. Vector2(size.x / 2.0, size.y / 2.0),
  556. Vector2(-size.x / 2.0, size.y / 2.0)
  557. };
  558. Vector<Vector2> uvs;
  559. AtlasTexture *atlas_texture = Object::cast_to<AtlasTexture>(*texture);
  560. if (atlas_texture && atlas_texture->get_atlas().is_valid()) {
  561. Rect2 region_rect = atlas_texture->get_region();
  562. Size2 atlas_size = atlas_texture->get_atlas()->get_size();
  563. uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, region_rect.position.y / atlas_size.y));
  564. uvs.push_back(Vector2((region_rect.position.x + region_rect.size.x) / atlas_size.x, region_rect.position.y / atlas_size.y));
  565. 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));
  566. uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, (region_rect.position.y + region_rect.size.y) / atlas_size.y));
  567. } else {
  568. uvs.push_back(Vector2(0, 0));
  569. uvs.push_back(Vector2(1, 0));
  570. uvs.push_back(Vector2(1, 1));
  571. uvs.push_back(Vector2(0, 1));
  572. }
  573. Vector<int> indices = { 0, 1, 2, 0, 2, 3 };
  574. Array arr;
  575. arr.resize(RS::ARRAY_MAX);
  576. arr[RS::ARRAY_VERTEX] = points;
  577. arr[RS::ARRAY_TEX_UV] = uvs;
  578. arr[RS::ARRAY_INDEX] = indices;
  579. RS::get_singleton()->mesh_add_surface_from_arrays(mesh, RS::PRIMITIVE_TRIANGLES, arr, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
  580. RS::get_singleton()->particles_set_trail_bind_poses(particles, Vector<Transform3D>());
  581. }
  582. RS::get_singleton()->canvas_item_add_particles(get_canvas_item(), particles, texture_rid);
  583. #ifdef TOOLS_ENABLED
  584. if (show_visibility_rect) {
  585. draw_rect(visibility_rect, Color(0, 0.7, 0.9, 0.4), false);
  586. }
  587. #endif
  588. } break;
  589. case NOTIFICATION_ENTER_TREE: {
  590. if (sub_emitter != NodePath()) {
  591. _attach_sub_emitter();
  592. }
  593. if (can_process()) {
  594. RS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
  595. } else {
  596. RS::get_singleton()->particles_set_speed_scale(particles, 0);
  597. }
  598. set_process_internal(true);
  599. set_physics_process_internal(true);
  600. previous_position = get_global_position();
  601. } break;
  602. case NOTIFICATION_EXIT_TREE: {
  603. RS::get_singleton()->particles_set_subemitter(particles, RID());
  604. } break;
  605. case NOTIFICATION_SUSPENDED:
  606. case NOTIFICATION_UNSUSPENDED:
  607. case NOTIFICATION_PAUSED:
  608. case NOTIFICATION_UNPAUSED: {
  609. if (is_inside_tree()) {
  610. if (can_process()) {
  611. RS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
  612. } else {
  613. RS::get_singleton()->particles_set_speed_scale(particles, 0);
  614. }
  615. }
  616. } break;
  617. case NOTIFICATION_TRANSFORM_CHANGED: {
  618. _update_particle_emission_transform();
  619. } break;
  620. case NOTIFICATION_INTERNAL_PROCESS: {
  621. if (one_shot) {
  622. time += get_process_delta_time();
  623. if (time > emission_time) {
  624. emitting = false;
  625. if (!active) {
  626. set_process_internal(false);
  627. }
  628. }
  629. if (time > active_time) {
  630. if (active && !signal_canceled) {
  631. emit_signal(SceneStringName(finished));
  632. }
  633. active = false;
  634. if (!emitting) {
  635. set_process_internal(false);
  636. }
  637. }
  638. }
  639. } break;
  640. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  641. // Update velocity in physics process, so that velocity calculations remain correct
  642. // if the physics tick rate is lower than the rendered framerate (especially without physics interpolation).
  643. const Vector3 velocity = Vector3((get_global_position() - previous_position).x, (get_global_position() - previous_position).y, 0.0) /
  644. get_physics_process_delta_time();
  645. if (velocity != previous_velocity) {
  646. RS::get_singleton()->particles_set_emitter_velocity(particles, velocity);
  647. previous_velocity = velocity;
  648. }
  649. previous_position = get_global_position();
  650. } break;
  651. }
  652. }
  653. void GPUParticles2D::_bind_methods() {
  654. ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &GPUParticles2D::set_emitting);
  655. ClassDB::bind_method(D_METHOD("set_amount", "amount"), &GPUParticles2D::set_amount);
  656. ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &GPUParticles2D::set_lifetime);
  657. ClassDB::bind_method(D_METHOD("set_one_shot", "secs"), &GPUParticles2D::set_one_shot);
  658. ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &GPUParticles2D::set_pre_process_time);
  659. ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &GPUParticles2D::set_explosiveness_ratio);
  660. ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &GPUParticles2D::set_randomness_ratio);
  661. ClassDB::bind_method(D_METHOD("set_visibility_rect", "visibility_rect"), &GPUParticles2D::set_visibility_rect);
  662. ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &GPUParticles2D::set_use_local_coordinates);
  663. ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &GPUParticles2D::set_fixed_fps);
  664. ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &GPUParticles2D::set_fractional_delta);
  665. ClassDB::bind_method(D_METHOD("set_interpolate", "enable"), &GPUParticles2D::set_interpolate);
  666. ClassDB::bind_method(D_METHOD("set_process_material", "material"), &GPUParticles2D::set_process_material);
  667. ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &GPUParticles2D::set_speed_scale);
  668. ClassDB::bind_method(D_METHOD("set_collision_base_size", "size"), &GPUParticles2D::set_collision_base_size);
  669. ClassDB::bind_method(D_METHOD("set_interp_to_end", "interp"), &GPUParticles2D::set_interp_to_end);
  670. ClassDB::bind_method(D_METHOD("request_particles_process", "process_time"), &GPUParticles2D::request_particles_process);
  671. ClassDB::bind_method(D_METHOD("is_emitting"), &GPUParticles2D::is_emitting);
  672. ClassDB::bind_method(D_METHOD("get_amount"), &GPUParticles2D::get_amount);
  673. ClassDB::bind_method(D_METHOD("get_lifetime"), &GPUParticles2D::get_lifetime);
  674. ClassDB::bind_method(D_METHOD("get_one_shot"), &GPUParticles2D::get_one_shot);
  675. ClassDB::bind_method(D_METHOD("get_pre_process_time"), &GPUParticles2D::get_pre_process_time);
  676. ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &GPUParticles2D::get_explosiveness_ratio);
  677. ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &GPUParticles2D::get_randomness_ratio);
  678. ClassDB::bind_method(D_METHOD("get_visibility_rect"), &GPUParticles2D::get_visibility_rect);
  679. ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &GPUParticles2D::get_use_local_coordinates);
  680. ClassDB::bind_method(D_METHOD("get_fixed_fps"), &GPUParticles2D::get_fixed_fps);
  681. ClassDB::bind_method(D_METHOD("get_fractional_delta"), &GPUParticles2D::get_fractional_delta);
  682. ClassDB::bind_method(D_METHOD("get_interpolate"), &GPUParticles2D::get_interpolate);
  683. ClassDB::bind_method(D_METHOD("get_process_material"), &GPUParticles2D::get_process_material);
  684. ClassDB::bind_method(D_METHOD("get_speed_scale"), &GPUParticles2D::get_speed_scale);
  685. ClassDB::bind_method(D_METHOD("get_collision_base_size"), &GPUParticles2D::get_collision_base_size);
  686. ClassDB::bind_method(D_METHOD("get_interp_to_end"), &GPUParticles2D::get_interp_to_end);
  687. ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &GPUParticles2D::set_draw_order);
  688. ClassDB::bind_method(D_METHOD("get_draw_order"), &GPUParticles2D::get_draw_order);
  689. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &GPUParticles2D::set_texture);
  690. ClassDB::bind_method(D_METHOD("get_texture"), &GPUParticles2D::get_texture);
  691. ClassDB::bind_method(D_METHOD("capture_rect"), &GPUParticles2D::capture_rect);
  692. ClassDB::bind_method(D_METHOD("restart", "keep_seed"), &GPUParticles2D::restart, DEFVAL(false));
  693. ClassDB::bind_method(D_METHOD("set_sub_emitter", "path"), &GPUParticles2D::set_sub_emitter);
  694. ClassDB::bind_method(D_METHOD("get_sub_emitter"), &GPUParticles2D::get_sub_emitter);
  695. ClassDB::bind_method(D_METHOD("emit_particle", "xform", "velocity", "color", "custom", "flags"), &GPUParticles2D::emit_particle);
  696. ClassDB::bind_method(D_METHOD("set_trail_enabled", "enabled"), &GPUParticles2D::set_trail_enabled);
  697. ClassDB::bind_method(D_METHOD("set_trail_lifetime", "secs"), &GPUParticles2D::set_trail_lifetime);
  698. ClassDB::bind_method(D_METHOD("is_trail_enabled"), &GPUParticles2D::is_trail_enabled);
  699. ClassDB::bind_method(D_METHOD("get_trail_lifetime"), &GPUParticles2D::get_trail_lifetime);
  700. ClassDB::bind_method(D_METHOD("set_trail_sections", "sections"), &GPUParticles2D::set_trail_sections);
  701. ClassDB::bind_method(D_METHOD("get_trail_sections"), &GPUParticles2D::get_trail_sections);
  702. ClassDB::bind_method(D_METHOD("set_trail_section_subdivisions", "subdivisions"), &GPUParticles2D::set_trail_section_subdivisions);
  703. ClassDB::bind_method(D_METHOD("get_trail_section_subdivisions"), &GPUParticles2D::get_trail_section_subdivisions);
  704. ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &GPUParticles2D::convert_from_particles);
  705. ClassDB::bind_method(D_METHOD("set_amount_ratio", "ratio"), &GPUParticles2D::set_amount_ratio);
  706. ClassDB::bind_method(D_METHOD("get_amount_ratio"), &GPUParticles2D::get_amount_ratio);
  707. ClassDB::bind_method(D_METHOD("set_use_fixed_seed", "use_fixed_seed"), &GPUParticles2D::set_use_fixed_seed);
  708. ClassDB::bind_method(D_METHOD("get_use_fixed_seed"), &GPUParticles2D::get_use_fixed_seed);
  709. ClassDB::bind_method(D_METHOD("set_seed", "seed"), &GPUParticles2D::set_seed);
  710. ClassDB::bind_method(D_METHOD("get_seed"), &GPUParticles2D::get_seed);
  711. ADD_SIGNAL(MethodInfo("finished"));
  712. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting", PROPERTY_HINT_ONESHOT), "set_emitting", "is_emitting");
  713. ADD_PROPERTY_DEFAULT("emitting", true); // Workaround for doctool in headless mode, as dummy rasterizer always returns false.
  714. ADD_PROPERTY(PropertyInfo(Variant::INT, "amount", PROPERTY_HINT_RANGE, "1,1000000,1,exp"), "set_amount", "get_amount"); // FIXME: Evaluate support for `exp` in integer properties, or remove this.
  715. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "amount_ratio", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_amount_ratio", "get_amount_ratio");
  716. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "sub_emitter", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "GPUParticles2D"), "set_sub_emitter", "get_sub_emitter");
  717. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  718. ADD_GROUP("Time", "");
  719. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime", PROPERTY_HINT_RANGE, "0.01,600.0,0.01,or_greater,exp,suffix:s"), "set_lifetime", "get_lifetime");
  720. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "interp_to_end", PROPERTY_HINT_RANGE, "0.00,1.0,0.001"), "set_interp_to_end", "get_interp_to_end");
  721. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
  722. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "preprocess", PROPERTY_HINT_RANGE, "0.00,10.0,0.01,or_greater,exp,suffix:s"), "set_pre_process_time", "get_pre_process_time");
  723. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
  724. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
  725. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
  726. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_fixed_seed", PROPERTY_HINT_ENUM), "set_use_fixed_seed", "get_use_fixed_seed");
  727. ADD_PROPERTY(PropertyInfo(Variant::INT, "seed", PROPERTY_HINT_RANGE, "0," + itos(UINT32_MAX) + ",1"), "set_seed", "get_seed");
  728. ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1,suffix:FPS"), "set_fixed_fps", "get_fixed_fps");
  729. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interpolate"), "set_interpolate", "get_interpolate");
  730. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
  731. ADD_GROUP("Collision", "collision_");
  732. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_base_size", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater"), "set_collision_base_size", "get_collision_base_size");
  733. ADD_GROUP("Drawing", "");
  734. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "visibility_rect", PROPERTY_HINT_NONE, "suffix:px"), "set_visibility_rect", "get_visibility_rect");
  735. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
  736. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime,Reverse Lifetime"), "set_draw_order", "get_draw_order");
  737. ADD_GROUP("Trails", "trail_");
  738. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "trail_enabled"), "set_trail_enabled", "is_trail_enabled");
  739. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "trail_lifetime", PROPERTY_HINT_RANGE, "0.01,10,0.01,or_greater,suffix:s"), "set_trail_lifetime", "get_trail_lifetime");
  740. ADD_PROPERTY(PropertyInfo(Variant::INT, "trail_sections", PROPERTY_HINT_RANGE, "2,128,1"), "set_trail_sections", "get_trail_sections");
  741. ADD_PROPERTY(PropertyInfo(Variant::INT, "trail_section_subdivisions", PROPERTY_HINT_RANGE, "1,1024,1"), "set_trail_section_subdivisions", "get_trail_section_subdivisions");
  742. ADD_GROUP("Process Material", "");
  743. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "process_material", PROPERTY_HINT_RESOURCE_TYPE, "ParticleProcessMaterial,ShaderMaterial"), "set_process_material", "get_process_material");
  744. BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
  745. BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
  746. BIND_ENUM_CONSTANT(DRAW_ORDER_REVERSE_LIFETIME);
  747. BIND_ENUM_CONSTANT(EMIT_FLAG_POSITION);
  748. BIND_ENUM_CONSTANT(EMIT_FLAG_ROTATION_SCALE);
  749. BIND_ENUM_CONSTANT(EMIT_FLAG_VELOCITY);
  750. BIND_ENUM_CONSTANT(EMIT_FLAG_COLOR);
  751. BIND_ENUM_CONSTANT(EMIT_FLAG_CUSTOM);
  752. ADD_PROPERTY_DEFAULT("seed", 0);
  753. }
  754. GPUParticles2D::GPUParticles2D() {
  755. particles = RS::get_singleton()->particles_create();
  756. RS::get_singleton()->particles_set_mode(particles, RS::PARTICLES_MODE_2D);
  757. mesh = RS::get_singleton()->mesh_create();
  758. RS::get_singleton()->particles_set_draw_passes(particles, 1);
  759. RS::get_singleton()->particles_set_draw_pass_mesh(particles, 0, mesh);
  760. one_shot = false; // Needed so that set_emitting doesn't access uninitialized values
  761. set_emitting(true);
  762. set_one_shot(false);
  763. set_seed(Math::rand());
  764. set_use_fixed_seed(false);
  765. set_amount(8);
  766. set_amount_ratio(1.0);
  767. set_lifetime(1);
  768. set_fixed_fps(0);
  769. set_fractional_delta(true);
  770. set_interpolate(true);
  771. set_pre_process_time(0);
  772. set_explosiveness_ratio(0);
  773. set_randomness_ratio(0);
  774. set_visibility_rect(Rect2(Vector2(-100, -100), Vector2(200, 200)));
  775. set_use_local_coordinates(false);
  776. set_draw_order(DRAW_ORDER_LIFETIME);
  777. set_speed_scale(1);
  778. set_fixed_fps(30);
  779. set_collision_base_size(collision_base_size);
  780. }
  781. GPUParticles2D::~GPUParticles2D() {
  782. ERR_FAIL_NULL(RenderingServer::get_singleton());
  783. RS::get_singleton()->free(particles);
  784. RS::get_singleton()->free(mesh);
  785. }