particles.cpp 15 KB

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