particles_2d.cpp 16 KB

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