gpu_particles_2d.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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. if (process_material == p_material) {
  128. return;
  129. }
  130. if (process_material.is_valid() && process_material->is_class("ParticleProcessMaterial")) {
  131. process_material->disconnect("emission_shape_changed", callable_mp((CanvasItem *)this, &GPUParticles2D::queue_redraw));
  132. }
  133. process_material = p_material;
  134. if (process_material.is_valid() && process_material->is_class("ParticleProcessMaterial")) {
  135. process_material->connect("emission_shape_changed", callable_mp((CanvasItem *)this, &GPUParticles2D::queue_redraw));
  136. }
  137. queue_redraw();
  138. Ref<ParticleProcessMaterial> pm = p_material;
  139. if (pm.is_valid() && !pm->get_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_DISABLE_Z) && pm->get_gravity() == Vector3(0, -9.8, 0)) {
  140. // Likely a new (3D) material, modify it to match 2D space
  141. pm->set_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_DISABLE_Z, true);
  142. pm->set_gravity(Vector3(0, 98, 0));
  143. }
  144. RID material_rid;
  145. if (process_material.is_valid()) {
  146. material_rid = process_material->get_rid();
  147. }
  148. RS::get_singleton()->particles_set_process_material(particles, material_rid);
  149. update_configuration_warnings();
  150. }
  151. void GPUParticles2D::set_trail_enabled(bool p_enabled) {
  152. trail_enabled = p_enabled;
  153. RS::get_singleton()->particles_set_trails(particles, trail_enabled, trail_lifetime);
  154. queue_redraw();
  155. update_configuration_warnings();
  156. RS::get_singleton()->particles_set_transform_align(particles, p_enabled ? RS::PARTICLES_TRANSFORM_ALIGN_Y_TO_VELOCITY : RS::PARTICLES_TRANSFORM_ALIGN_DISABLED);
  157. }
  158. void GPUParticles2D::set_trail_lifetime(double p_seconds) {
  159. ERR_FAIL_COND(p_seconds < 0.01);
  160. trail_lifetime = p_seconds;
  161. RS::get_singleton()->particles_set_trails(particles, trail_enabled, trail_lifetime);
  162. queue_redraw();
  163. }
  164. void GPUParticles2D::set_trail_sections(int p_sections) {
  165. ERR_FAIL_COND(p_sections < 2);
  166. ERR_FAIL_COND(p_sections > 128);
  167. trail_sections = p_sections;
  168. queue_redraw();
  169. }
  170. void GPUParticles2D::set_trail_section_subdivisions(int p_subdivisions) {
  171. ERR_FAIL_COND(p_subdivisions < 1);
  172. ERR_FAIL_COND(p_subdivisions > 1024);
  173. trail_section_subdivisions = p_subdivisions;
  174. queue_redraw();
  175. }
  176. void GPUParticles2D::set_interp_to_end(float p_interp) {
  177. interp_to_end_factor = CLAMP(p_interp, 0.0, 1.0);
  178. RS::get_singleton()->particles_set_interp_to_end(particles, interp_to_end_factor);
  179. }
  180. #ifdef TOOLS_ENABLED
  181. void GPUParticles2D::set_show_gizmos(bool p_show_gizmos) {
  182. if (show_gizmos == p_show_gizmos) {
  183. return;
  184. }
  185. show_gizmos = p_show_gizmos;
  186. queue_redraw();
  187. }
  188. #endif
  189. bool GPUParticles2D::is_trail_enabled() const {
  190. return trail_enabled;
  191. }
  192. double GPUParticles2D::get_trail_lifetime() const {
  193. return trail_lifetime;
  194. }
  195. void GPUParticles2D::_update_collision_size() {
  196. real_t csize = collision_base_size;
  197. if (texture.is_valid()) {
  198. csize *= (texture->get_width() + texture->get_height()) / 4.0; //half size since its a radius
  199. }
  200. RS::get_singleton()->particles_set_collision_base_size(particles, csize);
  201. }
  202. void GPUParticles2D::set_collision_base_size(real_t p_size) {
  203. collision_base_size = p_size;
  204. _update_collision_size();
  205. }
  206. real_t GPUParticles2D::get_collision_base_size() const {
  207. return collision_base_size;
  208. }
  209. void GPUParticles2D::set_speed_scale(double p_scale) {
  210. speed_scale = p_scale;
  211. RS::get_singleton()->particles_set_speed_scale(particles, p_scale);
  212. }
  213. bool GPUParticles2D::is_emitting() const {
  214. return emitting;
  215. }
  216. int GPUParticles2D::get_amount() const {
  217. return amount;
  218. }
  219. double GPUParticles2D::get_lifetime() const {
  220. return lifetime;
  221. }
  222. int GPUParticles2D::get_trail_sections() const {
  223. return trail_sections;
  224. }
  225. int GPUParticles2D::get_trail_section_subdivisions() const {
  226. return trail_section_subdivisions;
  227. }
  228. bool GPUParticles2D::get_one_shot() const {
  229. return one_shot;
  230. }
  231. double GPUParticles2D::get_pre_process_time() const {
  232. return pre_process_time;
  233. }
  234. real_t GPUParticles2D::get_explosiveness_ratio() const {
  235. return explosiveness_ratio;
  236. }
  237. real_t GPUParticles2D::get_randomness_ratio() const {
  238. return randomness_ratio;
  239. }
  240. Rect2 GPUParticles2D::get_visibility_rect() const {
  241. return visibility_rect;
  242. }
  243. bool GPUParticles2D::get_use_local_coordinates() const {
  244. return local_coords;
  245. }
  246. Ref<Material> GPUParticles2D::get_process_material() const {
  247. return process_material;
  248. }
  249. double GPUParticles2D::get_speed_scale() const {
  250. return speed_scale;
  251. }
  252. void GPUParticles2D::set_draw_order(DrawOrder p_order) {
  253. draw_order = p_order;
  254. RS::get_singleton()->particles_set_draw_order(particles, RS::ParticlesDrawOrder(p_order));
  255. }
  256. GPUParticles2D::DrawOrder GPUParticles2D::get_draw_order() const {
  257. return draw_order;
  258. }
  259. void GPUParticles2D::set_fixed_fps(int p_count) {
  260. fixed_fps = p_count;
  261. RS::get_singleton()->particles_set_fixed_fps(particles, p_count);
  262. }
  263. int GPUParticles2D::get_fixed_fps() const {
  264. return fixed_fps;
  265. }
  266. void GPUParticles2D::set_fractional_delta(bool p_enable) {
  267. fractional_delta = p_enable;
  268. RS::get_singleton()->particles_set_fractional_delta(particles, p_enable);
  269. }
  270. bool GPUParticles2D::get_fractional_delta() const {
  271. return fractional_delta;
  272. }
  273. void GPUParticles2D::set_interpolate(bool p_enable) {
  274. interpolate = p_enable;
  275. RS::get_singleton()->particles_set_interpolate(particles, p_enable);
  276. }
  277. bool GPUParticles2D::get_interpolate() const {
  278. return interpolate;
  279. }
  280. float GPUParticles2D::get_interp_to_end() const {
  281. return interp_to_end_factor;
  282. }
  283. void GPUParticles2D::set_use_fixed_seed(bool p_use_fixed_seed) {
  284. if (p_use_fixed_seed == use_fixed_seed) {
  285. return;
  286. }
  287. use_fixed_seed = p_use_fixed_seed;
  288. notify_property_list_changed();
  289. }
  290. bool GPUParticles2D::get_use_fixed_seed() const {
  291. return use_fixed_seed;
  292. }
  293. void GPUParticles2D::set_seed(uint32_t p_seed) {
  294. seed = p_seed;
  295. RS::get_singleton()->particles_set_seed(particles, p_seed);
  296. }
  297. uint32_t GPUParticles2D::get_seed() const {
  298. return seed;
  299. }
  300. void GPUParticles2D::request_particles_process(real_t p_requested_process_time) {
  301. RS::get_singleton()->particles_request_process_time(particles, p_requested_process_time);
  302. }
  303. PackedStringArray GPUParticles2D::get_configuration_warnings() const {
  304. PackedStringArray warnings = Node2D::get_configuration_warnings();
  305. if (process_material.is_null()) {
  306. warnings.push_back(RTR("A material to process the particles is not assigned, so no behavior is imprinted."));
  307. } else {
  308. CanvasItemMaterial *mat = Object::cast_to<CanvasItemMaterial>(get_material().ptr());
  309. if (get_material().is_null() || (mat && !mat->get_particles_animation())) {
  310. const ParticleProcessMaterial *process = Object::cast_to<ParticleProcessMaterial>(process_material.ptr());
  311. if (process &&
  312. (process->get_param_max(ParticleProcessMaterial::PARAM_ANIM_SPEED) != 0.0 || process->get_param_max(ParticleProcessMaterial::PARAM_ANIM_OFFSET) != 0.0 ||
  313. process->get_param_texture(ParticleProcessMaterial::PARAM_ANIM_SPEED).is_valid() || process->get_param_texture(ParticleProcessMaterial::PARAM_ANIM_OFFSET).is_valid())) {
  314. warnings.push_back(RTR("Particles2D animation requires the usage of a CanvasItemMaterial with \"Particles Animation\" enabled."));
  315. }
  316. }
  317. }
  318. if (trail_enabled && (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility" || OS::get_singleton()->get_current_rendering_method() == "dummy")) {
  319. warnings.push_back(RTR("Particle trails are only available when using the Forward+ or Mobile renderers."));
  320. }
  321. if (sub_emitter != NodePath() && (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility" || OS::get_singleton()->get_current_rendering_method() == "dummy")) {
  322. warnings.push_back(RTR("Particle sub-emitters are not available when using the Compatibility renderer."));
  323. }
  324. return warnings;
  325. }
  326. Rect2 GPUParticles2D::capture_rect() const {
  327. AABB aabb = RS::get_singleton()->particles_get_current_aabb(particles);
  328. Rect2 r;
  329. r.position.x = aabb.position.x;
  330. r.position.y = aabb.position.y;
  331. r.size.x = aabb.size.x;
  332. r.size.y = aabb.size.y;
  333. return r;
  334. }
  335. void GPUParticles2D::set_texture(const Ref<Texture2D> &p_texture) {
  336. if (texture.is_valid()) {
  337. texture->disconnect_changed(callable_mp(this, &GPUParticles2D::_texture_changed));
  338. }
  339. texture = p_texture;
  340. if (texture.is_valid()) {
  341. texture->connect_changed(callable_mp(this, &GPUParticles2D::_texture_changed));
  342. }
  343. _update_collision_size();
  344. queue_redraw();
  345. }
  346. Ref<Texture2D> GPUParticles2D::get_texture() const {
  347. return texture;
  348. }
  349. void GPUParticles2D::_validate_property(PropertyInfo &p_property) const {
  350. if (p_property.name == "seed" && !use_fixed_seed) {
  351. p_property.usage = PROPERTY_USAGE_NONE;
  352. }
  353. if (Engine::get_singleton()->is_editor_hint() && p_property.name == "emitting") {
  354. p_property.hint = one_shot ? PROPERTY_HINT_ONESHOT : PROPERTY_HINT_NONE;
  355. }
  356. }
  357. 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) {
  358. Transform3D emit_transform;
  359. emit_transform.basis.set_column(0, Vector3(p_transform2d.columns[0].x, p_transform2d.columns[0].y, 0));
  360. emit_transform.basis.set_column(1, Vector3(p_transform2d.columns[1].x, p_transform2d.columns[1].y, 0));
  361. emit_transform.set_origin(Vector3(p_transform2d.get_origin().x, p_transform2d.get_origin().y, 0));
  362. Vector3 velocity = Vector3(p_velocity2d.x, p_velocity2d.y, 0);
  363. RS::get_singleton()->particles_emit(particles, emit_transform, velocity, p_color, p_custom, p_emit_flags);
  364. }
  365. void GPUParticles2D::_attach_sub_emitter() {
  366. Node *n = get_node_or_null(sub_emitter);
  367. if (n) {
  368. GPUParticles2D *sen = Object::cast_to<GPUParticles2D>(n);
  369. if (sen && sen != this) {
  370. RS::get_singleton()->particles_set_subemitter(particles, sen->particles);
  371. }
  372. }
  373. }
  374. void GPUParticles2D::_texture_changed() {
  375. // Changes to the texture need to trigger an update to make
  376. // the editor redraw the sprite with the updated texture.
  377. if (texture.is_valid()) {
  378. queue_redraw();
  379. }
  380. }
  381. void GPUParticles2D::set_sub_emitter(const NodePath &p_path) {
  382. if (is_inside_tree()) {
  383. RS::get_singleton()->particles_set_subemitter(particles, RID());
  384. }
  385. sub_emitter = p_path;
  386. if (is_inside_tree() && sub_emitter != NodePath()) {
  387. _attach_sub_emitter();
  388. }
  389. update_configuration_warnings();
  390. }
  391. NodePath GPUParticles2D::get_sub_emitter() const {
  392. return sub_emitter;
  393. }
  394. void GPUParticles2D::set_amount_ratio(float p_ratio) {
  395. amount_ratio = p_ratio;
  396. RenderingServer::get_singleton()->particles_set_amount_ratio(particles, p_ratio);
  397. }
  398. float GPUParticles2D::get_amount_ratio() const {
  399. return amount_ratio;
  400. }
  401. void GPUParticles2D::restart(bool p_keep_seed) {
  402. if (!p_keep_seed && !use_fixed_seed) {
  403. set_seed(Math::rand());
  404. }
  405. RS::get_singleton()->particles_restart(particles);
  406. RS::get_singleton()->particles_set_emitting(particles, true);
  407. emitting = true;
  408. active = true;
  409. signal_canceled = false;
  410. time = 0;
  411. emission_time = lifetime;
  412. active_time = lifetime * (2 - explosiveness_ratio);
  413. if (one_shot) {
  414. set_process_internal(true);
  415. }
  416. }
  417. void GPUParticles2D::convert_from_particles(Node *p_particles) {
  418. CPUParticles2D *cpu_particles = Object::cast_to<CPUParticles2D>(p_particles);
  419. ERR_FAIL_NULL_MSG(cpu_particles, "Only CPUParticles2D nodes can be converted to GPUParticles2D.");
  420. set_emitting(cpu_particles->is_emitting());
  421. set_amount(cpu_particles->get_amount());
  422. set_lifetime(cpu_particles->get_lifetime());
  423. set_one_shot(cpu_particles->get_one_shot());
  424. set_pre_process_time(cpu_particles->get_pre_process_time());
  425. set_explosiveness_ratio(cpu_particles->get_explosiveness_ratio());
  426. set_randomness_ratio(cpu_particles->get_randomness_ratio());
  427. set_use_local_coordinates(cpu_particles->get_use_local_coordinates());
  428. set_fixed_fps(cpu_particles->get_fixed_fps());
  429. set_fractional_delta(cpu_particles->get_fractional_delta());
  430. set_speed_scale(cpu_particles->get_speed_scale());
  431. set_draw_order(DrawOrder(cpu_particles->get_draw_order()));
  432. set_texture(cpu_particles->get_texture());
  433. Ref<Material> mat = cpu_particles->get_material();
  434. if (mat.is_valid()) {
  435. set_material(mat);
  436. }
  437. Ref<ParticleProcessMaterial> proc_mat = memnew(ParticleProcessMaterial);
  438. set_process_material(proc_mat);
  439. Vector2 dir = cpu_particles->get_direction();
  440. proc_mat->set_direction(Vector3(dir.x, dir.y, 0));
  441. proc_mat->set_spread(cpu_particles->get_spread());
  442. proc_mat->set_color(cpu_particles->get_color());
  443. Ref<Gradient> color_grad = cpu_particles->get_color_ramp();
  444. if (color_grad.is_valid()) {
  445. Ref<GradientTexture1D> tex = memnew(GradientTexture1D);
  446. tex->set_gradient(color_grad);
  447. proc_mat->set_color_ramp(tex);
  448. }
  449. Ref<Gradient> color_init_grad = cpu_particles->get_color_initial_ramp();
  450. if (color_init_grad.is_valid()) {
  451. Ref<GradientTexture1D> tex = memnew(GradientTexture1D);
  452. tex->set_gradient(color_init_grad);
  453. proc_mat->set_color_initial_ramp(tex);
  454. }
  455. proc_mat->set_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY, cpu_particles->get_particle_flag(CPUParticles2D::PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY));
  456. proc_mat->set_emission_shape(ParticleProcessMaterial::EmissionShape(cpu_particles->get_emission_shape()));
  457. proc_mat->set_emission_sphere_radius(cpu_particles->get_emission_sphere_radius());
  458. Vector2 rect_extents = cpu_particles->get_emission_rect_extents();
  459. proc_mat->set_emission_box_extents(Vector3(rect_extents.x, rect_extents.y, 0));
  460. if (cpu_particles->get_split_scale()) {
  461. Ref<CurveXYZTexture> scale3D = memnew(CurveXYZTexture);
  462. scale3D->set_curve_x(cpu_particles->get_scale_curve_x());
  463. scale3D->set_curve_y(cpu_particles->get_scale_curve_y());
  464. proc_mat->set_param_texture(ParticleProcessMaterial::PARAM_SCALE, scale3D);
  465. }
  466. Vector2 gravity = cpu_particles->get_gravity();
  467. proc_mat->set_gravity(Vector3(gravity.x, gravity.y, 0));
  468. proc_mat->set_lifetime_randomness(cpu_particles->get_lifetime_randomness());
  469. #define CONVERT_PARAM(m_param) \
  470. proc_mat->set_param_min(ParticleProcessMaterial::m_param, cpu_particles->get_param_min(CPUParticles2D::m_param)); \
  471. { \
  472. Ref<Curve> curve = cpu_particles->get_param_curve(CPUParticles2D::m_param); \
  473. if (curve.is_valid()) { \
  474. Ref<CurveTexture> tex = memnew(CurveTexture); \
  475. tex->set_curve(curve); \
  476. proc_mat->set_param_texture(ParticleProcessMaterial::m_param, tex); \
  477. } \
  478. } \
  479. proc_mat->set_param_max(ParticleProcessMaterial::m_param, cpu_particles->get_param_max(CPUParticles2D::m_param));
  480. CONVERT_PARAM(PARAM_INITIAL_LINEAR_VELOCITY);
  481. CONVERT_PARAM(PARAM_ANGULAR_VELOCITY);
  482. CONVERT_PARAM(PARAM_ORBIT_VELOCITY);
  483. CONVERT_PARAM(PARAM_LINEAR_ACCEL);
  484. CONVERT_PARAM(PARAM_RADIAL_ACCEL);
  485. CONVERT_PARAM(PARAM_TANGENTIAL_ACCEL);
  486. CONVERT_PARAM(PARAM_DAMPING);
  487. CONVERT_PARAM(PARAM_ANGLE);
  488. CONVERT_PARAM(PARAM_SCALE);
  489. CONVERT_PARAM(PARAM_HUE_VARIATION);
  490. CONVERT_PARAM(PARAM_ANIM_SPEED);
  491. CONVERT_PARAM(PARAM_ANIM_OFFSET);
  492. #undef CONVERT_PARAM
  493. }
  494. void GPUParticles2D::_notification(int p_what) {
  495. switch (p_what) {
  496. case NOTIFICATION_DRAW: {
  497. RID texture_rid;
  498. Size2 size;
  499. if (texture.is_valid()) {
  500. texture_rid = texture->get_rid();
  501. size = texture->get_size();
  502. } else {
  503. size = Size2(1, 1);
  504. }
  505. if (trail_enabled) {
  506. RS::get_singleton()->mesh_clear(mesh);
  507. PackedVector2Array points;
  508. PackedVector2Array uvs;
  509. PackedInt32Array bone_indices;
  510. PackedFloat32Array bone_weights;
  511. PackedInt32Array indices;
  512. int total_segments = trail_sections * trail_section_subdivisions;
  513. real_t depth = size.height * trail_sections;
  514. for (int j = 0; j <= total_segments; j++) {
  515. real_t v = j;
  516. v /= total_segments;
  517. real_t y = depth * v;
  518. y = (depth * 0.5) - y;
  519. int bone = j / trail_section_subdivisions;
  520. real_t blend = 1.0 - real_t(j % trail_section_subdivisions) / real_t(trail_section_subdivisions);
  521. real_t s = size.width;
  522. points.push_back(Vector2(-s * 0.5, 0));
  523. points.push_back(Vector2(+s * 0.5, 0));
  524. uvs.push_back(Vector2(0, v));
  525. uvs.push_back(Vector2(1, v));
  526. for (int i = 0; i < 2; i++) {
  527. bone_indices.push_back(bone);
  528. bone_indices.push_back(MIN(trail_sections, bone + 1));
  529. bone_indices.push_back(0);
  530. bone_indices.push_back(0);
  531. bone_weights.push_back(blend);
  532. bone_weights.push_back(1.0 - blend);
  533. bone_weights.push_back(0);
  534. bone_weights.push_back(0);
  535. }
  536. if (j > 0) {
  537. int base = j * 2 - 2;
  538. indices.push_back(base + 0);
  539. indices.push_back(base + 1);
  540. indices.push_back(base + 2);
  541. indices.push_back(base + 1);
  542. indices.push_back(base + 3);
  543. indices.push_back(base + 2);
  544. }
  545. }
  546. Array arr;
  547. arr.resize(RS::ARRAY_MAX);
  548. arr[RS::ARRAY_VERTEX] = points;
  549. arr[RS::ARRAY_TEX_UV] = uvs;
  550. arr[RS::ARRAY_BONES] = bone_indices;
  551. arr[RS::ARRAY_WEIGHTS] = bone_weights;
  552. arr[RS::ARRAY_INDEX] = indices;
  553. RS::get_singleton()->mesh_add_surface_from_arrays(mesh, RS::PRIMITIVE_TRIANGLES, arr, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
  554. Vector<Transform3D> xforms;
  555. for (int i = 0; i <= trail_sections; i++) {
  556. Transform3D xform;
  557. /*
  558. xform.origin.y = depth / 2.0 - size.height * real_t(i);
  559. xform.origin.y = -xform.origin.y; //bind is an inverse transform, so negate y */
  560. xforms.push_back(xform);
  561. }
  562. RS::get_singleton()->particles_set_trail_bind_poses(particles, xforms);
  563. } else {
  564. RS::get_singleton()->mesh_clear(mesh);
  565. Vector<Vector2> points = {
  566. Vector2(-size.x / 2.0, -size.y / 2.0),
  567. Vector2(size.x / 2.0, -size.y / 2.0),
  568. Vector2(size.x / 2.0, size.y / 2.0),
  569. Vector2(-size.x / 2.0, size.y / 2.0)
  570. };
  571. Vector<Vector2> uvs;
  572. AtlasTexture *atlas_texture = Object::cast_to<AtlasTexture>(*texture);
  573. if (atlas_texture && atlas_texture->get_atlas().is_valid()) {
  574. Rect2 region_rect = atlas_texture->get_region();
  575. Size2 atlas_size = atlas_texture->get_atlas()->get_size();
  576. uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, region_rect.position.y / atlas_size.y));
  577. uvs.push_back(Vector2((region_rect.position.x + region_rect.size.x) / atlas_size.x, region_rect.position.y / atlas_size.y));
  578. 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));
  579. uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, (region_rect.position.y + region_rect.size.y) / atlas_size.y));
  580. } else {
  581. uvs.push_back(Vector2(0, 0));
  582. uvs.push_back(Vector2(1, 0));
  583. uvs.push_back(Vector2(1, 1));
  584. uvs.push_back(Vector2(0, 1));
  585. }
  586. Vector<int> indices = { 0, 1, 2, 0, 2, 3 };
  587. Array arr;
  588. arr.resize(RS::ARRAY_MAX);
  589. arr[RS::ARRAY_VERTEX] = points;
  590. arr[RS::ARRAY_TEX_UV] = uvs;
  591. arr[RS::ARRAY_INDEX] = indices;
  592. RS::get_singleton()->mesh_add_surface_from_arrays(mesh, RS::PRIMITIVE_TRIANGLES, arr, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
  593. RS::get_singleton()->particles_set_trail_bind_poses(particles, Vector<Transform3D>());
  594. }
  595. RS::get_singleton()->canvas_item_add_particles(get_canvas_item(), particles, texture_rid);
  596. #ifdef TOOLS_ENABLED
  597. if (show_gizmos) {
  598. draw_rect(visibility_rect, Color(0, 0.7, 0.9, 0.4), false);
  599. _draw_emission_gizmo();
  600. }
  601. #endif
  602. } break;
  603. case NOTIFICATION_ENTER_TREE: {
  604. if (sub_emitter != NodePath()) {
  605. _attach_sub_emitter();
  606. }
  607. if (can_process()) {
  608. RS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
  609. } else {
  610. RS::get_singleton()->particles_set_speed_scale(particles, 0);
  611. }
  612. set_process_internal(true);
  613. set_physics_process_internal(true);
  614. previous_position = get_global_position();
  615. } break;
  616. case NOTIFICATION_EXIT_TREE: {
  617. RS::get_singleton()->particles_set_subemitter(particles, RID());
  618. } break;
  619. case NOTIFICATION_SUSPENDED:
  620. case NOTIFICATION_UNSUSPENDED:
  621. case NOTIFICATION_PAUSED:
  622. case NOTIFICATION_UNPAUSED: {
  623. if (is_inside_tree()) {
  624. if (can_process()) {
  625. RS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
  626. } else {
  627. RS::get_singleton()->particles_set_speed_scale(particles, 0);
  628. }
  629. }
  630. } break;
  631. case NOTIFICATION_TRANSFORM_CHANGED: {
  632. _update_particle_emission_transform();
  633. } break;
  634. case NOTIFICATION_INTERNAL_PROCESS: {
  635. if (one_shot) {
  636. time += get_process_delta_time();
  637. if (time > emission_time) {
  638. emitting = false;
  639. if (!active) {
  640. set_process_internal(false);
  641. }
  642. }
  643. if (time > active_time) {
  644. if (active && !signal_canceled) {
  645. emit_signal(SceneStringName(finished));
  646. }
  647. active = false;
  648. if (!emitting) {
  649. set_process_internal(false);
  650. }
  651. }
  652. }
  653. } break;
  654. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  655. // Update velocity in physics process, so that velocity calculations remain correct
  656. // if the physics tick rate is lower than the rendered framerate (especially without physics interpolation).
  657. const Vector3 velocity = Vector3((get_global_position() - previous_position).x, (get_global_position() - previous_position).y, 0.0) /
  658. get_physics_process_delta_time();
  659. if (velocity != previous_velocity) {
  660. RS::get_singleton()->particles_set_emitter_velocity(particles, velocity);
  661. previous_velocity = velocity;
  662. }
  663. previous_position = get_global_position();
  664. } break;
  665. }
  666. }
  667. #ifdef TOOLS_ENABLED
  668. void GPUParticles2D::_draw_emission_gizmo() {
  669. Ref<ParticleProcessMaterial> pm = process_material;
  670. Color emission_ring_color = Color(0.8, 0.7, 0.4, 0.4);
  671. if (pm.is_null()) {
  672. return;
  673. }
  674. draw_set_transform(
  675. Vector2(pm->get_emission_shape_offset().x, pm->get_emission_shape_offset().y),
  676. 0.0,
  677. Vector2(pm->get_emission_shape_scale().x, pm->get_emission_shape_scale().y));
  678. switch (pm->get_emission_shape()) {
  679. case ParticleProcessMaterial::EmissionShape::EMISSION_SHAPE_BOX: {
  680. Vector2 extents2d = Vector2(pm->get_emission_box_extents().x, pm->get_emission_box_extents().y);
  681. draw_rect(Rect2(-extents2d, extents2d * 2.0), emission_ring_color, false);
  682. break;
  683. }
  684. case ParticleProcessMaterial::EmissionShape::EMISSION_SHAPE_SPHERE:
  685. case ParticleProcessMaterial::EmissionShape::EMISSION_SHAPE_SPHERE_SURFACE: {
  686. draw_circle(Vector2(), pm->get_emission_sphere_radius(), emission_ring_color, false);
  687. break;
  688. }
  689. case ParticleProcessMaterial::EmissionShape::EMISSION_SHAPE_RING: {
  690. Vector3 ring_axis = pm->get_emission_ring_axis();
  691. if (ring_axis.is_equal_approx(Vector3(0.0, 0.0, 1.0)) || ring_axis.is_zero_approx()) {
  692. draw_circle(Vector2(), pm->get_emission_ring_inner_radius(), emission_ring_color, false);
  693. draw_circle(Vector2(), pm->get_emission_ring_radius(), emission_ring_color, false);
  694. } else {
  695. Vector2 a = Vector2(pm->get_emission_ring_height() / -2.0, pm->get_emission_ring_radius() / -1.0);
  696. Vector2 b = Vector2(-a.x, MIN(a.y + std::tan((90.0 - pm->get_emission_ring_cone_angle()) * 0.01745329) * pm->get_emission_ring_height(), 0.0));
  697. Vector2 c = Vector2(b.x, -b.y);
  698. Vector2 d = Vector2(a.x, -a.y);
  699. if (ring_axis.is_equal_approx(Vector3(1.0, 0.0, 0.0))) {
  700. Vector<Vector2> pos = { a, b, b, c, c, d, d, a };
  701. draw_multiline(pos, emission_ring_color);
  702. } else if (ring_axis.is_equal_approx(Vector3(0.0, 1.0, 0.0))) {
  703. a = Vector2(a.y, a.x);
  704. b = Vector2(b.y, b.x);
  705. c = Vector2(c.y, c.x);
  706. d = Vector2(d.y, d.x);
  707. Vector<Vector2> pos = { a, b, b, c, c, d, d, a };
  708. draw_multiline(pos, emission_ring_color);
  709. }
  710. }
  711. break;
  712. }
  713. default: {
  714. break;
  715. }
  716. }
  717. }
  718. #endif
  719. void GPUParticles2D::_bind_methods() {
  720. ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &GPUParticles2D::set_emitting);
  721. ClassDB::bind_method(D_METHOD("set_amount", "amount"), &GPUParticles2D::set_amount);
  722. ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &GPUParticles2D::set_lifetime);
  723. ClassDB::bind_method(D_METHOD("set_one_shot", "secs"), &GPUParticles2D::set_one_shot);
  724. ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &GPUParticles2D::set_pre_process_time);
  725. ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &GPUParticles2D::set_explosiveness_ratio);
  726. ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &GPUParticles2D::set_randomness_ratio);
  727. ClassDB::bind_method(D_METHOD("set_visibility_rect", "visibility_rect"), &GPUParticles2D::set_visibility_rect);
  728. ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &GPUParticles2D::set_use_local_coordinates);
  729. ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &GPUParticles2D::set_fixed_fps);
  730. ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &GPUParticles2D::set_fractional_delta);
  731. ClassDB::bind_method(D_METHOD("set_interpolate", "enable"), &GPUParticles2D::set_interpolate);
  732. ClassDB::bind_method(D_METHOD("set_process_material", "material"), &GPUParticles2D::set_process_material);
  733. ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &GPUParticles2D::set_speed_scale);
  734. ClassDB::bind_method(D_METHOD("set_collision_base_size", "size"), &GPUParticles2D::set_collision_base_size);
  735. ClassDB::bind_method(D_METHOD("set_interp_to_end", "interp"), &GPUParticles2D::set_interp_to_end);
  736. ClassDB::bind_method(D_METHOD("request_particles_process", "process_time"), &GPUParticles2D::request_particles_process);
  737. ClassDB::bind_method(D_METHOD("is_emitting"), &GPUParticles2D::is_emitting);
  738. ClassDB::bind_method(D_METHOD("get_amount"), &GPUParticles2D::get_amount);
  739. ClassDB::bind_method(D_METHOD("get_lifetime"), &GPUParticles2D::get_lifetime);
  740. ClassDB::bind_method(D_METHOD("get_one_shot"), &GPUParticles2D::get_one_shot);
  741. ClassDB::bind_method(D_METHOD("get_pre_process_time"), &GPUParticles2D::get_pre_process_time);
  742. ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &GPUParticles2D::get_explosiveness_ratio);
  743. ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &GPUParticles2D::get_randomness_ratio);
  744. ClassDB::bind_method(D_METHOD("get_visibility_rect"), &GPUParticles2D::get_visibility_rect);
  745. ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &GPUParticles2D::get_use_local_coordinates);
  746. ClassDB::bind_method(D_METHOD("get_fixed_fps"), &GPUParticles2D::get_fixed_fps);
  747. ClassDB::bind_method(D_METHOD("get_fractional_delta"), &GPUParticles2D::get_fractional_delta);
  748. ClassDB::bind_method(D_METHOD("get_interpolate"), &GPUParticles2D::get_interpolate);
  749. ClassDB::bind_method(D_METHOD("get_process_material"), &GPUParticles2D::get_process_material);
  750. ClassDB::bind_method(D_METHOD("get_speed_scale"), &GPUParticles2D::get_speed_scale);
  751. ClassDB::bind_method(D_METHOD("get_collision_base_size"), &GPUParticles2D::get_collision_base_size);
  752. ClassDB::bind_method(D_METHOD("get_interp_to_end"), &GPUParticles2D::get_interp_to_end);
  753. ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &GPUParticles2D::set_draw_order);
  754. ClassDB::bind_method(D_METHOD("get_draw_order"), &GPUParticles2D::get_draw_order);
  755. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &GPUParticles2D::set_texture);
  756. ClassDB::bind_method(D_METHOD("get_texture"), &GPUParticles2D::get_texture);
  757. ClassDB::bind_method(D_METHOD("capture_rect"), &GPUParticles2D::capture_rect);
  758. ClassDB::bind_method(D_METHOD("restart", "keep_seed"), &GPUParticles2D::restart, DEFVAL(false));
  759. ClassDB::bind_method(D_METHOD("set_sub_emitter", "path"), &GPUParticles2D::set_sub_emitter);
  760. ClassDB::bind_method(D_METHOD("get_sub_emitter"), &GPUParticles2D::get_sub_emitter);
  761. ClassDB::bind_method(D_METHOD("emit_particle", "xform", "velocity", "color", "custom", "flags"), &GPUParticles2D::emit_particle);
  762. ClassDB::bind_method(D_METHOD("set_trail_enabled", "enabled"), &GPUParticles2D::set_trail_enabled);
  763. ClassDB::bind_method(D_METHOD("set_trail_lifetime", "secs"), &GPUParticles2D::set_trail_lifetime);
  764. ClassDB::bind_method(D_METHOD("is_trail_enabled"), &GPUParticles2D::is_trail_enabled);
  765. ClassDB::bind_method(D_METHOD("get_trail_lifetime"), &GPUParticles2D::get_trail_lifetime);
  766. ClassDB::bind_method(D_METHOD("set_trail_sections", "sections"), &GPUParticles2D::set_trail_sections);
  767. ClassDB::bind_method(D_METHOD("get_trail_sections"), &GPUParticles2D::get_trail_sections);
  768. ClassDB::bind_method(D_METHOD("set_trail_section_subdivisions", "subdivisions"), &GPUParticles2D::set_trail_section_subdivisions);
  769. ClassDB::bind_method(D_METHOD("get_trail_section_subdivisions"), &GPUParticles2D::get_trail_section_subdivisions);
  770. ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &GPUParticles2D::convert_from_particles);
  771. ClassDB::bind_method(D_METHOD("set_amount_ratio", "ratio"), &GPUParticles2D::set_amount_ratio);
  772. ClassDB::bind_method(D_METHOD("get_amount_ratio"), &GPUParticles2D::get_amount_ratio);
  773. ClassDB::bind_method(D_METHOD("set_use_fixed_seed", "use_fixed_seed"), &GPUParticles2D::set_use_fixed_seed);
  774. ClassDB::bind_method(D_METHOD("get_use_fixed_seed"), &GPUParticles2D::get_use_fixed_seed);
  775. ClassDB::bind_method(D_METHOD("set_seed", "seed"), &GPUParticles2D::set_seed);
  776. ClassDB::bind_method(D_METHOD("get_seed"), &GPUParticles2D::get_seed);
  777. ADD_SIGNAL(MethodInfo("finished"));
  778. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting", PROPERTY_HINT_ONESHOT), "set_emitting", "is_emitting");
  779. ADD_PROPERTY_DEFAULT("emitting", true); // Workaround for doctool in headless mode, as dummy rasterizer always returns false.
  780. 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.
  781. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "amount_ratio", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_amount_ratio", "get_amount_ratio");
  782. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "sub_emitter", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "GPUParticles2D"), "set_sub_emitter", "get_sub_emitter");
  783. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  784. ADD_GROUP("Time", "");
  785. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime", PROPERTY_HINT_RANGE, "0.01,600.0,0.01,or_greater,exp,suffix:s"), "set_lifetime", "get_lifetime");
  786. 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");
  787. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
  788. 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");
  789. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
  790. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
  791. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
  792. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_fixed_seed", PROPERTY_HINT_ENUM), "set_use_fixed_seed", "get_use_fixed_seed");
  793. ADD_PROPERTY(PropertyInfo(Variant::INT, "seed", PROPERTY_HINT_RANGE, "0," + itos(UINT32_MAX) + ",1"), "set_seed", "get_seed");
  794. ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1,suffix:FPS"), "set_fixed_fps", "get_fixed_fps");
  795. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interpolate"), "set_interpolate", "get_interpolate");
  796. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
  797. ADD_GROUP("Collision", "collision_");
  798. 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");
  799. ADD_GROUP("Drawing", "");
  800. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "visibility_rect", PROPERTY_HINT_NONE, "suffix:px"), "set_visibility_rect", "get_visibility_rect");
  801. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
  802. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime,Reverse Lifetime"), "set_draw_order", "get_draw_order");
  803. ADD_GROUP("Trails", "trail_");
  804. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "trail_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_trail_enabled", "is_trail_enabled");
  805. 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");
  806. ADD_PROPERTY(PropertyInfo(Variant::INT, "trail_sections", PROPERTY_HINT_RANGE, "2,128,1"), "set_trail_sections", "get_trail_sections");
  807. ADD_PROPERTY(PropertyInfo(Variant::INT, "trail_section_subdivisions", PROPERTY_HINT_RANGE, "1,1024,1"), "set_trail_section_subdivisions", "get_trail_section_subdivisions");
  808. ADD_GROUP("Process Material", "");
  809. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "process_material", PROPERTY_HINT_RESOURCE_TYPE, "ParticleProcessMaterial,ShaderMaterial"), "set_process_material", "get_process_material");
  810. BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
  811. BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
  812. BIND_ENUM_CONSTANT(DRAW_ORDER_REVERSE_LIFETIME);
  813. BIND_ENUM_CONSTANT(EMIT_FLAG_POSITION);
  814. BIND_ENUM_CONSTANT(EMIT_FLAG_ROTATION_SCALE);
  815. BIND_ENUM_CONSTANT(EMIT_FLAG_VELOCITY);
  816. BIND_ENUM_CONSTANT(EMIT_FLAG_COLOR);
  817. BIND_ENUM_CONSTANT(EMIT_FLAG_CUSTOM);
  818. ADD_PROPERTY_DEFAULT("seed", 0);
  819. }
  820. GPUParticles2D::GPUParticles2D() {
  821. particles = RS::get_singleton()->particles_create();
  822. RS::get_singleton()->particles_set_mode(particles, RS::PARTICLES_MODE_2D);
  823. mesh = RS::get_singleton()->mesh_create();
  824. RS::get_singleton()->particles_set_draw_passes(particles, 1);
  825. RS::get_singleton()->particles_set_draw_pass_mesh(particles, 0, mesh);
  826. one_shot = false; // Needed so that set_emitting doesn't access uninitialized values
  827. set_emitting(true);
  828. set_one_shot(false);
  829. set_seed(Math::rand());
  830. set_use_fixed_seed(false);
  831. set_amount(8);
  832. set_amount_ratio(1.0);
  833. set_lifetime(1);
  834. set_fixed_fps(0);
  835. set_fractional_delta(true);
  836. set_interpolate(true);
  837. set_pre_process_time(0);
  838. set_explosiveness_ratio(0);
  839. set_randomness_ratio(0);
  840. set_visibility_rect(Rect2(Vector2(-100, -100), Vector2(200, 200)));
  841. set_use_local_coordinates(false);
  842. set_draw_order(DRAW_ORDER_LIFETIME);
  843. set_speed_scale(1);
  844. set_fixed_fps(30);
  845. set_collision_base_size(collision_base_size);
  846. }
  847. GPUParticles2D::~GPUParticles2D() {
  848. ERR_FAIL_NULL(RenderingServer::get_singleton());
  849. RS::get_singleton()->free(particles);
  850. RS::get_singleton()->free(mesh);
  851. }