particles.cpp 16 KB

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