cpu_particles.cpp 60 KB

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