gpu_particles_3d.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /**************************************************************************/
  2. /* gpu_particles_3d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "gpu_particles_3d.h"
  31. #include "scene/resources/particle_process_material.h"
  32. AABB GPUParticles3D::get_aabb() const {
  33. return AABB();
  34. }
  35. void GPUParticles3D::set_emitting(bool p_emitting) {
  36. RS::get_singleton()->particles_set_emitting(particles, p_emitting);
  37. if (p_emitting && one_shot) {
  38. set_process_internal(true);
  39. } else if (!p_emitting) {
  40. set_process_internal(false);
  41. }
  42. }
  43. void GPUParticles3D::set_amount(int p_amount) {
  44. ERR_FAIL_COND_MSG(p_amount < 1, "Amount of particles cannot be smaller than 1.");
  45. amount = p_amount;
  46. RS::get_singleton()->particles_set_amount(particles, amount);
  47. }
  48. void GPUParticles3D::set_lifetime(double p_lifetime) {
  49. ERR_FAIL_COND_MSG(p_lifetime <= 0, "Particles lifetime must be greater than 0.");
  50. lifetime = p_lifetime;
  51. RS::get_singleton()->particles_set_lifetime(particles, lifetime);
  52. }
  53. void GPUParticles3D::set_one_shot(bool p_one_shot) {
  54. one_shot = p_one_shot;
  55. RS::get_singleton()->particles_set_one_shot(particles, one_shot);
  56. if (is_emitting()) {
  57. set_process_internal(true);
  58. if (!one_shot) {
  59. RenderingServer::get_singleton()->particles_restart(particles);
  60. }
  61. }
  62. if (!one_shot) {
  63. set_process_internal(false);
  64. }
  65. }
  66. void GPUParticles3D::set_pre_process_time(double p_time) {
  67. pre_process_time = p_time;
  68. RS::get_singleton()->particles_set_pre_process_time(particles, pre_process_time);
  69. }
  70. void GPUParticles3D::set_explosiveness_ratio(real_t p_ratio) {
  71. explosiveness_ratio = p_ratio;
  72. RS::get_singleton()->particles_set_explosiveness_ratio(particles, explosiveness_ratio);
  73. }
  74. void GPUParticles3D::set_randomness_ratio(real_t p_ratio) {
  75. randomness_ratio = p_ratio;
  76. RS::get_singleton()->particles_set_randomness_ratio(particles, randomness_ratio);
  77. }
  78. void GPUParticles3D::set_visibility_aabb(const AABB &p_aabb) {
  79. visibility_aabb = p_aabb;
  80. RS::get_singleton()->particles_set_custom_aabb(particles, visibility_aabb);
  81. update_gizmos();
  82. }
  83. void GPUParticles3D::set_use_local_coordinates(bool p_enable) {
  84. local_coords = p_enable;
  85. RS::get_singleton()->particles_set_use_local_coordinates(particles, local_coords);
  86. }
  87. void GPUParticles3D::set_process_material(const Ref<Material> &p_material) {
  88. process_material = p_material;
  89. RID material_rid;
  90. if (process_material.is_valid()) {
  91. material_rid = process_material->get_rid();
  92. }
  93. RS::get_singleton()->particles_set_process_material(particles, material_rid);
  94. update_configuration_warnings();
  95. }
  96. void GPUParticles3D::set_speed_scale(double p_scale) {
  97. speed_scale = p_scale;
  98. RS::get_singleton()->particles_set_speed_scale(particles, p_scale);
  99. }
  100. void GPUParticles3D::set_collision_base_size(real_t p_size) {
  101. collision_base_size = p_size;
  102. RS::get_singleton()->particles_set_collision_base_size(particles, p_size);
  103. }
  104. bool GPUParticles3D::is_emitting() const {
  105. return RS::get_singleton()->particles_get_emitting(particles);
  106. }
  107. int GPUParticles3D::get_amount() const {
  108. return amount;
  109. }
  110. double GPUParticles3D::get_lifetime() const {
  111. return lifetime;
  112. }
  113. bool GPUParticles3D::get_one_shot() const {
  114. return one_shot;
  115. }
  116. double GPUParticles3D::get_pre_process_time() const {
  117. return pre_process_time;
  118. }
  119. real_t GPUParticles3D::get_explosiveness_ratio() const {
  120. return explosiveness_ratio;
  121. }
  122. real_t GPUParticles3D::get_randomness_ratio() const {
  123. return randomness_ratio;
  124. }
  125. AABB GPUParticles3D::get_visibility_aabb() const {
  126. return visibility_aabb;
  127. }
  128. bool GPUParticles3D::get_use_local_coordinates() const {
  129. return local_coords;
  130. }
  131. Ref<Material> GPUParticles3D::get_process_material() const {
  132. return process_material;
  133. }
  134. double GPUParticles3D::get_speed_scale() const {
  135. return speed_scale;
  136. }
  137. real_t GPUParticles3D::get_collision_base_size() const {
  138. return collision_base_size;
  139. }
  140. void GPUParticles3D::set_draw_order(DrawOrder p_order) {
  141. draw_order = p_order;
  142. RS::get_singleton()->particles_set_draw_order(particles, RS::ParticlesDrawOrder(p_order));
  143. }
  144. void GPUParticles3D::set_trail_enabled(bool p_enabled) {
  145. trail_enabled = p_enabled;
  146. RS::get_singleton()->particles_set_trails(particles, trail_enabled, trail_lifetime);
  147. update_configuration_warnings();
  148. }
  149. void GPUParticles3D::set_trail_lifetime(double p_seconds) {
  150. ERR_FAIL_COND(p_seconds < 0.001);
  151. trail_lifetime = p_seconds;
  152. RS::get_singleton()->particles_set_trails(particles, trail_enabled, trail_lifetime);
  153. }
  154. bool GPUParticles3D::is_trail_enabled() const {
  155. return trail_enabled;
  156. }
  157. double GPUParticles3D::get_trail_lifetime() const {
  158. return trail_lifetime;
  159. }
  160. GPUParticles3D::DrawOrder GPUParticles3D::get_draw_order() const {
  161. return draw_order;
  162. }
  163. void GPUParticles3D::set_draw_passes(int p_count) {
  164. ERR_FAIL_COND(p_count < 1);
  165. for (int i = p_count; i < draw_passes.size(); i++) {
  166. set_draw_pass_mesh(i, Ref<Mesh>());
  167. }
  168. draw_passes.resize(p_count);
  169. RS::get_singleton()->particles_set_draw_passes(particles, p_count);
  170. notify_property_list_changed();
  171. }
  172. int GPUParticles3D::get_draw_passes() const {
  173. return draw_passes.size();
  174. }
  175. void GPUParticles3D::set_draw_pass_mesh(int p_pass, const Ref<Mesh> &p_mesh) {
  176. ERR_FAIL_INDEX(p_pass, draw_passes.size());
  177. if (Engine::get_singleton()->is_editor_hint() && draw_passes.write[p_pass].is_valid()) {
  178. draw_passes.write[p_pass]->disconnect("changed", callable_mp((Node *)this, &Node::update_configuration_warnings));
  179. }
  180. draw_passes.write[p_pass] = p_mesh;
  181. if (Engine::get_singleton()->is_editor_hint() && draw_passes.write[p_pass].is_valid()) {
  182. draw_passes.write[p_pass]->connect("changed", callable_mp((Node *)this, &Node::update_configuration_warnings), CONNECT_DEFERRED);
  183. }
  184. RID mesh_rid;
  185. if (p_mesh.is_valid()) {
  186. mesh_rid = p_mesh->get_rid();
  187. }
  188. RS::get_singleton()->particles_set_draw_pass_mesh(particles, p_pass, mesh_rid);
  189. _skinning_changed();
  190. update_configuration_warnings();
  191. }
  192. Ref<Mesh> GPUParticles3D::get_draw_pass_mesh(int p_pass) const {
  193. ERR_FAIL_INDEX_V(p_pass, draw_passes.size(), Ref<Mesh>());
  194. return draw_passes[p_pass];
  195. }
  196. void GPUParticles3D::set_fixed_fps(int p_count) {
  197. fixed_fps = p_count;
  198. RS::get_singleton()->particles_set_fixed_fps(particles, p_count);
  199. }
  200. int GPUParticles3D::get_fixed_fps() const {
  201. return fixed_fps;
  202. }
  203. void GPUParticles3D::set_fractional_delta(bool p_enable) {
  204. fractional_delta = p_enable;
  205. RS::get_singleton()->particles_set_fractional_delta(particles, p_enable);
  206. }
  207. bool GPUParticles3D::get_fractional_delta() const {
  208. return fractional_delta;
  209. }
  210. void GPUParticles3D::set_interpolate(bool p_enable) {
  211. interpolate = p_enable;
  212. RS::get_singleton()->particles_set_interpolate(particles, p_enable);
  213. }
  214. bool GPUParticles3D::get_interpolate() const {
  215. return interpolate;
  216. }
  217. PackedStringArray GPUParticles3D::get_configuration_warnings() const {
  218. PackedStringArray warnings = GeometryInstance3D::get_configuration_warnings();
  219. bool meshes_found = false;
  220. bool anim_material_found = false;
  221. for (int i = 0; i < draw_passes.size(); i++) {
  222. if (draw_passes[i].is_valid()) {
  223. meshes_found = true;
  224. for (int j = 0; j < draw_passes[i]->get_surface_count(); j++) {
  225. anim_material_found = Object::cast_to<ShaderMaterial>(draw_passes[i]->surface_get_material(j).ptr()) != nullptr;
  226. BaseMaterial3D *spat = Object::cast_to<BaseMaterial3D>(draw_passes[i]->surface_get_material(j).ptr());
  227. anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == StandardMaterial3D::BILLBOARD_PARTICLES);
  228. }
  229. if (anim_material_found) {
  230. break;
  231. }
  232. }
  233. }
  234. anim_material_found = anim_material_found || Object::cast_to<ShaderMaterial>(get_material_override().ptr()) != nullptr;
  235. {
  236. BaseMaterial3D *spat = Object::cast_to<BaseMaterial3D>(get_material_override().ptr());
  237. anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == BaseMaterial3D::BILLBOARD_PARTICLES);
  238. }
  239. if (!meshes_found) {
  240. warnings.push_back(RTR("Nothing is visible because meshes have not been assigned to draw passes."));
  241. }
  242. if (process_material.is_null()) {
  243. warnings.push_back(RTR("A material to process the particles is not assigned, so no behavior is imprinted."));
  244. } else {
  245. const ParticleProcessMaterial *process = Object::cast_to<ParticleProcessMaterial>(process_material.ptr());
  246. if (!anim_material_found && process &&
  247. (process->get_param_max(ParticleProcessMaterial::PARAM_ANIM_SPEED) != 0.0 || process->get_param_max(ParticleProcessMaterial::PARAM_ANIM_OFFSET) != 0.0 ||
  248. process->get_param_texture(ParticleProcessMaterial::PARAM_ANIM_SPEED).is_valid() || process->get_param_texture(ParticleProcessMaterial::PARAM_ANIM_OFFSET).is_valid())) {
  249. warnings.push_back(RTR("Particles animation requires the usage of a BaseMaterial3D whose Billboard Mode is set to \"Particle Billboard\"."));
  250. }
  251. }
  252. if (trail_enabled) {
  253. int dp_count = 0;
  254. bool missing_trails = false;
  255. bool no_materials = false;
  256. for (int i = 0; i < draw_passes.size(); i++) {
  257. Ref<Mesh> draw_pass = draw_passes[i];
  258. if (draw_pass.is_valid() && draw_pass->get_builtin_bind_pose_count() > 0) {
  259. dp_count++;
  260. }
  261. if (draw_pass.is_valid()) {
  262. int mats_found = 0;
  263. for (int j = 0; j < draw_passes[i]->get_surface_count(); j++) {
  264. BaseMaterial3D *spat = Object::cast_to<BaseMaterial3D>(draw_passes[i]->surface_get_material(j).ptr());
  265. if (spat) {
  266. mats_found++;
  267. }
  268. if (spat && !spat->get_flag(BaseMaterial3D::FLAG_PARTICLE_TRAILS_MODE)) {
  269. missing_trails = true;
  270. }
  271. }
  272. if (mats_found != draw_passes[i]->get_surface_count()) {
  273. no_materials = true;
  274. }
  275. }
  276. }
  277. BaseMaterial3D *spat = Object::cast_to<BaseMaterial3D>(get_material_override().ptr());
  278. if (spat) {
  279. no_materials = false;
  280. }
  281. if (spat && !spat->get_flag(BaseMaterial3D::FLAG_PARTICLE_TRAILS_MODE)) {
  282. missing_trails = true;
  283. }
  284. if (dp_count && skin.is_valid()) {
  285. warnings.push_back(RTR("Using Trail meshes with a skin causes Skin to override Trail poses. Suggest removing the Skin."));
  286. } else if (dp_count == 0 && skin.is_null()) {
  287. warnings.push_back(RTR("Trails active, but neither Trail meshes or a Skin were found."));
  288. } else if (dp_count > 1) {
  289. warnings.push_back(RTR("Only one Trail mesh is supported. If you want to use more than a single mesh, a Skin is needed (see documentation)."));
  290. }
  291. if ((dp_count || !skin.is_null()) && (missing_trails || no_materials)) {
  292. warnings.push_back(RTR("Trails enabled, but one or more mesh materials are either missing or not set for trails rendering."));
  293. }
  294. if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  295. warnings.push_back(RTR("Particle trails are only available when using the Forward+ or Mobile rendering backends."));
  296. }
  297. }
  298. return warnings;
  299. }
  300. void GPUParticles3D::restart() {
  301. RenderingServer::get_singleton()->particles_restart(particles);
  302. RenderingServer::get_singleton()->particles_set_emitting(particles, true);
  303. }
  304. AABB GPUParticles3D::capture_aabb() const {
  305. return RS::get_singleton()->particles_get_current_aabb(particles);
  306. }
  307. void GPUParticles3D::_validate_property(PropertyInfo &p_property) const {
  308. if (p_property.name.begins_with("draw_pass_")) {
  309. int index = p_property.name.get_slicec('_', 2).to_int() - 1;
  310. if (index >= draw_passes.size()) {
  311. p_property.usage = PROPERTY_USAGE_NONE;
  312. return;
  313. }
  314. }
  315. }
  316. void GPUParticles3D::emit_particle(const Transform3D &p_transform, const Vector3 &p_velocity, const Color &p_color, const Color &p_custom, uint32_t p_emit_flags) {
  317. RS::get_singleton()->particles_emit(particles, p_transform, p_velocity, p_color, p_custom, p_emit_flags);
  318. }
  319. void GPUParticles3D::_attach_sub_emitter() {
  320. Node *n = get_node_or_null(sub_emitter);
  321. if (n) {
  322. GPUParticles3D *sen = Object::cast_to<GPUParticles3D>(n);
  323. if (sen && sen != this) {
  324. RS::get_singleton()->particles_set_subemitter(particles, sen->particles);
  325. }
  326. }
  327. }
  328. void GPUParticles3D::set_sub_emitter(const NodePath &p_path) {
  329. if (is_inside_tree()) {
  330. RS::get_singleton()->particles_set_subemitter(particles, RID());
  331. }
  332. sub_emitter = p_path;
  333. if (is_inside_tree() && sub_emitter != NodePath()) {
  334. _attach_sub_emitter();
  335. }
  336. }
  337. NodePath GPUParticles3D::get_sub_emitter() const {
  338. return sub_emitter;
  339. }
  340. void GPUParticles3D::_notification(int p_what) {
  341. switch (p_what) {
  342. case NOTIFICATION_PAUSED:
  343. case NOTIFICATION_UNPAUSED: {
  344. if (can_process()) {
  345. RS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
  346. } else {
  347. RS::get_singleton()->particles_set_speed_scale(particles, 0);
  348. }
  349. } break;
  350. // Use internal process when emitting and one_shot is on so that when
  351. // the shot ends the editor can properly update.
  352. case NOTIFICATION_INTERNAL_PROCESS: {
  353. if (one_shot && !is_emitting()) {
  354. notify_property_list_changed();
  355. set_process_internal(false);
  356. }
  357. } break;
  358. case NOTIFICATION_ENTER_TREE: {
  359. if (sub_emitter != NodePath()) {
  360. _attach_sub_emitter();
  361. }
  362. } break;
  363. case NOTIFICATION_EXIT_TREE: {
  364. RS::get_singleton()->particles_set_subemitter(particles, RID());
  365. } break;
  366. case NOTIFICATION_VISIBILITY_CHANGED: {
  367. // Make sure particles are updated before rendering occurs if they were active before.
  368. if (is_visible_in_tree() && !RS::get_singleton()->particles_is_inactive(particles)) {
  369. RS::get_singleton()->particles_request_process(particles);
  370. }
  371. } break;
  372. }
  373. }
  374. void GPUParticles3D::_skinning_changed() {
  375. Vector<Transform3D> xforms;
  376. if (skin.is_valid()) {
  377. xforms.resize(skin->get_bind_count());
  378. for (int i = 0; i < skin->get_bind_count(); i++) {
  379. xforms.write[i] = skin->get_bind_pose(i);
  380. }
  381. } else {
  382. for (int i = 0; i < draw_passes.size(); i++) {
  383. Ref<Mesh> draw_pass = draw_passes[i];
  384. if (draw_pass.is_valid() && draw_pass->get_builtin_bind_pose_count() > 0) {
  385. xforms.resize(draw_pass->get_builtin_bind_pose_count());
  386. for (int j = 0; j < draw_pass->get_builtin_bind_pose_count(); j++) {
  387. xforms.write[j] = draw_pass->get_builtin_bind_pose(j);
  388. }
  389. break;
  390. }
  391. }
  392. }
  393. RS::get_singleton()->particles_set_trail_bind_poses(particles, xforms);
  394. update_configuration_warnings();
  395. }
  396. void GPUParticles3D::set_skin(const Ref<Skin> &p_skin) {
  397. skin = p_skin;
  398. _skinning_changed();
  399. }
  400. Ref<Skin> GPUParticles3D::get_skin() const {
  401. return skin;
  402. }
  403. void GPUParticles3D::set_transform_align(TransformAlign p_align) {
  404. ERR_FAIL_INDEX(uint32_t(p_align), 4);
  405. transform_align = p_align;
  406. RS::get_singleton()->particles_set_transform_align(particles, RS::ParticlesTransformAlign(transform_align));
  407. }
  408. GPUParticles3D::TransformAlign GPUParticles3D::get_transform_align() const {
  409. return transform_align;
  410. }
  411. void GPUParticles3D::_bind_methods() {
  412. ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &GPUParticles3D::set_emitting);
  413. ClassDB::bind_method(D_METHOD("set_amount", "amount"), &GPUParticles3D::set_amount);
  414. ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &GPUParticles3D::set_lifetime);
  415. ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &GPUParticles3D::set_one_shot);
  416. ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &GPUParticles3D::set_pre_process_time);
  417. ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &GPUParticles3D::set_explosiveness_ratio);
  418. ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &GPUParticles3D::set_randomness_ratio);
  419. ClassDB::bind_method(D_METHOD("set_visibility_aabb", "aabb"), &GPUParticles3D::set_visibility_aabb);
  420. ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &GPUParticles3D::set_use_local_coordinates);
  421. ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &GPUParticles3D::set_fixed_fps);
  422. ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &GPUParticles3D::set_fractional_delta);
  423. ClassDB::bind_method(D_METHOD("set_interpolate", "enable"), &GPUParticles3D::set_interpolate);
  424. ClassDB::bind_method(D_METHOD("set_process_material", "material"), &GPUParticles3D::set_process_material);
  425. ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &GPUParticles3D::set_speed_scale);
  426. ClassDB::bind_method(D_METHOD("set_collision_base_size", "size"), &GPUParticles3D::set_collision_base_size);
  427. ClassDB::bind_method(D_METHOD("is_emitting"), &GPUParticles3D::is_emitting);
  428. ClassDB::bind_method(D_METHOD("get_amount"), &GPUParticles3D::get_amount);
  429. ClassDB::bind_method(D_METHOD("get_lifetime"), &GPUParticles3D::get_lifetime);
  430. ClassDB::bind_method(D_METHOD("get_one_shot"), &GPUParticles3D::get_one_shot);
  431. ClassDB::bind_method(D_METHOD("get_pre_process_time"), &GPUParticles3D::get_pre_process_time);
  432. ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &GPUParticles3D::get_explosiveness_ratio);
  433. ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &GPUParticles3D::get_randomness_ratio);
  434. ClassDB::bind_method(D_METHOD("get_visibility_aabb"), &GPUParticles3D::get_visibility_aabb);
  435. ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &GPUParticles3D::get_use_local_coordinates);
  436. ClassDB::bind_method(D_METHOD("get_fixed_fps"), &GPUParticles3D::get_fixed_fps);
  437. ClassDB::bind_method(D_METHOD("get_fractional_delta"), &GPUParticles3D::get_fractional_delta);
  438. ClassDB::bind_method(D_METHOD("get_interpolate"), &GPUParticles3D::get_interpolate);
  439. ClassDB::bind_method(D_METHOD("get_process_material"), &GPUParticles3D::get_process_material);
  440. ClassDB::bind_method(D_METHOD("get_speed_scale"), &GPUParticles3D::get_speed_scale);
  441. ClassDB::bind_method(D_METHOD("get_collision_base_size"), &GPUParticles3D::get_collision_base_size);
  442. ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &GPUParticles3D::set_draw_order);
  443. ClassDB::bind_method(D_METHOD("get_draw_order"), &GPUParticles3D::get_draw_order);
  444. ClassDB::bind_method(D_METHOD("set_draw_passes", "passes"), &GPUParticles3D::set_draw_passes);
  445. ClassDB::bind_method(D_METHOD("set_draw_pass_mesh", "pass", "mesh"), &GPUParticles3D::set_draw_pass_mesh);
  446. ClassDB::bind_method(D_METHOD("get_draw_passes"), &GPUParticles3D::get_draw_passes);
  447. ClassDB::bind_method(D_METHOD("get_draw_pass_mesh", "pass"), &GPUParticles3D::get_draw_pass_mesh);
  448. ClassDB::bind_method(D_METHOD("set_skin", "skin"), &GPUParticles3D::set_skin);
  449. ClassDB::bind_method(D_METHOD("get_skin"), &GPUParticles3D::get_skin);
  450. ClassDB::bind_method(D_METHOD("restart"), &GPUParticles3D::restart);
  451. ClassDB::bind_method(D_METHOD("capture_aabb"), &GPUParticles3D::capture_aabb);
  452. ClassDB::bind_method(D_METHOD("set_sub_emitter", "path"), &GPUParticles3D::set_sub_emitter);
  453. ClassDB::bind_method(D_METHOD("get_sub_emitter"), &GPUParticles3D::get_sub_emitter);
  454. ClassDB::bind_method(D_METHOD("emit_particle", "xform", "velocity", "color", "custom", "flags"), &GPUParticles3D::emit_particle);
  455. ClassDB::bind_method(D_METHOD("set_trail_enabled", "enabled"), &GPUParticles3D::set_trail_enabled);
  456. ClassDB::bind_method(D_METHOD("set_trail_lifetime", "secs"), &GPUParticles3D::set_trail_lifetime);
  457. ClassDB::bind_method(D_METHOD("is_trail_enabled"), &GPUParticles3D::is_trail_enabled);
  458. ClassDB::bind_method(D_METHOD("get_trail_lifetime"), &GPUParticles3D::get_trail_lifetime);
  459. ClassDB::bind_method(D_METHOD("set_transform_align", "align"), &GPUParticles3D::set_transform_align);
  460. ClassDB::bind_method(D_METHOD("get_transform_align"), &GPUParticles3D::get_transform_align);
  461. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting"), "set_emitting", "is_emitting");
  462. ADD_PROPERTY_DEFAULT("emitting", true); // Workaround for doctool in headless mode, as dummy rasterizer always returns false.
  463. ADD_PROPERTY(PropertyInfo(Variant::INT, "amount", PROPERTY_HINT_RANGE, "1,1000000,1,exp"), "set_amount", "get_amount");
  464. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "sub_emitter", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "GPUParticles3D"), "set_sub_emitter", "get_sub_emitter");
  465. ADD_GROUP("Time", "");
  466. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime", PROPERTY_HINT_RANGE, "0.01,600.0,0.01,or_greater,exp,suffix:s"), "set_lifetime", "get_lifetime");
  467. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
  468. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "preprocess", PROPERTY_HINT_RANGE, "0.00,600.0,0.01,exp,suffix:s"), "set_pre_process_time", "get_pre_process_time");
  469. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
  470. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
  471. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
  472. ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1,suffix:FPS"), "set_fixed_fps", "get_fixed_fps");
  473. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interpolate"), "set_interpolate", "get_interpolate");
  474. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
  475. ADD_GROUP("Collision", "collision_");
  476. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_base_size", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater,suffix:m"), "set_collision_base_size", "get_collision_base_size");
  477. ADD_GROUP("Drawing", "");
  478. ADD_PROPERTY(PropertyInfo(Variant::AABB, "visibility_aabb", PROPERTY_HINT_NONE, "suffix:m"), "set_visibility_aabb", "get_visibility_aabb");
  479. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
  480. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime,Reverse Lifetime,View Depth"), "set_draw_order", "get_draw_order");
  481. ADD_PROPERTY(PropertyInfo(Variant::INT, "transform_align", PROPERTY_HINT_ENUM, "Disabled,Z-Billboard,Y to Velocity,Z-Billboard + Y to Velocity"), "set_transform_align", "get_transform_align");
  482. ADD_GROUP("Trails", "trail_");
  483. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "trail_enabled"), "set_trail_enabled", "is_trail_enabled");
  484. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "trail_lifetime", PROPERTY_HINT_RANGE, "0.01,10,0.01,or_greater,suffix:s"), "set_trail_lifetime", "get_trail_lifetime");
  485. ADD_GROUP("Process Material", "");
  486. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "process_material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,ParticleProcessMaterial"), "set_process_material", "get_process_material");
  487. ADD_GROUP("Draw Passes", "draw_");
  488. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_passes", PROPERTY_HINT_RANGE, "0," + itos(MAX_DRAW_PASSES) + ",1"), "set_draw_passes", "get_draw_passes");
  489. for (int i = 0; i < MAX_DRAW_PASSES; i++) {
  490. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "draw_pass_" + itos(i + 1), PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_draw_pass_mesh", "get_draw_pass_mesh", i);
  491. }
  492. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "draw_skin", PROPERTY_HINT_RESOURCE_TYPE, "Skin"), "set_skin", "get_skin");
  493. BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
  494. BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
  495. BIND_ENUM_CONSTANT(DRAW_ORDER_REVERSE_LIFETIME);
  496. BIND_ENUM_CONSTANT(DRAW_ORDER_VIEW_DEPTH);
  497. BIND_ENUM_CONSTANT(EMIT_FLAG_POSITION);
  498. BIND_ENUM_CONSTANT(EMIT_FLAG_ROTATION_SCALE);
  499. BIND_ENUM_CONSTANT(EMIT_FLAG_VELOCITY);
  500. BIND_ENUM_CONSTANT(EMIT_FLAG_COLOR);
  501. BIND_ENUM_CONSTANT(EMIT_FLAG_CUSTOM);
  502. BIND_CONSTANT(MAX_DRAW_PASSES);
  503. BIND_ENUM_CONSTANT(TRANSFORM_ALIGN_DISABLED);
  504. BIND_ENUM_CONSTANT(TRANSFORM_ALIGN_Z_BILLBOARD);
  505. BIND_ENUM_CONSTANT(TRANSFORM_ALIGN_Y_TO_VELOCITY);
  506. BIND_ENUM_CONSTANT(TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY);
  507. }
  508. GPUParticles3D::GPUParticles3D() {
  509. particles = RS::get_singleton()->particles_create();
  510. RS::get_singleton()->particles_set_mode(particles, RS::PARTICLES_MODE_3D);
  511. set_base(particles);
  512. one_shot = false; // Needed so that set_emitting doesn't access uninitialized values
  513. set_emitting(true);
  514. set_one_shot(false);
  515. set_amount(8);
  516. set_lifetime(1);
  517. set_fixed_fps(30);
  518. set_fractional_delta(true);
  519. set_interpolate(true);
  520. set_pre_process_time(0);
  521. set_explosiveness_ratio(0);
  522. set_randomness_ratio(0);
  523. set_trail_lifetime(0.3);
  524. set_visibility_aabb(AABB(Vector3(-4, -4, -4), Vector3(8, 8, 8)));
  525. set_use_local_coordinates(false);
  526. set_draw_passes(1);
  527. set_draw_order(DRAW_ORDER_INDEX);
  528. set_speed_scale(1);
  529. set_collision_base_size(collision_base_size);
  530. set_transform_align(TRANSFORM_ALIGN_DISABLED);
  531. }
  532. GPUParticles3D::~GPUParticles3D() {
  533. ERR_FAIL_NULL(RenderingServer::get_singleton());
  534. RS::get_singleton()->free(particles);
  535. }