particles.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*************************************************************************/
  2. /* particles.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.h"
  31. #include "core/os/os.h"
  32. #include "scene/resources/particles_material.h"
  33. #include "servers/visual_server.h"
  34. AABB Particles::get_aabb() const {
  35. return AABB();
  36. }
  37. Vector<Face3> Particles::get_faces(uint32_t p_usage_flags) const {
  38. return Vector<Face3>();
  39. }
  40. void Particles::set_emitting(bool p_emitting) {
  41. VS::get_singleton()->particles_set_emitting(particles, p_emitting);
  42. if (p_emitting && one_shot) {
  43. set_process_internal(true);
  44. } else if (!p_emitting) {
  45. set_process_internal(false);
  46. }
  47. }
  48. void Particles::set_amount(int p_amount) {
  49. ERR_FAIL_COND_MSG(p_amount < 1, "Amount of particles cannot be smaller than 1.");
  50. amount = p_amount;
  51. VS::get_singleton()->particles_set_amount(particles, amount);
  52. }
  53. void Particles::set_lifetime(float p_lifetime) {
  54. ERR_FAIL_COND_MSG(p_lifetime <= 0, "Particles lifetime must be greater than 0.");
  55. lifetime = p_lifetime;
  56. VS::get_singleton()->particles_set_lifetime(particles, lifetime);
  57. }
  58. void Particles::set_one_shot(bool p_one_shot) {
  59. one_shot = p_one_shot;
  60. VS::get_singleton()->particles_set_one_shot(particles, one_shot);
  61. if (is_emitting()) {
  62. set_process_internal(true);
  63. if (!one_shot)
  64. VisualServer::get_singleton()->particles_restart(particles);
  65. }
  66. if (!one_shot)
  67. set_process_internal(false);
  68. }
  69. void Particles::set_pre_process_time(float p_time) {
  70. pre_process_time = p_time;
  71. VS::get_singleton()->particles_set_pre_process_time(particles, pre_process_time);
  72. }
  73. void Particles::set_explosiveness_ratio(float p_ratio) {
  74. explosiveness_ratio = p_ratio;
  75. VS::get_singleton()->particles_set_explosiveness_ratio(particles, explosiveness_ratio);
  76. }
  77. void Particles::set_randomness_ratio(float p_ratio) {
  78. randomness_ratio = p_ratio;
  79. VS::get_singleton()->particles_set_randomness_ratio(particles, randomness_ratio);
  80. }
  81. void Particles::set_visibility_aabb(const AABB &p_aabb) {
  82. visibility_aabb = p_aabb;
  83. VS::get_singleton()->particles_set_custom_aabb(particles, visibility_aabb);
  84. update_gizmo();
  85. _change_notify("visibility_aabb");
  86. }
  87. void Particles::set_use_local_coordinates(bool p_enable) {
  88. local_coords = p_enable;
  89. VS::get_singleton()->particles_set_use_local_coordinates(particles, local_coords);
  90. }
  91. void Particles::set_process_material(const Ref<Material> &p_material) {
  92. process_material = p_material;
  93. RID material_rid;
  94. if (process_material.is_valid())
  95. material_rid = process_material->get_rid();
  96. VS::get_singleton()->particles_set_process_material(particles, material_rid);
  97. update_configuration_warning();
  98. }
  99. void Particles::set_speed_scale(float p_scale) {
  100. speed_scale = p_scale;
  101. VS::get_singleton()->particles_set_speed_scale(particles, p_scale);
  102. }
  103. bool Particles::is_emitting() const {
  104. return VS::get_singleton()->particles_get_emitting(particles);
  105. }
  106. int Particles::get_amount() const {
  107. return amount;
  108. }
  109. float Particles::get_lifetime() const {
  110. return lifetime;
  111. }
  112. bool Particles::get_one_shot() const {
  113. return one_shot;
  114. }
  115. float Particles::get_pre_process_time() const {
  116. return pre_process_time;
  117. }
  118. float Particles::get_explosiveness_ratio() const {
  119. return explosiveness_ratio;
  120. }
  121. float Particles::get_randomness_ratio() const {
  122. return randomness_ratio;
  123. }
  124. AABB Particles::get_visibility_aabb() const {
  125. return visibility_aabb;
  126. }
  127. bool Particles::get_use_local_coordinates() const {
  128. return local_coords;
  129. }
  130. Ref<Material> Particles::get_process_material() const {
  131. return process_material;
  132. }
  133. float Particles::get_speed_scale() const {
  134. return speed_scale;
  135. }
  136. void Particles::set_draw_order(DrawOrder p_order) {
  137. draw_order = p_order;
  138. VS::get_singleton()->particles_set_draw_order(particles, VS::ParticlesDrawOrder(p_order));
  139. }
  140. Particles::DrawOrder Particles::get_draw_order() const {
  141. return draw_order;
  142. }
  143. void Particles::set_draw_passes(int p_count) {
  144. ERR_FAIL_COND(p_count < 1);
  145. draw_passes.resize(p_count);
  146. VS::get_singleton()->particles_set_draw_passes(particles, p_count);
  147. _change_notify();
  148. }
  149. int Particles::get_draw_passes() const {
  150. return draw_passes.size();
  151. }
  152. void Particles::set_draw_pass_mesh(int p_pass, const Ref<Mesh> &p_mesh) {
  153. ERR_FAIL_INDEX(p_pass, draw_passes.size());
  154. draw_passes.write[p_pass] = p_mesh;
  155. RID mesh_rid;
  156. if (p_mesh.is_valid())
  157. mesh_rid = p_mesh->get_rid();
  158. VS::get_singleton()->particles_set_draw_pass_mesh(particles, p_pass, mesh_rid);
  159. update_configuration_warning();
  160. }
  161. Ref<Mesh> Particles::get_draw_pass_mesh(int p_pass) const {
  162. ERR_FAIL_INDEX_V(p_pass, draw_passes.size(), Ref<Mesh>());
  163. return draw_passes[p_pass];
  164. }
  165. void Particles::set_fixed_fps(int p_count) {
  166. fixed_fps = p_count;
  167. VS::get_singleton()->particles_set_fixed_fps(particles, p_count);
  168. }
  169. int Particles::get_fixed_fps() const {
  170. return fixed_fps;
  171. }
  172. void Particles::set_fractional_delta(bool p_enable) {
  173. fractional_delta = p_enable;
  174. VS::get_singleton()->particles_set_fractional_delta(particles, p_enable);
  175. }
  176. bool Particles::get_fractional_delta() const {
  177. return fractional_delta;
  178. }
  179. String Particles::get_configuration_warning() const {
  180. if (VisualServer::get_singleton()->is_low_end()) {
  181. return TTR("GPU-based particles are not supported by the GLES2 video driver.\nUse the CPUParticles node instead. You can use the \"Convert to CPUParticles\" option for this purpose.");
  182. }
  183. String warnings;
  184. bool meshes_found = false;
  185. bool anim_material_found = false;
  186. for (int i = 0; i < draw_passes.size(); i++) {
  187. if (draw_passes[i].is_valid()) {
  188. meshes_found = true;
  189. for (int j = 0; j < draw_passes[i]->get_surface_count(); j++) {
  190. anim_material_found = Object::cast_to<ShaderMaterial>(draw_passes[i]->surface_get_material(j).ptr()) != NULL;
  191. StandardMaterial3D *spat = Object::cast_to<StandardMaterial3D>(draw_passes[i]->surface_get_material(j).ptr());
  192. anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == StandardMaterial3D::BILLBOARD_PARTICLES);
  193. }
  194. if (anim_material_found) break;
  195. }
  196. }
  197. anim_material_found = anim_material_found || Object::cast_to<ShaderMaterial>(get_material_override().ptr()) != NULL;
  198. StandardMaterial3D *spat = Object::cast_to<StandardMaterial3D>(get_material_override().ptr());
  199. anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == StandardMaterial3D::BILLBOARD_PARTICLES);
  200. if (!meshes_found) {
  201. if (warnings != String())
  202. warnings += "\n";
  203. warnings += "- " + TTR("Nothing is visible because meshes have not been assigned to draw passes.");
  204. }
  205. if (process_material.is_null()) {
  206. if (warnings != String())
  207. warnings += "\n";
  208. warnings += "- " + TTR("A material to process the particles is not assigned, so no behavior is imprinted.");
  209. } else {
  210. const ParticlesMaterial *process = Object::cast_to<ParticlesMaterial>(process_material.ptr());
  211. if (!anim_material_found && process &&
  212. (process->get_param(ParticlesMaterial::PARAM_ANIM_SPEED) != 0.0 || process->get_param(ParticlesMaterial::PARAM_ANIM_OFFSET) != 0.0 ||
  213. process->get_param_texture(ParticlesMaterial::PARAM_ANIM_SPEED).is_valid() || process->get_param_texture(ParticlesMaterial::PARAM_ANIM_OFFSET).is_valid())) {
  214. if (warnings != String())
  215. warnings += "\n";
  216. warnings += "- " + TTR("Particles animation requires the usage of a StandardMaterial3D whose Billboard Mode is set to \"Particle Billboard\".");
  217. }
  218. }
  219. return warnings;
  220. }
  221. void Particles::restart() {
  222. VisualServer::get_singleton()->particles_restart(particles);
  223. VisualServer::get_singleton()->particles_set_emitting(particles, true);
  224. }
  225. AABB Particles::capture_aabb() const {
  226. return VS::get_singleton()->particles_get_current_aabb(particles);
  227. }
  228. void Particles::_validate_property(PropertyInfo &property) const {
  229. if (property.name.begins_with("draw_pass_")) {
  230. int index = property.name.get_slicec('_', 2).to_int() - 1;
  231. if (index >= draw_passes.size()) {
  232. property.usage = 0;
  233. return;
  234. }
  235. }
  236. }
  237. void Particles::_notification(int p_what) {
  238. if (p_what == NOTIFICATION_PAUSED || p_what == NOTIFICATION_UNPAUSED) {
  239. if (can_process()) {
  240. VS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
  241. } else {
  242. VS::get_singleton()->particles_set_speed_scale(particles, 0);
  243. }
  244. }
  245. // Use internal process when emitting and one_shot are on so that when
  246. // the shot ends the editor can properly update
  247. if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
  248. if (one_shot && !is_emitting()) {
  249. _change_notify();
  250. set_process_internal(false);
  251. }
  252. }
  253. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  254. // make sure particles are updated before rendering occurs if they were active before
  255. if (is_visible_in_tree() && !VS::get_singleton()->particles_is_inactive(particles)) {
  256. VS::get_singleton()->particles_request_process(particles);
  257. }
  258. }
  259. }
  260. void Particles::_bind_methods() {
  261. ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &Particles::set_emitting);
  262. ClassDB::bind_method(D_METHOD("set_amount", "amount"), &Particles::set_amount);
  263. ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &Particles::set_lifetime);
  264. ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &Particles::set_one_shot);
  265. ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &Particles::set_pre_process_time);
  266. ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &Particles::set_explosiveness_ratio);
  267. ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &Particles::set_randomness_ratio);
  268. ClassDB::bind_method(D_METHOD("set_visibility_aabb", "aabb"), &Particles::set_visibility_aabb);
  269. ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &Particles::set_use_local_coordinates);
  270. ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &Particles::set_fixed_fps);
  271. ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &Particles::set_fractional_delta);
  272. ClassDB::bind_method(D_METHOD("set_process_material", "material"), &Particles::set_process_material);
  273. ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &Particles::set_speed_scale);
  274. ClassDB::bind_method(D_METHOD("is_emitting"), &Particles::is_emitting);
  275. ClassDB::bind_method(D_METHOD("get_amount"), &Particles::get_amount);
  276. ClassDB::bind_method(D_METHOD("get_lifetime"), &Particles::get_lifetime);
  277. ClassDB::bind_method(D_METHOD("get_one_shot"), &Particles::get_one_shot);
  278. ClassDB::bind_method(D_METHOD("get_pre_process_time"), &Particles::get_pre_process_time);
  279. ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &Particles::get_explosiveness_ratio);
  280. ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &Particles::get_randomness_ratio);
  281. ClassDB::bind_method(D_METHOD("get_visibility_aabb"), &Particles::get_visibility_aabb);
  282. ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &Particles::get_use_local_coordinates);
  283. ClassDB::bind_method(D_METHOD("get_fixed_fps"), &Particles::get_fixed_fps);
  284. ClassDB::bind_method(D_METHOD("get_fractional_delta"), &Particles::get_fractional_delta);
  285. ClassDB::bind_method(D_METHOD("get_process_material"), &Particles::get_process_material);
  286. ClassDB::bind_method(D_METHOD("get_speed_scale"), &Particles::get_speed_scale);
  287. ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &Particles::set_draw_order);
  288. ClassDB::bind_method(D_METHOD("get_draw_order"), &Particles::get_draw_order);
  289. ClassDB::bind_method(D_METHOD("set_draw_passes", "passes"), &Particles::set_draw_passes);
  290. ClassDB::bind_method(D_METHOD("set_draw_pass_mesh", "pass", "mesh"), &Particles::set_draw_pass_mesh);
  291. ClassDB::bind_method(D_METHOD("get_draw_passes"), &Particles::get_draw_passes);
  292. ClassDB::bind_method(D_METHOD("get_draw_pass_mesh", "pass"), &Particles::get_draw_pass_mesh);
  293. ClassDB::bind_method(D_METHOD("restart"), &Particles::restart);
  294. ClassDB::bind_method(D_METHOD("capture_aabb"), &Particles::capture_aabb);
  295. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting"), "set_emitting", "is_emitting");
  296. ADD_PROPERTY(PropertyInfo(Variant::INT, "amount", PROPERTY_HINT_EXP_RANGE, "1,1000000,1"), "set_amount", "get_amount");
  297. ADD_GROUP("Time", "");
  298. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime", PROPERTY_HINT_EXP_RANGE, "0.01,600.0,0.01,or_greater"), "set_lifetime", "get_lifetime");
  299. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
  300. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "preprocess", PROPERTY_HINT_EXP_RANGE, "0.00,600.0,0.01"), "set_pre_process_time", "get_pre_process_time");
  301. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
  302. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
  303. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
  304. ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1"), "set_fixed_fps", "get_fixed_fps");
  305. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
  306. ADD_GROUP("Drawing", "");
  307. ADD_PROPERTY(PropertyInfo(Variant::AABB, "visibility_aabb"), "set_visibility_aabb", "get_visibility_aabb");
  308. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
  309. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime,View Depth"), "set_draw_order", "get_draw_order");
  310. ADD_GROUP("Process Material", "");
  311. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "process_material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,ParticlesMaterial"), "set_process_material", "get_process_material");
  312. ADD_GROUP("Draw Passes", "draw_");
  313. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_passes", PROPERTY_HINT_RANGE, "0," + itos(MAX_DRAW_PASSES) + ",1"), "set_draw_passes", "get_draw_passes");
  314. for (int i = 0; i < MAX_DRAW_PASSES; i++) {
  315. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "draw_pass_" + itos(i + 1), PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_draw_pass_mesh", "get_draw_pass_mesh", i);
  316. }
  317. BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
  318. BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
  319. BIND_ENUM_CONSTANT(DRAW_ORDER_VIEW_DEPTH);
  320. BIND_CONSTANT(MAX_DRAW_PASSES);
  321. }
  322. Particles::Particles() {
  323. particles = VS::get_singleton()->particles_create();
  324. set_base(particles);
  325. one_shot = false; // Needed so that set_emitting doesn't access uninitialized values
  326. set_emitting(true);
  327. set_one_shot(false);
  328. set_amount(8);
  329. set_lifetime(1);
  330. set_fixed_fps(0);
  331. set_fractional_delta(true);
  332. set_pre_process_time(0);
  333. set_explosiveness_ratio(0);
  334. set_randomness_ratio(0);
  335. set_visibility_aabb(AABB(Vector3(-4, -4, -4), Vector3(8, 8, 8)));
  336. set_use_local_coordinates(true);
  337. set_draw_passes(1);
  338. set_draw_order(DRAW_ORDER_INDEX);
  339. set_speed_scale(1);
  340. }
  341. Particles::~Particles() {
  342. VS::get_singleton()->free(particles);
  343. }