cpu_particles.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  1. /**************************************************************************/
  2. /* cpu_particles.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 "cpu_particles.h"
  31. #include "core/os/os.h"
  32. #include "scene/3d/camera.h"
  33. #include "scene/3d/particles.h"
  34. #include "scene/resources/particles_material.h"
  35. #include "servers/visual_server.h"
  36. AABB CPUParticles::get_aabb() const {
  37. return AABB();
  38. }
  39. PoolVector<Face3> CPUParticles::get_faces(uint32_t p_usage_flags) const {
  40. return PoolVector<Face3>();
  41. }
  42. void CPUParticles::_set_particles_processing(bool p_enable) {
  43. if (_interpolated) {
  44. set_physics_process_internal(p_enable);
  45. } else {
  46. set_process_internal(p_enable);
  47. }
  48. }
  49. void CPUParticles::set_emitting(bool p_emitting) {
  50. if (emitting == p_emitting) {
  51. return;
  52. }
  53. emitting = p_emitting;
  54. if (emitting) {
  55. _set_particles_processing(true);
  56. // first update before rendering to avoid one frame delay after emitting starts
  57. if ((time == 0) && !_interpolated) {
  58. _update_internal(false);
  59. }
  60. }
  61. }
  62. void CPUParticles::set_amount(int p_amount) {
  63. ERR_FAIL_COND_MSG(p_amount < 1, "Amount of particles must be greater than 0.");
  64. particles.resize(p_amount);
  65. particles_prev.resize(p_amount);
  66. {
  67. PoolVector<Particle>::Write w = particles.write();
  68. for (int i = 0; i < p_amount; i++) {
  69. w[i].active = false;
  70. w[i].custom[3] = 0.0; // Make sure w component isn't garbage data
  71. particles_prev[i].blank();
  72. }
  73. }
  74. particle_data.resize((12 + 4 + 1) * p_amount);
  75. particle_data_prev.resize(particle_data.size());
  76. // We must fill immediately to prevent garbage data and Nans
  77. // being sent to the visual server with set_as_bulk_array,
  78. // if this is sent before being regularly updated.
  79. particle_data.fill(0);
  80. particle_data_prev.fill(0);
  81. VS::get_singleton()->multimesh_allocate(multimesh, p_amount, VS::MULTIMESH_TRANSFORM_3D, VS::MULTIMESH_COLOR_8BIT, VS::MULTIMESH_CUSTOM_DATA_FLOAT);
  82. particle_order.resize(p_amount);
  83. }
  84. void CPUParticles::set_lifetime(float p_lifetime) {
  85. ERR_FAIL_COND_MSG(p_lifetime <= 0, "Particles lifetime must be greater than 0.");
  86. lifetime = p_lifetime;
  87. }
  88. void CPUParticles::set_one_shot(bool p_one_shot) {
  89. one_shot = p_one_shot;
  90. }
  91. void CPUParticles::set_pre_process_time(float p_time) {
  92. pre_process_time = p_time;
  93. }
  94. void CPUParticles::set_explosiveness_ratio(float p_ratio) {
  95. explosiveness_ratio = p_ratio;
  96. }
  97. void CPUParticles::set_randomness_ratio(float p_ratio) {
  98. randomness_ratio = p_ratio;
  99. }
  100. void CPUParticles::set_lifetime_randomness(float p_random) {
  101. lifetime_randomness = p_random;
  102. }
  103. void CPUParticles::set_use_local_coordinates(bool p_enable) {
  104. local_coords = p_enable;
  105. // prevent sending instance transforms when using global coords
  106. set_instance_use_identity_transform(!p_enable);
  107. }
  108. void CPUParticles::set_speed_scale(float p_scale) {
  109. speed_scale = p_scale;
  110. }
  111. bool CPUParticles::is_emitting() const {
  112. return emitting;
  113. }
  114. int CPUParticles::get_amount() const {
  115. return particles.size();
  116. }
  117. float CPUParticles::get_lifetime() const {
  118. return lifetime;
  119. }
  120. bool CPUParticles::get_one_shot() const {
  121. return one_shot;
  122. }
  123. float CPUParticles::get_pre_process_time() const {
  124. return pre_process_time;
  125. }
  126. float CPUParticles::get_explosiveness_ratio() const {
  127. return explosiveness_ratio;
  128. }
  129. float CPUParticles::get_randomness_ratio() const {
  130. return randomness_ratio;
  131. }
  132. float CPUParticles::get_lifetime_randomness() const {
  133. return lifetime_randomness;
  134. }
  135. bool CPUParticles::get_use_local_coordinates() const {
  136. return local_coords;
  137. }
  138. float CPUParticles::get_speed_scale() const {
  139. return speed_scale;
  140. }
  141. void CPUParticles::set_draw_order(DrawOrder p_order) {
  142. ERR_FAIL_INDEX(p_order, DRAW_ORDER_MAX);
  143. draw_order = p_order;
  144. }
  145. CPUParticles::DrawOrder CPUParticles::get_draw_order() const {
  146. return draw_order;
  147. }
  148. void CPUParticles::set_mesh(const Ref<Mesh> &p_mesh) {
  149. mesh = p_mesh;
  150. if (mesh.is_valid()) {
  151. VS::get_singleton()->multimesh_set_mesh(multimesh, mesh->get_rid());
  152. } else {
  153. VS::get_singleton()->multimesh_set_mesh(multimesh, RID());
  154. }
  155. }
  156. Ref<Mesh> CPUParticles::get_mesh() const {
  157. return mesh;
  158. }
  159. void CPUParticles::set_fixed_fps(int p_count) {
  160. fixed_fps = p_count;
  161. }
  162. int CPUParticles::get_fixed_fps() const {
  163. return fixed_fps;
  164. }
  165. void CPUParticles::set_fractional_delta(bool p_enable) {
  166. fractional_delta = p_enable;
  167. }
  168. bool CPUParticles::get_fractional_delta() const {
  169. return fractional_delta;
  170. }
  171. String CPUParticles::get_configuration_warning() const {
  172. String warnings = GeometryInstance::get_configuration_warning();
  173. bool mesh_found = false;
  174. bool anim_material_found = false;
  175. if (get_mesh().is_valid()) {
  176. mesh_found = true;
  177. for (int j = 0; j < get_mesh()->get_surface_count(); j++) {
  178. anim_material_found = Object::cast_to<ShaderMaterial>(get_mesh()->surface_get_material(j).ptr()) != nullptr;
  179. Material3D *spat = Object::cast_to<Material3D>(get_mesh()->surface_get_material(j).ptr());
  180. anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == Material3D::BILLBOARD_PARTICLES);
  181. }
  182. }
  183. anim_material_found = anim_material_found || Object::cast_to<ShaderMaterial>(get_material_override().ptr()) != nullptr;
  184. Material3D *spat = Object::cast_to<Material3D>(get_material_override().ptr());
  185. anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == Material3D::BILLBOARD_PARTICLES);
  186. if (!mesh_found) {
  187. if (warnings != String()) {
  188. warnings += "\n";
  189. }
  190. warnings += "- " + TTR("Nothing is visible because no mesh has been assigned.");
  191. }
  192. if (!anim_material_found && (get_param(PARAM_ANIM_SPEED) != 0.0 || get_param(PARAM_ANIM_OFFSET) != 0.0 || get_param_curve(PARAM_ANIM_SPEED).is_valid() || get_param_curve(PARAM_ANIM_OFFSET).is_valid())) {
  193. if (warnings != String()) {
  194. warnings += "\n";
  195. }
  196. warnings += "- " + TTR("CPUParticles animation requires the usage of a SpatialMaterial whose Billboard Mode is set to \"Particle Billboard\".");
  197. }
  198. return warnings;
  199. }
  200. void CPUParticles::restart() {
  201. time = 0;
  202. inactive_time = 0;
  203. frame_remainder = 0;
  204. cycle = 0;
  205. emitting = false;
  206. {
  207. int pc = particles.size();
  208. PoolVector<Particle>::Write w = particles.write();
  209. for (int i = 0; i < pc; i++) {
  210. w[i].active = false;
  211. }
  212. }
  213. set_emitting(true);
  214. }
  215. void CPUParticles::set_direction(Vector3 p_direction) {
  216. direction = p_direction;
  217. }
  218. Vector3 CPUParticles::get_direction() const {
  219. return direction;
  220. }
  221. void CPUParticles::set_spread(float p_spread) {
  222. spread = p_spread;
  223. }
  224. float CPUParticles::get_spread() const {
  225. return spread;
  226. }
  227. void CPUParticles::set_flatness(float p_flatness) {
  228. flatness = p_flatness;
  229. }
  230. float CPUParticles::get_flatness() const {
  231. return flatness;
  232. }
  233. void CPUParticles::set_param(Parameter p_param, float p_value) {
  234. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  235. parameters[p_param] = p_value;
  236. }
  237. float CPUParticles::get_param(Parameter p_param) const {
  238. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  239. return parameters[p_param];
  240. }
  241. void CPUParticles::set_param_randomness(Parameter p_param, float p_value) {
  242. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  243. randomness[p_param] = p_value;
  244. }
  245. float CPUParticles::get_param_randomness(Parameter p_param) const {
  246. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  247. return randomness[p_param];
  248. }
  249. static void _adjust_curve_range(const Ref<Curve> &p_curve, float p_min, float p_max) {
  250. Ref<Curve> curve = p_curve;
  251. if (!curve.is_valid()) {
  252. return;
  253. }
  254. curve->ensure_default_setup(p_min, p_max);
  255. }
  256. void CPUParticles::set_param_curve(Parameter p_param, const Ref<Curve> &p_curve) {
  257. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  258. curve_parameters[p_param] = p_curve;
  259. switch (p_param) {
  260. case PARAM_INITIAL_LINEAR_VELOCITY: {
  261. //do none for this one
  262. } break;
  263. case PARAM_ANGULAR_VELOCITY: {
  264. _adjust_curve_range(p_curve, -360, 360);
  265. } break;
  266. case PARAM_ORBIT_VELOCITY: {
  267. _adjust_curve_range(p_curve, -500, 500);
  268. } break;
  269. case PARAM_LINEAR_ACCEL: {
  270. _adjust_curve_range(p_curve, -200, 200);
  271. } break;
  272. case PARAM_RADIAL_ACCEL: {
  273. _adjust_curve_range(p_curve, -200, 200);
  274. } break;
  275. case PARAM_TANGENTIAL_ACCEL: {
  276. _adjust_curve_range(p_curve, -200, 200);
  277. } break;
  278. case PARAM_DAMPING: {
  279. _adjust_curve_range(p_curve, 0, 100);
  280. } break;
  281. case PARAM_ANGLE: {
  282. _adjust_curve_range(p_curve, -360, 360);
  283. } break;
  284. case PARAM_SCALE: {
  285. } break;
  286. case PARAM_HUE_VARIATION: {
  287. _adjust_curve_range(p_curve, -1, 1);
  288. } break;
  289. case PARAM_ANIM_SPEED: {
  290. _adjust_curve_range(p_curve, 0, 200);
  291. } break;
  292. case PARAM_ANIM_OFFSET: {
  293. } break;
  294. default: {
  295. }
  296. }
  297. }
  298. Ref<Curve> CPUParticles::get_param_curve(Parameter p_param) const {
  299. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, Ref<Curve>());
  300. return curve_parameters[p_param];
  301. }
  302. void CPUParticles::set_color(const Color &p_color) {
  303. color = p_color;
  304. }
  305. Color CPUParticles::get_color() const {
  306. return color;
  307. }
  308. void CPUParticles::set_color_ramp(const Ref<Gradient> &p_ramp) {
  309. color_ramp = p_ramp;
  310. }
  311. Ref<Gradient> CPUParticles::get_color_ramp() const {
  312. return color_ramp;
  313. }
  314. void CPUParticles::set_color_initial_ramp(const Ref<Gradient> &p_ramp) {
  315. color_initial_ramp = p_ramp;
  316. }
  317. Ref<Gradient> CPUParticles::get_color_initial_ramp() const {
  318. return color_initial_ramp;
  319. }
  320. void CPUParticles::set_particle_flag(Flags p_flag, bool p_enable) {
  321. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  322. flags[p_flag] = p_enable;
  323. if (p_flag == FLAG_DISABLE_Z) {
  324. _change_notify();
  325. }
  326. }
  327. bool CPUParticles::get_particle_flag(Flags p_flag) const {
  328. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  329. return flags[p_flag];
  330. }
  331. void CPUParticles::set_emission_shape(EmissionShape p_shape) {
  332. ERR_FAIL_INDEX(p_shape, EMISSION_SHAPE_MAX);
  333. emission_shape = p_shape;
  334. }
  335. void CPUParticles::set_emission_sphere_radius(float p_radius) {
  336. emission_sphere_radius = p_radius;
  337. }
  338. void CPUParticles::set_emission_box_extents(Vector3 p_extents) {
  339. emission_box_extents = p_extents;
  340. }
  341. void CPUParticles::set_emission_points(const PoolVector<Vector3> &p_points) {
  342. emission_points = p_points;
  343. }
  344. void CPUParticles::set_emission_normals(const PoolVector<Vector3> &p_normals) {
  345. emission_normals = p_normals;
  346. }
  347. void CPUParticles::set_emission_colors(const PoolVector<Color> &p_colors) {
  348. emission_colors = p_colors;
  349. }
  350. void CPUParticles::set_emission_ring_height(float p_height) {
  351. emission_ring_height = p_height;
  352. }
  353. void CPUParticles::set_emission_ring_radius(float p_radius) {
  354. emission_ring_radius = p_radius;
  355. }
  356. void CPUParticles::set_emission_ring_inner_radius(float p_offset) {
  357. emission_ring_inner_radius = p_offset;
  358. }
  359. void CPUParticles::set_emission_ring_axis(Vector3 p_axis) {
  360. emission_ring_axis = p_axis;
  361. }
  362. float CPUParticles::get_emission_sphere_radius() const {
  363. return emission_sphere_radius;
  364. }
  365. Vector3 CPUParticles::get_emission_box_extents() const {
  366. return emission_box_extents;
  367. }
  368. PoolVector<Vector3> CPUParticles::get_emission_points() const {
  369. return emission_points;
  370. }
  371. PoolVector<Vector3> CPUParticles::get_emission_normals() const {
  372. return emission_normals;
  373. }
  374. PoolVector<Color> CPUParticles::get_emission_colors() const {
  375. return emission_colors;
  376. }
  377. float CPUParticles::get_emission_ring_height() const {
  378. return emission_ring_height;
  379. }
  380. float CPUParticles::get_emission_ring_inner_radius() const {
  381. return emission_ring_inner_radius;
  382. }
  383. float CPUParticles::get_emission_ring_radius() const {
  384. return emission_ring_radius;
  385. }
  386. Vector3 CPUParticles::get_emission_ring_axis() const {
  387. return emission_ring_axis;
  388. }
  389. CPUParticles::EmissionShape CPUParticles::get_emission_shape() const {
  390. return emission_shape;
  391. }
  392. void CPUParticles::set_gravity(const Vector3 &p_gravity) {
  393. gravity = p_gravity;
  394. }
  395. Vector3 CPUParticles::get_gravity() const {
  396. return gravity;
  397. }
  398. void CPUParticles::_validate_property(PropertyInfo &property) const {
  399. if (property.name == "emission_sphere_radius" && emission_shape != EMISSION_SHAPE_SPHERE) {
  400. property.usage = 0;
  401. }
  402. if (property.name == "emission_box_extents" && emission_shape != EMISSION_SHAPE_BOX) {
  403. property.usage = 0;
  404. }
  405. if ((property.name == "emission_points" || property.name == "emission_colors") && (emission_shape != EMISSION_SHAPE_POINTS) && (emission_shape != EMISSION_SHAPE_DIRECTED_POINTS)) {
  406. property.usage = 0;
  407. }
  408. if (property.name == "emission_normals" && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
  409. property.usage = 0;
  410. }
  411. if (property.name.begins_with("emission_ring") && emission_shape != EMISSION_SHAPE_RING) {
  412. property.usage = 0;
  413. }
  414. if (property.name.begins_with("orbit_") && !flags[FLAG_DISABLE_Z]) {
  415. property.usage = 0;
  416. }
  417. }
  418. static uint32_t idhash(uint32_t x) {
  419. x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
  420. x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
  421. x = (x >> uint32_t(16)) ^ x;
  422. return x;
  423. }
  424. static float rand_from_seed(uint32_t &seed) {
  425. int k;
  426. int s = int(seed);
  427. if (s == 0) {
  428. s = 305420679;
  429. }
  430. k = s / 127773;
  431. s = 16807 * (s - k * 127773) - 2836 * k;
  432. if (s < 0) {
  433. s += 2147483647;
  434. }
  435. seed = uint32_t(s);
  436. return float(seed % uint32_t(65536)) / 65535.0;
  437. }
  438. void CPUParticles::_update_internal(bool p_on_physics_tick) {
  439. if (particles.size() == 0 || !is_visible_in_tree()) {
  440. _set_redraw(false);
  441. return;
  442. }
  443. // change update mode?
  444. _refresh_interpolation_state();
  445. float delta = 0.0f;
  446. // Is this update occurring on a physics tick (i.e. interpolated), or a frame tick?
  447. if (p_on_physics_tick) {
  448. delta = get_physics_process_delta_time();
  449. } else {
  450. delta = get_process_delta_time();
  451. }
  452. if (emitting) {
  453. inactive_time = 0;
  454. } else {
  455. inactive_time += delta;
  456. if (inactive_time > lifetime * 1.2) {
  457. _set_particles_processing(false);
  458. _set_redraw(false);
  459. //reset variables
  460. time = 0;
  461. inactive_time = 0;
  462. frame_remainder = 0;
  463. cycle = 0;
  464. return;
  465. }
  466. }
  467. _set_redraw(true);
  468. bool processed = false;
  469. if (time == 0 && pre_process_time > 0.0) {
  470. float frame_time;
  471. if (fixed_fps > 0) {
  472. frame_time = 1.0 / fixed_fps;
  473. } else {
  474. frame_time = 1.0 / 30.0;
  475. }
  476. float todo = pre_process_time;
  477. while (todo >= 0) {
  478. _particles_process(frame_time);
  479. processed = true;
  480. todo -= frame_time;
  481. }
  482. }
  483. if (fixed_fps > 0) {
  484. float frame_time = 1.0 / fixed_fps;
  485. float decr = frame_time;
  486. float ldelta = delta;
  487. if (ldelta > 0.1) { //avoid recursive stalls if fps goes below 10
  488. ldelta = 0.1;
  489. } else if (ldelta <= 0.0) { //unlikely but..
  490. ldelta = 0.001;
  491. }
  492. float todo = frame_remainder + ldelta;
  493. while (todo >= frame_time) {
  494. _particles_process(frame_time);
  495. processed = true;
  496. todo -= decr;
  497. }
  498. frame_remainder = todo;
  499. } else {
  500. _particles_process(delta);
  501. processed = true;
  502. }
  503. if (processed) {
  504. _update_particle_data_buffer();
  505. }
  506. // If we are interpolating, we send the data to the VisualServer
  507. // right away on a physics tick instead of waiting until a render frame.
  508. if (p_on_physics_tick && redraw) {
  509. _update_render_thread();
  510. }
  511. }
  512. void CPUParticles::_particles_process(float p_delta) {
  513. p_delta *= speed_scale;
  514. int pcount = particles.size();
  515. PoolVector<Particle>::Write w = particles.write();
  516. Particle *parray = w.ptr();
  517. float prev_time = time;
  518. time += p_delta;
  519. if (time > lifetime) {
  520. time = Math::fmod(time, lifetime);
  521. cycle++;
  522. if (one_shot && cycle > 0) {
  523. set_emitting(false);
  524. _change_notify();
  525. }
  526. }
  527. Transform emission_xform;
  528. Basis velocity_xform;
  529. if (!local_coords) {
  530. emission_xform = get_global_transform();
  531. velocity_xform = emission_xform.basis;
  532. }
  533. float system_phase = time / lifetime;
  534. real_t physics_tick_delta = 1.0 / Engine::get_singleton()->get_iterations_per_second();
  535. // Streaky particles can "prime" started particles by placing them back in time
  536. // from the current physics tick, to place them in the position they would have reached
  537. // had they been created in an infinite timestream (rather than at fixed iteration times).
  538. bool streaky = _streaky && _interpolated && fractional_delta;
  539. real_t streak_fraction = 1.0f;
  540. for (int i = 0; i < pcount; i++) {
  541. Particle &p = parray[i];
  542. if (!emitting && !p.active) {
  543. continue;
  544. }
  545. // For interpolation we need to keep a record of previous particles
  546. if (_interpolated) {
  547. p.copy_to(particles_prev[i]);
  548. }
  549. float local_delta = p_delta;
  550. // The phase is a ratio between 0 (birth) and 1 (end of life) for each particle.
  551. // While we use time in tests later on, for randomness we use the phase as done in the
  552. // original shader code, and we later multiply by lifetime to get the time.
  553. float restart_phase = float(i) / float(pcount);
  554. if (randomness_ratio > 0.0) {
  555. uint32_t seed = cycle;
  556. if (restart_phase >= system_phase) {
  557. seed -= uint32_t(1);
  558. }
  559. seed *= uint32_t(pcount);
  560. seed += uint32_t(i);
  561. float random = float(idhash(seed) % uint32_t(65536)) / 65536.0;
  562. restart_phase += randomness_ratio * random * 1.0 / float(pcount);
  563. }
  564. restart_phase *= (1.0 - explosiveness_ratio);
  565. float restart_time = restart_phase * lifetime;
  566. bool restart = false;
  567. if (time > prev_time) {
  568. // restart_time >= prev_time is used so particles emit in the first frame they are processed
  569. if (restart_time >= prev_time && restart_time < time) {
  570. restart = true;
  571. if (fractional_delta) {
  572. local_delta = time - restart_time;
  573. }
  574. }
  575. } else if (local_delta > 0.0) {
  576. if (restart_time >= prev_time) {
  577. restart = true;
  578. if (fractional_delta) {
  579. local_delta = lifetime - restart_time + time;
  580. }
  581. } else if (restart_time < time) {
  582. restart = true;
  583. if (fractional_delta) {
  584. local_delta = time - restart_time;
  585. }
  586. }
  587. }
  588. // Normal condition for a starting particle, allow priming.
  589. // Possibly test emitting flag here too, if profiling shows it helps.
  590. if (streaky && restart) {
  591. streak_fraction = local_delta / physics_tick_delta;
  592. streak_fraction = CLAMP(streak_fraction, 0.0f, 1.0f);
  593. }
  594. if (p.time * (1.0 - explosiveness_ratio) > p.lifetime) {
  595. restart = true;
  596. // Not absolutely sure on this, may be able to streak this case,
  597. // but turning off in case this is expected to be a similar timed
  598. // explosion.
  599. if (streaky) {
  600. streak_fraction = 1.0f;
  601. }
  602. }
  603. float tv = 0.0;
  604. if (restart) {
  605. if (!emitting) {
  606. p.active = false;
  607. continue;
  608. }
  609. p.active = true;
  610. /*float tex_linear_velocity = 0;
  611. if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  612. tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->interpolate(0);
  613. }*/
  614. float tex_angle = 0.0;
  615. if (curve_parameters[PARAM_ANGLE].is_valid()) {
  616. tex_angle = curve_parameters[PARAM_ANGLE]->interpolate(tv);
  617. }
  618. float tex_anim_offset = 0.0;
  619. if (curve_parameters[PARAM_ANGLE].is_valid()) {
  620. tex_anim_offset = curve_parameters[PARAM_ANGLE]->interpolate(tv);
  621. }
  622. p.seed = Math::rand();
  623. p.angle_rand = Math::randf();
  624. p.scale_rand = Math::randf();
  625. p.hue_rot_rand = Math::randf();
  626. p.anim_offset_rand = Math::randf();
  627. if (color_initial_ramp.is_valid()) {
  628. p.start_color_rand = color_initial_ramp->get_color_at_offset(Math::randf());
  629. } else {
  630. p.start_color_rand = Color(1, 1, 1, 1);
  631. }
  632. if (flags[FLAG_DISABLE_Z]) {
  633. float angle1_rad = Math::atan2(direction.y, direction.x) + (Math::randf() * 2.0 - 1.0) * Math_PI * spread / 180.0;
  634. Vector3 rot = Vector3(Math::cos(angle1_rad), Math::sin(angle1_rad), 0.0);
  635. p.velocity = rot * parameters[PARAM_INITIAL_LINEAR_VELOCITY] * Math::lerp(1.0f, float(Math::randf()), randomness[PARAM_INITIAL_LINEAR_VELOCITY]);
  636. } else {
  637. //initiate velocity spread in 3D
  638. float angle1_rad = (Math::randf() * 2.0 - 1.0) * Math_PI * spread / 180.0;
  639. float angle2_rad = (Math::randf() * 2.0 - 1.0) * (1.0 - flatness) * Math_PI * spread / 180.0;
  640. Vector3 direction_xz = Vector3(Math::sin(angle1_rad), 0, Math::cos(angle1_rad));
  641. Vector3 direction_yz = Vector3(0, Math::sin(angle2_rad), Math::cos(angle2_rad));
  642. Vector3 spread_direction = Vector3(direction_xz.x * direction_yz.z, direction_yz.y, direction_xz.z * direction_yz.z);
  643. Vector3 direction_nrm = direction;
  644. if (direction_nrm.length_squared() > 0) {
  645. direction_nrm.normalize();
  646. } else {
  647. direction_nrm = Vector3(0, 0, 1);
  648. }
  649. // rotate spread to direction
  650. Vector3 binormal = Vector3(0.0, 1.0, 0.0).cross(direction_nrm);
  651. if (binormal.length_squared() < 0.00000001) {
  652. // direction is parallel to Y. Choose Z as the binormal.
  653. binormal = Vector3(0.0, 0.0, 1.0);
  654. }
  655. binormal.normalize();
  656. Vector3 normal = binormal.cross(direction_nrm);
  657. spread_direction = binormal * spread_direction.x + normal * spread_direction.y + direction_nrm * spread_direction.z;
  658. p.velocity = spread_direction * parameters[PARAM_INITIAL_LINEAR_VELOCITY] * Math::lerp(1.0f, float(Math::randf()), randomness[PARAM_INITIAL_LINEAR_VELOCITY]);
  659. }
  660. float base_angle = (parameters[PARAM_ANGLE] + tex_angle) * Math::lerp(1.0f, p.angle_rand, randomness[PARAM_ANGLE]);
  661. p.custom[0] = Math::deg2rad(base_angle); //angle
  662. p.custom[1] = 0.0; //phase
  663. p.custom[2] = (parameters[PARAM_ANIM_OFFSET] + tex_anim_offset) * Math::lerp(1.0f, p.anim_offset_rand, randomness[PARAM_ANIM_OFFSET]); //animation offset (0-1)
  664. p.transform = Transform();
  665. p.time = 0;
  666. p.lifetime = lifetime * (1.0 - Math::randf() * lifetime_randomness);
  667. p.base_color = Color(1, 1, 1, 1);
  668. switch (emission_shape) {
  669. case EMISSION_SHAPE_POINT: {
  670. //do none
  671. } break;
  672. case EMISSION_SHAPE_SPHERE: {
  673. float s = 2.0 * Math::randf() - 1.0, t = 2.0 * Math_PI * Math::randf();
  674. float radius = emission_sphere_radius * Math::sqrt(1.0 - s * s);
  675. p.transform.origin = Vector3(radius * Math::cos(t), radius * Math::sin(t), emission_sphere_radius * s);
  676. } break;
  677. case EMISSION_SHAPE_BOX: {
  678. p.transform.origin = Vector3(Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0, Math::randf() * 2.0 - 1.0) * emission_box_extents;
  679. } break;
  680. case EMISSION_SHAPE_POINTS:
  681. case EMISSION_SHAPE_DIRECTED_POINTS: {
  682. int pc = emission_points.size();
  683. if (pc == 0) {
  684. break;
  685. }
  686. int random_idx = Math::rand() % pc;
  687. p.transform.origin = emission_points.get(random_idx);
  688. if (emission_shape == EMISSION_SHAPE_DIRECTED_POINTS && emission_normals.size() == pc) {
  689. if (flags[FLAG_DISABLE_Z]) {
  690. Vector3 normal = emission_normals.get(random_idx);
  691. Vector2 normal_2d(normal.x, normal.y);
  692. Transform2D m2;
  693. m2.set_axis(0, normal_2d);
  694. m2.set_axis(1, normal_2d.tangent());
  695. Vector2 velocity_2d(p.velocity.x, p.velocity.y);
  696. velocity_2d = m2.basis_xform(velocity_2d);
  697. p.velocity.x = velocity_2d.x;
  698. p.velocity.y = velocity_2d.y;
  699. } else {
  700. Vector3 normal = emission_normals.get(random_idx);
  701. Vector3 v0 = Math::abs(normal.z) < 0.999 ? Vector3(0.0, 0.0, 1.0) : Vector3(0, 1.0, 0.0);
  702. Vector3 tangent = v0.cross(normal).normalized();
  703. Vector3 bitangent = tangent.cross(normal).normalized();
  704. Basis m3;
  705. m3.set_axis(0, tangent);
  706. m3.set_axis(1, bitangent);
  707. m3.set_axis(2, normal);
  708. p.velocity = m3.xform(p.velocity);
  709. }
  710. }
  711. if (emission_colors.size() == pc) {
  712. p.base_color = emission_colors.get(random_idx);
  713. }
  714. } break;
  715. case EMISSION_SHAPE_RING: {
  716. float ring_random_angle = Math::randf() * 2.0 * Math_PI;
  717. float ring_random_radius = Math::randf() * (emission_ring_radius - emission_ring_inner_radius) + emission_ring_inner_radius;
  718. Vector3 axis = emission_ring_axis.normalized();
  719. Vector3 ortho_axis = Vector3();
  720. if (axis == Vector3(1.0, 0.0, 0.0)) {
  721. ortho_axis = Vector3(0.0, 1.0, 0.0).cross(axis);
  722. } else {
  723. ortho_axis = Vector3(1.0, 0.0, 0.0).cross(axis);
  724. }
  725. ortho_axis = ortho_axis.normalized();
  726. ortho_axis.rotate(axis, ring_random_angle);
  727. ortho_axis = ortho_axis.normalized();
  728. p.transform.origin = ortho_axis * ring_random_radius + (Math::randf() * emission_ring_height - emission_ring_height / 2.0) * axis;
  729. }
  730. case EMISSION_SHAPE_MAX: { // Max value for validity check.
  731. break;
  732. }
  733. }
  734. // We could possibly attempt streaking with local_coords as well, but NYI
  735. if (!local_coords) {
  736. // Apply streaking interpolation of start positions between ticks
  737. if (streaky) {
  738. emission_xform = _get_global_transform_interpolated(streak_fraction);
  739. velocity_xform = emission_xform.basis;
  740. p.velocity = velocity_xform.xform(p.velocity);
  741. // prime the particle by moving "backward" in time
  742. real_t adjusted_delta = (1.0f - streak_fraction) * physics_tick_delta;
  743. _particle_process(p, emission_xform, adjusted_delta, tv);
  744. } else {
  745. p.velocity = velocity_xform.xform(p.velocity);
  746. }
  747. p.transform = emission_xform * p.transform;
  748. }
  749. if (flags[FLAG_DISABLE_Z]) {
  750. p.velocity.z = 0.0;
  751. p.transform.origin.z = 0.0;
  752. }
  753. } else if (!p.active) {
  754. continue;
  755. } else if (p.time > p.lifetime) {
  756. p.active = false;
  757. tv = 1.0;
  758. } else {
  759. _particle_process(p, emission_xform, local_delta, tv);
  760. }
  761. //apply color
  762. //apply hue rotation
  763. float tex_scale = 1.0;
  764. if (curve_parameters[PARAM_SCALE].is_valid()) {
  765. tex_scale = curve_parameters[PARAM_SCALE]->interpolate(tv);
  766. }
  767. float tex_hue_variation = 0.0;
  768. if (curve_parameters[PARAM_HUE_VARIATION].is_valid()) {
  769. tex_hue_variation = curve_parameters[PARAM_HUE_VARIATION]->interpolate(tv);
  770. }
  771. float hue_rot_angle = (parameters[PARAM_HUE_VARIATION] + tex_hue_variation) * Math_PI * 2.0 * Math::lerp(1.0f, p.hue_rot_rand * 2.0f - 1.0f, randomness[PARAM_HUE_VARIATION]);
  772. float hue_rot_c = Math::cos(hue_rot_angle);
  773. float hue_rot_s = Math::sin(hue_rot_angle);
  774. Basis hue_rot_mat;
  775. {
  776. Basis mat1(0.299, 0.587, 0.114, 0.299, 0.587, 0.114, 0.299, 0.587, 0.114);
  777. Basis mat2(0.701, -0.587, -0.114, -0.299, 0.413, -0.114, -0.300, -0.588, 0.886);
  778. Basis mat3(0.168, 0.330, -0.497, -0.328, 0.035, 0.292, 1.250, -1.050, -0.203);
  779. for (int j = 0; j < 3; j++) {
  780. hue_rot_mat[j] = mat1[j] + mat2[j] * hue_rot_c + mat3[j] * hue_rot_s;
  781. }
  782. }
  783. if (color_ramp.is_valid()) {
  784. p.color = color_ramp->get_color_at_offset(tv) * color;
  785. } else {
  786. p.color = color;
  787. }
  788. Vector3 color_rgb = hue_rot_mat.xform_inv(Vector3(p.color.r, p.color.g, p.color.b));
  789. p.color.r = color_rgb.x;
  790. p.color.g = color_rgb.y;
  791. p.color.b = color_rgb.z;
  792. p.color *= p.base_color * p.start_color_rand;
  793. if (flags[FLAG_DISABLE_Z]) {
  794. if (flags[FLAG_ALIGN_Y_TO_VELOCITY]) {
  795. if (p.velocity.length() > 0.0) {
  796. p.transform.basis.set_axis(1, p.velocity.normalized());
  797. } else {
  798. p.transform.basis.set_axis(1, p.transform.basis.get_axis(1));
  799. }
  800. p.transform.basis.set_axis(0, p.transform.basis.get_axis(1).cross(p.transform.basis.get_axis(2)).normalized());
  801. p.transform.basis.set_axis(2, Vector3(0, 0, 1));
  802. } else {
  803. p.transform.basis.set_axis(0, Vector3(Math::cos(p.custom[0]), -Math::sin(p.custom[0]), 0.0));
  804. p.transform.basis.set_axis(1, Vector3(Math::sin(p.custom[0]), Math::cos(p.custom[0]), 0.0));
  805. p.transform.basis.set_axis(2, Vector3(0, 0, 1));
  806. }
  807. } else {
  808. //orient particle Y towards velocity
  809. if (flags[FLAG_ALIGN_Y_TO_VELOCITY]) {
  810. if (p.velocity.length() > 0.0) {
  811. p.transform.basis.set_axis(1, p.velocity.normalized());
  812. } else {
  813. p.transform.basis.set_axis(1, p.transform.basis.get_axis(1).normalized());
  814. }
  815. if (p.transform.basis.get_axis(1) == p.transform.basis.get_axis(0)) {
  816. p.transform.basis.set_axis(0, p.transform.basis.get_axis(1).cross(p.transform.basis.get_axis(2)).normalized());
  817. p.transform.basis.set_axis(2, p.transform.basis.get_axis(0).cross(p.transform.basis.get_axis(1)).normalized());
  818. } else {
  819. p.transform.basis.set_axis(2, p.transform.basis.get_axis(0).cross(p.transform.basis.get_axis(1)).normalized());
  820. p.transform.basis.set_axis(0, p.transform.basis.get_axis(1).cross(p.transform.basis.get_axis(2)).normalized());
  821. }
  822. } else {
  823. p.transform.basis.orthonormalize();
  824. }
  825. //turn particle by rotation in Y
  826. if (flags[FLAG_ROTATE_Y]) {
  827. Basis rot_y(Vector3(0, 1, 0), p.custom[0]);
  828. p.transform.basis = p.transform.basis * rot_y;
  829. }
  830. }
  831. //scale by scale
  832. float base_scale = tex_scale * Math::lerp(parameters[PARAM_SCALE], 1.0f, p.scale_rand * randomness[PARAM_SCALE]);
  833. // Prevent zero scale (which can cause rendering issues).
  834. base_scale = SGN(base_scale) * MAX(Math::abs(base_scale), 0.000001);
  835. p.transform.basis.scale(Vector3(1, 1, 1) * base_scale);
  836. if (flags[FLAG_DISABLE_Z]) {
  837. p.velocity.z = 0.0;
  838. p.transform.origin.z = 0.0;
  839. }
  840. p.transform.origin += p.velocity * local_delta;
  841. // Teleport if starting a new particle, so
  842. // we don't get a streak from the old position
  843. // to this new start.
  844. if (restart && _interpolated) {
  845. p.copy_to(particles_prev[i]);
  846. }
  847. }
  848. }
  849. void CPUParticles::_particle_process(Particle &r_p, const Transform &p_emission_xform, float p_local_delta, float &r_tv) {
  850. uint32_t alt_seed = r_p.seed;
  851. r_p.time += p_local_delta;
  852. r_p.custom[1] = r_p.time / lifetime;
  853. r_tv = r_p.time / r_p.lifetime;
  854. float tex_linear_velocity = 0.0;
  855. if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  856. tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->interpolate(r_tv);
  857. }
  858. float tex_orbit_velocity = 0.0;
  859. if (flags[FLAG_DISABLE_Z]) {
  860. if (curve_parameters[PARAM_ORBIT_VELOCITY].is_valid()) {
  861. tex_orbit_velocity = curve_parameters[PARAM_ORBIT_VELOCITY]->interpolate(r_tv);
  862. }
  863. }
  864. float tex_angular_velocity = 0.0;
  865. if (curve_parameters[PARAM_ANGULAR_VELOCITY].is_valid()) {
  866. tex_angular_velocity = curve_parameters[PARAM_ANGULAR_VELOCITY]->interpolate(r_tv);
  867. }
  868. float tex_linear_accel = 0.0;
  869. if (curve_parameters[PARAM_LINEAR_ACCEL].is_valid()) {
  870. tex_linear_accel = curve_parameters[PARAM_LINEAR_ACCEL]->interpolate(r_tv);
  871. }
  872. float tex_tangential_accel = 0.0;
  873. if (curve_parameters[PARAM_TANGENTIAL_ACCEL].is_valid()) {
  874. tex_tangential_accel = curve_parameters[PARAM_TANGENTIAL_ACCEL]->interpolate(r_tv);
  875. }
  876. float tex_radial_accel = 0.0;
  877. if (curve_parameters[PARAM_RADIAL_ACCEL].is_valid()) {
  878. tex_radial_accel = curve_parameters[PARAM_RADIAL_ACCEL]->interpolate(r_tv);
  879. }
  880. float tex_damping = 0.0;
  881. if (curve_parameters[PARAM_DAMPING].is_valid()) {
  882. tex_damping = curve_parameters[PARAM_DAMPING]->interpolate(r_tv);
  883. }
  884. float tex_angle = 0.0;
  885. if (curve_parameters[PARAM_ANGLE].is_valid()) {
  886. tex_angle = curve_parameters[PARAM_ANGLE]->interpolate(r_tv);
  887. }
  888. float tex_anim_speed = 0.0;
  889. if (curve_parameters[PARAM_ANIM_SPEED].is_valid()) {
  890. tex_anim_speed = curve_parameters[PARAM_ANIM_SPEED]->interpolate(r_tv);
  891. }
  892. float tex_anim_offset = 0.0;
  893. if (curve_parameters[PARAM_ANIM_OFFSET].is_valid()) {
  894. tex_anim_offset = curve_parameters[PARAM_ANIM_OFFSET]->interpolate(r_tv);
  895. }
  896. Vector3 force = gravity;
  897. Vector3 position = r_p.transform.origin;
  898. if (flags[FLAG_DISABLE_Z]) {
  899. position.z = 0.0;
  900. }
  901. //apply linear acceleration
  902. force += r_p.velocity.length() > 0.0 ? r_p.velocity.normalized() * (parameters[PARAM_LINEAR_ACCEL] + tex_linear_accel) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_LINEAR_ACCEL]) : Vector3();
  903. //apply radial acceleration
  904. Vector3 org = p_emission_xform.origin;
  905. Vector3 diff = position - org;
  906. force += diff.length() > 0.0 ? diff.normalized() * (parameters[PARAM_RADIAL_ACCEL] + tex_radial_accel) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_RADIAL_ACCEL]) : Vector3();
  907. //apply tangential acceleration;
  908. if (flags[FLAG_DISABLE_Z]) {
  909. Vector2 yx = Vector2(diff.y, diff.x);
  910. Vector2 yx2 = (yx * Vector2(-1.0, 1.0)).normalized();
  911. force += yx.length() > 0.0 ? Vector3(yx2.x, yx2.y, 0.0) * ((parameters[PARAM_TANGENTIAL_ACCEL] + tex_tangential_accel) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_TANGENTIAL_ACCEL])) : Vector3();
  912. } else {
  913. Vector3 crossDiff = diff.normalized().cross(gravity.normalized());
  914. force += crossDiff.length() > 0.0 ? crossDiff.normalized() * ((parameters[PARAM_TANGENTIAL_ACCEL] + tex_tangential_accel) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_TANGENTIAL_ACCEL])) : Vector3();
  915. }
  916. //apply attractor forces
  917. r_p.velocity += force * p_local_delta;
  918. //orbit velocity
  919. if (flags[FLAG_DISABLE_Z]) {
  920. float orbit_amount = (parameters[PARAM_ORBIT_VELOCITY] + tex_orbit_velocity) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_ORBIT_VELOCITY]);
  921. if (orbit_amount != 0.0) {
  922. float ang = orbit_amount * p_local_delta * Math_PI * 2.0;
  923. // Not sure why the ParticlesMaterial code uses a clockwise rotation matrix,
  924. // but we use -ang here to reproduce its behavior.
  925. Transform2D rot = Transform2D(-ang, Vector2());
  926. Vector2 rotv = rot.basis_xform(Vector2(diff.x, diff.y));
  927. r_p.transform.origin -= Vector3(diff.x, diff.y, 0);
  928. r_p.transform.origin += Vector3(rotv.x, rotv.y, 0);
  929. }
  930. }
  931. if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  932. r_p.velocity = r_p.velocity.normalized() * tex_linear_velocity;
  933. }
  934. if (parameters[PARAM_DAMPING] + tex_damping > 0.0) {
  935. float v = r_p.velocity.length();
  936. float damp = (parameters[PARAM_DAMPING] + tex_damping) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_DAMPING]);
  937. v -= damp * p_local_delta;
  938. if (v < 0.0) {
  939. r_p.velocity = Vector3();
  940. } else {
  941. r_p.velocity = r_p.velocity.normalized() * v;
  942. }
  943. }
  944. float base_angle = (parameters[PARAM_ANGLE] + tex_angle) * Math::lerp(1.0f, r_p.angle_rand, randomness[PARAM_ANGLE]);
  945. base_angle += r_p.custom[1] * lifetime * (parameters[PARAM_ANGULAR_VELOCITY] + tex_angular_velocity) * Math::lerp(1.0f, rand_from_seed(alt_seed) * 2.0f - 1.0f, randomness[PARAM_ANGULAR_VELOCITY]);
  946. r_p.custom[0] = Math::deg2rad(base_angle); //angle
  947. r_p.custom[2] = (parameters[PARAM_ANIM_OFFSET] + tex_anim_offset) * Math::lerp(1.0f, r_p.anim_offset_rand, randomness[PARAM_ANIM_OFFSET]) + r_tv * (parameters[PARAM_ANIM_SPEED] + tex_anim_speed) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_ANIM_SPEED]); //angle
  948. }
  949. void CPUParticles::_update_particle_data_buffer() {
  950. update_mutex.lock();
  951. {
  952. int pc = particles.size();
  953. PoolVector<int>::Write ow;
  954. int *order = nullptr;
  955. PoolVector<float>::Write w = particle_data.write();
  956. PoolVector<Particle>::Read r = particles.read();
  957. float *ptr = w.ptr();
  958. PoolVector<float>::Write w_prev;
  959. float *ptr_prev = nullptr;
  960. if (_interpolated) {
  961. w_prev = particle_data_prev.write();
  962. ptr_prev = w_prev.ptr();
  963. }
  964. if (draw_order != DRAW_ORDER_INDEX) {
  965. ow = particle_order.write();
  966. order = ow.ptr();
  967. for (int i = 0; i < pc; i++) {
  968. order[i] = i;
  969. }
  970. if (draw_order == DRAW_ORDER_LIFETIME) {
  971. SortArray<int, SortLifetime> sorter;
  972. sorter.compare.particles = r.ptr();
  973. sorter.sort(order, pc);
  974. } else if (draw_order == DRAW_ORDER_VIEW_DEPTH) {
  975. ERR_FAIL_NULL(get_viewport());
  976. Camera *c = get_viewport()->get_camera();
  977. if (c) {
  978. Vector3 dir = c->get_global_transform().basis.get_axis(2); //far away to close
  979. // now if local_coords is not set, the particles are in global coords
  980. // so should be sorted according to the camera direction
  981. // will look different from Particles in editor as this is based on the camera in the scenetree
  982. // and not the editor camera
  983. dir = dir.normalized();
  984. SortArray<int, SortAxis> sorter;
  985. sorter.compare.particles = r.ptr();
  986. sorter.compare.axis = dir;
  987. sorter.sort(order, pc);
  988. }
  989. }
  990. }
  991. if (_interpolated) {
  992. for (int i = 0; i < pc; i++) {
  993. int idx = order ? order[i] : i;
  994. _fill_particle_data(r[idx], ptr, r[idx].active);
  995. ptr += 17;
  996. _fill_particle_data(particles_prev[idx], ptr_prev, r[idx].active);
  997. ptr_prev += 17;
  998. }
  999. } else {
  1000. for (int i = 0; i < pc; i++) {
  1001. int idx = order ? order[i] : i;
  1002. _fill_particle_data(r[idx], ptr, r[idx].active);
  1003. ptr += 17;
  1004. }
  1005. }
  1006. can_update.set();
  1007. }
  1008. update_mutex.unlock();
  1009. }
  1010. void CPUParticles::_refresh_interpolation_state() {
  1011. if (!is_inside_tree()) {
  1012. return;
  1013. }
  1014. bool interpolated = is_physics_interpolated_and_enabled();
  1015. if (_interpolated == interpolated) {
  1016. return;
  1017. }
  1018. bool curr_redraw = redraw;
  1019. // Remove all connections
  1020. // This isn't super efficient, but should only happen rarely.
  1021. _set_redraw(false);
  1022. _interpolated = interpolated;
  1023. if (_interpolated) {
  1024. set_process_internal(false);
  1025. set_physics_process_internal(emitting);
  1026. } else {
  1027. set_physics_process_internal(false);
  1028. set_process_internal(emitting);
  1029. }
  1030. // re-establish all connections
  1031. _set_redraw(curr_redraw);
  1032. }
  1033. void CPUParticles::_set_redraw(bool p_redraw) {
  1034. if (redraw == p_redraw) {
  1035. return;
  1036. }
  1037. redraw = p_redraw;
  1038. update_mutex.lock();
  1039. if (!_interpolated) {
  1040. if (redraw) {
  1041. VS::get_singleton()->connect("frame_pre_draw", this, "_update_render_thread");
  1042. } else {
  1043. if (VS::get_singleton()->is_connected("frame_pre_draw", this, "_update_render_thread")) {
  1044. VS::get_singleton()->disconnect("frame_pre_draw", this, "_update_render_thread");
  1045. }
  1046. }
  1047. }
  1048. if (redraw) {
  1049. VS::get_singleton()->instance_geometry_set_flag(get_instance(), VS::INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE, true);
  1050. VS::get_singleton()->multimesh_set_visible_instances(multimesh, -1);
  1051. } else {
  1052. VS::get_singleton()->instance_geometry_set_flag(get_instance(), VS::INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE, false);
  1053. VS::get_singleton()->multimesh_set_visible_instances(multimesh, 0);
  1054. }
  1055. update_mutex.unlock();
  1056. }
  1057. void CPUParticles::_update_render_thread() {
  1058. if (OS::get_singleton()->is_update_pending(true)) {
  1059. update_mutex.lock();
  1060. if (can_update.is_set()) {
  1061. if (_interpolated) {
  1062. VS::get_singleton()->multimesh_set_as_bulk_array_interpolated(multimesh, particle_data, particle_data_prev);
  1063. } else {
  1064. VS::get_singleton()->multimesh_set_as_bulk_array(multimesh, particle_data);
  1065. }
  1066. can_update.clear(); //wait for next time
  1067. }
  1068. update_mutex.unlock();
  1069. }
  1070. }
  1071. void CPUParticles::_notification(int p_what) {
  1072. if (p_what == NOTIFICATION_ENTER_TREE) {
  1073. set_process_internal(emitting);
  1074. // first update before rendering to avoid one frame delay after emitting starts
  1075. if (emitting && (time == 0) && !_interpolated) {
  1076. _update_internal(false);
  1077. }
  1078. }
  1079. if (p_what == NOTIFICATION_EXIT_TREE) {
  1080. _set_redraw(false);
  1081. }
  1082. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  1083. // first update before rendering to avoid one frame delay after emitting starts
  1084. if (emitting && (time == 0) && !_interpolated) {
  1085. _update_internal(false);
  1086. }
  1087. }
  1088. if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
  1089. _update_internal(false);
  1090. }
  1091. if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  1092. _update_internal(true);
  1093. }
  1094. }
  1095. void CPUParticles::convert_from_particles(Node *p_particles) {
  1096. Particles *particles = Object::cast_to<Particles>(p_particles);
  1097. ERR_FAIL_COND_MSG(!particles, "Only Particles nodes can be converted to CPUParticles.");
  1098. set_emitting(particles->is_emitting());
  1099. set_amount(particles->get_amount());
  1100. set_lifetime(particles->get_lifetime());
  1101. set_one_shot(particles->get_one_shot());
  1102. set_pre_process_time(particles->get_pre_process_time());
  1103. set_explosiveness_ratio(particles->get_explosiveness_ratio());
  1104. set_randomness_ratio(particles->get_randomness_ratio());
  1105. set_use_local_coordinates(particles->get_use_local_coordinates());
  1106. set_fixed_fps(particles->get_fixed_fps());
  1107. set_fractional_delta(particles->get_fractional_delta());
  1108. set_speed_scale(particles->get_speed_scale());
  1109. set_draw_order(DrawOrder(particles->get_draw_order()));
  1110. set_mesh(particles->get_draw_pass_mesh(0));
  1111. Ref<ParticlesMaterial> material = particles->get_process_material();
  1112. if (material.is_null()) {
  1113. return;
  1114. }
  1115. set_direction(material->get_direction());
  1116. set_spread(material->get_spread());
  1117. set_flatness(material->get_flatness());
  1118. set_color(material->get_color());
  1119. Ref<GradientTexture> gt = material->get_color_ramp();
  1120. if (gt.is_valid()) {
  1121. set_color_ramp(gt->get_gradient());
  1122. }
  1123. Ref<GradientTexture> gti = material->get_color_initial_ramp();
  1124. if (gti.is_valid()) {
  1125. set_color_initial_ramp(gti->get_gradient());
  1126. }
  1127. set_particle_flag(FLAG_ALIGN_Y_TO_VELOCITY, material->get_flag(ParticlesMaterial::FLAG_ALIGN_Y_TO_VELOCITY));
  1128. set_particle_flag(FLAG_ROTATE_Y, material->get_flag(ParticlesMaterial::FLAG_ROTATE_Y));
  1129. set_particle_flag(FLAG_DISABLE_Z, material->get_flag(ParticlesMaterial::FLAG_DISABLE_Z));
  1130. set_emission_shape(EmissionShape(material->get_emission_shape()));
  1131. set_emission_sphere_radius(material->get_emission_sphere_radius());
  1132. set_emission_box_extents(material->get_emission_box_extents());
  1133. set_emission_ring_height(material->get_emission_ring_height());
  1134. set_emission_ring_inner_radius(material->get_emission_ring_inner_radius());
  1135. set_emission_ring_radius(material->get_emission_ring_radius());
  1136. set_emission_ring_axis(material->get_emission_ring_axis());
  1137. set_gravity(material->get_gravity());
  1138. set_lifetime_randomness(material->get_lifetime_randomness());
  1139. #define CONVERT_PARAM(m_param) \
  1140. set_param(m_param, material->get_param(ParticlesMaterial::m_param)); \
  1141. { \
  1142. Ref<CurveTexture> ctex = material->get_param_texture(ParticlesMaterial::m_param); \
  1143. if (ctex.is_valid()) \
  1144. set_param_curve(m_param, ctex->get_curve()); \
  1145. } \
  1146. set_param_randomness(m_param, material->get_param_randomness(ParticlesMaterial::m_param));
  1147. CONVERT_PARAM(PARAM_INITIAL_LINEAR_VELOCITY);
  1148. CONVERT_PARAM(PARAM_ANGULAR_VELOCITY);
  1149. CONVERT_PARAM(PARAM_ORBIT_VELOCITY);
  1150. CONVERT_PARAM(PARAM_LINEAR_ACCEL);
  1151. CONVERT_PARAM(PARAM_RADIAL_ACCEL);
  1152. CONVERT_PARAM(PARAM_TANGENTIAL_ACCEL);
  1153. CONVERT_PARAM(PARAM_DAMPING);
  1154. CONVERT_PARAM(PARAM_ANGLE);
  1155. CONVERT_PARAM(PARAM_SCALE);
  1156. CONVERT_PARAM(PARAM_HUE_VARIATION);
  1157. CONVERT_PARAM(PARAM_ANIM_SPEED);
  1158. CONVERT_PARAM(PARAM_ANIM_OFFSET);
  1159. #undef CONVERT_PARAM
  1160. }
  1161. void CPUParticles::_bind_methods() {
  1162. ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &CPUParticles::set_emitting);
  1163. ClassDB::bind_method(D_METHOD("set_amount", "amount"), &CPUParticles::set_amount);
  1164. ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &CPUParticles::set_lifetime);
  1165. ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &CPUParticles::set_one_shot);
  1166. ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &CPUParticles::set_pre_process_time);
  1167. ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &CPUParticles::set_explosiveness_ratio);
  1168. ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &CPUParticles::set_randomness_ratio);
  1169. ClassDB::bind_method(D_METHOD("set_lifetime_randomness", "random"), &CPUParticles::set_lifetime_randomness);
  1170. ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &CPUParticles::set_use_local_coordinates);
  1171. ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &CPUParticles::set_fixed_fps);
  1172. ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &CPUParticles::set_fractional_delta);
  1173. ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &CPUParticles::set_speed_scale);
  1174. ClassDB::bind_method(D_METHOD("is_emitting"), &CPUParticles::is_emitting);
  1175. ClassDB::bind_method(D_METHOD("get_amount"), &CPUParticles::get_amount);
  1176. ClassDB::bind_method(D_METHOD("get_lifetime"), &CPUParticles::get_lifetime);
  1177. ClassDB::bind_method(D_METHOD("get_one_shot"), &CPUParticles::get_one_shot);
  1178. ClassDB::bind_method(D_METHOD("get_pre_process_time"), &CPUParticles::get_pre_process_time);
  1179. ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &CPUParticles::get_explosiveness_ratio);
  1180. ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &CPUParticles::get_randomness_ratio);
  1181. ClassDB::bind_method(D_METHOD("get_lifetime_randomness"), &CPUParticles::get_lifetime_randomness);
  1182. ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &CPUParticles::get_use_local_coordinates);
  1183. ClassDB::bind_method(D_METHOD("get_fixed_fps"), &CPUParticles::get_fixed_fps);
  1184. ClassDB::bind_method(D_METHOD("get_fractional_delta"), &CPUParticles::get_fractional_delta);
  1185. ClassDB::bind_method(D_METHOD("get_speed_scale"), &CPUParticles::get_speed_scale);
  1186. ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &CPUParticles::set_draw_order);
  1187. ClassDB::bind_method(D_METHOD("get_draw_order"), &CPUParticles::get_draw_order);
  1188. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &CPUParticles::set_mesh);
  1189. ClassDB::bind_method(D_METHOD("get_mesh"), &CPUParticles::get_mesh);
  1190. ClassDB::bind_method(D_METHOD("restart"), &CPUParticles::restart);
  1191. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting"), "set_emitting", "is_emitting");
  1192. ADD_PROPERTY(PropertyInfo(Variant::INT, "amount", PROPERTY_HINT_EXP_RANGE, "1,1000000,1"), "set_amount", "get_amount");
  1193. ADD_GROUP("Time", "");
  1194. ADD_PROPERTY(PropertyInfo(Variant::REAL, "lifetime", PROPERTY_HINT_EXP_RANGE, "0.01,600.0,0.01,or_greater"), "set_lifetime", "get_lifetime");
  1195. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
  1196. ADD_PROPERTY(PropertyInfo(Variant::REAL, "preprocess", PROPERTY_HINT_EXP_RANGE, "0.00,600.0,0.01"), "set_pre_process_time", "get_pre_process_time");
  1197. ADD_PROPERTY(PropertyInfo(Variant::REAL, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
  1198. ADD_PROPERTY(PropertyInfo(Variant::REAL, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
  1199. ADD_PROPERTY(PropertyInfo(Variant::REAL, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
  1200. ADD_PROPERTY(PropertyInfo(Variant::REAL, "lifetime_randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_lifetime_randomness", "get_lifetime_randomness");
  1201. ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1"), "set_fixed_fps", "get_fixed_fps");
  1202. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
  1203. ADD_GROUP("Drawing", "");
  1204. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
  1205. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime,View Depth"), "set_draw_order", "get_draw_order");
  1206. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  1207. BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
  1208. BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
  1209. BIND_ENUM_CONSTANT(DRAW_ORDER_VIEW_DEPTH);
  1210. ////////////////////////////////
  1211. ClassDB::bind_method(D_METHOD("set_direction", "direction"), &CPUParticles::set_direction);
  1212. ClassDB::bind_method(D_METHOD("get_direction"), &CPUParticles::get_direction);
  1213. ClassDB::bind_method(D_METHOD("set_spread", "degrees"), &CPUParticles::set_spread);
  1214. ClassDB::bind_method(D_METHOD("get_spread"), &CPUParticles::get_spread);
  1215. ClassDB::bind_method(D_METHOD("set_flatness", "amount"), &CPUParticles::set_flatness);
  1216. ClassDB::bind_method(D_METHOD("get_flatness"), &CPUParticles::get_flatness);
  1217. ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &CPUParticles::set_param);
  1218. ClassDB::bind_method(D_METHOD("get_param", "param"), &CPUParticles::get_param);
  1219. ClassDB::bind_method(D_METHOD("set_param_randomness", "param", "randomness"), &CPUParticles::set_param_randomness);
  1220. ClassDB::bind_method(D_METHOD("get_param_randomness", "param"), &CPUParticles::get_param_randomness);
  1221. ClassDB::bind_method(D_METHOD("set_param_curve", "param", "curve"), &CPUParticles::set_param_curve);
  1222. ClassDB::bind_method(D_METHOD("get_param_curve", "param"), &CPUParticles::get_param_curve);
  1223. ClassDB::bind_method(D_METHOD("set_color", "color"), &CPUParticles::set_color);
  1224. ClassDB::bind_method(D_METHOD("get_color"), &CPUParticles::get_color);
  1225. ClassDB::bind_method(D_METHOD("set_color_ramp", "ramp"), &CPUParticles::set_color_ramp);
  1226. ClassDB::bind_method(D_METHOD("get_color_ramp"), &CPUParticles::get_color_ramp);
  1227. ClassDB::bind_method(D_METHOD("set_color_initial_ramp", "ramp"), &CPUParticles::set_color_initial_ramp);
  1228. ClassDB::bind_method(D_METHOD("get_color_initial_ramp"), &CPUParticles::get_color_initial_ramp);
  1229. ClassDB::bind_method(D_METHOD("set_particle_flag", "flag", "enable"), &CPUParticles::set_particle_flag);
  1230. ClassDB::bind_method(D_METHOD("get_particle_flag", "flag"), &CPUParticles::get_particle_flag);
  1231. ClassDB::bind_method(D_METHOD("set_emission_shape", "shape"), &CPUParticles::set_emission_shape);
  1232. ClassDB::bind_method(D_METHOD("get_emission_shape"), &CPUParticles::get_emission_shape);
  1233. ClassDB::bind_method(D_METHOD("set_emission_sphere_radius", "radius"), &CPUParticles::set_emission_sphere_radius);
  1234. ClassDB::bind_method(D_METHOD("get_emission_sphere_radius"), &CPUParticles::get_emission_sphere_radius);
  1235. ClassDB::bind_method(D_METHOD("set_emission_box_extents", "extents"), &CPUParticles::set_emission_box_extents);
  1236. ClassDB::bind_method(D_METHOD("get_emission_box_extents"), &CPUParticles::get_emission_box_extents);
  1237. ClassDB::bind_method(D_METHOD("set_emission_points", "array"), &CPUParticles::set_emission_points);
  1238. ClassDB::bind_method(D_METHOD("get_emission_points"), &CPUParticles::get_emission_points);
  1239. ClassDB::bind_method(D_METHOD("set_emission_normals", "array"), &CPUParticles::set_emission_normals);
  1240. ClassDB::bind_method(D_METHOD("get_emission_normals"), &CPUParticles::get_emission_normals);
  1241. ClassDB::bind_method(D_METHOD("set_emission_colors", "array"), &CPUParticles::set_emission_colors);
  1242. ClassDB::bind_method(D_METHOD("get_emission_colors"), &CPUParticles::get_emission_colors);
  1243. ClassDB::bind_method(D_METHOD("set_emission_ring_radius", "radius"), &CPUParticles::set_emission_ring_radius);
  1244. ClassDB::bind_method(D_METHOD("get_emission_ring_radius"), &CPUParticles::get_emission_ring_radius);
  1245. ClassDB::bind_method(D_METHOD("set_emission_ring_inner_radius", "offset"), &CPUParticles::set_emission_ring_inner_radius);
  1246. ClassDB::bind_method(D_METHOD("get_emission_ring_inner_radius"), &CPUParticles::get_emission_ring_inner_radius);
  1247. ClassDB::bind_method(D_METHOD("set_emission_ring_height", "height"), &CPUParticles::set_emission_ring_height);
  1248. ClassDB::bind_method(D_METHOD("get_emission_ring_height"), &CPUParticles::get_emission_ring_height);
  1249. ClassDB::bind_method(D_METHOD("set_emission_ring_axis", "axis"), &CPUParticles::set_emission_ring_axis);
  1250. ClassDB::bind_method(D_METHOD("get_emission_ring_axis"), &CPUParticles::get_emission_ring_axis);
  1251. ClassDB::bind_method(D_METHOD("get_gravity"), &CPUParticles::get_gravity);
  1252. ClassDB::bind_method(D_METHOD("set_gravity", "accel_vec"), &CPUParticles::set_gravity);
  1253. ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &CPUParticles::convert_from_particles);
  1254. ClassDB::bind_method(D_METHOD("_update_render_thread"), &CPUParticles::_update_render_thread);
  1255. ADD_GROUP("Emission Shape", "emission_");
  1256. ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_shape", PROPERTY_HINT_ENUM, "Point,Sphere,Box,Points,Directed Points,Ring", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_emission_shape", "get_emission_shape");
  1257. ADD_PROPERTY(PropertyInfo(Variant::REAL, "emission_sphere_radius", PROPERTY_HINT_RANGE, "0.01,128,0.01,or_greater"), "set_emission_sphere_radius", "get_emission_sphere_radius");
  1258. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "emission_box_extents"), "set_emission_box_extents", "get_emission_box_extents");
  1259. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR3_ARRAY, "emission_points"), "set_emission_points", "get_emission_points");
  1260. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR3_ARRAY, "emission_normals"), "set_emission_normals", "get_emission_normals");
  1261. ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "emission_colors"), "set_emission_colors", "get_emission_colors");
  1262. ADD_PROPERTY(PropertyInfo(Variant::REAL, "emission_ring_radius", PROPERTY_HINT_RANGE, "0.01,1000,0.01,or_greater"), "set_emission_ring_radius", "get_emission_ring_radius");
  1263. ADD_PROPERTY(PropertyInfo(Variant::REAL, "emission_ring_inner_radius", PROPERTY_HINT_RANGE, "0.0,1000,0.01,or_greater"), "set_emission_ring_inner_radius", "get_emission_ring_inner_radius");
  1264. ADD_PROPERTY(PropertyInfo(Variant::REAL, "emission_ring_height", PROPERTY_HINT_RANGE, "0.0,100,0.01,or_greater"), "set_emission_ring_height", "get_emission_ring_height");
  1265. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "emission_ring_axis"), "set_emission_ring_axis", "get_emission_ring_axis");
  1266. ADD_GROUP("Flags", "flag_");
  1267. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flag_align_y"), "set_particle_flag", "get_particle_flag", FLAG_ALIGN_Y_TO_VELOCITY);
  1268. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flag_rotate_y"), "set_particle_flag", "get_particle_flag", FLAG_ROTATE_Y);
  1269. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flag_disable_z"), "set_particle_flag", "get_particle_flag", FLAG_DISABLE_Z);
  1270. ADD_GROUP("Direction", "");
  1271. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "direction"), "set_direction", "get_direction");
  1272. ADD_PROPERTY(PropertyInfo(Variant::REAL, "spread", PROPERTY_HINT_RANGE, "0,180,0.01"), "set_spread", "get_spread");
  1273. ADD_PROPERTY(PropertyInfo(Variant::REAL, "flatness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_flatness", "get_flatness");
  1274. ADD_GROUP("Gravity", "");
  1275. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "gravity"), "set_gravity", "get_gravity");
  1276. ADD_GROUP("Initial Velocity", "initial_");
  1277. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "initial_velocity", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param", "get_param", PARAM_INITIAL_LINEAR_VELOCITY);
  1278. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "initial_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_INITIAL_LINEAR_VELOCITY);
  1279. ADD_GROUP("Angular Velocity", "angular_");
  1280. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_velocity", PROPERTY_HINT_RANGE, "-720,720,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_ANGULAR_VELOCITY);
  1281. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANGULAR_VELOCITY);
  1282. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angular_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGULAR_VELOCITY);
  1283. ADD_GROUP("Orbit Velocity", "orbit_");
  1284. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "orbit_velocity", PROPERTY_HINT_RANGE, "-1000,1000,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_ORBIT_VELOCITY);
  1285. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "orbit_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ORBIT_VELOCITY);
  1286. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "orbit_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ORBIT_VELOCITY);
  1287. ADD_GROUP("Linear Accel", "linear_");
  1288. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_accel", PROPERTY_HINT_RANGE, "-100,100,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_LINEAR_ACCEL);
  1289. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_LINEAR_ACCEL);
  1290. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "linear_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_LINEAR_ACCEL);
  1291. ADD_GROUP("Radial Accel", "radial_");
  1292. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "radial_accel", PROPERTY_HINT_RANGE, "-100,100,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_RADIAL_ACCEL);
  1293. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "radial_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_RADIAL_ACCEL);
  1294. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "radial_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_RADIAL_ACCEL);
  1295. ADD_GROUP("Tangential Accel", "tangential_");
  1296. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "tangential_accel", PROPERTY_HINT_RANGE, "-100,100,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_TANGENTIAL_ACCEL);
  1297. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "tangential_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_TANGENTIAL_ACCEL);
  1298. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "tangential_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_TANGENTIAL_ACCEL);
  1299. ADD_GROUP("Damping", "");
  1300. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "damping", PROPERTY_HINT_RANGE, "0,100,0.01,or_greater"), "set_param", "get_param", PARAM_DAMPING);
  1301. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "damping_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_DAMPING);
  1302. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "damping_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_DAMPING);
  1303. ADD_GROUP("Angle", "");
  1304. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angle", PROPERTY_HINT_RANGE, "-720,720,0.1,or_lesser,or_greater"), "set_param", "get_param", PARAM_ANGLE);
  1305. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angle_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANGLE);
  1306. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angle_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGLE);
  1307. ADD_GROUP("Scale", "");
  1308. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale_amount", PROPERTY_HINT_RANGE, "-1000,1000,0.01,or_greater"), "set_param", "get_param", PARAM_SCALE);
  1309. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale_amount_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_SCALE);
  1310. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_amount_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_SCALE);
  1311. ADD_GROUP("Color", "");
  1312. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  1313. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");
  1314. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_initial_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_initial_ramp", "get_color_initial_ramp");
  1315. ADD_GROUP("Hue Variation", "hue_");
  1316. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "hue_variation", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param", "get_param", PARAM_HUE_VARIATION);
  1317. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "hue_variation_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_HUE_VARIATION);
  1318. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "hue_variation_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_HUE_VARIATION);
  1319. ADD_GROUP("Animation", "anim_");
  1320. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_speed", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater"), "set_param", "get_param", PARAM_ANIM_SPEED);
  1321. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_speed_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANIM_SPEED);
  1322. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_speed_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_SPEED);
  1323. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_offset", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_param", "get_param", PARAM_ANIM_OFFSET);
  1324. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_offset_random", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_param_randomness", "get_param_randomness", PARAM_ANIM_OFFSET);
  1325. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_offset_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_OFFSET);
  1326. BIND_ENUM_CONSTANT(PARAM_INITIAL_LINEAR_VELOCITY);
  1327. BIND_ENUM_CONSTANT(PARAM_ANGULAR_VELOCITY);
  1328. BIND_ENUM_CONSTANT(PARAM_ORBIT_VELOCITY);
  1329. BIND_ENUM_CONSTANT(PARAM_LINEAR_ACCEL);
  1330. BIND_ENUM_CONSTANT(PARAM_RADIAL_ACCEL);
  1331. BIND_ENUM_CONSTANT(PARAM_TANGENTIAL_ACCEL);
  1332. BIND_ENUM_CONSTANT(PARAM_DAMPING);
  1333. BIND_ENUM_CONSTANT(PARAM_ANGLE);
  1334. BIND_ENUM_CONSTANT(PARAM_SCALE);
  1335. BIND_ENUM_CONSTANT(PARAM_HUE_VARIATION);
  1336. BIND_ENUM_CONSTANT(PARAM_ANIM_SPEED);
  1337. BIND_ENUM_CONSTANT(PARAM_ANIM_OFFSET);
  1338. BIND_ENUM_CONSTANT(PARAM_MAX);
  1339. BIND_ENUM_CONSTANT(FLAG_ALIGN_Y_TO_VELOCITY);
  1340. BIND_ENUM_CONSTANT(FLAG_ROTATE_Y);
  1341. BIND_ENUM_CONSTANT(FLAG_DISABLE_Z);
  1342. BIND_ENUM_CONSTANT(FLAG_MAX);
  1343. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINT);
  1344. BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE);
  1345. BIND_ENUM_CONSTANT(EMISSION_SHAPE_BOX);
  1346. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINTS);
  1347. BIND_ENUM_CONSTANT(EMISSION_SHAPE_DIRECTED_POINTS);
  1348. BIND_ENUM_CONSTANT(EMISSION_SHAPE_RING);
  1349. BIND_ENUM_CONSTANT(EMISSION_SHAPE_MAX);
  1350. }
  1351. CPUParticles::CPUParticles() {
  1352. time = 0;
  1353. inactive_time = 0;
  1354. frame_remainder = 0;
  1355. cycle = 0;
  1356. redraw = false;
  1357. emitting = false;
  1358. set_notify_transform(true);
  1359. multimesh = RID_PRIME(VisualServer::get_singleton()->multimesh_create());
  1360. VisualServer::get_singleton()->multimesh_set_visible_instances(multimesh, 0);
  1361. set_base(multimesh);
  1362. set_emitting(true);
  1363. set_one_shot(false);
  1364. set_amount(8);
  1365. set_lifetime(1);
  1366. set_fixed_fps(0);
  1367. set_fractional_delta(true);
  1368. set_pre_process_time(0);
  1369. set_explosiveness_ratio(0);
  1370. set_randomness_ratio(0);
  1371. set_lifetime_randomness(0);
  1372. set_use_local_coordinates(true);
  1373. set_draw_order(DRAW_ORDER_INDEX);
  1374. set_speed_scale(1);
  1375. set_direction(Vector3(1, 0, 0));
  1376. set_spread(45);
  1377. set_flatness(0);
  1378. set_param(PARAM_INITIAL_LINEAR_VELOCITY, 0);
  1379. set_param(PARAM_ANGULAR_VELOCITY, 0);
  1380. set_param(PARAM_ORBIT_VELOCITY, 0);
  1381. set_param(PARAM_LINEAR_ACCEL, 0);
  1382. set_param(PARAM_RADIAL_ACCEL, 0);
  1383. set_param(PARAM_TANGENTIAL_ACCEL, 0);
  1384. set_param(PARAM_DAMPING, 0);
  1385. set_param(PARAM_ANGLE, 0);
  1386. set_param(PARAM_SCALE, 1);
  1387. set_param(PARAM_HUE_VARIATION, 0);
  1388. set_param(PARAM_ANIM_SPEED, 0);
  1389. set_param(PARAM_ANIM_OFFSET, 0);
  1390. set_emission_shape(EMISSION_SHAPE_POINT);
  1391. set_emission_sphere_radius(1);
  1392. set_emission_box_extents(Vector3(1, 1, 1));
  1393. set_emission_ring_height(1.0);
  1394. set_emission_ring_radius(1.0);
  1395. set_emission_ring_inner_radius(0.0);
  1396. set_emission_ring_axis(Vector3(0.0, 0.0, 1.0));
  1397. set_gravity(Vector3(0, -9.8, 0));
  1398. for (int i = 0; i < PARAM_MAX; i++) {
  1399. set_param_randomness(Parameter(i), 0);
  1400. }
  1401. for (int i = 0; i < FLAG_MAX; i++) {
  1402. flags[i] = false;
  1403. }
  1404. set_color(Color(1, 1, 1, 1));
  1405. }
  1406. CPUParticles::~CPUParticles() {
  1407. VS::get_singleton()->free(multimesh);
  1408. }