gpu_particles_2d.cpp 35 KB

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