gpu_particles_3d.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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/3d/cpu_particles_3d.h"
  32. #include "scene/resources/curve_texture.h"
  33. #include "scene/resources/gradient_texture.h"
  34. #include "scene/resources/particle_process_material.h"
  35. AABB GPUParticles3D::get_aabb() const {
  36. return AABB();
  37. }
  38. void GPUParticles3D::set_emitting(bool p_emitting) {
  39. // Do not return even if `p_emitting == emitting` because `emitting` is just an approximation.
  40. if (p_emitting && one_shot) {
  41. if (!active && !emitting) {
  42. // Last cycle ended.
  43. active = true;
  44. time = 0;
  45. signal_canceled = false;
  46. emission_time = lifetime;
  47. active_time = lifetime * (2 - explosiveness_ratio);
  48. } else {
  49. signal_canceled = true;
  50. }
  51. set_process_internal(true);
  52. } else if (!p_emitting) {
  53. if (one_shot) {
  54. set_process_internal(true);
  55. } else {
  56. set_process_internal(false);
  57. }
  58. } else {
  59. set_process_internal(true);
  60. }
  61. emitting = p_emitting;
  62. RS::get_singleton()->particles_set_emitting(particles, p_emitting);
  63. }
  64. void GPUParticles3D::set_amount(int p_amount) {
  65. ERR_FAIL_COND_MSG(p_amount < 1, "Amount of particles cannot be smaller than 1.");
  66. amount = p_amount;
  67. RS::get_singleton()->particles_set_amount(particles, amount);
  68. }
  69. void GPUParticles3D::set_lifetime(double p_lifetime) {
  70. ERR_FAIL_COND_MSG(p_lifetime <= 0, "Particles lifetime must be greater than 0.");
  71. lifetime = p_lifetime;
  72. RS::get_singleton()->particles_set_lifetime(particles, lifetime);
  73. }
  74. void GPUParticles3D::set_interp_to_end(float p_interp) {
  75. interp_to_end_factor = CLAMP(p_interp, 0.0, 1.0);
  76. RS::get_singleton()->particles_set_interp_to_end(particles, interp_to_end_factor);
  77. }
  78. void GPUParticles3D::set_one_shot(bool p_one_shot) {
  79. one_shot = p_one_shot;
  80. RS::get_singleton()->particles_set_one_shot(particles, one_shot);
  81. if (is_emitting()) {
  82. if (!one_shot) {
  83. RenderingServer::get_singleton()->particles_restart(particles);
  84. }
  85. }
  86. }
  87. void GPUParticles3D::set_pre_process_time(double p_time) {
  88. pre_process_time = p_time;
  89. RS::get_singleton()->particles_set_pre_process_time(particles, pre_process_time);
  90. }
  91. void GPUParticles3D::set_explosiveness_ratio(real_t p_ratio) {
  92. explosiveness_ratio = p_ratio;
  93. RS::get_singleton()->particles_set_explosiveness_ratio(particles, explosiveness_ratio);
  94. }
  95. void GPUParticles3D::set_randomness_ratio(real_t p_ratio) {
  96. randomness_ratio = p_ratio;
  97. RS::get_singleton()->particles_set_randomness_ratio(particles, randomness_ratio);
  98. }
  99. void GPUParticles3D::set_visibility_aabb(const AABB &p_aabb) {
  100. visibility_aabb = p_aabb;
  101. RS::get_singleton()->particles_set_custom_aabb(particles, visibility_aabb);
  102. update_gizmos();
  103. }
  104. void GPUParticles3D::set_use_local_coordinates(bool p_enable) {
  105. local_coords = p_enable;
  106. RS::get_singleton()->particles_set_use_local_coordinates(particles, local_coords);
  107. }
  108. void GPUParticles3D::set_process_material(const Ref<Material> &p_material) {
  109. process_material = p_material;
  110. RID material_rid;
  111. if (process_material.is_valid()) {
  112. material_rid = process_material->get_rid();
  113. }
  114. RS::get_singleton()->particles_set_process_material(particles, material_rid);
  115. update_configuration_warnings();
  116. }
  117. void GPUParticles3D::set_speed_scale(double p_scale) {
  118. speed_scale = p_scale;
  119. RS::get_singleton()->particles_set_speed_scale(particles, p_scale);
  120. }
  121. void GPUParticles3D::set_collision_base_size(real_t p_size) {
  122. collision_base_size = p_size;
  123. RS::get_singleton()->particles_set_collision_base_size(particles, p_size);
  124. }
  125. bool GPUParticles3D::is_emitting() const {
  126. return emitting;
  127. }
  128. int GPUParticles3D::get_amount() const {
  129. return amount;
  130. }
  131. double GPUParticles3D::get_lifetime() const {
  132. return lifetime;
  133. }
  134. float GPUParticles3D::get_interp_to_end() const {
  135. return interp_to_end_factor;
  136. }
  137. bool GPUParticles3D::get_one_shot() const {
  138. return one_shot;
  139. }
  140. double GPUParticles3D::get_pre_process_time() const {
  141. return pre_process_time;
  142. }
  143. real_t GPUParticles3D::get_explosiveness_ratio() const {
  144. return explosiveness_ratio;
  145. }
  146. real_t GPUParticles3D::get_randomness_ratio() const {
  147. return randomness_ratio;
  148. }
  149. AABB GPUParticles3D::get_visibility_aabb() const {
  150. return visibility_aabb;
  151. }
  152. bool GPUParticles3D::get_use_local_coordinates() const {
  153. return local_coords;
  154. }
  155. Ref<Material> GPUParticles3D::get_process_material() const {
  156. return process_material;
  157. }
  158. double GPUParticles3D::get_speed_scale() const {
  159. return speed_scale;
  160. }
  161. real_t GPUParticles3D::get_collision_base_size() const {
  162. return collision_base_size;
  163. }
  164. void GPUParticles3D::set_draw_order(DrawOrder p_order) {
  165. draw_order = p_order;
  166. RS::get_singleton()->particles_set_draw_order(particles, RS::ParticlesDrawOrder(p_order));
  167. }
  168. void GPUParticles3D::set_trail_enabled(bool p_enabled) {
  169. trail_enabled = p_enabled;
  170. RS::get_singleton()->particles_set_trails(particles, trail_enabled, trail_lifetime);
  171. update_configuration_warnings();
  172. }
  173. void GPUParticles3D::set_trail_lifetime(double p_seconds) {
  174. ERR_FAIL_COND(p_seconds < 0.01);
  175. trail_lifetime = p_seconds;
  176. RS::get_singleton()->particles_set_trails(particles, trail_enabled, trail_lifetime);
  177. }
  178. bool GPUParticles3D::is_trail_enabled() const {
  179. return trail_enabled;
  180. }
  181. double GPUParticles3D::get_trail_lifetime() const {
  182. return trail_lifetime;
  183. }
  184. GPUParticles3D::DrawOrder GPUParticles3D::get_draw_order() const {
  185. return draw_order;
  186. }
  187. void GPUParticles3D::set_draw_passes(int p_count) {
  188. ERR_FAIL_COND(p_count < 1);
  189. for (int i = p_count; i < draw_passes.size(); i++) {
  190. set_draw_pass_mesh(i, Ref<Mesh>());
  191. }
  192. draw_passes.resize(p_count);
  193. RS::get_singleton()->particles_set_draw_passes(particles, p_count);
  194. notify_property_list_changed();
  195. }
  196. int GPUParticles3D::get_draw_passes() const {
  197. return draw_passes.size();
  198. }
  199. void GPUParticles3D::set_draw_pass_mesh(int p_pass, const Ref<Mesh> &p_mesh) {
  200. ERR_FAIL_INDEX(p_pass, draw_passes.size());
  201. if (Engine::get_singleton()->is_editor_hint() && draw_passes.write[p_pass].is_valid()) {
  202. draw_passes.write[p_pass]->disconnect_changed(callable_mp((Node *)this, &Node::update_configuration_warnings));
  203. }
  204. draw_passes.write[p_pass] = p_mesh;
  205. if (Engine::get_singleton()->is_editor_hint() && draw_passes.write[p_pass].is_valid()) {
  206. draw_passes.write[p_pass]->connect_changed(callable_mp((Node *)this, &Node::update_configuration_warnings), CONNECT_DEFERRED);
  207. }
  208. RID mesh_rid;
  209. if (p_mesh.is_valid()) {
  210. mesh_rid = p_mesh->get_rid();
  211. }
  212. RS::get_singleton()->particles_set_draw_pass_mesh(particles, p_pass, mesh_rid);
  213. _skinning_changed();
  214. update_configuration_warnings();
  215. }
  216. Ref<Mesh> GPUParticles3D::get_draw_pass_mesh(int p_pass) const {
  217. ERR_FAIL_INDEX_V(p_pass, draw_passes.size(), Ref<Mesh>());
  218. return draw_passes[p_pass];
  219. }
  220. void GPUParticles3D::set_fixed_fps(int p_count) {
  221. fixed_fps = p_count;
  222. RS::get_singleton()->particles_set_fixed_fps(particles, p_count);
  223. }
  224. int GPUParticles3D::get_fixed_fps() const {
  225. return fixed_fps;
  226. }
  227. void GPUParticles3D::set_fractional_delta(bool p_enable) {
  228. fractional_delta = p_enable;
  229. RS::get_singleton()->particles_set_fractional_delta(particles, p_enable);
  230. }
  231. bool GPUParticles3D::get_fractional_delta() const {
  232. return fractional_delta;
  233. }
  234. void GPUParticles3D::set_interpolate(bool p_enable) {
  235. interpolate = p_enable;
  236. RS::get_singleton()->particles_set_interpolate(particles, p_enable);
  237. }
  238. bool GPUParticles3D::get_interpolate() const {
  239. return interpolate;
  240. }
  241. PackedStringArray GPUParticles3D::get_configuration_warnings() const {
  242. PackedStringArray warnings = GeometryInstance3D::get_configuration_warnings();
  243. bool meshes_found = false;
  244. bool anim_material_found = false;
  245. for (int i = 0; i < draw_passes.size(); i++) {
  246. if (draw_passes[i].is_valid()) {
  247. meshes_found = true;
  248. for (int j = 0; j < draw_passes[i]->get_surface_count(); j++) {
  249. anim_material_found = Object::cast_to<ShaderMaterial>(draw_passes[i]->surface_get_material(j).ptr()) != nullptr;
  250. BaseMaterial3D *spat = Object::cast_to<BaseMaterial3D>(draw_passes[i]->surface_get_material(j).ptr());
  251. anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == StandardMaterial3D::BILLBOARD_PARTICLES);
  252. }
  253. if (anim_material_found) {
  254. break;
  255. }
  256. }
  257. }
  258. anim_material_found = anim_material_found || Object::cast_to<ShaderMaterial>(get_material_override().ptr()) != nullptr;
  259. {
  260. BaseMaterial3D *spat = Object::cast_to<BaseMaterial3D>(get_material_override().ptr());
  261. anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == BaseMaterial3D::BILLBOARD_PARTICLES);
  262. }
  263. if (!meshes_found) {
  264. warnings.push_back(RTR("Nothing is visible because meshes have not been assigned to draw passes."));
  265. }
  266. if (process_material.is_null()) {
  267. warnings.push_back(RTR("A material to process the particles is not assigned, so no behavior is imprinted."));
  268. } else {
  269. const ParticleProcessMaterial *process = Object::cast_to<ParticleProcessMaterial>(process_material.ptr());
  270. if (!anim_material_found && process &&
  271. (process->get_param_max(ParticleProcessMaterial::PARAM_ANIM_SPEED) != 0.0 || process->get_param_max(ParticleProcessMaterial::PARAM_ANIM_OFFSET) != 0.0 ||
  272. process->get_param_texture(ParticleProcessMaterial::PARAM_ANIM_SPEED).is_valid() || process->get_param_texture(ParticleProcessMaterial::PARAM_ANIM_OFFSET).is_valid())) {
  273. warnings.push_back(RTR("Particles animation requires the usage of a BaseMaterial3D whose Billboard Mode is set to \"Particle Billboard\"."));
  274. }
  275. }
  276. if (trail_enabled) {
  277. int dp_count = 0;
  278. bool missing_trails = false;
  279. bool no_materials = false;
  280. for (int i = 0; i < draw_passes.size(); i++) {
  281. Ref<Mesh> draw_pass = draw_passes[i];
  282. if (draw_pass.is_valid() && draw_pass->get_builtin_bind_pose_count() > 0) {
  283. dp_count++;
  284. }
  285. if (draw_pass.is_valid()) {
  286. int mats_found = 0;
  287. for (int j = 0; j < draw_passes[i]->get_surface_count(); j++) {
  288. BaseMaterial3D *spat = Object::cast_to<BaseMaterial3D>(draw_passes[i]->surface_get_material(j).ptr());
  289. if (spat) {
  290. mats_found++;
  291. }
  292. if (spat && !spat->get_flag(BaseMaterial3D::FLAG_PARTICLE_TRAILS_MODE)) {
  293. missing_trails = true;
  294. }
  295. }
  296. if (mats_found != draw_passes[i]->get_surface_count()) {
  297. no_materials = true;
  298. }
  299. }
  300. }
  301. BaseMaterial3D *spat = Object::cast_to<BaseMaterial3D>(get_material_override().ptr());
  302. if (spat) {
  303. no_materials = false;
  304. }
  305. if (spat && !spat->get_flag(BaseMaterial3D::FLAG_PARTICLE_TRAILS_MODE)) {
  306. missing_trails = true;
  307. }
  308. if (dp_count && skin.is_valid()) {
  309. warnings.push_back(RTR("Using Trail meshes with a skin causes Skin to override Trail poses. Suggest removing the Skin."));
  310. } else if (dp_count == 0 && skin.is_null()) {
  311. warnings.push_back(RTR("Trails active, but neither Trail meshes or a Skin were found."));
  312. } else if (dp_count > 1) {
  313. 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)."));
  314. }
  315. if ((dp_count || !skin.is_null()) && (missing_trails || no_materials)) {
  316. warnings.push_back(RTR("Trails enabled, but one or more mesh materials are either missing or not set for trails rendering."));
  317. }
  318. if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  319. warnings.push_back(RTR("Particle trails are only available when using the Forward+ or Mobile rendering backends."));
  320. }
  321. }
  322. if (sub_emitter != NodePath() && OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  323. warnings.push_back(RTR("Particle sub-emitters are only available when using the Forward+ or Mobile rendering backends."));
  324. }
  325. return warnings;
  326. }
  327. void GPUParticles3D::restart() {
  328. RenderingServer::get_singleton()->particles_restart(particles);
  329. RenderingServer::get_singleton()->particles_set_emitting(particles, true);
  330. emitting = true;
  331. active = true;
  332. signal_canceled = false;
  333. time = 0;
  334. emission_time = lifetime * (1 - explosiveness_ratio);
  335. active_time = lifetime * (2 - explosiveness_ratio);
  336. set_process_internal(true);
  337. }
  338. AABB GPUParticles3D::capture_aabb() const {
  339. return RS::get_singleton()->particles_get_current_aabb(particles);
  340. }
  341. void GPUParticles3D::_validate_property(PropertyInfo &p_property) const {
  342. if (p_property.name == "emitting") {
  343. p_property.hint = one_shot ? PROPERTY_HINT_ONESHOT : PROPERTY_HINT_NONE;
  344. }
  345. if (p_property.name.begins_with("draw_pass_")) {
  346. int index = p_property.name.get_slicec('_', 2).to_int() - 1;
  347. if (index >= draw_passes.size()) {
  348. p_property.usage = PROPERTY_USAGE_NONE;
  349. return;
  350. }
  351. }
  352. }
  353. 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) {
  354. RS::get_singleton()->particles_emit(particles, p_transform, p_velocity, p_color, p_custom, p_emit_flags);
  355. }
  356. void GPUParticles3D::_attach_sub_emitter() {
  357. Node *n = get_node_or_null(sub_emitter);
  358. if (n) {
  359. GPUParticles3D *sen = Object::cast_to<GPUParticles3D>(n);
  360. if (sen && sen != this) {
  361. RS::get_singleton()->particles_set_subemitter(particles, sen->particles);
  362. }
  363. }
  364. }
  365. void GPUParticles3D::set_sub_emitter(const NodePath &p_path) {
  366. if (is_inside_tree()) {
  367. RS::get_singleton()->particles_set_subemitter(particles, RID());
  368. }
  369. sub_emitter = p_path;
  370. if (is_inside_tree() && sub_emitter != NodePath()) {
  371. _attach_sub_emitter();
  372. }
  373. update_configuration_warnings();
  374. }
  375. NodePath GPUParticles3D::get_sub_emitter() const {
  376. return sub_emitter;
  377. }
  378. void GPUParticles3D::_notification(int p_what) {
  379. switch (p_what) {
  380. // Use internal process when emitting and one_shot is on so that when
  381. // the shot ends the editor can properly update.
  382. case NOTIFICATION_INTERNAL_PROCESS: {
  383. if (one_shot) {
  384. time += get_process_delta_time();
  385. if (time > emission_time) {
  386. emitting = false;
  387. if (!active) {
  388. set_process_internal(false);
  389. }
  390. }
  391. if (time > active_time) {
  392. if (active && !signal_canceled) {
  393. emit_signal(SceneStringName(finished));
  394. }
  395. active = false;
  396. if (!emitting) {
  397. set_process_internal(false);
  398. }
  399. }
  400. }
  401. } break;
  402. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  403. // Update velocity in physics process, so that velocity calculations remain correct
  404. // if the physics tick rate is lower than the rendered framerate (especially without physics interpolation).
  405. const Vector3 velocity = (get_global_position() - previous_position) / get_physics_process_delta_time();
  406. if (velocity != previous_velocity) {
  407. RS::get_singleton()->particles_set_emitter_velocity(particles, velocity);
  408. previous_velocity = velocity;
  409. }
  410. previous_position = get_global_position();
  411. } break;
  412. case NOTIFICATION_ENTER_TREE: {
  413. set_process_internal(false);
  414. set_physics_process_internal(false);
  415. if (sub_emitter != NodePath()) {
  416. _attach_sub_emitter();
  417. }
  418. if (can_process()) {
  419. RS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
  420. } else {
  421. RS::get_singleton()->particles_set_speed_scale(particles, 0);
  422. }
  423. previous_position = get_global_transform().origin;
  424. set_process_internal(true);
  425. set_physics_process_internal(true);
  426. } break;
  427. case NOTIFICATION_EXIT_TREE: {
  428. RS::get_singleton()->particles_set_subemitter(particles, RID());
  429. } break;
  430. case NOTIFICATION_SUSPENDED:
  431. case NOTIFICATION_UNSUSPENDED:
  432. case NOTIFICATION_PAUSED:
  433. case NOTIFICATION_UNPAUSED: {
  434. if (is_inside_tree()) {
  435. if (can_process()) {
  436. RS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
  437. } else {
  438. RS::get_singleton()->particles_set_speed_scale(particles, 0);
  439. }
  440. }
  441. } break;
  442. case NOTIFICATION_VISIBILITY_CHANGED: {
  443. // Make sure particles are updated before rendering occurs if they were active before.
  444. if (is_visible_in_tree() && !RS::get_singleton()->particles_is_inactive(particles)) {
  445. RS::get_singleton()->particles_request_process(particles);
  446. }
  447. } break;
  448. }
  449. }
  450. void GPUParticles3D::_skinning_changed() {
  451. Vector<Transform3D> xforms;
  452. if (skin.is_valid()) {
  453. xforms.resize(skin->get_bind_count());
  454. for (int i = 0; i < skin->get_bind_count(); i++) {
  455. xforms.write[i] = skin->get_bind_pose(i);
  456. }
  457. } else {
  458. for (int i = 0; i < draw_passes.size(); i++) {
  459. Ref<Mesh> draw_pass = draw_passes[i];
  460. if (draw_pass.is_valid() && draw_pass->get_builtin_bind_pose_count() > 0) {
  461. xforms.resize(draw_pass->get_builtin_bind_pose_count());
  462. for (int j = 0; j < draw_pass->get_builtin_bind_pose_count(); j++) {
  463. xforms.write[j] = draw_pass->get_builtin_bind_pose(j);
  464. }
  465. break;
  466. }
  467. }
  468. }
  469. RS::get_singleton()->particles_set_trail_bind_poses(particles, xforms);
  470. update_configuration_warnings();
  471. }
  472. void GPUParticles3D::set_skin(const Ref<Skin> &p_skin) {
  473. skin = p_skin;
  474. _skinning_changed();
  475. }
  476. Ref<Skin> GPUParticles3D::get_skin() const {
  477. return skin;
  478. }
  479. void GPUParticles3D::set_transform_align(TransformAlign p_align) {
  480. ERR_FAIL_INDEX(uint32_t(p_align), 4);
  481. transform_align = p_align;
  482. RS::get_singleton()->particles_set_transform_align(particles, RS::ParticlesTransformAlign(transform_align));
  483. }
  484. GPUParticles3D::TransformAlign GPUParticles3D::get_transform_align() const {
  485. return transform_align;
  486. }
  487. void GPUParticles3D::convert_from_particles(Node *p_particles) {
  488. CPUParticles3D *cpu_particles = Object::cast_to<CPUParticles3D>(p_particles);
  489. ERR_FAIL_NULL_MSG(cpu_particles, "Only CPUParticles3D nodes can be converted to GPUParticles3D.");
  490. set_emitting(cpu_particles->is_emitting());
  491. set_amount(cpu_particles->get_amount());
  492. set_lifetime(cpu_particles->get_lifetime());
  493. set_one_shot(cpu_particles->get_one_shot());
  494. set_pre_process_time(cpu_particles->get_pre_process_time());
  495. set_explosiveness_ratio(cpu_particles->get_explosiveness_ratio());
  496. set_randomness_ratio(cpu_particles->get_randomness_ratio());
  497. set_use_local_coordinates(cpu_particles->get_use_local_coordinates());
  498. set_fixed_fps(cpu_particles->get_fixed_fps());
  499. set_fractional_delta(cpu_particles->get_fractional_delta());
  500. set_speed_scale(cpu_particles->get_speed_scale());
  501. set_draw_order(DrawOrder(cpu_particles->get_draw_order()));
  502. set_draw_pass_mesh(0, cpu_particles->get_mesh());
  503. Ref<ParticleProcessMaterial> proc_mat = memnew(ParticleProcessMaterial);
  504. set_process_material(proc_mat);
  505. proc_mat->set_direction(cpu_particles->get_direction());
  506. proc_mat->set_spread(cpu_particles->get_spread());
  507. proc_mat->set_flatness(cpu_particles->get_flatness());
  508. proc_mat->set_color(cpu_particles->get_color());
  509. Ref<Gradient> grad = cpu_particles->get_color_ramp();
  510. if (grad.is_valid()) {
  511. Ref<GradientTexture1D> tex = memnew(GradientTexture1D);
  512. tex->set_gradient(grad);
  513. proc_mat->set_color_ramp(tex);
  514. }
  515. Ref<Gradient> grad_init = cpu_particles->get_color_initial_ramp();
  516. if (grad_init.is_valid()) {
  517. Ref<GradientTexture1D> tex = memnew(GradientTexture1D);
  518. tex->set_gradient(grad_init);
  519. proc_mat->set_color_initial_ramp(tex);
  520. }
  521. proc_mat->set_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY, cpu_particles->get_particle_flag(CPUParticles3D::PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY));
  522. proc_mat->set_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_ROTATE_Y, cpu_particles->get_particle_flag(CPUParticles3D::PARTICLE_FLAG_ROTATE_Y));
  523. proc_mat->set_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_DISABLE_Z, cpu_particles->get_particle_flag(CPUParticles3D::PARTICLE_FLAG_DISABLE_Z));
  524. proc_mat->set_emission_shape(ParticleProcessMaterial::EmissionShape(cpu_particles->get_emission_shape()));
  525. proc_mat->set_emission_sphere_radius(cpu_particles->get_emission_sphere_radius());
  526. proc_mat->set_emission_box_extents(cpu_particles->get_emission_box_extents());
  527. if (cpu_particles->get_split_scale()) {
  528. Ref<CurveXYZTexture> scale3D = memnew(CurveXYZTexture);
  529. scale3D->set_curve_x(cpu_particles->get_scale_curve_x());
  530. scale3D->set_curve_y(cpu_particles->get_scale_curve_y());
  531. scale3D->set_curve_z(cpu_particles->get_scale_curve_z());
  532. proc_mat->set_param_texture(ParticleProcessMaterial::PARAM_SCALE, scale3D);
  533. }
  534. proc_mat->set_gravity(cpu_particles->get_gravity());
  535. proc_mat->set_lifetime_randomness(cpu_particles->get_lifetime_randomness());
  536. #define CONVERT_PARAM(m_param) \
  537. proc_mat->set_param_min(ParticleProcessMaterial::m_param, cpu_particles->get_param_min(CPUParticles3D::m_param)); \
  538. { \
  539. Ref<Curve> curve = cpu_particles->get_param_curve(CPUParticles3D::m_param); \
  540. if (curve.is_valid()) { \
  541. Ref<CurveTexture> tex = memnew(CurveTexture); \
  542. tex->set_curve(curve); \
  543. proc_mat->set_param_texture(ParticleProcessMaterial::m_param, tex); \
  544. } \
  545. } \
  546. proc_mat->set_param_max(ParticleProcessMaterial::m_param, cpu_particles->get_param_max(CPUParticles3D::m_param));
  547. CONVERT_PARAM(PARAM_INITIAL_LINEAR_VELOCITY);
  548. CONVERT_PARAM(PARAM_ANGULAR_VELOCITY);
  549. CONVERT_PARAM(PARAM_ORBIT_VELOCITY);
  550. CONVERT_PARAM(PARAM_LINEAR_ACCEL);
  551. CONVERT_PARAM(PARAM_RADIAL_ACCEL);
  552. CONVERT_PARAM(PARAM_TANGENTIAL_ACCEL);
  553. CONVERT_PARAM(PARAM_DAMPING);
  554. CONVERT_PARAM(PARAM_ANGLE);
  555. CONVERT_PARAM(PARAM_SCALE);
  556. CONVERT_PARAM(PARAM_HUE_VARIATION);
  557. CONVERT_PARAM(PARAM_ANIM_SPEED);
  558. CONVERT_PARAM(PARAM_ANIM_OFFSET);
  559. #undef CONVERT_PARAM
  560. }
  561. void GPUParticles3D::set_amount_ratio(float p_ratio) {
  562. amount_ratio = p_ratio;
  563. RS::get_singleton()->particles_set_amount_ratio(particles, p_ratio);
  564. }
  565. float GPUParticles3D::get_amount_ratio() const {
  566. return amount_ratio;
  567. }
  568. void GPUParticles3D::_bind_methods() {
  569. ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &GPUParticles3D::set_emitting);
  570. ClassDB::bind_method(D_METHOD("set_amount", "amount"), &GPUParticles3D::set_amount);
  571. ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &GPUParticles3D::set_lifetime);
  572. ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &GPUParticles3D::set_one_shot);
  573. ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &GPUParticles3D::set_pre_process_time);
  574. ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &GPUParticles3D::set_explosiveness_ratio);
  575. ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &GPUParticles3D::set_randomness_ratio);
  576. ClassDB::bind_method(D_METHOD("set_visibility_aabb", "aabb"), &GPUParticles3D::set_visibility_aabb);
  577. ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &GPUParticles3D::set_use_local_coordinates);
  578. ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &GPUParticles3D::set_fixed_fps);
  579. ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &GPUParticles3D::set_fractional_delta);
  580. ClassDB::bind_method(D_METHOD("set_interpolate", "enable"), &GPUParticles3D::set_interpolate);
  581. ClassDB::bind_method(D_METHOD("set_process_material", "material"), &GPUParticles3D::set_process_material);
  582. ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &GPUParticles3D::set_speed_scale);
  583. ClassDB::bind_method(D_METHOD("set_collision_base_size", "size"), &GPUParticles3D::set_collision_base_size);
  584. ClassDB::bind_method(D_METHOD("set_interp_to_end", "interp"), &GPUParticles3D::set_interp_to_end);
  585. ClassDB::bind_method(D_METHOD("is_emitting"), &GPUParticles3D::is_emitting);
  586. ClassDB::bind_method(D_METHOD("get_amount"), &GPUParticles3D::get_amount);
  587. ClassDB::bind_method(D_METHOD("get_lifetime"), &GPUParticles3D::get_lifetime);
  588. ClassDB::bind_method(D_METHOD("get_one_shot"), &GPUParticles3D::get_one_shot);
  589. ClassDB::bind_method(D_METHOD("get_pre_process_time"), &GPUParticles3D::get_pre_process_time);
  590. ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &GPUParticles3D::get_explosiveness_ratio);
  591. ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &GPUParticles3D::get_randomness_ratio);
  592. ClassDB::bind_method(D_METHOD("get_visibility_aabb"), &GPUParticles3D::get_visibility_aabb);
  593. ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &GPUParticles3D::get_use_local_coordinates);
  594. ClassDB::bind_method(D_METHOD("get_fixed_fps"), &GPUParticles3D::get_fixed_fps);
  595. ClassDB::bind_method(D_METHOD("get_fractional_delta"), &GPUParticles3D::get_fractional_delta);
  596. ClassDB::bind_method(D_METHOD("get_interpolate"), &GPUParticles3D::get_interpolate);
  597. ClassDB::bind_method(D_METHOD("get_process_material"), &GPUParticles3D::get_process_material);
  598. ClassDB::bind_method(D_METHOD("get_speed_scale"), &GPUParticles3D::get_speed_scale);
  599. ClassDB::bind_method(D_METHOD("get_collision_base_size"), &GPUParticles3D::get_collision_base_size);
  600. ClassDB::bind_method(D_METHOD("get_interp_to_end"), &GPUParticles3D::get_interp_to_end);
  601. ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &GPUParticles3D::set_draw_order);
  602. ClassDB::bind_method(D_METHOD("get_draw_order"), &GPUParticles3D::get_draw_order);
  603. ClassDB::bind_method(D_METHOD("set_draw_passes", "passes"), &GPUParticles3D::set_draw_passes);
  604. ClassDB::bind_method(D_METHOD("set_draw_pass_mesh", "pass", "mesh"), &GPUParticles3D::set_draw_pass_mesh);
  605. ClassDB::bind_method(D_METHOD("get_draw_passes"), &GPUParticles3D::get_draw_passes);
  606. ClassDB::bind_method(D_METHOD("get_draw_pass_mesh", "pass"), &GPUParticles3D::get_draw_pass_mesh);
  607. ClassDB::bind_method(D_METHOD("set_skin", "skin"), &GPUParticles3D::set_skin);
  608. ClassDB::bind_method(D_METHOD("get_skin"), &GPUParticles3D::get_skin);
  609. ClassDB::bind_method(D_METHOD("restart"), &GPUParticles3D::restart);
  610. ClassDB::bind_method(D_METHOD("capture_aabb"), &GPUParticles3D::capture_aabb);
  611. ClassDB::bind_method(D_METHOD("set_sub_emitter", "path"), &GPUParticles3D::set_sub_emitter);
  612. ClassDB::bind_method(D_METHOD("get_sub_emitter"), &GPUParticles3D::get_sub_emitter);
  613. ClassDB::bind_method(D_METHOD("emit_particle", "xform", "velocity", "color", "custom", "flags"), &GPUParticles3D::emit_particle);
  614. ClassDB::bind_method(D_METHOD("set_trail_enabled", "enabled"), &GPUParticles3D::set_trail_enabled);
  615. ClassDB::bind_method(D_METHOD("set_trail_lifetime", "secs"), &GPUParticles3D::set_trail_lifetime);
  616. ClassDB::bind_method(D_METHOD("is_trail_enabled"), &GPUParticles3D::is_trail_enabled);
  617. ClassDB::bind_method(D_METHOD("get_trail_lifetime"), &GPUParticles3D::get_trail_lifetime);
  618. ClassDB::bind_method(D_METHOD("set_transform_align", "align"), &GPUParticles3D::set_transform_align);
  619. ClassDB::bind_method(D_METHOD("get_transform_align"), &GPUParticles3D::get_transform_align);
  620. ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &GPUParticles3D::convert_from_particles);
  621. ClassDB::bind_method(D_METHOD("set_amount_ratio", "ratio"), &GPUParticles3D::set_amount_ratio);
  622. ClassDB::bind_method(D_METHOD("get_amount_ratio"), &GPUParticles3D::get_amount_ratio);
  623. ADD_SIGNAL(MethodInfo("finished"));
  624. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting", PROPERTY_HINT_ONESHOT), "set_emitting", "is_emitting");
  625. ADD_PROPERTY_DEFAULT("emitting", true); // Workaround for doctool in headless mode, as dummy rasterizer always returns false.
  626. ADD_PROPERTY(PropertyInfo(Variant::INT, "amount", PROPERTY_HINT_RANGE, "1,1000000,1,exp"), "set_amount", "get_amount"); // FIXME: Evaluate support for `exp` in integer properties, or remove this.
  627. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "amount_ratio", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_amount_ratio", "get_amount_ratio");
  628. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "sub_emitter", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "GPUParticles3D"), "set_sub_emitter", "get_sub_emitter");
  629. ADD_GROUP("Time", "");
  630. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime", PROPERTY_HINT_RANGE, "0.01,600.0,0.01,or_greater,exp,suffix:s"), "set_lifetime", "get_lifetime");
  631. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "interp_to_end", PROPERTY_HINT_RANGE, "0.00,1.0,0.01"), "set_interp_to_end", "get_interp_to_end");
  632. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
  633. 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");
  634. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
  635. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
  636. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
  637. ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1,suffix:FPS"), "set_fixed_fps", "get_fixed_fps");
  638. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interpolate"), "set_interpolate", "get_interpolate");
  639. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
  640. ADD_GROUP("Collision", "collision_");
  641. 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");
  642. ADD_GROUP("Drawing", "");
  643. ADD_PROPERTY(PropertyInfo(Variant::AABB, "visibility_aabb", PROPERTY_HINT_NONE, "suffix:m"), "set_visibility_aabb", "get_visibility_aabb");
  644. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
  645. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime,Reverse Lifetime,View Depth"), "set_draw_order", "get_draw_order");
  646. 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");
  647. ADD_GROUP("Trails", "trail_");
  648. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "trail_enabled"), "set_trail_enabled", "is_trail_enabled");
  649. 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");
  650. ADD_GROUP("Process Material", "");
  651. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "process_material", PROPERTY_HINT_RESOURCE_TYPE, "ParticleProcessMaterial,ShaderMaterial"), "set_process_material", "get_process_material");
  652. ADD_GROUP("Draw Passes", "draw_");
  653. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_passes", PROPERTY_HINT_RANGE, "0," + itos(MAX_DRAW_PASSES) + ",1"), "set_draw_passes", "get_draw_passes");
  654. for (int i = 0; i < MAX_DRAW_PASSES; i++) {
  655. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "draw_pass_" + itos(i + 1), PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_draw_pass_mesh", "get_draw_pass_mesh", i);
  656. }
  657. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "draw_skin", PROPERTY_HINT_RESOURCE_TYPE, "Skin"), "set_skin", "get_skin");
  658. BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
  659. BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
  660. BIND_ENUM_CONSTANT(DRAW_ORDER_REVERSE_LIFETIME);
  661. BIND_ENUM_CONSTANT(DRAW_ORDER_VIEW_DEPTH);
  662. BIND_ENUM_CONSTANT(EMIT_FLAG_POSITION);
  663. BIND_ENUM_CONSTANT(EMIT_FLAG_ROTATION_SCALE);
  664. BIND_ENUM_CONSTANT(EMIT_FLAG_VELOCITY);
  665. BIND_ENUM_CONSTANT(EMIT_FLAG_COLOR);
  666. BIND_ENUM_CONSTANT(EMIT_FLAG_CUSTOM);
  667. BIND_CONSTANT(MAX_DRAW_PASSES);
  668. BIND_ENUM_CONSTANT(TRANSFORM_ALIGN_DISABLED);
  669. BIND_ENUM_CONSTANT(TRANSFORM_ALIGN_Z_BILLBOARD);
  670. BIND_ENUM_CONSTANT(TRANSFORM_ALIGN_Y_TO_VELOCITY);
  671. BIND_ENUM_CONSTANT(TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY);
  672. }
  673. GPUParticles3D::GPUParticles3D() {
  674. particles = RS::get_singleton()->particles_create();
  675. RS::get_singleton()->particles_set_mode(particles, RS::PARTICLES_MODE_3D);
  676. set_base(particles);
  677. one_shot = false; // Needed so that set_emitting doesn't access uninitialized values
  678. set_emitting(true);
  679. set_one_shot(false);
  680. set_amount_ratio(1.0);
  681. set_amount(8);
  682. set_lifetime(1);
  683. set_fixed_fps(30);
  684. set_fractional_delta(true);
  685. set_interpolate(true);
  686. set_pre_process_time(0);
  687. set_explosiveness_ratio(0);
  688. set_randomness_ratio(0);
  689. set_trail_lifetime(0.3);
  690. set_visibility_aabb(AABB(Vector3(-4, -4, -4), Vector3(8, 8, 8)));
  691. set_use_local_coordinates(false);
  692. set_draw_passes(1);
  693. set_draw_order(DRAW_ORDER_INDEX);
  694. set_speed_scale(1);
  695. set_collision_base_size(collision_base_size);
  696. set_transform_align(TRANSFORM_ALIGN_DISABLED);
  697. }
  698. GPUParticles3D::~GPUParticles3D() {
  699. ERR_FAIL_NULL(RenderingServer::get_singleton());
  700. RS::get_singleton()->free(particles);
  701. }