cpu_particles_3d.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  1. /*************************************************************************/
  2. /* cpu_particles_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "cpu_particles_3d.h"
  31. #include "scene/3d/camera_3d.h"
  32. #include "scene/3d/gpu_particles_3d.h"
  33. #include "scene/resources/particles_material.h"
  34. #include "servers/rendering_server.h"
  35. AABB CPUParticles3D::get_aabb() const {
  36. return AABB();
  37. }
  38. Vector<Face3> CPUParticles3D::get_faces(uint32_t p_usage_particle_flags) const {
  39. return Vector<Face3>();
  40. }
  41. void CPUParticles3D::set_emitting(bool p_emitting) {
  42. if (emitting == p_emitting) {
  43. return;
  44. }
  45. emitting = p_emitting;
  46. if (emitting) {
  47. set_process_internal(true);
  48. // first update before rendering to avoid one frame delay after emitting starts
  49. if (time == 0) {
  50. _update_internal();
  51. }
  52. }
  53. }
  54. void CPUParticles3D::set_amount(int p_amount) {
  55. ERR_FAIL_COND_MSG(p_amount < 1, "Amount of particles must be greater than 0.");
  56. particles.resize(p_amount);
  57. {
  58. Particle *w = particles.ptrw();
  59. for (int i = 0; i < p_amount; i++) {
  60. w[i].active = false;
  61. w[i].custom[3] = 0.0; // Make sure w component isn't garbage data
  62. }
  63. }
  64. particle_data.resize((12 + 4 + 4) * p_amount);
  65. RS::get_singleton()->multimesh_allocate_data(multimesh, p_amount, RS::MULTIMESH_TRANSFORM_3D, true, true);
  66. particle_order.resize(p_amount);
  67. }
  68. void CPUParticles3D::set_lifetime(float p_lifetime) {
  69. ERR_FAIL_COND_MSG(p_lifetime <= 0, "Particles lifetime must be greater than 0.");
  70. lifetime = p_lifetime;
  71. }
  72. void CPUParticles3D::set_one_shot(bool p_one_shot) {
  73. one_shot = p_one_shot;
  74. }
  75. void CPUParticles3D::set_pre_process_time(float p_time) {
  76. pre_process_time = p_time;
  77. }
  78. void CPUParticles3D::set_explosiveness_ratio(float p_ratio) {
  79. explosiveness_ratio = p_ratio;
  80. }
  81. void CPUParticles3D::set_randomness_ratio(float p_ratio) {
  82. randomness_ratio = p_ratio;
  83. }
  84. void CPUParticles3D::set_lifetime_randomness(float p_random) {
  85. lifetime_randomness = p_random;
  86. }
  87. void CPUParticles3D::set_use_local_coordinates(bool p_enable) {
  88. local_coords = p_enable;
  89. }
  90. void CPUParticles3D::set_speed_scale(float p_scale) {
  91. speed_scale = p_scale;
  92. }
  93. bool CPUParticles3D::is_emitting() const {
  94. return emitting;
  95. }
  96. int CPUParticles3D::get_amount() const {
  97. return particles.size();
  98. }
  99. float CPUParticles3D::get_lifetime() const {
  100. return lifetime;
  101. }
  102. bool CPUParticles3D::get_one_shot() const {
  103. return one_shot;
  104. }
  105. float CPUParticles3D::get_pre_process_time() const {
  106. return pre_process_time;
  107. }
  108. float CPUParticles3D::get_explosiveness_ratio() const {
  109. return explosiveness_ratio;
  110. }
  111. float CPUParticles3D::get_randomness_ratio() const {
  112. return randomness_ratio;
  113. }
  114. float CPUParticles3D::get_lifetime_randomness() const {
  115. return lifetime_randomness;
  116. }
  117. bool CPUParticles3D::get_use_local_coordinates() const {
  118. return local_coords;
  119. }
  120. float CPUParticles3D::get_speed_scale() const {
  121. return speed_scale;
  122. }
  123. void CPUParticles3D::set_draw_order(DrawOrder p_order) {
  124. draw_order = p_order;
  125. }
  126. CPUParticles3D::DrawOrder CPUParticles3D::get_draw_order() const {
  127. return draw_order;
  128. }
  129. void CPUParticles3D::set_mesh(const Ref<Mesh> &p_mesh) {
  130. mesh = p_mesh;
  131. if (mesh.is_valid()) {
  132. RS::get_singleton()->multimesh_set_mesh(multimesh, mesh->get_rid());
  133. } else {
  134. RS::get_singleton()->multimesh_set_mesh(multimesh, RID());
  135. }
  136. }
  137. Ref<Mesh> CPUParticles3D::get_mesh() const {
  138. return mesh;
  139. }
  140. void CPUParticles3D::set_fixed_fps(int p_count) {
  141. fixed_fps = p_count;
  142. }
  143. int CPUParticles3D::get_fixed_fps() const {
  144. return fixed_fps;
  145. }
  146. void CPUParticles3D::set_fractional_delta(bool p_enable) {
  147. fractional_delta = p_enable;
  148. }
  149. bool CPUParticles3D::get_fractional_delta() const {
  150. return fractional_delta;
  151. }
  152. String CPUParticles3D::get_configuration_warning() const {
  153. String warnings = GeometryInstance3D::get_configuration_warning();
  154. bool mesh_found = false;
  155. bool anim_material_found = false;
  156. if (get_mesh().is_valid()) {
  157. mesh_found = true;
  158. for (int j = 0; j < get_mesh()->get_surface_count(); j++) {
  159. anim_material_found = Object::cast_to<ShaderMaterial>(get_mesh()->surface_get_material(j).ptr()) != nullptr;
  160. StandardMaterial3D *spat = Object::cast_to<StandardMaterial3D>(get_mesh()->surface_get_material(j).ptr());
  161. anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == StandardMaterial3D::BILLBOARD_PARTICLES);
  162. }
  163. }
  164. anim_material_found = anim_material_found || Object::cast_to<ShaderMaterial>(get_material_override().ptr()) != nullptr;
  165. StandardMaterial3D *spat = Object::cast_to<StandardMaterial3D>(get_material_override().ptr());
  166. anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == StandardMaterial3D::BILLBOARD_PARTICLES);
  167. if (!mesh_found) {
  168. if (warnings != String()) {
  169. warnings += "\n";
  170. }
  171. warnings += "- " + TTR("Nothing is visible because no mesh has been assigned.");
  172. }
  173. if (!anim_material_found && (get_param(PARAM_ANIM_SPEED) != 0.0 || get_param(PARAM_ANIM_OFFSET) != 0.0 ||
  174. get_param_curve(PARAM_ANIM_SPEED).is_valid() || get_param_curve(PARAM_ANIM_OFFSET).is_valid())) {
  175. if (warnings != String()) {
  176. warnings += "\n";
  177. }
  178. warnings += "- " + TTR("CPUParticles3D animation requires the usage of a StandardMaterial3D whose Billboard Mode is set to \"Particle Billboard\".");
  179. }
  180. return warnings;
  181. }
  182. void CPUParticles3D::restart() {
  183. time = 0;
  184. inactive_time = 0;
  185. frame_remainder = 0;
  186. cycle = 0;
  187. emitting = false;
  188. {
  189. int pc = particles.size();
  190. Particle *w = particles.ptrw();
  191. for (int i = 0; i < pc; i++) {
  192. w[i].active = false;
  193. }
  194. }
  195. set_emitting(true);
  196. }
  197. void CPUParticles3D::set_direction(Vector3 p_direction) {
  198. direction = p_direction;
  199. }
  200. Vector3 CPUParticles3D::get_direction() const {
  201. return direction;
  202. }
  203. void CPUParticles3D::set_spread(float p_spread) {
  204. spread = p_spread;
  205. }
  206. float CPUParticles3D::get_spread() const {
  207. return spread;
  208. }
  209. void CPUParticles3D::set_flatness(float p_flatness) {
  210. flatness = p_flatness;
  211. }
  212. float CPUParticles3D::get_flatness() const {
  213. return flatness;
  214. }
  215. void CPUParticles3D::set_param(Parameter p_param, float p_value) {
  216. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  217. parameters[p_param] = p_value;
  218. }
  219. float CPUParticles3D::get_param(Parameter p_param) const {
  220. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  221. return parameters[p_param];
  222. }
  223. void CPUParticles3D::set_param_randomness(Parameter p_param, float p_value) {
  224. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  225. randomness[p_param] = p_value;
  226. }
  227. float CPUParticles3D::get_param_randomness(Parameter p_param) const {
  228. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  229. return randomness[p_param];
  230. }
  231. static void _adjust_curve_range(const Ref<Curve> &p_curve, float p_min, float p_max) {
  232. Ref<Curve> curve = p_curve;
  233. if (!curve.is_valid()) {
  234. return;
  235. }
  236. curve->ensure_default_setup(p_min, p_max);
  237. }
  238. void CPUParticles3D::set_param_curve(Parameter p_param, const Ref<Curve> &p_curve) {
  239. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  240. curve_parameters[p_param] = p_curve;
  241. switch (p_param) {
  242. case PARAM_INITIAL_LINEAR_VELOCITY: {
  243. //do none for this one
  244. } break;
  245. case PARAM_ANGULAR_VELOCITY: {
  246. _adjust_curve_range(p_curve, -360, 360);
  247. } break;
  248. case PARAM_ORBIT_VELOCITY: {
  249. _adjust_curve_range(p_curve, -500, 500);
  250. } break;
  251. case PARAM_LINEAR_ACCEL: {
  252. _adjust_curve_range(p_curve, -200, 200);
  253. } break;
  254. case PARAM_RADIAL_ACCEL: {
  255. _adjust_curve_range(p_curve, -200, 200);
  256. } break;
  257. case PARAM_TANGENTIAL_ACCEL: {
  258. _adjust_curve_range(p_curve, -200, 200);
  259. } break;
  260. case PARAM_DAMPING: {
  261. _adjust_curve_range(p_curve, 0, 100);
  262. } break;
  263. case PARAM_ANGLE: {
  264. _adjust_curve_range(p_curve, -360, 360);
  265. } break;
  266. case PARAM_SCALE: {
  267. } break;
  268. case PARAM_HUE_VARIATION: {
  269. _adjust_curve_range(p_curve, -1, 1);
  270. } break;
  271. case PARAM_ANIM_SPEED: {
  272. _adjust_curve_range(p_curve, 0, 200);
  273. } break;
  274. case PARAM_ANIM_OFFSET: {
  275. } break;
  276. default: {
  277. }
  278. }
  279. }
  280. Ref<Curve> CPUParticles3D::get_param_curve(Parameter p_param) const {
  281. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, Ref<Curve>());
  282. return curve_parameters[p_param];
  283. }
  284. void CPUParticles3D::set_color(const Color &p_color) {
  285. color = p_color;
  286. }
  287. Color CPUParticles3D::get_color() const {
  288. return color;
  289. }
  290. void CPUParticles3D::set_color_ramp(const Ref<Gradient> &p_ramp) {
  291. color_ramp = p_ramp;
  292. }
  293. Ref<Gradient> CPUParticles3D::get_color_ramp() const {
  294. return color_ramp;
  295. }
  296. void CPUParticles3D::set_particle_flag(ParticleFlags p_particle_flag, bool p_enable) {
  297. ERR_FAIL_INDEX(p_particle_flag, PARTICLE_FLAG_MAX);
  298. particle_flags[p_particle_flag] = p_enable;
  299. if (p_particle_flag == PARTICLE_FLAG_DISABLE_Z) {
  300. _change_notify();
  301. }
  302. }
  303. bool CPUParticles3D::get_particle_flag(ParticleFlags p_particle_flag) const {
  304. ERR_FAIL_INDEX_V(p_particle_flag, PARTICLE_FLAG_MAX, false);
  305. return particle_flags[p_particle_flag];
  306. }
  307. void CPUParticles3D::set_emission_shape(EmissionShape p_shape) {
  308. ERR_FAIL_INDEX(p_shape, EMISSION_SHAPE_MAX);
  309. emission_shape = p_shape;
  310. }
  311. void CPUParticles3D::set_emission_sphere_radius(float p_radius) {
  312. emission_sphere_radius = p_radius;
  313. }
  314. void CPUParticles3D::set_emission_box_extents(Vector3 p_extents) {
  315. emission_box_extents = p_extents;
  316. }
  317. void CPUParticles3D::set_emission_points(const Vector<Vector3> &p_points) {
  318. emission_points = p_points;
  319. }
  320. void CPUParticles3D::set_emission_normals(const Vector<Vector3> &p_normals) {
  321. emission_normals = p_normals;
  322. }
  323. void CPUParticles3D::set_emission_colors(const Vector<Color> &p_colors) {
  324. emission_colors = p_colors;
  325. }
  326. float CPUParticles3D::get_emission_sphere_radius() const {
  327. return emission_sphere_radius;
  328. }
  329. Vector3 CPUParticles3D::get_emission_box_extents() const {
  330. return emission_box_extents;
  331. }
  332. Vector<Vector3> CPUParticles3D::get_emission_points() const {
  333. return emission_points;
  334. }
  335. Vector<Vector3> CPUParticles3D::get_emission_normals() const {
  336. return emission_normals;
  337. }
  338. Vector<Color> CPUParticles3D::get_emission_colors() const {
  339. return emission_colors;
  340. }
  341. CPUParticles3D::EmissionShape CPUParticles3D::get_emission_shape() const {
  342. return emission_shape;
  343. }
  344. void CPUParticles3D::set_gravity(const Vector3 &p_gravity) {
  345. gravity = p_gravity;
  346. }
  347. Vector3 CPUParticles3D::get_gravity() const {
  348. return gravity;
  349. }
  350. void CPUParticles3D::_validate_property(PropertyInfo &property) const {
  351. if (property.name == "color" && color_ramp.is_valid()) {
  352. property.usage = 0;
  353. }
  354. if (property.name == "emission_sphere_radius" && emission_shape != EMISSION_SHAPE_SPHERE) {
  355. property.usage = 0;
  356. }
  357. if (property.name == "emission_box_extents" && emission_shape != EMISSION_SHAPE_BOX) {
  358. property.usage = 0;
  359. }
  360. if ((property.name == "emission_point_texture" || property.name == "emission_color_texture") && (emission_shape < EMISSION_SHAPE_POINTS)) {
  361. property.usage = 0;
  362. }
  363. if (property.name == "emission_normals" && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
  364. property.usage = 0;
  365. }
  366. if (property.name.begins_with("orbit_") && !particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  367. property.usage = 0;
  368. }
  369. }
  370. static uint32_t idhash(uint32_t x) {
  371. x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
  372. x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
  373. x = (x >> uint32_t(16)) ^ x;
  374. return x;
  375. }
  376. static float rand_from_seed(uint32_t &seed) {
  377. int k;
  378. int s = int(seed);
  379. if (s == 0) {
  380. s = 305420679;
  381. }
  382. k = s / 127773;
  383. s = 16807 * (s - k * 127773) - 2836 * k;
  384. if (s < 0) {
  385. s += 2147483647;
  386. }
  387. seed = uint32_t(s);
  388. return float(seed % uint32_t(65536)) / 65535.0;
  389. }
  390. void CPUParticles3D::_update_internal() {
  391. if (particles.size() == 0 || !is_visible_in_tree()) {
  392. _set_redraw(false);
  393. return;
  394. }
  395. float delta = get_process_delta_time();
  396. if (emitting) {
  397. inactive_time = 0;
  398. } else {
  399. inactive_time += delta;
  400. if (inactive_time > lifetime * 1.2) {
  401. set_process_internal(false);
  402. _set_redraw(false);
  403. //reset variables
  404. time = 0;
  405. inactive_time = 0;
  406. frame_remainder = 0;
  407. cycle = 0;
  408. return;
  409. }
  410. }
  411. _set_redraw(true);
  412. bool processed = false;
  413. if (time == 0 && pre_process_time > 0.0) {
  414. float frame_time;
  415. if (fixed_fps > 0) {
  416. frame_time = 1.0 / fixed_fps;
  417. } else {
  418. frame_time = 1.0 / 30.0;
  419. }
  420. float todo = pre_process_time;
  421. while (todo >= 0) {
  422. _particles_process(frame_time);
  423. processed = true;
  424. todo -= frame_time;
  425. }
  426. }
  427. if (fixed_fps > 0) {
  428. float frame_time = 1.0 / fixed_fps;
  429. float decr = frame_time;
  430. float ldelta = delta;
  431. if (ldelta > 0.1) { //avoid recursive stalls if fps goes below 10
  432. ldelta = 0.1;
  433. } else if (ldelta <= 0.0) { //unlikely but..
  434. ldelta = 0.001;
  435. }
  436. float todo = frame_remainder + ldelta;
  437. while (todo >= frame_time) {
  438. _particles_process(frame_time);
  439. processed = true;
  440. todo -= decr;
  441. }
  442. frame_remainder = todo;
  443. } else {
  444. _particles_process(delta);
  445. processed = true;
  446. }
  447. if (processed) {
  448. _update_particle_data_buffer();
  449. }
  450. }
  451. void CPUParticles3D::_particles_process(float p_delta) {
  452. p_delta *= speed_scale;
  453. int pcount = particles.size();
  454. Particle *w = particles.ptrw();
  455. Particle *parray = w;
  456. float prev_time = time;
  457. time += p_delta;
  458. if (time > lifetime) {
  459. time = Math::fmod(time, lifetime);
  460. cycle++;
  461. if (one_shot && cycle > 0) {
  462. set_emitting(false);
  463. _change_notify();
  464. }
  465. }
  466. Transform emission_xform;
  467. Basis velocity_xform;
  468. if (!local_coords) {
  469. emission_xform = get_global_transform();
  470. velocity_xform = emission_xform.basis;
  471. }
  472. float system_phase = time / lifetime;
  473. for (int i = 0; i < pcount; i++) {
  474. Particle &p = parray[i];
  475. if (!emitting && !p.active) {
  476. continue;
  477. }
  478. float local_delta = p_delta;
  479. // The phase is a ratio between 0 (birth) and 1 (end of life) for each particle.
  480. // While we use time in tests later on, for randomness we use the phase as done in the
  481. // original shader code, and we later multiply by lifetime to get the time.
  482. float restart_phase = float(i) / float(pcount);
  483. if (randomness_ratio > 0.0) {
  484. uint32_t seed = cycle;
  485. if (restart_phase >= system_phase) {
  486. seed -= uint32_t(1);
  487. }
  488. seed *= uint32_t(pcount);
  489. seed += uint32_t(i);
  490. float random = float(idhash(seed) % uint32_t(65536)) / 65536.0;
  491. restart_phase += randomness_ratio * random * 1.0 / float(pcount);
  492. }
  493. restart_phase *= (1.0 - explosiveness_ratio);
  494. float restart_time = restart_phase * lifetime;
  495. bool restart = false;
  496. if (time > prev_time) {
  497. // restart_time >= prev_time is used so particles emit in the first frame they are processed
  498. if (restart_time >= prev_time && restart_time < time) {
  499. restart = true;
  500. if (fractional_delta) {
  501. local_delta = time - restart_time;
  502. }
  503. }
  504. } else if (local_delta > 0.0) {
  505. if (restart_time >= prev_time) {
  506. restart = true;
  507. if (fractional_delta) {
  508. local_delta = lifetime - restart_time + time;
  509. }
  510. } else if (restart_time < time) {
  511. restart = true;
  512. if (fractional_delta) {
  513. local_delta = time - restart_time;
  514. }
  515. }
  516. }
  517. if (p.time * (1.0 - explosiveness_ratio) > p.lifetime) {
  518. restart = true;
  519. }
  520. float tv = 0.0;
  521. if (restart) {
  522. if (!emitting) {
  523. p.active = false;
  524. continue;
  525. }
  526. p.active = true;
  527. /*float tex_linear_velocity = 0;
  528. if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  529. tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->interpolate(0);
  530. }*/
  531. float tex_angle = 0.0;
  532. if (curve_parameters[PARAM_ANGLE].is_valid()) {
  533. tex_angle = curve_parameters[PARAM_ANGLE]->interpolate(tv);
  534. }
  535. float tex_anim_offset = 0.0;
  536. if (curve_parameters[PARAM_ANGLE].is_valid()) {
  537. tex_anim_offset = curve_parameters[PARAM_ANGLE]->interpolate(tv);
  538. }
  539. p.seed = Math::rand();
  540. p.angle_rand = Math::randf();
  541. p.scale_rand = Math::randf();
  542. p.hue_rot_rand = Math::randf();
  543. p.anim_offset_rand = Math::randf();
  544. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  545. float angle1_rad = Math::atan2(direction.y, direction.x) + Math::deg2rad((Math::randf() * 2.0 - 1.0) * spread);
  546. Vector3 rot = Vector3(Math::cos(angle1_rad), Math::sin(angle1_rad), 0.0);
  547. p.velocity = rot * parameters[PARAM_INITIAL_LINEAR_VELOCITY] * Math::lerp(1.0f, float(Math::randf()), randomness[PARAM_INITIAL_LINEAR_VELOCITY]);
  548. } else {
  549. //initiate velocity spread in 3D
  550. float angle1_rad = Math::atan2(direction.x, direction.z) + Math::deg2rad((Math::randf() * 2.0 - 1.0) * spread);
  551. float angle2_rad = Math::atan2(direction.y, Math::abs(direction.z)) + Math::deg2rad((Math::randf() * 2.0 - 1.0) * (1.0 - flatness) * spread);
  552. Vector3 direction_xz = Vector3(Math::sin(angle1_rad), 0, Math::cos(angle1_rad));
  553. Vector3 direction_yz = Vector3(0, Math::sin(angle2_rad), Math::cos(angle2_rad));
  554. direction_yz.z = direction_yz.z / MAX(0.0001, Math::sqrt(ABS(direction_yz.z))); //better uniform distribution
  555. Vector3 direction = Vector3(direction_xz.x * direction_yz.z, direction_yz.y, direction_xz.z * direction_yz.z);
  556. direction.normalize();
  557. p.velocity = direction * parameters[PARAM_INITIAL_LINEAR_VELOCITY] * Math::lerp(1.0f, float(Math::randf()), randomness[PARAM_INITIAL_LINEAR_VELOCITY]);
  558. }
  559. float base_angle = (parameters[PARAM_ANGLE] + tex_angle) * Math::lerp(1.0f, p.angle_rand, randomness[PARAM_ANGLE]);
  560. p.custom[0] = Math::deg2rad(base_angle); //angle
  561. p.custom[1] = 0.0; //phase
  562. 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)
  563. p.transform = Transform();
  564. p.time = 0;
  565. p.lifetime = lifetime * (1.0 - Math::randf() * lifetime_randomness);
  566. p.base_color = Color(1, 1, 1, 1);
  567. switch (emission_shape) {
  568. case EMISSION_SHAPE_POINT: {
  569. //do none
  570. } break;
  571. case EMISSION_SHAPE_SPHERE: {
  572. real_t s = 2.0 * Math::randf() - 1.0;
  573. real_t t = Math_TAU * Math::randf();
  574. real_t radius = emission_sphere_radius * Math::sqrt(1.0 - s * s);
  575. p.transform.origin = Vector3(radius * Math::cos(t), radius * Math::sin(t), emission_sphere_radius * s);
  576. } break;
  577. case EMISSION_SHAPE_BOX: {
  578. 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;
  579. } break;
  580. case EMISSION_SHAPE_POINTS:
  581. case EMISSION_SHAPE_DIRECTED_POINTS: {
  582. int pc = emission_points.size();
  583. if (pc == 0) {
  584. break;
  585. }
  586. int random_idx = Math::rand() % pc;
  587. p.transform.origin = emission_points.get(random_idx);
  588. if (emission_shape == EMISSION_SHAPE_DIRECTED_POINTS && emission_normals.size() == pc) {
  589. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  590. Vector3 normal = emission_normals.get(random_idx);
  591. Vector2 normal_2d(normal.x, normal.y);
  592. Transform2D m2;
  593. m2.set_axis(0, normal_2d);
  594. m2.set_axis(1, normal_2d.orthogonal());
  595. Vector2 velocity_2d(p.velocity.x, p.velocity.y);
  596. velocity_2d = m2.basis_xform(velocity_2d);
  597. p.velocity.x = velocity_2d.x;
  598. p.velocity.y = velocity_2d.y;
  599. } else {
  600. Vector3 normal = emission_normals.get(random_idx);
  601. Vector3 v0 = Math::abs(normal.z) < 0.999 ? Vector3(0.0, 0.0, 1.0) : Vector3(0, 1.0, 0.0);
  602. Vector3 tangent = v0.cross(normal).normalized();
  603. Vector3 bitangent = tangent.cross(normal).normalized();
  604. Basis m3;
  605. m3.set_axis(0, tangent);
  606. m3.set_axis(1, bitangent);
  607. m3.set_axis(2, normal);
  608. p.velocity = m3.xform(p.velocity);
  609. }
  610. }
  611. if (emission_colors.size() == pc) {
  612. p.base_color = emission_colors.get(random_idx);
  613. }
  614. } break;
  615. case EMISSION_SHAPE_MAX: { // Max value for validity check.
  616. break;
  617. }
  618. }
  619. if (!local_coords) {
  620. p.velocity = velocity_xform.xform(p.velocity);
  621. p.transform = emission_xform * p.transform;
  622. }
  623. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  624. p.velocity.z = 0.0;
  625. p.transform.origin.z = 0.0;
  626. }
  627. } else if (!p.active) {
  628. continue;
  629. } else if (p.time > p.lifetime) {
  630. p.active = false;
  631. tv = 1.0;
  632. } else {
  633. uint32_t alt_seed = p.seed;
  634. p.time += local_delta;
  635. p.custom[1] = p.time / lifetime;
  636. tv = p.time / p.lifetime;
  637. float tex_linear_velocity = 0.0;
  638. if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  639. tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->interpolate(tv);
  640. }
  641. float tex_orbit_velocity = 0.0;
  642. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  643. if (curve_parameters[PARAM_ORBIT_VELOCITY].is_valid()) {
  644. tex_orbit_velocity = curve_parameters[PARAM_ORBIT_VELOCITY]->interpolate(tv);
  645. }
  646. }
  647. float tex_angular_velocity = 0.0;
  648. if (curve_parameters[PARAM_ANGULAR_VELOCITY].is_valid()) {
  649. tex_angular_velocity = curve_parameters[PARAM_ANGULAR_VELOCITY]->interpolate(tv);
  650. }
  651. float tex_linear_accel = 0.0;
  652. if (curve_parameters[PARAM_LINEAR_ACCEL].is_valid()) {
  653. tex_linear_accel = curve_parameters[PARAM_LINEAR_ACCEL]->interpolate(tv);
  654. }
  655. float tex_tangential_accel = 0.0;
  656. if (curve_parameters[PARAM_TANGENTIAL_ACCEL].is_valid()) {
  657. tex_tangential_accel = curve_parameters[PARAM_TANGENTIAL_ACCEL]->interpolate(tv);
  658. }
  659. float tex_radial_accel = 0.0;
  660. if (curve_parameters[PARAM_RADIAL_ACCEL].is_valid()) {
  661. tex_radial_accel = curve_parameters[PARAM_RADIAL_ACCEL]->interpolate(tv);
  662. }
  663. float tex_damping = 0.0;
  664. if (curve_parameters[PARAM_DAMPING].is_valid()) {
  665. tex_damping = curve_parameters[PARAM_DAMPING]->interpolate(tv);
  666. }
  667. float tex_angle = 0.0;
  668. if (curve_parameters[PARAM_ANGLE].is_valid()) {
  669. tex_angle = curve_parameters[PARAM_ANGLE]->interpolate(tv);
  670. }
  671. float tex_anim_speed = 0.0;
  672. if (curve_parameters[PARAM_ANIM_SPEED].is_valid()) {
  673. tex_anim_speed = curve_parameters[PARAM_ANIM_SPEED]->interpolate(tv);
  674. }
  675. float tex_anim_offset = 0.0;
  676. if (curve_parameters[PARAM_ANIM_OFFSET].is_valid()) {
  677. tex_anim_offset = curve_parameters[PARAM_ANIM_OFFSET]->interpolate(tv);
  678. }
  679. Vector3 force = gravity;
  680. Vector3 position = p.transform.origin;
  681. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  682. position.z = 0.0;
  683. }
  684. //apply linear acceleration
  685. force += p.velocity.length() > 0.0 ? p.velocity.normalized() * (parameters[PARAM_LINEAR_ACCEL] + tex_linear_accel) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_LINEAR_ACCEL]) : Vector3();
  686. //apply radial acceleration
  687. Vector3 org = emission_xform.origin;
  688. Vector3 diff = position - org;
  689. 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();
  690. //apply tangential acceleration;
  691. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  692. Vector2 yx = Vector2(diff.y, diff.x);
  693. Vector2 yx2 = (yx * Vector2(-1.0, 1.0)).normalized();
  694. 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();
  695. } else {
  696. Vector3 crossDiff = diff.normalized().cross(gravity.normalized());
  697. 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();
  698. }
  699. //apply attractor forces
  700. p.velocity += force * local_delta;
  701. //orbit velocity
  702. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  703. float orbit_amount = (parameters[PARAM_ORBIT_VELOCITY] + tex_orbit_velocity) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_ORBIT_VELOCITY]);
  704. if (orbit_amount != 0.0) {
  705. float ang = orbit_amount * local_delta * Math_TAU;
  706. // Not sure why the ParticlesMaterial code uses a clockwise rotation matrix,
  707. // but we use -ang here to reproduce its behavior.
  708. Transform2D rot = Transform2D(-ang, Vector2());
  709. Vector2 rotv = rot.basis_xform(Vector2(diff.x, diff.y));
  710. p.transform.origin -= Vector3(diff.x, diff.y, 0);
  711. p.transform.origin += Vector3(rotv.x, rotv.y, 0);
  712. }
  713. }
  714. if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  715. p.velocity = p.velocity.normalized() * tex_linear_velocity;
  716. }
  717. if (parameters[PARAM_DAMPING] + tex_damping > 0.0) {
  718. float v = p.velocity.length();
  719. float damp = (parameters[PARAM_DAMPING] + tex_damping) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_DAMPING]);
  720. v -= damp * local_delta;
  721. if (v < 0.0) {
  722. p.velocity = Vector3();
  723. } else {
  724. p.velocity = p.velocity.normalized() * v;
  725. }
  726. }
  727. float base_angle = (parameters[PARAM_ANGLE] + tex_angle) * Math::lerp(1.0f, p.angle_rand, randomness[PARAM_ANGLE]);
  728. base_angle += 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]);
  729. p.custom[0] = Math::deg2rad(base_angle); //angle
  730. p.custom[2] = (parameters[PARAM_ANIM_OFFSET] + tex_anim_offset) * Math::lerp(1.0f, p.anim_offset_rand, randomness[PARAM_ANIM_OFFSET]) + p.custom[1] * (parameters[PARAM_ANIM_SPEED] + tex_anim_speed) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_ANIM_SPEED]); //angle
  731. }
  732. //apply color
  733. //apply hue rotation
  734. float tex_scale = 1.0;
  735. if (curve_parameters[PARAM_SCALE].is_valid()) {
  736. tex_scale = curve_parameters[PARAM_SCALE]->interpolate(tv);
  737. }
  738. float tex_hue_variation = 0.0;
  739. if (curve_parameters[PARAM_HUE_VARIATION].is_valid()) {
  740. tex_hue_variation = curve_parameters[PARAM_HUE_VARIATION]->interpolate(tv);
  741. }
  742. float hue_rot_angle = (parameters[PARAM_HUE_VARIATION] + tex_hue_variation) * Math_TAU * Math::lerp(1.0f, p.hue_rot_rand * 2.0f - 1.0f, randomness[PARAM_HUE_VARIATION]);
  743. float hue_rot_c = Math::cos(hue_rot_angle);
  744. float hue_rot_s = Math::sin(hue_rot_angle);
  745. Basis hue_rot_mat;
  746. {
  747. Basis mat1(0.299, 0.587, 0.114, 0.299, 0.587, 0.114, 0.299, 0.587, 0.114);
  748. Basis mat2(0.701, -0.587, -0.114, -0.299, 0.413, -0.114, -0.300, -0.588, 0.886);
  749. Basis mat3(0.168, 0.330, -0.497, -0.328, 0.035, 0.292, 1.250, -1.050, -0.203);
  750. for (int j = 0; j < 3; j++) {
  751. hue_rot_mat[j] = mat1[j] + mat2[j] * hue_rot_c + mat3[j] * hue_rot_s;
  752. }
  753. }
  754. if (color_ramp.is_valid()) {
  755. p.color = color_ramp->get_color_at_offset(tv) * color;
  756. } else {
  757. p.color = color;
  758. }
  759. Vector3 color_rgb = hue_rot_mat.xform_inv(Vector3(p.color.r, p.color.g, p.color.b));
  760. p.color.r = color_rgb.x;
  761. p.color.g = color_rgb.y;
  762. p.color.b = color_rgb.z;
  763. p.color *= p.base_color;
  764. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  765. if (particle_flags[PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY]) {
  766. if (p.velocity.length() > 0.0) {
  767. p.transform.basis.set_axis(1, p.velocity.normalized());
  768. } else {
  769. p.transform.basis.set_axis(1, p.transform.basis.get_axis(1));
  770. }
  771. p.transform.basis.set_axis(0, p.transform.basis.get_axis(1).cross(p.transform.basis.get_axis(2)).normalized());
  772. p.transform.basis.set_axis(2, Vector3(0, 0, 1));
  773. } else {
  774. p.transform.basis.set_axis(0, Vector3(Math::cos(p.custom[0]), -Math::sin(p.custom[0]), 0.0));
  775. p.transform.basis.set_axis(1, Vector3(Math::sin(p.custom[0]), Math::cos(p.custom[0]), 0.0));
  776. p.transform.basis.set_axis(2, Vector3(0, 0, 1));
  777. }
  778. } else {
  779. //orient particle Y towards velocity
  780. if (particle_flags[PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY]) {
  781. if (p.velocity.length() > 0.0) {
  782. p.transform.basis.set_axis(1, p.velocity.normalized());
  783. } else {
  784. p.transform.basis.set_axis(1, p.transform.basis.get_axis(1).normalized());
  785. }
  786. if (p.transform.basis.get_axis(1) == p.transform.basis.get_axis(0)) {
  787. p.transform.basis.set_axis(0, p.transform.basis.get_axis(1).cross(p.transform.basis.get_axis(2)).normalized());
  788. p.transform.basis.set_axis(2, p.transform.basis.get_axis(0).cross(p.transform.basis.get_axis(1)).normalized());
  789. } else {
  790. p.transform.basis.set_axis(2, p.transform.basis.get_axis(0).cross(p.transform.basis.get_axis(1)).normalized());
  791. p.transform.basis.set_axis(0, p.transform.basis.get_axis(1).cross(p.transform.basis.get_axis(2)).normalized());
  792. }
  793. } else {
  794. p.transform.basis.orthonormalize();
  795. }
  796. //turn particle by rotation in Y
  797. if (particle_flags[PARTICLE_FLAG_ROTATE_Y]) {
  798. Basis rot_y(Vector3(0, 1, 0), p.custom[0]);
  799. p.transform.basis = p.transform.basis * rot_y;
  800. }
  801. }
  802. //scale by scale
  803. float base_scale = tex_scale * Math::lerp(parameters[PARAM_SCALE], 1.0f, p.scale_rand * randomness[PARAM_SCALE]);
  804. if (base_scale < 0.000001) {
  805. base_scale = 0.000001;
  806. }
  807. p.transform.basis.scale(Vector3(1, 1, 1) * base_scale);
  808. if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) {
  809. p.velocity.z = 0.0;
  810. p.transform.origin.z = 0.0;
  811. }
  812. p.transform.origin += p.velocity * local_delta;
  813. }
  814. }
  815. void CPUParticles3D::_update_particle_data_buffer() {
  816. MutexLock lock(update_mutex);
  817. int pc = particles.size();
  818. int *ow;
  819. int *order = nullptr;
  820. float *w = particle_data.ptrw();
  821. const Particle *r = particles.ptr();
  822. float *ptr = w;
  823. if (draw_order != DRAW_ORDER_INDEX) {
  824. ow = particle_order.ptrw();
  825. order = ow;
  826. for (int i = 0; i < pc; i++) {
  827. order[i] = i;
  828. }
  829. if (draw_order == DRAW_ORDER_LIFETIME) {
  830. SortArray<int, SortLifetime> sorter;
  831. sorter.compare.particles = r;
  832. sorter.sort(order, pc);
  833. } else if (draw_order == DRAW_ORDER_VIEW_DEPTH) {
  834. Camera3D *c = get_viewport()->get_camera();
  835. if (c) {
  836. Vector3 dir = c->get_global_transform().basis.get_axis(2); //far away to close
  837. if (local_coords) {
  838. // will look different from Particles in editor as this is based on the camera in the scenetree
  839. // and not the editor camera
  840. dir = inv_emission_transform.xform(dir).normalized();
  841. } else {
  842. dir = dir.normalized();
  843. }
  844. SortArray<int, SortAxis> sorter;
  845. sorter.compare.particles = r;
  846. sorter.compare.axis = dir;
  847. sorter.sort(order, pc);
  848. }
  849. }
  850. }
  851. for (int i = 0; i < pc; i++) {
  852. int idx = order ? order[i] : i;
  853. Transform t = r[idx].transform;
  854. if (!local_coords) {
  855. t = inv_emission_transform * t;
  856. }
  857. if (r[idx].active) {
  858. ptr[0] = t.basis.elements[0][0];
  859. ptr[1] = t.basis.elements[0][1];
  860. ptr[2] = t.basis.elements[0][2];
  861. ptr[3] = t.origin.x;
  862. ptr[4] = t.basis.elements[1][0];
  863. ptr[5] = t.basis.elements[1][1];
  864. ptr[6] = t.basis.elements[1][2];
  865. ptr[7] = t.origin.y;
  866. ptr[8] = t.basis.elements[2][0];
  867. ptr[9] = t.basis.elements[2][1];
  868. ptr[10] = t.basis.elements[2][2];
  869. ptr[11] = t.origin.z;
  870. } else {
  871. zeromem(ptr, sizeof(float) * 12);
  872. }
  873. Color c = r[idx].color;
  874. ptr[12] = c.r;
  875. ptr[13] = c.g;
  876. ptr[14] = c.b;
  877. ptr[15] = c.a;
  878. ptr[16] = r[idx].custom[0];
  879. ptr[17] = r[idx].custom[1];
  880. ptr[18] = r[idx].custom[2];
  881. ptr[19] = r[idx].custom[3];
  882. ptr += 20;
  883. }
  884. can_update = true;
  885. }
  886. void CPUParticles3D::_set_redraw(bool p_redraw) {
  887. if (redraw == p_redraw) {
  888. return;
  889. }
  890. redraw = p_redraw;
  891. {
  892. MutexLock lock(update_mutex);
  893. if (redraw) {
  894. RS::get_singleton()->connect("frame_pre_draw", callable_mp(this, &CPUParticles3D::_update_render_thread));
  895. RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE, true);
  896. RS::get_singleton()->multimesh_set_visible_instances(multimesh, -1);
  897. } else {
  898. if (RS::get_singleton()->is_connected("frame_pre_draw", callable_mp(this, &CPUParticles3D::_update_render_thread))) {
  899. RS::get_singleton()->disconnect("frame_pre_draw", callable_mp(this, &CPUParticles3D::_update_render_thread));
  900. }
  901. RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE, false);
  902. RS::get_singleton()->multimesh_set_visible_instances(multimesh, 0);
  903. }
  904. }
  905. }
  906. void CPUParticles3D::_update_render_thread() {
  907. MutexLock lock(update_mutex);
  908. if (can_update) {
  909. RS::get_singleton()->multimesh_set_buffer(multimesh, particle_data);
  910. can_update = false; //wait for next time
  911. }
  912. }
  913. void CPUParticles3D::_notification(int p_what) {
  914. if (p_what == NOTIFICATION_ENTER_TREE) {
  915. set_process_internal(emitting);
  916. // first update before rendering to avoid one frame delay after emitting starts
  917. if (emitting && (time == 0)) {
  918. _update_internal();
  919. }
  920. }
  921. if (p_what == NOTIFICATION_EXIT_TREE) {
  922. _set_redraw(false);
  923. }
  924. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  925. // first update before rendering to avoid one frame delay after emitting starts
  926. if (emitting && (time == 0)) {
  927. _update_internal();
  928. }
  929. }
  930. if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
  931. _update_internal();
  932. }
  933. if (p_what == NOTIFICATION_TRANSFORM_CHANGED) {
  934. inv_emission_transform = get_global_transform().affine_inverse();
  935. if (!local_coords) {
  936. int pc = particles.size();
  937. float *w = particle_data.ptrw();
  938. const Particle *r = particles.ptr();
  939. float *ptr = w;
  940. for (int i = 0; i < pc; i++) {
  941. Transform t = inv_emission_transform * r[i].transform;
  942. if (r[i].active) {
  943. ptr[0] = t.basis.elements[0][0];
  944. ptr[1] = t.basis.elements[0][1];
  945. ptr[2] = t.basis.elements[0][2];
  946. ptr[3] = t.origin.x;
  947. ptr[4] = t.basis.elements[1][0];
  948. ptr[5] = t.basis.elements[1][1];
  949. ptr[6] = t.basis.elements[1][2];
  950. ptr[7] = t.origin.y;
  951. ptr[8] = t.basis.elements[2][0];
  952. ptr[9] = t.basis.elements[2][1];
  953. ptr[10] = t.basis.elements[2][2];
  954. ptr[11] = t.origin.z;
  955. } else {
  956. zeromem(ptr, sizeof(float) * 12);
  957. }
  958. ptr += 20;
  959. }
  960. can_update = true;
  961. }
  962. }
  963. }
  964. void CPUParticles3D::convert_from_particles(Node *p_particles) {
  965. GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(p_particles);
  966. ERR_FAIL_COND_MSG(!particles, "Only GPUParticles3D nodes can be converted to CPUParticles3D.");
  967. set_emitting(particles->is_emitting());
  968. set_amount(particles->get_amount());
  969. set_lifetime(particles->get_lifetime());
  970. set_one_shot(particles->get_one_shot());
  971. set_pre_process_time(particles->get_pre_process_time());
  972. set_explosiveness_ratio(particles->get_explosiveness_ratio());
  973. set_randomness_ratio(particles->get_randomness_ratio());
  974. set_use_local_coordinates(particles->get_use_local_coordinates());
  975. set_fixed_fps(particles->get_fixed_fps());
  976. set_fractional_delta(particles->get_fractional_delta());
  977. set_speed_scale(particles->get_speed_scale());
  978. set_draw_order(DrawOrder(particles->get_draw_order()));
  979. set_mesh(particles->get_draw_pass_mesh(0));
  980. Ref<ParticlesMaterial> material = particles->get_process_material();
  981. if (material.is_null()) {
  982. return;
  983. }
  984. set_direction(material->get_direction());
  985. set_spread(material->get_spread());
  986. set_flatness(material->get_flatness());
  987. set_color(material->get_color());
  988. Ref<GradientTexture> gt = material->get_color_ramp();
  989. if (gt.is_valid()) {
  990. set_color_ramp(gt->get_gradient());
  991. }
  992. set_particle_flag(PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY, material->get_particle_flag(ParticlesMaterial::PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY));
  993. set_particle_flag(PARTICLE_FLAG_ROTATE_Y, material->get_particle_flag(ParticlesMaterial::PARTICLE_FLAG_ROTATE_Y));
  994. set_particle_flag(PARTICLE_FLAG_DISABLE_Z, material->get_particle_flag(ParticlesMaterial::PARTICLE_FLAG_DISABLE_Z));
  995. set_emission_shape(EmissionShape(material->get_emission_shape()));
  996. set_emission_sphere_radius(material->get_emission_sphere_radius());
  997. set_emission_box_extents(material->get_emission_box_extents());
  998. set_gravity(material->get_gravity());
  999. set_lifetime_randomness(material->get_lifetime_randomness());
  1000. #define CONVERT_PARAM(m_param) \
  1001. set_param(m_param, material->get_param(ParticlesMaterial::m_param)); \
  1002. { \
  1003. Ref<CurveTexture> ctex = material->get_param_texture(ParticlesMaterial::m_param); \
  1004. if (ctex.is_valid()) \
  1005. set_param_curve(m_param, ctex->get_curve()); \
  1006. } \
  1007. set_param_randomness(m_param, material->get_param_randomness(ParticlesMaterial::m_param));
  1008. CONVERT_PARAM(PARAM_INITIAL_LINEAR_VELOCITY);
  1009. CONVERT_PARAM(PARAM_ANGULAR_VELOCITY);
  1010. CONVERT_PARAM(PARAM_ORBIT_VELOCITY);
  1011. CONVERT_PARAM(PARAM_LINEAR_ACCEL);
  1012. CONVERT_PARAM(PARAM_RADIAL_ACCEL);
  1013. CONVERT_PARAM(PARAM_TANGENTIAL_ACCEL);
  1014. CONVERT_PARAM(PARAM_DAMPING);
  1015. CONVERT_PARAM(PARAM_ANGLE);
  1016. CONVERT_PARAM(PARAM_SCALE);
  1017. CONVERT_PARAM(PARAM_HUE_VARIATION);
  1018. CONVERT_PARAM(PARAM_ANIM_SPEED);
  1019. CONVERT_PARAM(PARAM_ANIM_OFFSET);
  1020. #undef CONVERT_PARAM
  1021. }
  1022. void CPUParticles3D::_bind_methods() {
  1023. ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &CPUParticles3D::set_emitting);
  1024. ClassDB::bind_method(D_METHOD("set_amount", "amount"), &CPUParticles3D::set_amount);
  1025. ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &CPUParticles3D::set_lifetime);
  1026. ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &CPUParticles3D::set_one_shot);
  1027. ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &CPUParticles3D::set_pre_process_time);
  1028. ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &CPUParticles3D::set_explosiveness_ratio);
  1029. ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &CPUParticles3D::set_randomness_ratio);
  1030. ClassDB::bind_method(D_METHOD("set_lifetime_randomness", "random"), &CPUParticles3D::set_lifetime_randomness);
  1031. ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &CPUParticles3D::set_use_local_coordinates);
  1032. ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &CPUParticles3D::set_fixed_fps);
  1033. ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &CPUParticles3D::set_fractional_delta);
  1034. ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &CPUParticles3D::set_speed_scale);
  1035. ClassDB::bind_method(D_METHOD("is_emitting"), &CPUParticles3D::is_emitting);
  1036. ClassDB::bind_method(D_METHOD("get_amount"), &CPUParticles3D::get_amount);
  1037. ClassDB::bind_method(D_METHOD("get_lifetime"), &CPUParticles3D::get_lifetime);
  1038. ClassDB::bind_method(D_METHOD("get_one_shot"), &CPUParticles3D::get_one_shot);
  1039. ClassDB::bind_method(D_METHOD("get_pre_process_time"), &CPUParticles3D::get_pre_process_time);
  1040. ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &CPUParticles3D::get_explosiveness_ratio);
  1041. ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &CPUParticles3D::get_randomness_ratio);
  1042. ClassDB::bind_method(D_METHOD("get_lifetime_randomness"), &CPUParticles3D::get_lifetime_randomness);
  1043. ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &CPUParticles3D::get_use_local_coordinates);
  1044. ClassDB::bind_method(D_METHOD("get_fixed_fps"), &CPUParticles3D::get_fixed_fps);
  1045. ClassDB::bind_method(D_METHOD("get_fractional_delta"), &CPUParticles3D::get_fractional_delta);
  1046. ClassDB::bind_method(D_METHOD("get_speed_scale"), &CPUParticles3D::get_speed_scale);
  1047. ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &CPUParticles3D::set_draw_order);
  1048. ClassDB::bind_method(D_METHOD("get_draw_order"), &CPUParticles3D::get_draw_order);
  1049. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &CPUParticles3D::set_mesh);
  1050. ClassDB::bind_method(D_METHOD("get_mesh"), &CPUParticles3D::get_mesh);
  1051. ClassDB::bind_method(D_METHOD("restart"), &CPUParticles3D::restart);
  1052. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting"), "set_emitting", "is_emitting");
  1053. ADD_PROPERTY(PropertyInfo(Variant::INT, "amount", PROPERTY_HINT_EXP_RANGE, "1,1000000,1"), "set_amount", "get_amount");
  1054. ADD_GROUP("Time", "");
  1055. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime", PROPERTY_HINT_EXP_RANGE, "0.01,600.0,0.01,or_greater"), "set_lifetime", "get_lifetime");
  1056. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
  1057. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "preprocess", PROPERTY_HINT_EXP_RANGE, "0.00,600.0,0.01"), "set_pre_process_time", "get_pre_process_time");
  1058. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
  1059. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
  1060. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
  1061. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime_randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_lifetime_randomness", "get_lifetime_randomness");
  1062. ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1"), "set_fixed_fps", "get_fixed_fps");
  1063. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
  1064. ADD_GROUP("Drawing", "");
  1065. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
  1066. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime,View Depth"), "set_draw_order", "get_draw_order");
  1067. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  1068. BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
  1069. BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
  1070. BIND_ENUM_CONSTANT(DRAW_ORDER_VIEW_DEPTH);
  1071. ////////////////////////////////
  1072. ClassDB::bind_method(D_METHOD("set_direction", "direction"), &CPUParticles3D::set_direction);
  1073. ClassDB::bind_method(D_METHOD("get_direction"), &CPUParticles3D::get_direction);
  1074. ClassDB::bind_method(D_METHOD("set_spread", "degrees"), &CPUParticles3D::set_spread);
  1075. ClassDB::bind_method(D_METHOD("get_spread"), &CPUParticles3D::get_spread);
  1076. ClassDB::bind_method(D_METHOD("set_flatness", "amount"), &CPUParticles3D::set_flatness);
  1077. ClassDB::bind_method(D_METHOD("get_flatness"), &CPUParticles3D::get_flatness);
  1078. ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &CPUParticles3D::set_param);
  1079. ClassDB::bind_method(D_METHOD("get_param", "param"), &CPUParticles3D::get_param);
  1080. ClassDB::bind_method(D_METHOD("set_param_randomness", "param", "randomness"), &CPUParticles3D::set_param_randomness);
  1081. ClassDB::bind_method(D_METHOD("get_param_randomness", "param"), &CPUParticles3D::get_param_randomness);
  1082. ClassDB::bind_method(D_METHOD("set_param_curve", "param", "curve"), &CPUParticles3D::set_param_curve);
  1083. ClassDB::bind_method(D_METHOD("get_param_curve", "param"), &CPUParticles3D::get_param_curve);
  1084. ClassDB::bind_method(D_METHOD("set_color", "color"), &CPUParticles3D::set_color);
  1085. ClassDB::bind_method(D_METHOD("get_color"), &CPUParticles3D::get_color);
  1086. ClassDB::bind_method(D_METHOD("set_color_ramp", "ramp"), &CPUParticles3D::set_color_ramp);
  1087. ClassDB::bind_method(D_METHOD("get_color_ramp"), &CPUParticles3D::get_color_ramp);
  1088. ClassDB::bind_method(D_METHOD("set_particle_flag", "particle_flag", "enable"), &CPUParticles3D::set_particle_flag);
  1089. ClassDB::bind_method(D_METHOD("get_particle_flag", "particle_flag"), &CPUParticles3D::get_particle_flag);
  1090. ClassDB::bind_method(D_METHOD("set_emission_shape", "shape"), &CPUParticles3D::set_emission_shape);
  1091. ClassDB::bind_method(D_METHOD("get_emission_shape"), &CPUParticles3D::get_emission_shape);
  1092. ClassDB::bind_method(D_METHOD("set_emission_sphere_radius", "radius"), &CPUParticles3D::set_emission_sphere_radius);
  1093. ClassDB::bind_method(D_METHOD("get_emission_sphere_radius"), &CPUParticles3D::get_emission_sphere_radius);
  1094. ClassDB::bind_method(D_METHOD("set_emission_box_extents", "extents"), &CPUParticles3D::set_emission_box_extents);
  1095. ClassDB::bind_method(D_METHOD("get_emission_box_extents"), &CPUParticles3D::get_emission_box_extents);
  1096. ClassDB::bind_method(D_METHOD("set_emission_points", "array"), &CPUParticles3D::set_emission_points);
  1097. ClassDB::bind_method(D_METHOD("get_emission_points"), &CPUParticles3D::get_emission_points);
  1098. ClassDB::bind_method(D_METHOD("set_emission_normals", "array"), &CPUParticles3D::set_emission_normals);
  1099. ClassDB::bind_method(D_METHOD("get_emission_normals"), &CPUParticles3D::get_emission_normals);
  1100. ClassDB::bind_method(D_METHOD("set_emission_colors", "array"), &CPUParticles3D::set_emission_colors);
  1101. ClassDB::bind_method(D_METHOD("get_emission_colors"), &CPUParticles3D::get_emission_colors);
  1102. ClassDB::bind_method(D_METHOD("get_gravity"), &CPUParticles3D::get_gravity);
  1103. ClassDB::bind_method(D_METHOD("set_gravity", "accel_vec"), &CPUParticles3D::set_gravity);
  1104. ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &CPUParticles3D::convert_from_particles);
  1105. ADD_GROUP("Emission Shape", "emission_");
  1106. ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_shape", PROPERTY_HINT_ENUM, "Point,Sphere,Box,Points,Directed Points", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_emission_shape", "get_emission_shape");
  1107. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_sphere_radius", PROPERTY_HINT_RANGE, "0.01,128,0.01"), "set_emission_sphere_radius", "get_emission_sphere_radius");
  1108. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "emission_box_extents"), "set_emission_box_extents", "get_emission_box_extents");
  1109. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "emission_points"), "set_emission_points", "get_emission_points");
  1110. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "emission_normals"), "set_emission_normals", "get_emission_normals");
  1111. ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "emission_colors"), "set_emission_colors", "get_emission_colors");
  1112. ADD_GROUP("Particle Flags", "particle_flag_");
  1113. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_align_y"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY);
  1114. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_rotate_y"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_ROTATE_Y);
  1115. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_disable_z"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_DISABLE_Z);
  1116. ADD_GROUP("Direction", "");
  1117. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "direction"), "set_direction", "get_direction");
  1118. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "spread", PROPERTY_HINT_RANGE, "0,180,0.01"), "set_spread", "get_spread");
  1119. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "flatness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_flatness", "get_flatness");
  1120. ADD_GROUP("Gravity", "");
  1121. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "gravity"), "set_gravity", "get_gravity");
  1122. ADD_GROUP("Initial Velocity", "initial_");
  1123. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "initial_velocity", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param", "get_param", PARAM_INITIAL_LINEAR_VELOCITY);
  1124. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "initial_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_INITIAL_LINEAR_VELOCITY);
  1125. ADD_GROUP("Angular Velocity", "angular_");
  1126. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angular_velocity", PROPERTY_HINT_RANGE, "-720,720,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_ANGULAR_VELOCITY);
  1127. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angular_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANGULAR_VELOCITY);
  1128. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angular_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGULAR_VELOCITY);
  1129. ADD_GROUP("Orbit Velocity", "orbit_");
  1130. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "orbit_velocity", PROPERTY_HINT_RANGE, "-1000,1000,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_ORBIT_VELOCITY);
  1131. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "orbit_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ORBIT_VELOCITY);
  1132. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "orbit_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ORBIT_VELOCITY);
  1133. ADD_GROUP("Linear Accel", "linear_");
  1134. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "linear_accel", PROPERTY_HINT_RANGE, "-100,100,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_LINEAR_ACCEL);
  1135. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "linear_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_LINEAR_ACCEL);
  1136. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "linear_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_LINEAR_ACCEL);
  1137. ADD_GROUP("Radial Accel", "radial_");
  1138. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "radial_accel", PROPERTY_HINT_RANGE, "-100,100,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_RADIAL_ACCEL);
  1139. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "radial_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_RADIAL_ACCEL);
  1140. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "radial_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_RADIAL_ACCEL);
  1141. ADD_GROUP("Tangential Accel", "tangential_");
  1142. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "tangential_accel", PROPERTY_HINT_RANGE, "-100,100,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_TANGENTIAL_ACCEL);
  1143. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "tangential_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_TANGENTIAL_ACCEL);
  1144. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "tangential_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_TANGENTIAL_ACCEL);
  1145. ADD_GROUP("Damping", "");
  1146. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "damping", PROPERTY_HINT_RANGE, "0,100,0.01"), "set_param", "get_param", PARAM_DAMPING);
  1147. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "damping_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_DAMPING);
  1148. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "damping_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_DAMPING);
  1149. ADD_GROUP("Angle", "");
  1150. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angle", PROPERTY_HINT_RANGE, "-720,720,0.1,or_lesser,or_greater"), "set_param", "get_param", PARAM_ANGLE);
  1151. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angle_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANGLE);
  1152. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angle_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGLE);
  1153. ADD_GROUP("Scale", "");
  1154. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "scale_amount", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param", "get_param", PARAM_SCALE);
  1155. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "scale_amount_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_SCALE);
  1156. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_amount_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_SCALE);
  1157. ADD_GROUP("Color", "");
  1158. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  1159. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");
  1160. ADD_GROUP("Hue Variation", "hue_");
  1161. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "hue_variation", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param", "get_param", PARAM_HUE_VARIATION);
  1162. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "hue_variation_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_HUE_VARIATION);
  1163. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "hue_variation_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_HUE_VARIATION);
  1164. ADD_GROUP("Animation", "anim_");
  1165. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_speed", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater"), "set_param", "get_param", PARAM_ANIM_SPEED);
  1166. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_speed_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANIM_SPEED);
  1167. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_speed_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_SPEED);
  1168. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_offset", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param", "get_param", PARAM_ANIM_OFFSET);
  1169. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_offset_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANIM_OFFSET);
  1170. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_offset_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_OFFSET);
  1171. BIND_ENUM_CONSTANT(PARAM_INITIAL_LINEAR_VELOCITY);
  1172. BIND_ENUM_CONSTANT(PARAM_ANGULAR_VELOCITY);
  1173. BIND_ENUM_CONSTANT(PARAM_ORBIT_VELOCITY);
  1174. BIND_ENUM_CONSTANT(PARAM_LINEAR_ACCEL);
  1175. BIND_ENUM_CONSTANT(PARAM_RADIAL_ACCEL);
  1176. BIND_ENUM_CONSTANT(PARAM_TANGENTIAL_ACCEL);
  1177. BIND_ENUM_CONSTANT(PARAM_DAMPING);
  1178. BIND_ENUM_CONSTANT(PARAM_ANGLE);
  1179. BIND_ENUM_CONSTANT(PARAM_SCALE);
  1180. BIND_ENUM_CONSTANT(PARAM_HUE_VARIATION);
  1181. BIND_ENUM_CONSTANT(PARAM_ANIM_SPEED);
  1182. BIND_ENUM_CONSTANT(PARAM_ANIM_OFFSET);
  1183. BIND_ENUM_CONSTANT(PARAM_MAX);
  1184. BIND_ENUM_CONSTANT(PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY);
  1185. BIND_ENUM_CONSTANT(PARTICLE_FLAG_ROTATE_Y);
  1186. BIND_ENUM_CONSTANT(PARTICLE_FLAG_DISABLE_Z);
  1187. BIND_ENUM_CONSTANT(PARTICLE_FLAG_MAX);
  1188. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINT);
  1189. BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE);
  1190. BIND_ENUM_CONSTANT(EMISSION_SHAPE_BOX);
  1191. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINTS);
  1192. BIND_ENUM_CONSTANT(EMISSION_SHAPE_DIRECTED_POINTS);
  1193. BIND_ENUM_CONSTANT(EMISSION_SHAPE_MAX);
  1194. }
  1195. CPUParticles3D::CPUParticles3D() {
  1196. set_notify_transform(true);
  1197. multimesh = RenderingServer::get_singleton()->multimesh_create();
  1198. RenderingServer::get_singleton()->multimesh_set_visible_instances(multimesh, 0);
  1199. set_base(multimesh);
  1200. set_emitting(true);
  1201. set_amount(8);
  1202. set_param(PARAM_INITIAL_LINEAR_VELOCITY, 0);
  1203. set_param(PARAM_ANGULAR_VELOCITY, 0);
  1204. set_param(PARAM_ORBIT_VELOCITY, 0);
  1205. set_param(PARAM_LINEAR_ACCEL, 0);
  1206. set_param(PARAM_RADIAL_ACCEL, 0);
  1207. set_param(PARAM_TANGENTIAL_ACCEL, 0);
  1208. set_param(PARAM_DAMPING, 0);
  1209. set_param(PARAM_ANGLE, 0);
  1210. set_param(PARAM_SCALE, 1);
  1211. set_param(PARAM_HUE_VARIATION, 0);
  1212. set_param(PARAM_ANIM_SPEED, 0);
  1213. set_param(PARAM_ANIM_OFFSET, 0);
  1214. }
  1215. CPUParticles3D::~CPUParticles3D() {
  1216. RS::get_singleton()->free(multimesh);
  1217. }