particles_2d.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*************************************************************************/
  2. /* particles_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "particles_2d.h"
  31. #include "core/os/os.h"
  32. #include "scene/resources/particles_material.h"
  33. #include "scene/scene_string_names.h"
  34. #ifdef TOOLS_ENABLED
  35. #include "core/engine.h"
  36. #endif
  37. void Particles2D::set_emitting(bool p_emitting) {
  38. VS::get_singleton()->particles_set_emitting(particles, p_emitting);
  39. if (p_emitting && one_shot) {
  40. set_process_internal(true);
  41. } else if (!p_emitting) {
  42. set_process_internal(false);
  43. }
  44. }
  45. void Particles2D::set_amount(int p_amount) {
  46. ERR_FAIL_COND_MSG(p_amount < 1, "Amount of particles cannot be smaller than 1.");
  47. amount = p_amount;
  48. VS::get_singleton()->particles_set_amount(particles, amount);
  49. }
  50. void Particles2D::set_lifetime(float p_lifetime) {
  51. ERR_FAIL_COND_MSG(p_lifetime <= 0, "Particles lifetime must be greater than 0.");
  52. lifetime = p_lifetime;
  53. VS::get_singleton()->particles_set_lifetime(particles, lifetime);
  54. }
  55. void Particles2D::set_one_shot(bool p_enable) {
  56. one_shot = p_enable;
  57. VS::get_singleton()->particles_set_one_shot(particles, one_shot);
  58. if (is_emitting()) {
  59. set_process_internal(true);
  60. if (!one_shot)
  61. VisualServer::get_singleton()->particles_restart(particles);
  62. }
  63. if (!one_shot)
  64. set_process_internal(false);
  65. }
  66. void Particles2D::set_pre_process_time(float p_time) {
  67. pre_process_time = p_time;
  68. VS::get_singleton()->particles_set_pre_process_time(particles, pre_process_time);
  69. }
  70. void Particles2D::set_explosiveness_ratio(float p_ratio) {
  71. explosiveness_ratio = p_ratio;
  72. VS::get_singleton()->particles_set_explosiveness_ratio(particles, explosiveness_ratio);
  73. }
  74. void Particles2D::set_randomness_ratio(float p_ratio) {
  75. randomness_ratio = p_ratio;
  76. VS::get_singleton()->particles_set_randomness_ratio(particles, randomness_ratio);
  77. }
  78. void Particles2D::set_visibility_rect(const Rect2 &p_visibility_rect) {
  79. visibility_rect = p_visibility_rect;
  80. AABB aabb;
  81. aabb.position.x = p_visibility_rect.position.x;
  82. aabb.position.y = p_visibility_rect.position.y;
  83. aabb.size.x = p_visibility_rect.size.x;
  84. aabb.size.y = p_visibility_rect.size.y;
  85. VS::get_singleton()->particles_set_custom_aabb(particles, aabb);
  86. _change_notify("visibility_rect");
  87. update();
  88. }
  89. void Particles2D::set_use_local_coordinates(bool p_enable) {
  90. local_coords = p_enable;
  91. VS::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 Particles2D::_update_particle_emission_transform() {
  98. Transform2D xf2d = get_global_transform();
  99. Transform xf;
  100. xf.basis.set_axis(0, Vector3(xf2d.get_axis(0).x, xf2d.get_axis(0).y, 0));
  101. xf.basis.set_axis(1, Vector3(xf2d.get_axis(1).x, xf2d.get_axis(1).y, 0));
  102. xf.set_origin(Vector3(xf2d.get_origin().x, xf2d.get_origin().y, 0));
  103. VS::get_singleton()->particles_set_emission_transform(particles, xf);
  104. }
  105. void Particles2D::set_process_material(const Ref<Material> &p_material) {
  106. process_material = p_material;
  107. Ref<ParticlesMaterial> pm = p_material;
  108. if (pm.is_valid() && !pm->get_flag(ParticlesMaterial::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_flag(ParticlesMaterial::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. VS::get_singleton()->particles_set_process_material(particles, material_rid);
  117. update_configuration_warning();
  118. }
  119. void Particles2D::set_speed_scale(float p_scale) {
  120. speed_scale = p_scale;
  121. VS::get_singleton()->particles_set_speed_scale(particles, p_scale);
  122. }
  123. bool Particles2D::is_emitting() const {
  124. return VS::get_singleton()->particles_get_emitting(particles);
  125. }
  126. int Particles2D::get_amount() const {
  127. return amount;
  128. }
  129. float Particles2D::get_lifetime() const {
  130. return lifetime;
  131. }
  132. bool Particles2D::get_one_shot() const {
  133. return one_shot;
  134. }
  135. float Particles2D::get_pre_process_time() const {
  136. return pre_process_time;
  137. }
  138. float Particles2D::get_explosiveness_ratio() const {
  139. return explosiveness_ratio;
  140. }
  141. float Particles2D::get_randomness_ratio() const {
  142. return randomness_ratio;
  143. }
  144. Rect2 Particles2D::get_visibility_rect() const {
  145. return visibility_rect;
  146. }
  147. bool Particles2D::get_use_local_coordinates() const {
  148. return local_coords;
  149. }
  150. Ref<Material> Particles2D::get_process_material() const {
  151. return process_material;
  152. }
  153. float Particles2D::get_speed_scale() const {
  154. return speed_scale;
  155. }
  156. void Particles2D::set_draw_order(DrawOrder p_order) {
  157. draw_order = p_order;
  158. VS::get_singleton()->particles_set_draw_order(particles, VS::ParticlesDrawOrder(p_order));
  159. }
  160. Particles2D::DrawOrder Particles2D::get_draw_order() const {
  161. return draw_order;
  162. }
  163. void Particles2D::set_fixed_fps(int p_count) {
  164. fixed_fps = p_count;
  165. VS::get_singleton()->particles_set_fixed_fps(particles, p_count);
  166. }
  167. int Particles2D::get_fixed_fps() const {
  168. return fixed_fps;
  169. }
  170. void Particles2D::set_fractional_delta(bool p_enable) {
  171. fractional_delta = p_enable;
  172. VS::get_singleton()->particles_set_fractional_delta(particles, p_enable);
  173. }
  174. bool Particles2D::get_fractional_delta() const {
  175. return fractional_delta;
  176. }
  177. String Particles2D::get_configuration_warning() const {
  178. if (OS::get_singleton()->get_current_video_driver() == OS::VIDEO_DRIVER_GLES2) {
  179. return TTR("GPU-based particles are not supported by the GLES2 video driver.\nUse the CPUParticles2D node instead. You can use the \"Convert to CPUParticles\" option for this purpose.");
  180. }
  181. String warnings;
  182. if (process_material.is_null()) {
  183. if (warnings != String())
  184. warnings += "\n";
  185. warnings += "- " + TTR("A material to process the particles is not assigned, so no behavior is imprinted.");
  186. } else {
  187. CanvasItemMaterial *mat = Object::cast_to<CanvasItemMaterial>(get_material().ptr());
  188. if (get_material().is_null() || (mat && !mat->get_particles_animation())) {
  189. const ParticlesMaterial *process = Object::cast_to<ParticlesMaterial>(process_material.ptr());
  190. if (process &&
  191. (process->get_param(ParticlesMaterial::PARAM_ANIM_SPEED) != 0.0 || process->get_param(ParticlesMaterial::PARAM_ANIM_OFFSET) != 0.0 ||
  192. process->get_param_texture(ParticlesMaterial::PARAM_ANIM_SPEED).is_valid() || process->get_param_texture(ParticlesMaterial::PARAM_ANIM_OFFSET).is_valid())) {
  193. if (warnings != String())
  194. warnings += "\n";
  195. warnings += "- " + TTR("Particles2D animation requires the usage of a CanvasItemMaterial with \"Particles Animation\" enabled.");
  196. }
  197. }
  198. }
  199. return warnings;
  200. }
  201. Rect2 Particles2D::capture_rect() const {
  202. AABB aabb = VS::get_singleton()->particles_get_current_aabb(particles);
  203. Rect2 r;
  204. r.position.x = aabb.position.x;
  205. r.position.y = aabb.position.y;
  206. r.size.x = aabb.size.x;
  207. r.size.y = aabb.size.y;
  208. return r;
  209. }
  210. void Particles2D::set_texture(const Ref<Texture> &p_texture) {
  211. texture = p_texture;
  212. update();
  213. }
  214. Ref<Texture> Particles2D::get_texture() const {
  215. return texture;
  216. }
  217. void Particles2D::set_normal_map(const Ref<Texture> &p_normal_map) {
  218. normal_map = p_normal_map;
  219. update();
  220. }
  221. Ref<Texture> Particles2D::get_normal_map() const {
  222. return normal_map;
  223. }
  224. void Particles2D::_validate_property(PropertyInfo &property) const {
  225. }
  226. void Particles2D::restart() {
  227. VS::get_singleton()->particles_restart(particles);
  228. VS::get_singleton()->particles_set_emitting(particles, true);
  229. }
  230. void Particles2D::_notification(int p_what) {
  231. if (p_what == NOTIFICATION_DRAW) {
  232. RID texture_rid;
  233. if (texture.is_valid())
  234. texture_rid = texture->get_rid();
  235. RID normal_rid;
  236. if (normal_map.is_valid())
  237. normal_rid = normal_map->get_rid();
  238. VS::get_singleton()->canvas_item_add_particles(get_canvas_item(), particles, texture_rid, normal_rid);
  239. #ifdef TOOLS_ENABLED
  240. if (Engine::get_singleton()->is_editor_hint() && (this == get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->is_a_parent_of(this))) {
  241. draw_rect(visibility_rect, Color(0, 0.7, 0.9, 0.4), false);
  242. }
  243. #endif
  244. }
  245. if (p_what == NOTIFICATION_PAUSED || p_what == NOTIFICATION_UNPAUSED) {
  246. if (can_process()) {
  247. VS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
  248. } else {
  249. VS::get_singleton()->particles_set_speed_scale(particles, 0);
  250. }
  251. }
  252. if (p_what == NOTIFICATION_TRANSFORM_CHANGED) {
  253. _update_particle_emission_transform();
  254. }
  255. if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
  256. if (one_shot && !is_emitting()) {
  257. _change_notify();
  258. set_process_internal(false);
  259. }
  260. }
  261. }
  262. void Particles2D::_bind_methods() {
  263. ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &Particles2D::set_emitting);
  264. ClassDB::bind_method(D_METHOD("set_amount", "amount"), &Particles2D::set_amount);
  265. ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &Particles2D::set_lifetime);
  266. ClassDB::bind_method(D_METHOD("set_one_shot", "secs"), &Particles2D::set_one_shot);
  267. ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &Particles2D::set_pre_process_time);
  268. ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &Particles2D::set_explosiveness_ratio);
  269. ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &Particles2D::set_randomness_ratio);
  270. ClassDB::bind_method(D_METHOD("set_visibility_rect", "visibility_rect"), &Particles2D::set_visibility_rect);
  271. ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &Particles2D::set_use_local_coordinates);
  272. ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &Particles2D::set_fixed_fps);
  273. ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &Particles2D::set_fractional_delta);
  274. ClassDB::bind_method(D_METHOD("set_process_material", "material"), &Particles2D::set_process_material);
  275. ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &Particles2D::set_speed_scale);
  276. ClassDB::bind_method(D_METHOD("is_emitting"), &Particles2D::is_emitting);
  277. ClassDB::bind_method(D_METHOD("get_amount"), &Particles2D::get_amount);
  278. ClassDB::bind_method(D_METHOD("get_lifetime"), &Particles2D::get_lifetime);
  279. ClassDB::bind_method(D_METHOD("get_one_shot"), &Particles2D::get_one_shot);
  280. ClassDB::bind_method(D_METHOD("get_pre_process_time"), &Particles2D::get_pre_process_time);
  281. ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &Particles2D::get_explosiveness_ratio);
  282. ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &Particles2D::get_randomness_ratio);
  283. ClassDB::bind_method(D_METHOD("get_visibility_rect"), &Particles2D::get_visibility_rect);
  284. ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &Particles2D::get_use_local_coordinates);
  285. ClassDB::bind_method(D_METHOD("get_fixed_fps"), &Particles2D::get_fixed_fps);
  286. ClassDB::bind_method(D_METHOD("get_fractional_delta"), &Particles2D::get_fractional_delta);
  287. ClassDB::bind_method(D_METHOD("get_process_material"), &Particles2D::get_process_material);
  288. ClassDB::bind_method(D_METHOD("get_speed_scale"), &Particles2D::get_speed_scale);
  289. ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &Particles2D::set_draw_order);
  290. ClassDB::bind_method(D_METHOD("get_draw_order"), &Particles2D::get_draw_order);
  291. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Particles2D::set_texture);
  292. ClassDB::bind_method(D_METHOD("get_texture"), &Particles2D::get_texture);
  293. ClassDB::bind_method(D_METHOD("set_normal_map", "texture"), &Particles2D::set_normal_map);
  294. ClassDB::bind_method(D_METHOD("get_normal_map"), &Particles2D::get_normal_map);
  295. ClassDB::bind_method(D_METHOD("capture_rect"), &Particles2D::capture_rect);
  296. ClassDB::bind_method(D_METHOD("restart"), &Particles2D::restart);
  297. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting"), "set_emitting", "is_emitting");
  298. ADD_PROPERTY(PropertyInfo(Variant::INT, "amount", PROPERTY_HINT_EXP_RANGE, "1,1000000,1"), "set_amount", "get_amount");
  299. ADD_GROUP("Time", "");
  300. ADD_PROPERTY(PropertyInfo(Variant::REAL, "lifetime", PROPERTY_HINT_RANGE, "0.01,600.0,0.01,or_greater"), "set_lifetime", "get_lifetime");
  301. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
  302. ADD_PROPERTY(PropertyInfo(Variant::REAL, "preprocess", PROPERTY_HINT_RANGE, "0.00,600.0,0.01"), "set_pre_process_time", "get_pre_process_time");
  303. ADD_PROPERTY(PropertyInfo(Variant::REAL, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
  304. ADD_PROPERTY(PropertyInfo(Variant::REAL, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
  305. ADD_PROPERTY(PropertyInfo(Variant::REAL, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
  306. ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1"), "set_fixed_fps", "get_fixed_fps");
  307. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
  308. ADD_GROUP("Drawing", "");
  309. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "visibility_rect"), "set_visibility_rect", "get_visibility_rect");
  310. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
  311. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime"), "set_draw_order", "get_draw_order");
  312. ADD_GROUP("Process Material", "process_");
  313. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "process_material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,ParticlesMaterial"), "set_process_material", "get_process_material");
  314. ADD_GROUP("Textures", "");
  315. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  316. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "normal_map", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_normal_map", "get_normal_map");
  317. BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
  318. BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
  319. }
  320. Particles2D::Particles2D() {
  321. particles = VS::get_singleton()->particles_create();
  322. one_shot = false; // Needed so that set_emitting doesn't access uninitialized values
  323. set_emitting(true);
  324. set_one_shot(false);
  325. set_amount(8);
  326. set_lifetime(1);
  327. set_fixed_fps(0);
  328. set_fractional_delta(true);
  329. set_pre_process_time(0);
  330. set_explosiveness_ratio(0);
  331. set_randomness_ratio(0);
  332. set_visibility_rect(Rect2(Vector2(-100, -100), Vector2(200, 200)));
  333. set_use_local_coordinates(true);
  334. set_draw_order(DRAW_ORDER_INDEX);
  335. set_speed_scale(1);
  336. }
  337. Particles2D::~Particles2D() {
  338. VS::get_singleton()->free(particles);
  339. }