particles_2d.cpp 15 KB

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