gpu_particles_3d.cpp 34 KB

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