particles_2d.cpp 15 KB

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