gpu_particles_2d.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /*************************************************************************/
  2. /* gpu_particles_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gpu_particles_2d.h"
  31. #include "core/core_string_names.h"
  32. #include "scene/resources/particle_process_material.h"
  33. #ifdef TOOLS_ENABLED
  34. #include "core/config/engine.h"
  35. #endif
  36. void GPUParticles2D::set_emitting(bool p_emitting) {
  37. RS::get_singleton()->particles_set_emitting(particles, p_emitting);
  38. if (p_emitting && one_shot) {
  39. set_process_internal(true);
  40. } else if (!p_emitting) {
  41. set_process_internal(false);
  42. }
  43. }
  44. void GPUParticles2D::set_amount(int p_amount) {
  45. ERR_FAIL_COND_MSG(p_amount < 1, "Amount of particles cannot be smaller than 1.");
  46. amount = p_amount;
  47. RS::get_singleton()->particles_set_amount(particles, amount);
  48. }
  49. void GPUParticles2D::set_lifetime(double p_lifetime) {
  50. ERR_FAIL_COND_MSG(p_lifetime <= 0, "Particles lifetime must be greater than 0.");
  51. lifetime = p_lifetime;
  52. RS::get_singleton()->particles_set_lifetime(particles, lifetime);
  53. }
  54. void GPUParticles2D::set_one_shot(bool p_enable) {
  55. one_shot = p_enable;
  56. RS::get_singleton()->particles_set_one_shot(particles, one_shot);
  57. if (is_emitting()) {
  58. set_process_internal(true);
  59. if (!one_shot) {
  60. RenderingServer::get_singleton()->particles_restart(particles);
  61. }
  62. }
  63. if (!one_shot) {
  64. set_process_internal(false);
  65. }
  66. }
  67. void GPUParticles2D::set_pre_process_time(double p_time) {
  68. pre_process_time = p_time;
  69. RS::get_singleton()->particles_set_pre_process_time(particles, pre_process_time);
  70. }
  71. void GPUParticles2D::set_explosiveness_ratio(real_t p_ratio) {
  72. explosiveness_ratio = p_ratio;
  73. RS::get_singleton()->particles_set_explosiveness_ratio(particles, explosiveness_ratio);
  74. }
  75. void GPUParticles2D::set_randomness_ratio(real_t p_ratio) {
  76. randomness_ratio = p_ratio;
  77. RS::get_singleton()->particles_set_randomness_ratio(particles, randomness_ratio);
  78. }
  79. void GPUParticles2D::set_visibility_rect(const Rect2 &p_visibility_rect) {
  80. visibility_rect = p_visibility_rect;
  81. AABB aabb;
  82. aabb.position.x = p_visibility_rect.position.x;
  83. aabb.position.y = p_visibility_rect.position.y;
  84. aabb.size.x = p_visibility_rect.size.x;
  85. aabb.size.y = p_visibility_rect.size.y;
  86. RS::get_singleton()->particles_set_custom_aabb(particles, aabb);
  87. queue_redraw();
  88. }
  89. void GPUParticles2D::set_use_local_coordinates(bool p_enable) {
  90. local_coords = p_enable;
  91. RS::get_singleton()->particles_set_use_local_coordinates(particles, local_coords);
  92. set_notify_transform(!p_enable);
  93. if (!p_enable && is_inside_tree()) {
  94. _update_particle_emission_transform();
  95. }
  96. }
  97. void GPUParticles2D::_update_particle_emission_transform() {
  98. Transform2D xf2d = get_global_transform();
  99. Transform3D xf;
  100. xf.basis.set_column(0, Vector3(xf2d.columns[0].x, xf2d.columns[0].y, 0));
  101. xf.basis.set_column(1, Vector3(xf2d.columns[1].x, xf2d.columns[1].y, 0));
  102. xf.set_origin(Vector3(xf2d.get_origin().x, xf2d.get_origin().y, 0));
  103. RS::get_singleton()->particles_set_emission_transform(particles, xf);
  104. }
  105. void GPUParticles2D::set_process_material(const Ref<Material> &p_material) {
  106. process_material = p_material;
  107. Ref<ParticleProcessMaterial> pm = p_material;
  108. if (pm.is_valid() && !pm->get_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_DISABLE_Z) && pm->get_gravity() == Vector3(0, -9.8, 0)) {
  109. // Likely a new (3D) material, modify it to match 2D space
  110. pm->set_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_DISABLE_Z, true);
  111. pm->set_gravity(Vector3(0, 98, 0));
  112. }
  113. RID material_rid;
  114. if (process_material.is_valid()) {
  115. material_rid = process_material->get_rid();
  116. }
  117. RS::get_singleton()->particles_set_process_material(particles, material_rid);
  118. update_configuration_warnings();
  119. }
  120. void GPUParticles2D::set_trail_enabled(bool p_enabled) {
  121. trail_enabled = p_enabled;
  122. RS::get_singleton()->particles_set_trails(particles, trail_enabled, trail_length);
  123. queue_redraw();
  124. RS::get_singleton()->particles_set_transform_align(particles, p_enabled ? RS::PARTICLES_TRANSFORM_ALIGN_Y_TO_VELOCITY : RS::PARTICLES_TRANSFORM_ALIGN_DISABLED);
  125. }
  126. void GPUParticles2D::set_trail_length(double p_seconds) {
  127. ERR_FAIL_COND(p_seconds < 0.001);
  128. trail_length = p_seconds;
  129. RS::get_singleton()->particles_set_trails(particles, trail_enabled, trail_length);
  130. queue_redraw();
  131. }
  132. void GPUParticles2D::set_trail_sections(int p_sections) {
  133. ERR_FAIL_COND(p_sections < 2);
  134. ERR_FAIL_COND(p_sections > 128);
  135. trail_sections = p_sections;
  136. queue_redraw();
  137. }
  138. void GPUParticles2D::set_trail_section_subdivisions(int p_subdivisions) {
  139. ERR_FAIL_COND(p_subdivisions < 1);
  140. ERR_FAIL_COND(p_subdivisions > 1024);
  141. trail_section_subdivisions = p_subdivisions;
  142. queue_redraw();
  143. }
  144. #ifdef TOOLS_ENABLED
  145. void GPUParticles2D::set_show_visibility_rect(bool p_show_visibility_rect) {
  146. show_visibility_rect = p_show_visibility_rect;
  147. queue_redraw();
  148. }
  149. #endif
  150. bool GPUParticles2D::is_trail_enabled() const {
  151. return trail_enabled;
  152. }
  153. double GPUParticles2D::get_trail_length() const {
  154. return trail_length;
  155. }
  156. void GPUParticles2D::_update_collision_size() {
  157. real_t csize = collision_base_size;
  158. if (texture.is_valid()) {
  159. csize *= (texture->get_width() + texture->get_height()) / 4.0; //half size since its a radius
  160. }
  161. RS::get_singleton()->particles_set_collision_base_size(particles, csize);
  162. }
  163. void GPUParticles2D::set_collision_base_size(real_t p_size) {
  164. collision_base_size = p_size;
  165. _update_collision_size();
  166. }
  167. real_t GPUParticles2D::get_collision_base_size() const {
  168. return collision_base_size;
  169. }
  170. void GPUParticles2D::set_speed_scale(double p_scale) {
  171. speed_scale = p_scale;
  172. RS::get_singleton()->particles_set_speed_scale(particles, p_scale);
  173. }
  174. bool GPUParticles2D::is_emitting() const {
  175. return RS::get_singleton()->particles_get_emitting(particles);
  176. }
  177. int GPUParticles2D::get_amount() const {
  178. return amount;
  179. }
  180. double GPUParticles2D::get_lifetime() const {
  181. return lifetime;
  182. }
  183. int GPUParticles2D::get_trail_sections() const {
  184. return trail_sections;
  185. }
  186. int GPUParticles2D::get_trail_section_subdivisions() const {
  187. return trail_section_subdivisions;
  188. }
  189. bool GPUParticles2D::get_one_shot() const {
  190. return one_shot;
  191. }
  192. double GPUParticles2D::get_pre_process_time() const {
  193. return pre_process_time;
  194. }
  195. real_t GPUParticles2D::get_explosiveness_ratio() const {
  196. return explosiveness_ratio;
  197. }
  198. real_t GPUParticles2D::get_randomness_ratio() const {
  199. return randomness_ratio;
  200. }
  201. Rect2 GPUParticles2D::get_visibility_rect() const {
  202. return visibility_rect;
  203. }
  204. bool GPUParticles2D::get_use_local_coordinates() const {
  205. return local_coords;
  206. }
  207. Ref<Material> GPUParticles2D::get_process_material() const {
  208. return process_material;
  209. }
  210. double GPUParticles2D::get_speed_scale() const {
  211. return speed_scale;
  212. }
  213. void GPUParticles2D::set_draw_order(DrawOrder p_order) {
  214. draw_order = p_order;
  215. RS::get_singleton()->particles_set_draw_order(particles, RS::ParticlesDrawOrder(p_order));
  216. }
  217. GPUParticles2D::DrawOrder GPUParticles2D::get_draw_order() const {
  218. return draw_order;
  219. }
  220. void GPUParticles2D::set_fixed_fps(int p_count) {
  221. fixed_fps = p_count;
  222. RS::get_singleton()->particles_set_fixed_fps(particles, p_count);
  223. }
  224. int GPUParticles2D::get_fixed_fps() const {
  225. return fixed_fps;
  226. }
  227. void GPUParticles2D::set_fractional_delta(bool p_enable) {
  228. fractional_delta = p_enable;
  229. RS::get_singleton()->particles_set_fractional_delta(particles, p_enable);
  230. }
  231. bool GPUParticles2D::get_fractional_delta() const {
  232. return fractional_delta;
  233. }
  234. void GPUParticles2D::set_interpolate(bool p_enable) {
  235. interpolate = p_enable;
  236. RS::get_singleton()->particles_set_interpolate(particles, p_enable);
  237. }
  238. bool GPUParticles2D::get_interpolate() const {
  239. return interpolate;
  240. }
  241. TypedArray<String> GPUParticles2D::get_configuration_warnings() const {
  242. TypedArray<String> warnings = Node2D::get_configuration_warnings();
  243. if (RenderingServer::get_singleton()->is_low_end()) {
  244. warnings.push_back(RTR("GPU-based particles are not supported by the OpenGL video driver.\nUse the CPUParticles2D node instead. You can use the \"Convert to CPUParticles2D\" option for this purpose."));
  245. }
  246. if (process_material.is_null()) {
  247. warnings.push_back(RTR("A material to process the particles is not assigned, so no behavior is imprinted."));
  248. } else {
  249. CanvasItemMaterial *mat = Object::cast_to<CanvasItemMaterial>(get_material().ptr());
  250. if (get_material().is_null() || (mat && !mat->get_particles_animation())) {
  251. const ParticleProcessMaterial *process = Object::cast_to<ParticleProcessMaterial>(process_material.ptr());
  252. if (process &&
  253. (process->get_param_max(ParticleProcessMaterial::PARAM_ANIM_SPEED) != 0.0 || process->get_param_max(ParticleProcessMaterial::PARAM_ANIM_OFFSET) != 0.0 ||
  254. process->get_param_texture(ParticleProcessMaterial::PARAM_ANIM_SPEED).is_valid() || process->get_param_texture(ParticleProcessMaterial::PARAM_ANIM_OFFSET).is_valid())) {
  255. warnings.push_back(RTR("Particles2D animation requires the usage of a CanvasItemMaterial with \"Particles Animation\" enabled."));
  256. }
  257. }
  258. }
  259. return warnings;
  260. }
  261. Rect2 GPUParticles2D::capture_rect() const {
  262. AABB aabb = RS::get_singleton()->particles_get_current_aabb(particles);
  263. Rect2 r;
  264. r.position.x = aabb.position.x;
  265. r.position.y = aabb.position.y;
  266. r.size.x = aabb.size.x;
  267. r.size.y = aabb.size.y;
  268. return r;
  269. }
  270. void GPUParticles2D::set_texture(const Ref<Texture2D> &p_texture) {
  271. if (texture.is_valid()) {
  272. texture->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &GPUParticles2D::_texture_changed));
  273. }
  274. texture = p_texture;
  275. if (texture.is_valid()) {
  276. texture->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &GPUParticles2D::_texture_changed));
  277. }
  278. _update_collision_size();
  279. queue_redraw();
  280. }
  281. Ref<Texture2D> GPUParticles2D::get_texture() const {
  282. return texture;
  283. }
  284. void GPUParticles2D::_validate_property(PropertyInfo &p_property) const {
  285. }
  286. 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) {
  287. Transform3D transform;
  288. transform.basis.set_column(0, Vector3(p_transform2d.columns[0].x, p_transform2d.columns[0].y, 0));
  289. transform.basis.set_column(1, Vector3(p_transform2d.columns[1].x, p_transform2d.columns[1].y, 0));
  290. transform.set_origin(Vector3(p_transform2d.get_origin().x, p_transform2d.get_origin().y, 0));
  291. Vector3 velocity = Vector3(p_velocity2d.x, p_velocity2d.y, 0);
  292. RS::get_singleton()->particles_emit(particles, transform, velocity, p_color, p_custom, p_emit_flags);
  293. }
  294. void GPUParticles2D::_attach_sub_emitter() {
  295. Node *n = get_node_or_null(sub_emitter);
  296. if (n) {
  297. GPUParticles2D *sen = Object::cast_to<GPUParticles2D>(n);
  298. if (sen && sen != this) {
  299. RS::get_singleton()->particles_set_subemitter(particles, sen->particles);
  300. }
  301. }
  302. }
  303. void GPUParticles2D::_texture_changed() {
  304. // Changes to the texture need to trigger an update to make
  305. // the editor redraw the sprite with the updated texture.
  306. if (texture.is_valid()) {
  307. queue_redraw();
  308. }
  309. }
  310. void GPUParticles2D::set_sub_emitter(const NodePath &p_path) {
  311. if (is_inside_tree()) {
  312. RS::get_singleton()->particles_set_subemitter(particles, RID());
  313. }
  314. sub_emitter = p_path;
  315. if (is_inside_tree() && sub_emitter != NodePath()) {
  316. _attach_sub_emitter();
  317. }
  318. }
  319. NodePath GPUParticles2D::get_sub_emitter() const {
  320. return sub_emitter;
  321. }
  322. void GPUParticles2D::restart() {
  323. RS::get_singleton()->particles_restart(particles);
  324. RS::get_singleton()->particles_set_emitting(particles, true);
  325. }
  326. void GPUParticles2D::_notification(int p_what) {
  327. switch (p_what) {
  328. case NOTIFICATION_DRAW: {
  329. RID texture_rid;
  330. Size2 size;
  331. if (texture.is_valid()) {
  332. texture_rid = texture->get_rid();
  333. size = texture->get_size();
  334. } else {
  335. size = Size2(1, 1);
  336. }
  337. if (trail_enabled) {
  338. RS::get_singleton()->mesh_clear(mesh);
  339. PackedVector2Array points;
  340. PackedVector2Array uvs;
  341. PackedInt32Array bone_indices;
  342. PackedFloat32Array bone_weights;
  343. PackedInt32Array indices;
  344. int total_segments = trail_sections * trail_section_subdivisions;
  345. real_t depth = size.height * trail_sections;
  346. for (int j = 0; j <= total_segments; j++) {
  347. real_t v = j;
  348. v /= total_segments;
  349. real_t y = depth * v;
  350. y = (depth * 0.5) - y;
  351. int bone = j / trail_section_subdivisions;
  352. real_t blend = 1.0 - real_t(j % trail_section_subdivisions) / real_t(trail_section_subdivisions);
  353. real_t s = size.width;
  354. points.push_back(Vector2(-s * 0.5, 0));
  355. points.push_back(Vector2(+s * 0.5, 0));
  356. uvs.push_back(Vector2(0, v));
  357. uvs.push_back(Vector2(1, v));
  358. for (int i = 0; i < 2; i++) {
  359. bone_indices.push_back(bone);
  360. bone_indices.push_back(MIN(trail_sections, bone + 1));
  361. bone_indices.push_back(0);
  362. bone_indices.push_back(0);
  363. bone_weights.push_back(blend);
  364. bone_weights.push_back(1.0 - blend);
  365. bone_weights.push_back(0);
  366. bone_weights.push_back(0);
  367. }
  368. if (j > 0) {
  369. int base = j * 2 - 2;
  370. indices.push_back(base + 0);
  371. indices.push_back(base + 1);
  372. indices.push_back(base + 2);
  373. indices.push_back(base + 1);
  374. indices.push_back(base + 3);
  375. indices.push_back(base + 2);
  376. }
  377. }
  378. Array arr;
  379. arr.resize(RS::ARRAY_MAX);
  380. arr[RS::ARRAY_VERTEX] = points;
  381. arr[RS::ARRAY_TEX_UV] = uvs;
  382. arr[RS::ARRAY_BONES] = bone_indices;
  383. arr[RS::ARRAY_WEIGHTS] = bone_weights;
  384. arr[RS::ARRAY_INDEX] = indices;
  385. RS::get_singleton()->mesh_add_surface_from_arrays(mesh, RS::PRIMITIVE_TRIANGLES, arr, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
  386. Vector<Transform3D> xforms;
  387. for (int i = 0; i <= trail_sections; i++) {
  388. Transform3D xform;
  389. /*
  390. xform.origin.y = depth / 2.0 - size.height * real_t(i);
  391. xform.origin.y = -xform.origin.y; //bind is an inverse transform, so negate y */
  392. xforms.push_back(xform);
  393. }
  394. RS::get_singleton()->particles_set_trail_bind_poses(particles, xforms);
  395. } else {
  396. RS::get_singleton()->mesh_clear(mesh);
  397. Vector<Vector2> points = {
  398. Vector2(-size.x / 2.0, -size.y / 2.0),
  399. Vector2(size.x / 2.0, -size.y / 2.0),
  400. Vector2(size.x / 2.0, size.y / 2.0),
  401. Vector2(-size.x / 2.0, size.y / 2.0)
  402. };
  403. Vector<Vector2> uvs;
  404. AtlasTexture *atlas_texure = Object::cast_to<AtlasTexture>(*texture);
  405. if (atlas_texure && atlas_texure->get_atlas().is_valid()) {
  406. Rect2 region_rect = atlas_texure->get_region();
  407. Size2 atlas_size = atlas_texure->get_atlas()->get_size();
  408. uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, region_rect.position.y / atlas_size.y));
  409. uvs.push_back(Vector2((region_rect.position.x + region_rect.size.x) / atlas_size.x, region_rect.position.y / atlas_size.y));
  410. 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));
  411. uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, (region_rect.position.y + region_rect.size.y) / atlas_size.y));
  412. } else {
  413. uvs.push_back(Vector2(0, 0));
  414. uvs.push_back(Vector2(1, 0));
  415. uvs.push_back(Vector2(1, 1));
  416. uvs.push_back(Vector2(0, 1));
  417. }
  418. Vector<int> indices = { 0, 1, 2, 0, 2, 3 };
  419. Array arr;
  420. arr.resize(RS::ARRAY_MAX);
  421. arr[RS::ARRAY_VERTEX] = points;
  422. arr[RS::ARRAY_TEX_UV] = uvs;
  423. arr[RS::ARRAY_INDEX] = indices;
  424. RS::get_singleton()->mesh_add_surface_from_arrays(mesh, RS::PRIMITIVE_TRIANGLES, arr, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
  425. RS::get_singleton()->particles_set_trail_bind_poses(particles, Vector<Transform3D>());
  426. }
  427. RS::get_singleton()->canvas_item_add_particles(get_canvas_item(), particles, texture_rid);
  428. #ifdef TOOLS_ENABLED
  429. if (show_visibility_rect) {
  430. draw_rect(visibility_rect, Color(0, 0.7, 0.9, 0.4), false);
  431. }
  432. #endif
  433. } break;
  434. case NOTIFICATION_ENTER_TREE: {
  435. if (sub_emitter != NodePath()) {
  436. _attach_sub_emitter();
  437. }
  438. } break;
  439. case NOTIFICATION_EXIT_TREE: {
  440. RS::get_singleton()->particles_set_subemitter(particles, RID());
  441. } break;
  442. case NOTIFICATION_PAUSED:
  443. case NOTIFICATION_UNPAUSED: {
  444. if (can_process()) {
  445. RS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
  446. } else {
  447. RS::get_singleton()->particles_set_speed_scale(particles, 0);
  448. }
  449. } break;
  450. case NOTIFICATION_TRANSFORM_CHANGED: {
  451. _update_particle_emission_transform();
  452. } break;
  453. case NOTIFICATION_INTERNAL_PROCESS: {
  454. if (one_shot && !is_emitting()) {
  455. notify_property_list_changed();
  456. set_process_internal(false);
  457. }
  458. } break;
  459. }
  460. }
  461. void GPUParticles2D::_bind_methods() {
  462. ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &GPUParticles2D::set_emitting);
  463. ClassDB::bind_method(D_METHOD("set_amount", "amount"), &GPUParticles2D::set_amount);
  464. ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &GPUParticles2D::set_lifetime);
  465. ClassDB::bind_method(D_METHOD("set_one_shot", "secs"), &GPUParticles2D::set_one_shot);
  466. ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &GPUParticles2D::set_pre_process_time);
  467. ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &GPUParticles2D::set_explosiveness_ratio);
  468. ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &GPUParticles2D::set_randomness_ratio);
  469. ClassDB::bind_method(D_METHOD("set_visibility_rect", "visibility_rect"), &GPUParticles2D::set_visibility_rect);
  470. ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &GPUParticles2D::set_use_local_coordinates);
  471. ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &GPUParticles2D::set_fixed_fps);
  472. ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &GPUParticles2D::set_fractional_delta);
  473. ClassDB::bind_method(D_METHOD("set_interpolate", "enable"), &GPUParticles2D::set_interpolate);
  474. ClassDB::bind_method(D_METHOD("set_process_material", "material"), &GPUParticles2D::set_process_material);
  475. ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &GPUParticles2D::set_speed_scale);
  476. ClassDB::bind_method(D_METHOD("set_collision_base_size", "size"), &GPUParticles2D::set_collision_base_size);
  477. ClassDB::bind_method(D_METHOD("is_emitting"), &GPUParticles2D::is_emitting);
  478. ClassDB::bind_method(D_METHOD("get_amount"), &GPUParticles2D::get_amount);
  479. ClassDB::bind_method(D_METHOD("get_lifetime"), &GPUParticles2D::get_lifetime);
  480. ClassDB::bind_method(D_METHOD("get_one_shot"), &GPUParticles2D::get_one_shot);
  481. ClassDB::bind_method(D_METHOD("get_pre_process_time"), &GPUParticles2D::get_pre_process_time);
  482. ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &GPUParticles2D::get_explosiveness_ratio);
  483. ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &GPUParticles2D::get_randomness_ratio);
  484. ClassDB::bind_method(D_METHOD("get_visibility_rect"), &GPUParticles2D::get_visibility_rect);
  485. ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &GPUParticles2D::get_use_local_coordinates);
  486. ClassDB::bind_method(D_METHOD("get_fixed_fps"), &GPUParticles2D::get_fixed_fps);
  487. ClassDB::bind_method(D_METHOD("get_fractional_delta"), &GPUParticles2D::get_fractional_delta);
  488. ClassDB::bind_method(D_METHOD("get_interpolate"), &GPUParticles2D::get_interpolate);
  489. ClassDB::bind_method(D_METHOD("get_process_material"), &GPUParticles2D::get_process_material);
  490. ClassDB::bind_method(D_METHOD("get_speed_scale"), &GPUParticles2D::get_speed_scale);
  491. ClassDB::bind_method(D_METHOD("get_collision_base_size"), &GPUParticles2D::get_collision_base_size);
  492. ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &GPUParticles2D::set_draw_order);
  493. ClassDB::bind_method(D_METHOD("get_draw_order"), &GPUParticles2D::get_draw_order);
  494. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &GPUParticles2D::set_texture);
  495. ClassDB::bind_method(D_METHOD("get_texture"), &GPUParticles2D::get_texture);
  496. ClassDB::bind_method(D_METHOD("capture_rect"), &GPUParticles2D::capture_rect);
  497. ClassDB::bind_method(D_METHOD("restart"), &GPUParticles2D::restart);
  498. ClassDB::bind_method(D_METHOD("set_sub_emitter", "path"), &GPUParticles2D::set_sub_emitter);
  499. ClassDB::bind_method(D_METHOD("get_sub_emitter"), &GPUParticles2D::get_sub_emitter);
  500. ClassDB::bind_method(D_METHOD("emit_particle", "xform", "velocity", "color", "custom", "flags"), &GPUParticles2D::emit_particle);
  501. ClassDB::bind_method(D_METHOD("set_trail_enabled", "enabled"), &GPUParticles2D::set_trail_enabled);
  502. ClassDB::bind_method(D_METHOD("set_trail_length", "secs"), &GPUParticles2D::set_trail_length);
  503. ClassDB::bind_method(D_METHOD("is_trail_enabled"), &GPUParticles2D::is_trail_enabled);
  504. ClassDB::bind_method(D_METHOD("get_trail_length"), &GPUParticles2D::get_trail_length);
  505. ClassDB::bind_method(D_METHOD("set_trail_sections", "sections"), &GPUParticles2D::set_trail_sections);
  506. ClassDB::bind_method(D_METHOD("get_trail_sections"), &GPUParticles2D::get_trail_sections);
  507. ClassDB::bind_method(D_METHOD("set_trail_section_subdivisions", "subdivisions"), &GPUParticles2D::set_trail_section_subdivisions);
  508. ClassDB::bind_method(D_METHOD("get_trail_section_subdivisions"), &GPUParticles2D::get_trail_section_subdivisions);
  509. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting"), "set_emitting", "is_emitting");
  510. ADD_PROPERTY_DEFAULT("emitting", true); // Workaround for doctool in headless mode, as dummy rasterizer always returns false.
  511. ADD_PROPERTY(PropertyInfo(Variant::INT, "amount", PROPERTY_HINT_RANGE, "1,1000000,1,exp"), "set_amount", "get_amount");
  512. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "sub_emitter", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "GPUParticles2D"), "set_sub_emitter", "get_sub_emitter");
  513. ADD_GROUP("Time", "");
  514. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime", PROPERTY_HINT_RANGE, "0.01,600.0,0.01,or_greater,suffix:s"), "set_lifetime", "get_lifetime");
  515. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
  516. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "preprocess", PROPERTY_HINT_RANGE, "0.00,600.0,0.01,suffix:s"), "set_pre_process_time", "get_pre_process_time");
  517. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
  518. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
  519. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
  520. ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1,suffix:FPS"), "set_fixed_fps", "get_fixed_fps");
  521. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interpolate"), "set_interpolate", "get_interpolate");
  522. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
  523. ADD_GROUP("Collision", "collision_");
  524. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_base_size", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater,suffix:px"), "set_collision_base_size", "get_collision_base_size");
  525. ADD_GROUP("Drawing", "");
  526. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "visibility_rect", PROPERTY_HINT_NONE, "suffix:px"), "set_visibility_rect", "get_visibility_rect");
  527. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
  528. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime,Reverse Lifetime"), "set_draw_order", "get_draw_order");
  529. ADD_GROUP("Trails", "trail_");
  530. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "trail_enabled"), "set_trail_enabled", "is_trail_enabled");
  531. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "trail_length_secs", PROPERTY_HINT_RANGE, "0.01,10,0.01,suffix:s"), "set_trail_length", "get_trail_length");
  532. ADD_PROPERTY(PropertyInfo(Variant::INT, "trail_sections", PROPERTY_HINT_RANGE, "2,128,1"), "set_trail_sections", "get_trail_sections");
  533. ADD_PROPERTY(PropertyInfo(Variant::INT, "trail_section_subdivisions", PROPERTY_HINT_RANGE, "1,1024,1"), "set_trail_section_subdivisions", "get_trail_section_subdivisions");
  534. ADD_GROUP("Process Material", "process_");
  535. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "process_material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,ParticleProcessMaterial"), "set_process_material", "get_process_material");
  536. ADD_GROUP("Textures", "");
  537. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  538. BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
  539. BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
  540. BIND_ENUM_CONSTANT(DRAW_ORDER_REVERSE_LIFETIME);
  541. BIND_ENUM_CONSTANT(EMIT_FLAG_POSITION);
  542. BIND_ENUM_CONSTANT(EMIT_FLAG_ROTATION_SCALE);
  543. BIND_ENUM_CONSTANT(EMIT_FLAG_VELOCITY);
  544. BIND_ENUM_CONSTANT(EMIT_FLAG_COLOR);
  545. BIND_ENUM_CONSTANT(EMIT_FLAG_CUSTOM);
  546. }
  547. GPUParticles2D::GPUParticles2D() {
  548. particles = RS::get_singleton()->particles_create();
  549. RS::get_singleton()->particles_set_mode(particles, RS::PARTICLES_MODE_2D);
  550. mesh = RS::get_singleton()->mesh_create();
  551. RS::get_singleton()->particles_set_draw_passes(particles, 1);
  552. RS::get_singleton()->particles_set_draw_pass_mesh(particles, 0, mesh);
  553. one_shot = false; // Needed so that set_emitting doesn't access uninitialized values
  554. set_emitting(true);
  555. set_one_shot(false);
  556. set_amount(8);
  557. set_lifetime(1);
  558. set_fixed_fps(0);
  559. set_fractional_delta(true);
  560. set_interpolate(true);
  561. set_pre_process_time(0);
  562. set_explosiveness_ratio(0);
  563. set_randomness_ratio(0);
  564. set_visibility_rect(Rect2(Vector2(-100, -100), Vector2(200, 200)));
  565. set_use_local_coordinates(false);
  566. set_draw_order(DRAW_ORDER_LIFETIME);
  567. set_speed_scale(1);
  568. set_fixed_fps(30);
  569. set_collision_base_size(collision_base_size);
  570. }
  571. GPUParticles2D::~GPUParticles2D() {
  572. RS::get_singleton()->free(particles);
  573. RS::get_singleton()->free(mesh);
  574. }