2
0

cpu_particles_2d.cpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  1. /**************************************************************************/
  2. /* cpu_particles_2d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "cpu_particles_2d.h"
  31. #include "cpu_particles_2d.compat.inc"
  32. #include "core/math/random_number_generator.h"
  33. #include "core/math/transform_interpolator.h"
  34. #include "scene/2d/gpu_particles_2d.h"
  35. #include "scene/resources/atlas_texture.h"
  36. #include "scene/resources/canvas_item_material.h"
  37. #include "scene/resources/curve_texture.h"
  38. #include "scene/resources/gradient_texture.h"
  39. #include "scene/resources/particle_process_material.h"
  40. void CPUParticles2D::set_emitting(bool p_emitting) {
  41. if (emitting == p_emitting) {
  42. return;
  43. }
  44. if (p_emitting && !use_fixed_seed) {
  45. set_seed(Math::rand());
  46. }
  47. emitting = p_emitting;
  48. if (emitting) {
  49. _set_emitting();
  50. }
  51. }
  52. void CPUParticles2D::_set_emitting() {
  53. active = true;
  54. set_process_internal(true);
  55. // first update before rendering to avoid one frame delay after emitting starts
  56. if (time == 0) {
  57. _update_internal();
  58. }
  59. }
  60. void CPUParticles2D::set_amount(int p_amount) {
  61. ERR_FAIL_COND_MSG(p_amount < 1, "Amount of particles must be greater than 0.");
  62. particles.resize(p_amount);
  63. {
  64. Particle *w = particles.ptrw();
  65. for (int i = 0; i < p_amount; i++) {
  66. w[i].active = false;
  67. }
  68. }
  69. particle_data.resize((8 + 4 + 4) * p_amount);
  70. RS::get_singleton()->multimesh_allocate_data(multimesh, p_amount, RS::MULTIMESH_TRANSFORM_2D, true, true);
  71. particle_order.resize(p_amount);
  72. }
  73. void CPUParticles2D::set_lifetime(double p_lifetime) {
  74. ERR_FAIL_COND_MSG(p_lifetime <= 0, "Particles lifetime must be greater than 0.");
  75. lifetime = p_lifetime;
  76. }
  77. void CPUParticles2D::set_one_shot(bool p_one_shot) {
  78. one_shot = p_one_shot;
  79. }
  80. void CPUParticles2D::set_pre_process_time(double p_time) {
  81. pre_process_time = p_time;
  82. }
  83. void CPUParticles2D::set_explosiveness_ratio(real_t p_ratio) {
  84. explosiveness_ratio = p_ratio;
  85. }
  86. void CPUParticles2D::set_randomness_ratio(real_t p_ratio) {
  87. randomness_ratio = p_ratio;
  88. }
  89. void CPUParticles2D::set_lifetime_randomness(double p_random) {
  90. lifetime_randomness = p_random;
  91. }
  92. void CPUParticles2D::set_use_local_coordinates(bool p_enable) {
  93. local_coords = p_enable;
  94. // Prevent sending item transforms when using global coords,
  95. // and inform the RenderingServer to use identity mode.
  96. set_canvas_item_use_identity_transform(!local_coords);
  97. // We only need NOTIFICATION_TRANSFORM_CHANGED
  98. // when following an interpolated target.
  99. set_notify_transform(_interpolation_data.interpolated_follow);
  100. }
  101. void CPUParticles2D::set_speed_scale(double p_scale) {
  102. speed_scale = p_scale;
  103. }
  104. bool CPUParticles2D::is_emitting() const {
  105. return emitting;
  106. }
  107. int CPUParticles2D::get_amount() const {
  108. return particles.size();
  109. }
  110. double CPUParticles2D::get_lifetime() const {
  111. return lifetime;
  112. }
  113. bool CPUParticles2D::get_one_shot() const {
  114. return one_shot;
  115. }
  116. double CPUParticles2D::get_pre_process_time() const {
  117. return pre_process_time;
  118. }
  119. real_t CPUParticles2D::get_explosiveness_ratio() const {
  120. return explosiveness_ratio;
  121. }
  122. real_t CPUParticles2D::get_randomness_ratio() const {
  123. return randomness_ratio;
  124. }
  125. double CPUParticles2D::get_lifetime_randomness() const {
  126. return lifetime_randomness;
  127. }
  128. bool CPUParticles2D::get_use_local_coordinates() const {
  129. return local_coords;
  130. }
  131. double CPUParticles2D::get_speed_scale() const {
  132. return speed_scale;
  133. }
  134. void CPUParticles2D::set_draw_order(DrawOrder p_order) {
  135. draw_order = p_order;
  136. }
  137. CPUParticles2D::DrawOrder CPUParticles2D::get_draw_order() const {
  138. return draw_order;
  139. }
  140. void CPUParticles2D::_update_mesh_texture() {
  141. Size2 tex_size;
  142. if (texture.is_valid()) {
  143. tex_size = texture->get_size();
  144. } else {
  145. tex_size = Size2(1, 1);
  146. }
  147. Vector<Vector2> vertices = {
  148. -tex_size * 0.5,
  149. -tex_size * 0.5 + Vector2(tex_size.x, 0),
  150. -tex_size * 0.5 + tex_size,
  151. -tex_size * 0.5 + Vector2(0, tex_size.y)
  152. };
  153. Vector<Vector2> uvs;
  154. AtlasTexture *atlas_texture = Object::cast_to<AtlasTexture>(*texture);
  155. if (atlas_texture && atlas_texture->get_atlas().is_valid()) {
  156. Rect2 region_rect = atlas_texture->get_region();
  157. Size2 atlas_size = atlas_texture->get_atlas()->get_size();
  158. uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, region_rect.position.y / atlas_size.y));
  159. uvs.push_back(Vector2((region_rect.position.x + region_rect.size.x) / atlas_size.x, region_rect.position.y / atlas_size.y));
  160. uvs.push_back(Vector2((region_rect.position.x + region_rect.size.x) / atlas_size.x, (region_rect.position.y + region_rect.size.y) / atlas_size.y));
  161. uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, (region_rect.position.y + region_rect.size.y) / atlas_size.y));
  162. } else {
  163. uvs.push_back(Vector2(0, 0));
  164. uvs.push_back(Vector2(1, 0));
  165. uvs.push_back(Vector2(1, 1));
  166. uvs.push_back(Vector2(0, 1));
  167. }
  168. Vector<Color> colors = {
  169. Color(1, 1, 1, 1),
  170. Color(1, 1, 1, 1),
  171. Color(1, 1, 1, 1),
  172. Color(1, 1, 1, 1)
  173. };
  174. Vector<int> indices = { 0, 1, 2, 2, 3, 0 };
  175. Array arr;
  176. arr.resize(RS::ARRAY_MAX);
  177. arr[RS::ARRAY_VERTEX] = vertices;
  178. arr[RS::ARRAY_TEX_UV] = uvs;
  179. arr[RS::ARRAY_COLOR] = colors;
  180. arr[RS::ARRAY_INDEX] = indices;
  181. RS::get_singleton()->mesh_clear(mesh);
  182. RS::get_singleton()->mesh_add_surface_from_arrays(mesh, RS::PRIMITIVE_TRIANGLES, arr);
  183. }
  184. void CPUParticles2D::set_texture(const Ref<Texture2D> &p_texture) {
  185. if (p_texture == texture) {
  186. return;
  187. }
  188. if (texture.is_valid()) {
  189. texture->disconnect_changed(callable_mp(this, &CPUParticles2D::_texture_changed));
  190. }
  191. texture = p_texture;
  192. if (texture.is_valid()) {
  193. texture->connect_changed(callable_mp(this, &CPUParticles2D::_texture_changed));
  194. }
  195. queue_redraw();
  196. _update_mesh_texture();
  197. }
  198. void CPUParticles2D::_texture_changed() {
  199. if (texture.is_valid()) {
  200. queue_redraw();
  201. _update_mesh_texture();
  202. }
  203. }
  204. void CPUParticles2D::_refresh_interpolation_state() {
  205. if (!is_inside_tree()) {
  206. return;
  207. }
  208. // The logic for whether to do an interpolated follow.
  209. // This is rather complex, but basically:
  210. // If project setting interpolation is ON and this particle system is in global mode,
  211. // we will follow the INTERPOLATED position rather than the actual position.
  212. // This is so that particles aren't generated AHEAD of the interpolated parent.
  213. bool follow = !local_coords && get_tree()->is_physics_interpolation_enabled();
  214. if (follow == _interpolation_data.interpolated_follow) {
  215. return;
  216. }
  217. _interpolation_data.interpolated_follow = follow;
  218. set_physics_process_internal(_interpolation_data.interpolated_follow);
  219. }
  220. Ref<Texture2D> CPUParticles2D::get_texture() const {
  221. return texture;
  222. }
  223. void CPUParticles2D::set_fixed_fps(int p_count) {
  224. fixed_fps = p_count;
  225. }
  226. int CPUParticles2D::get_fixed_fps() const {
  227. return fixed_fps;
  228. }
  229. void CPUParticles2D::set_fractional_delta(bool p_enable) {
  230. fractional_delta = p_enable;
  231. }
  232. bool CPUParticles2D::get_fractional_delta() const {
  233. return fractional_delta;
  234. }
  235. PackedStringArray CPUParticles2D::get_configuration_warnings() const {
  236. PackedStringArray warnings = Node2D::get_configuration_warnings();
  237. CanvasItemMaterial *mat = Object::cast_to<CanvasItemMaterial>(get_material().ptr());
  238. if (get_material().is_null() || (mat && !mat->get_particles_animation())) {
  239. if (get_param_max(PARAM_ANIM_SPEED) != 0.0 || get_param_max(PARAM_ANIM_OFFSET) != 0.0 ||
  240. get_param_curve(PARAM_ANIM_SPEED).is_valid() || get_param_curve(PARAM_ANIM_OFFSET).is_valid()) {
  241. warnings.push_back(RTR("CPUParticles2D animation requires the usage of a CanvasItemMaterial with \"Particles Animation\" enabled."));
  242. }
  243. }
  244. return warnings;
  245. }
  246. void CPUParticles2D::restart(bool p_keep_seed) {
  247. time = 0;
  248. frame_remainder = 0;
  249. cycle = 0;
  250. emitting = false;
  251. {
  252. int pc = particles.size();
  253. Particle *w = particles.ptrw();
  254. for (int i = 0; i < pc; i++) {
  255. w[i].active = false;
  256. }
  257. }
  258. if (!p_keep_seed && !use_fixed_seed) {
  259. seed = Math::rand();
  260. }
  261. emitting = true;
  262. _set_emitting();
  263. }
  264. void CPUParticles2D::set_direction(Vector2 p_direction) {
  265. direction = p_direction;
  266. }
  267. Vector2 CPUParticles2D::get_direction() const {
  268. return direction;
  269. }
  270. void CPUParticles2D::set_spread(real_t p_spread) {
  271. spread = p_spread;
  272. }
  273. real_t CPUParticles2D::get_spread() const {
  274. return spread;
  275. }
  276. void CPUParticles2D::set_param_min(Parameter p_param, real_t p_value) {
  277. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  278. parameters_min[p_param] = p_value;
  279. if (parameters_min[p_param] > parameters_max[p_param]) {
  280. set_param_max(p_param, p_value);
  281. }
  282. }
  283. real_t CPUParticles2D::get_param_min(Parameter p_param) const {
  284. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  285. return parameters_min[p_param];
  286. }
  287. void CPUParticles2D::set_param_max(Parameter p_param, real_t p_value) {
  288. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  289. parameters_max[p_param] = p_value;
  290. if (parameters_min[p_param] > parameters_max[p_param]) {
  291. set_param_min(p_param, p_value);
  292. }
  293. update_configuration_warnings();
  294. }
  295. real_t CPUParticles2D::get_param_max(Parameter p_param) const {
  296. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  297. return parameters_max[p_param];
  298. }
  299. static void _adjust_curve_range(const Ref<Curve> &p_curve, real_t p_min, real_t p_max) {
  300. Ref<Curve> curve = p_curve;
  301. if (curve.is_null()) {
  302. return;
  303. }
  304. curve->ensure_default_setup(p_min, p_max);
  305. }
  306. void CPUParticles2D::set_param_curve(Parameter p_param, const Ref<Curve> &p_curve) {
  307. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  308. curve_parameters[p_param] = p_curve;
  309. switch (p_param) {
  310. case PARAM_INITIAL_LINEAR_VELOCITY: {
  311. //do none for this one
  312. } break;
  313. case PARAM_ANGULAR_VELOCITY: {
  314. _adjust_curve_range(p_curve, -360, 360);
  315. } break;
  316. case PARAM_ORBIT_VELOCITY: {
  317. _adjust_curve_range(p_curve, -500, 500);
  318. } break;
  319. case PARAM_LINEAR_ACCEL: {
  320. _adjust_curve_range(p_curve, -200, 200);
  321. } break;
  322. case PARAM_RADIAL_ACCEL: {
  323. _adjust_curve_range(p_curve, -200, 200);
  324. } break;
  325. case PARAM_TANGENTIAL_ACCEL: {
  326. _adjust_curve_range(p_curve, -200, 200);
  327. } break;
  328. case PARAM_DAMPING: {
  329. _adjust_curve_range(p_curve, 0, 100);
  330. } break;
  331. case PARAM_ANGLE: {
  332. _adjust_curve_range(p_curve, -360, 360);
  333. } break;
  334. case PARAM_SCALE: {
  335. } break;
  336. case PARAM_HUE_VARIATION: {
  337. _adjust_curve_range(p_curve, -1, 1);
  338. } break;
  339. case PARAM_ANIM_SPEED: {
  340. _adjust_curve_range(p_curve, 0, 200);
  341. } break;
  342. case PARAM_ANIM_OFFSET: {
  343. } break;
  344. default: {
  345. }
  346. }
  347. update_configuration_warnings();
  348. }
  349. Ref<Curve> CPUParticles2D::get_param_curve(Parameter p_param) const {
  350. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, Ref<Curve>());
  351. return curve_parameters[p_param];
  352. }
  353. void CPUParticles2D::set_color(const Color &p_color) {
  354. color = p_color;
  355. }
  356. Color CPUParticles2D::get_color() const {
  357. return color;
  358. }
  359. void CPUParticles2D::set_color_ramp(const Ref<Gradient> &p_ramp) {
  360. color_ramp = p_ramp;
  361. }
  362. Ref<Gradient> CPUParticles2D::get_color_ramp() const {
  363. return color_ramp;
  364. }
  365. void CPUParticles2D::set_color_initial_ramp(const Ref<Gradient> &p_ramp) {
  366. color_initial_ramp = p_ramp;
  367. }
  368. Ref<Gradient> CPUParticles2D::get_color_initial_ramp() const {
  369. return color_initial_ramp;
  370. }
  371. void CPUParticles2D::set_particle_flag(ParticleFlags p_particle_flag, bool p_enable) {
  372. ERR_FAIL_INDEX(p_particle_flag, PARTICLE_FLAG_MAX);
  373. particle_flags[p_particle_flag] = p_enable;
  374. }
  375. bool CPUParticles2D::get_particle_flag(ParticleFlags p_particle_flag) const {
  376. ERR_FAIL_INDEX_V(p_particle_flag, PARTICLE_FLAG_MAX, false);
  377. return particle_flags[p_particle_flag];
  378. }
  379. void CPUParticles2D::set_emission_shape(EmissionShape p_shape) {
  380. ERR_FAIL_INDEX(p_shape, EMISSION_SHAPE_MAX);
  381. emission_shape = p_shape;
  382. notify_property_list_changed();
  383. }
  384. void CPUParticles2D::set_emission_sphere_radius(real_t p_radius) {
  385. emission_sphere_radius = p_radius;
  386. }
  387. void CPUParticles2D::set_emission_rect_extents(Vector2 p_extents) {
  388. emission_rect_extents = p_extents;
  389. }
  390. void CPUParticles2D::set_emission_points(const Vector<Vector2> &p_points) {
  391. emission_points = p_points;
  392. }
  393. void CPUParticles2D::set_emission_normals(const Vector<Vector2> &p_normals) {
  394. emission_normals = p_normals;
  395. }
  396. void CPUParticles2D::set_emission_colors(const Vector<Color> &p_colors) {
  397. emission_colors = p_colors;
  398. }
  399. real_t CPUParticles2D::get_emission_sphere_radius() const {
  400. return emission_sphere_radius;
  401. }
  402. Vector2 CPUParticles2D::get_emission_rect_extents() const {
  403. return emission_rect_extents;
  404. }
  405. Vector<Vector2> CPUParticles2D::get_emission_points() const {
  406. return emission_points;
  407. }
  408. Vector<Vector2> CPUParticles2D::get_emission_normals() const {
  409. return emission_normals;
  410. }
  411. Vector<Color> CPUParticles2D::get_emission_colors() const {
  412. return emission_colors;
  413. }
  414. CPUParticles2D::EmissionShape CPUParticles2D::get_emission_shape() const {
  415. return emission_shape;
  416. }
  417. void CPUParticles2D::set_gravity(const Vector2 &p_gravity) {
  418. gravity = p_gravity;
  419. }
  420. Vector2 CPUParticles2D::get_gravity() const {
  421. return gravity;
  422. }
  423. void CPUParticles2D::set_scale_curve_x(Ref<Curve> p_scale_curve) {
  424. scale_curve_x = p_scale_curve;
  425. }
  426. void CPUParticles2D::set_scale_curve_y(Ref<Curve> p_scale_curve) {
  427. scale_curve_y = p_scale_curve;
  428. }
  429. void CPUParticles2D::set_split_scale(bool p_split_scale) {
  430. split_scale = p_split_scale;
  431. notify_property_list_changed();
  432. }
  433. Ref<Curve> CPUParticles2D::get_scale_curve_x() const {
  434. return scale_curve_x;
  435. }
  436. Ref<Curve> CPUParticles2D::get_scale_curve_y() const {
  437. return scale_curve_y;
  438. }
  439. bool CPUParticles2D::get_split_scale() {
  440. return split_scale;
  441. }
  442. void CPUParticles2D::set_use_fixed_seed(bool p_use_fixed_seed) {
  443. if (p_use_fixed_seed == use_fixed_seed) {
  444. return;
  445. }
  446. use_fixed_seed = p_use_fixed_seed;
  447. notify_property_list_changed();
  448. }
  449. bool CPUParticles2D::get_use_fixed_seed() const {
  450. return use_fixed_seed;
  451. }
  452. void CPUParticles2D::set_seed(uint32_t p_seed) {
  453. seed = p_seed;
  454. }
  455. uint32_t CPUParticles2D::get_seed() const {
  456. return seed;
  457. }
  458. void CPUParticles2D::request_particles_process(real_t p_requested_process_time) {
  459. _requested_process_time = p_requested_process_time;
  460. }
  461. void CPUParticles2D::_validate_property(PropertyInfo &p_property) const {
  462. if (p_property.name == "emitting") {
  463. p_property.hint = one_shot ? PROPERTY_HINT_ONESHOT : PROPERTY_HINT_NONE;
  464. }
  465. if (p_property.name == "emission_sphere_radius" && (emission_shape != EMISSION_SHAPE_SPHERE && emission_shape != EMISSION_SHAPE_SPHERE_SURFACE)) {
  466. p_property.usage = PROPERTY_USAGE_NONE;
  467. }
  468. if (p_property.name == "emission_rect_extents" && emission_shape != EMISSION_SHAPE_RECTANGLE) {
  469. p_property.usage = PROPERTY_USAGE_NONE;
  470. }
  471. if ((p_property.name == "emission_point_texture" || p_property.name == "emission_color_texture") && (emission_shape < EMISSION_SHAPE_POINTS)) {
  472. p_property.usage = PROPERTY_USAGE_NONE;
  473. }
  474. if (p_property.name == "emission_normals" && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
  475. p_property.usage = PROPERTY_USAGE_NONE;
  476. }
  477. if (p_property.name == "emission_points" && emission_shape != EMISSION_SHAPE_POINTS && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
  478. p_property.usage = PROPERTY_USAGE_NONE;
  479. }
  480. if (p_property.name == "emission_colors" && emission_shape != EMISSION_SHAPE_POINTS && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
  481. p_property.usage = PROPERTY_USAGE_NONE;
  482. }
  483. if (p_property.name.begins_with("scale_curve_") && !split_scale) {
  484. p_property.usage = PROPERTY_USAGE_NONE;
  485. }
  486. if (p_property.name == "seed" && !use_fixed_seed) {
  487. p_property.usage = PROPERTY_USAGE_NONE;
  488. }
  489. }
  490. static uint32_t idhash(uint32_t x) {
  491. x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
  492. x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b);
  493. x = (x >> uint32_t(16)) ^ x;
  494. return x;
  495. }
  496. static real_t rand_from_seed(uint32_t &seed) {
  497. int k;
  498. int s = int(seed);
  499. if (s == 0) {
  500. s = 305420679;
  501. }
  502. k = s / 127773;
  503. s = 16807 * (s - k * 127773) - 2836 * k;
  504. if (s < 0) {
  505. s += 2147483647;
  506. }
  507. seed = uint32_t(s);
  508. return (seed % uint32_t(65536)) / 65535.0;
  509. }
  510. void CPUParticles2D::_update_internal() {
  511. if (particles.size() == 0 || !is_visible_in_tree()) {
  512. _set_do_redraw(false);
  513. return;
  514. }
  515. // Change update mode?
  516. _refresh_interpolation_state();
  517. double delta = get_process_delta_time();
  518. if (!active && !emitting) {
  519. set_process_internal(false);
  520. _set_do_redraw(false);
  521. //reset variables
  522. time = 0;
  523. frame_remainder = 0;
  524. cycle = 0;
  525. return;
  526. }
  527. _set_do_redraw(true);
  528. double frame_time;
  529. if (fixed_fps > 0) {
  530. frame_time = 1.0 / fixed_fps;
  531. } else {
  532. frame_time = 1.0 / 30.0;
  533. }
  534. double todo = _requested_process_time;
  535. _requested_process_time = 0;
  536. if (time == 0 && pre_process_time > 0.0) {
  537. todo += pre_process_time;
  538. }
  539. real_t tmp_speed = speed_scale;
  540. speed_scale = 1.0;
  541. while (todo > 0) {
  542. _particles_process(frame_time);
  543. todo -= frame_time;
  544. }
  545. speed_scale = tmp_speed;
  546. todo = 0.0;
  547. if (fixed_fps > 0) {
  548. double decr = frame_time;
  549. double ldelta = delta;
  550. if (ldelta > 0.1) { //avoid recursive stalls if fps goes below 10
  551. ldelta = 0.1;
  552. } else if (ldelta <= 0.0) { //unlikely but..
  553. ldelta = 0.001;
  554. }
  555. todo = frame_remainder + ldelta;
  556. while (todo >= frame_time) {
  557. _particles_process(frame_time);
  558. todo -= decr;
  559. }
  560. frame_remainder = todo;
  561. } else {
  562. _particles_process(delta);
  563. }
  564. _update_particle_data_buffer();
  565. }
  566. void CPUParticles2D::_particles_process(double p_delta) {
  567. p_delta *= speed_scale;
  568. int pcount = particles.size();
  569. Particle *w = particles.ptrw();
  570. Particle *parray = w;
  571. double prev_time = time;
  572. time += p_delta;
  573. if (time > lifetime) {
  574. time = Math::fmod(time, lifetime);
  575. cycle++;
  576. if (one_shot && cycle > 0) {
  577. set_emitting(false);
  578. notify_property_list_changed();
  579. }
  580. }
  581. Transform2D emission_xform;
  582. Transform2D velocity_xform;
  583. if (!local_coords) {
  584. if (!_interpolation_data.interpolated_follow) {
  585. emission_xform = get_global_transform();
  586. } else {
  587. TransformInterpolator::interpolate_transform_2d(_interpolation_data.global_xform_prev, _interpolation_data.global_xform_curr, emission_xform, Engine::get_singleton()->get_physics_interpolation_fraction());
  588. }
  589. velocity_xform = emission_xform;
  590. velocity_xform[2] = Vector2();
  591. }
  592. double system_phase = time / lifetime;
  593. bool should_be_active = false;
  594. for (int i = 0; i < pcount; i++) {
  595. Particle &p = parray[i];
  596. if (!emitting && !p.active) {
  597. continue;
  598. }
  599. double local_delta = p_delta;
  600. // The phase is a ratio between 0 (birth) and 1 (end of life) for each particle.
  601. // While we use time in tests later on, for randomness we use the phase as done in the
  602. // original shader code, and we later multiply by lifetime to get the time.
  603. double restart_phase = double(i) / double(pcount);
  604. if (randomness_ratio > 0.0) {
  605. uint32_t _seed = cycle;
  606. if (restart_phase >= system_phase) {
  607. _seed -= uint32_t(1);
  608. }
  609. _seed *= uint32_t(pcount);
  610. _seed += uint32_t(i);
  611. double random = double(idhash(_seed) % uint32_t(65536)) / 65536.0;
  612. restart_phase += randomness_ratio * random * 1.0 / double(pcount);
  613. }
  614. restart_phase *= (1.0 - explosiveness_ratio);
  615. double restart_time = restart_phase * lifetime;
  616. bool restart = false;
  617. if (time > prev_time) {
  618. // restart_time >= prev_time is used so particles emit in the first frame they are processed
  619. if (restart_time >= prev_time && restart_time < time) {
  620. restart = true;
  621. if (fractional_delta) {
  622. local_delta = time - restart_time;
  623. }
  624. }
  625. } else if (local_delta > 0.0) {
  626. if (restart_time >= prev_time) {
  627. restart = true;
  628. if (fractional_delta) {
  629. local_delta = lifetime - restart_time + time;
  630. }
  631. } else if (restart_time < time) {
  632. restart = true;
  633. if (fractional_delta) {
  634. local_delta = time - restart_time;
  635. }
  636. }
  637. }
  638. if (p.time * (1.0 - explosiveness_ratio) > p.lifetime) {
  639. restart = true;
  640. }
  641. float tv = 0.0;
  642. if (restart) {
  643. if (!emitting) {
  644. p.active = false;
  645. continue;
  646. }
  647. p.active = true;
  648. /*real_t tex_linear_velocity = 0;
  649. if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  650. tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->sample(0);
  651. }*/
  652. real_t tex_angle = 1.0;
  653. if (curve_parameters[PARAM_ANGLE].is_valid()) {
  654. tex_angle = curve_parameters[PARAM_ANGLE]->sample(tv);
  655. }
  656. real_t tex_anim_offset = 1.0;
  657. if (curve_parameters[PARAM_ANGLE].is_valid()) {
  658. tex_anim_offset = curve_parameters[PARAM_ANGLE]->sample(tv);
  659. }
  660. p.seed = seed + uint32_t(i) + i + cycle;
  661. rng->set_seed(p.seed);
  662. p.angle_rand = rng->randf();
  663. p.scale_rand = rng->randf();
  664. p.hue_rot_rand = rng->randf();
  665. p.anim_offset_rand = rng->randf();
  666. if (color_initial_ramp.is_valid()) {
  667. p.start_color_rand = color_initial_ramp->get_color_at_offset(rng->randf());
  668. } else {
  669. p.start_color_rand = Color(1, 1, 1, 1);
  670. }
  671. real_t angle1_rad = direction.angle() + Math::deg_to_rad((rng->randf() * 2.0 - 1.0) * spread);
  672. Vector2 rot = Vector2(Math::cos(angle1_rad), Math::sin(angle1_rad));
  673. p.velocity = rot * Math::lerp(parameters_min[PARAM_INITIAL_LINEAR_VELOCITY], parameters_max[PARAM_INITIAL_LINEAR_VELOCITY], rng->randf());
  674. real_t base_angle = tex_angle * Math::lerp(parameters_min[PARAM_ANGLE], parameters_max[PARAM_ANGLE], p.angle_rand);
  675. p.rotation = Math::deg_to_rad(base_angle);
  676. p.custom[0] = 0.0; // unused
  677. p.custom[1] = 0.0; // phase [0..1]
  678. p.custom[2] = tex_anim_offset * Math::lerp(parameters_min[PARAM_ANIM_OFFSET], parameters_max[PARAM_ANIM_OFFSET], p.anim_offset_rand);
  679. p.custom[3] = (1.0 - rng->randf() * lifetime_randomness);
  680. p.transform = Transform2D();
  681. p.time = 0;
  682. p.lifetime = lifetime * p.custom[3];
  683. p.base_color = Color(1, 1, 1, 1);
  684. switch (emission_shape) {
  685. case EMISSION_SHAPE_POINT: {
  686. //do none
  687. } break;
  688. case EMISSION_SHAPE_SPHERE: {
  689. real_t t = Math_TAU * rng->randf();
  690. real_t radius = emission_sphere_radius * rng->randf();
  691. p.transform[2] = Vector2(Math::cos(t), Math::sin(t)) * radius;
  692. } break;
  693. case EMISSION_SHAPE_SPHERE_SURFACE: {
  694. real_t s = rng->randf(), t = Math_TAU * rng->randf();
  695. real_t radius = emission_sphere_radius * Math::sqrt(1.0 - s * s);
  696. p.transform[2] = Vector2(Math::cos(t), Math::sin(t)) * radius;
  697. } break;
  698. case EMISSION_SHAPE_RECTANGLE: {
  699. p.transform[2] = Vector2(rng->randf() * 2.0 - 1.0, rng->randf() * 2.0 - 1.0) * emission_rect_extents;
  700. } break;
  701. case EMISSION_SHAPE_POINTS:
  702. case EMISSION_SHAPE_DIRECTED_POINTS: {
  703. int pc = emission_points.size();
  704. if (pc == 0) {
  705. break;
  706. }
  707. int random_idx = Math::rand() % pc;
  708. p.transform[2] = emission_points.get(random_idx);
  709. if (emission_shape == EMISSION_SHAPE_DIRECTED_POINTS && emission_normals.size() == pc) {
  710. Vector2 normal = emission_normals.get(random_idx);
  711. Transform2D m2;
  712. m2.columns[0] = normal;
  713. m2.columns[1] = normal.orthogonal();
  714. p.velocity = m2.basis_xform(p.velocity);
  715. }
  716. if (emission_colors.size() == pc) {
  717. p.base_color = emission_colors.get(random_idx);
  718. }
  719. } break;
  720. case EMISSION_SHAPE_MAX: { // Max value for validity check.
  721. break;
  722. }
  723. }
  724. if (!local_coords) {
  725. p.velocity = velocity_xform.xform(p.velocity);
  726. p.transform = emission_xform * p.transform;
  727. }
  728. } else if (!p.active) {
  729. continue;
  730. } else if (p.time > p.lifetime) {
  731. p.active = false;
  732. tv = 1.0;
  733. } else {
  734. uint32_t _seed = p.seed;
  735. p.time += local_delta;
  736. p.custom[1] = p.time / lifetime;
  737. tv = p.time / p.lifetime;
  738. real_t tex_linear_velocity = 1.0;
  739. if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  740. tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->sample(tv);
  741. }
  742. real_t tex_orbit_velocity = 1.0;
  743. if (curve_parameters[PARAM_ORBIT_VELOCITY].is_valid()) {
  744. tex_orbit_velocity = curve_parameters[PARAM_ORBIT_VELOCITY]->sample(tv);
  745. }
  746. real_t tex_angular_velocity = 1.0;
  747. if (curve_parameters[PARAM_ANGULAR_VELOCITY].is_valid()) {
  748. tex_angular_velocity = curve_parameters[PARAM_ANGULAR_VELOCITY]->sample(tv);
  749. }
  750. real_t tex_linear_accel = 1.0;
  751. if (curve_parameters[PARAM_LINEAR_ACCEL].is_valid()) {
  752. tex_linear_accel = curve_parameters[PARAM_LINEAR_ACCEL]->sample(tv);
  753. }
  754. real_t tex_tangential_accel = 1.0;
  755. if (curve_parameters[PARAM_TANGENTIAL_ACCEL].is_valid()) {
  756. tex_tangential_accel = curve_parameters[PARAM_TANGENTIAL_ACCEL]->sample(tv);
  757. }
  758. real_t tex_radial_accel = 1.0;
  759. if (curve_parameters[PARAM_RADIAL_ACCEL].is_valid()) {
  760. tex_radial_accel = curve_parameters[PARAM_RADIAL_ACCEL]->sample(tv);
  761. }
  762. real_t tex_damping = 1.0;
  763. if (curve_parameters[PARAM_DAMPING].is_valid()) {
  764. tex_damping = curve_parameters[PARAM_DAMPING]->sample(tv);
  765. }
  766. real_t tex_angle = 1.0;
  767. if (curve_parameters[PARAM_ANGLE].is_valid()) {
  768. tex_angle = curve_parameters[PARAM_ANGLE]->sample(tv);
  769. }
  770. real_t tex_anim_speed = 1.0;
  771. if (curve_parameters[PARAM_ANIM_SPEED].is_valid()) {
  772. tex_anim_speed = curve_parameters[PARAM_ANIM_SPEED]->sample(tv);
  773. }
  774. real_t tex_anim_offset = 1.0;
  775. if (curve_parameters[PARAM_ANIM_OFFSET].is_valid()) {
  776. tex_anim_offset = curve_parameters[PARAM_ANIM_OFFSET]->sample(tv);
  777. }
  778. Vector2 force = gravity;
  779. Vector2 pos = p.transform[2];
  780. //apply linear acceleration
  781. force += p.velocity.length() > 0.0 ? p.velocity.normalized() * tex_linear_accel * Math::lerp(parameters_min[PARAM_LINEAR_ACCEL], parameters_max[PARAM_LINEAR_ACCEL], rand_from_seed(_seed)) : Vector2();
  782. //apply radial acceleration
  783. Vector2 org = emission_xform[2];
  784. Vector2 diff = pos - org;
  785. force += diff.length() > 0.0 ? diff.normalized() * (tex_radial_accel)*Math::lerp(parameters_min[PARAM_RADIAL_ACCEL], parameters_max[PARAM_RADIAL_ACCEL], rand_from_seed(_seed)) : Vector2();
  786. //apply tangential acceleration;
  787. Vector2 yx = Vector2(diff.y, diff.x);
  788. force += yx.length() > 0.0 ? (yx * Vector2(-1.0, 1.0)).normalized() * (tex_tangential_accel * Math::lerp(parameters_min[PARAM_TANGENTIAL_ACCEL], parameters_max[PARAM_TANGENTIAL_ACCEL], rand_from_seed(_seed))) : Vector2();
  789. //apply attractor forces
  790. p.velocity += force * local_delta;
  791. //orbit velocity
  792. real_t orbit_amount = tex_orbit_velocity * Math::lerp(parameters_min[PARAM_ORBIT_VELOCITY], parameters_max[PARAM_ORBIT_VELOCITY], rand_from_seed(_seed));
  793. if (orbit_amount != 0.0) {
  794. real_t ang = orbit_amount * local_delta * Math_TAU;
  795. // Not sure why the ParticleProcessMaterial code uses a clockwise rotation matrix,
  796. // but we use -ang here to reproduce its behavior.
  797. Transform2D rot = Transform2D(-ang, Vector2());
  798. p.transform[2] -= diff;
  799. p.transform[2] += rot.basis_xform(diff);
  800. }
  801. if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  802. p.velocity = p.velocity.normalized() * tex_linear_velocity;
  803. }
  804. if (parameters_max[PARAM_DAMPING] + tex_damping > 0.0) {
  805. real_t v = p.velocity.length();
  806. real_t damp = tex_damping * Math::lerp(parameters_min[PARAM_DAMPING], parameters_max[PARAM_DAMPING], rand_from_seed(_seed));
  807. v -= damp * local_delta;
  808. if (v < 0.0) {
  809. p.velocity = Vector2();
  810. } else {
  811. p.velocity = p.velocity.normalized() * v;
  812. }
  813. }
  814. real_t base_angle = (tex_angle)*Math::lerp(parameters_min[PARAM_ANGLE], parameters_max[PARAM_ANGLE], p.angle_rand);
  815. base_angle += p.custom[1] * lifetime * tex_angular_velocity * Math::lerp(parameters_min[PARAM_ANGULAR_VELOCITY], parameters_max[PARAM_ANGULAR_VELOCITY], rand_from_seed(_seed));
  816. p.rotation = Math::deg_to_rad(base_angle); //angle
  817. p.custom[2] = tex_anim_offset * Math::lerp(parameters_min[PARAM_ANIM_OFFSET], parameters_max[PARAM_ANIM_OFFSET], p.anim_offset_rand) + tv * tex_anim_speed * Math::lerp(parameters_min[PARAM_ANIM_SPEED], parameters_max[PARAM_ANIM_SPEED], rand_from_seed(_seed));
  818. }
  819. //apply color
  820. //apply hue rotation
  821. Vector2 tex_scale = Vector2(1.0, 1.0);
  822. if (split_scale) {
  823. if (scale_curve_x.is_valid()) {
  824. tex_scale.x = scale_curve_x->sample(tv);
  825. } else {
  826. tex_scale.x = 1.0;
  827. }
  828. if (scale_curve_y.is_valid()) {
  829. tex_scale.y = scale_curve_y->sample(tv);
  830. } else {
  831. tex_scale.y = 1.0;
  832. }
  833. } else {
  834. if (curve_parameters[PARAM_SCALE].is_valid()) {
  835. real_t tmp_scale = curve_parameters[PARAM_SCALE]->sample(tv);
  836. tex_scale.x = tmp_scale;
  837. tex_scale.y = tmp_scale;
  838. }
  839. }
  840. real_t tex_hue_variation = 0.0;
  841. if (curve_parameters[PARAM_HUE_VARIATION].is_valid()) {
  842. tex_hue_variation = curve_parameters[PARAM_HUE_VARIATION]->sample(tv);
  843. }
  844. real_t hue_rot_angle = (tex_hue_variation)*Math_TAU * Math::lerp(parameters_min[PARAM_HUE_VARIATION], parameters_max[PARAM_HUE_VARIATION], p.hue_rot_rand);
  845. real_t hue_rot_c = Math::cos(hue_rot_angle);
  846. real_t hue_rot_s = Math::sin(hue_rot_angle);
  847. Basis hue_rot_mat;
  848. {
  849. Basis mat1(0.299, 0.587, 0.114, 0.299, 0.587, 0.114, 0.299, 0.587, 0.114);
  850. Basis mat2(0.701, -0.587, -0.114, -0.299, 0.413, -0.114, -0.300, -0.588, 0.886);
  851. Basis mat3(0.168, 0.330, -0.497, -0.328, 0.035, 0.292, 1.250, -1.050, -0.203);
  852. for (int j = 0; j < 3; j++) {
  853. hue_rot_mat[j] = mat1[j] + mat2[j] * hue_rot_c + mat3[j] * hue_rot_s;
  854. }
  855. }
  856. if (color_ramp.is_valid()) {
  857. p.color = color_ramp->get_color_at_offset(tv) * color;
  858. } else {
  859. p.color = color;
  860. }
  861. Vector3 color_rgb = hue_rot_mat.xform_inv(Vector3(p.color.r, p.color.g, p.color.b));
  862. p.color.r = color_rgb.x;
  863. p.color.g = color_rgb.y;
  864. p.color.b = color_rgb.z;
  865. p.color *= p.base_color * p.start_color_rand;
  866. if (particle_flags[PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY]) {
  867. if (p.velocity.length() > 0.0) {
  868. p.transform.columns[1] = p.velocity.normalized();
  869. p.transform.columns[0] = p.transform.columns[1].orthogonal();
  870. }
  871. } else {
  872. p.transform.columns[0] = Vector2(Math::cos(p.rotation), -Math::sin(p.rotation));
  873. p.transform.columns[1] = Vector2(Math::sin(p.rotation), Math::cos(p.rotation));
  874. }
  875. //scale by scale
  876. Vector2 base_scale = tex_scale * Math::lerp(parameters_min[PARAM_SCALE], parameters_max[PARAM_SCALE], p.scale_rand);
  877. if (base_scale.x < 0.00001) {
  878. base_scale.x = 0.00001;
  879. }
  880. if (base_scale.y < 0.00001) {
  881. base_scale.y = 0.00001;
  882. }
  883. p.transform.columns[0] *= base_scale.x;
  884. p.transform.columns[1] *= base_scale.y;
  885. p.transform[2] += p.velocity * local_delta;
  886. should_be_active = true;
  887. }
  888. if (!Math::is_equal_approx(time, 0.0) && active && !should_be_active) {
  889. active = false;
  890. emit_signal(SceneStringName(finished));
  891. }
  892. }
  893. void CPUParticles2D::_update_particle_data_buffer() {
  894. MutexLock lock(update_mutex);
  895. int pc = particles.size();
  896. int *ow;
  897. int *order = nullptr;
  898. float *w = particle_data.ptrw();
  899. const Particle *r = particles.ptr();
  900. float *ptr = w;
  901. if (draw_order != DRAW_ORDER_INDEX) {
  902. ow = particle_order.ptrw();
  903. order = ow;
  904. for (int i = 0; i < pc; i++) {
  905. order[i] = i;
  906. }
  907. if (draw_order == DRAW_ORDER_LIFETIME) {
  908. SortArray<int, SortLifetime> sorter;
  909. sorter.compare.particles = r;
  910. sorter.sort(order, pc);
  911. }
  912. }
  913. for (int i = 0; i < pc; i++) {
  914. int idx = order ? order[i] : i;
  915. Transform2D t = r[idx].transform;
  916. if (!local_coords) {
  917. t = inv_emission_transform * t;
  918. }
  919. if (r[idx].active) {
  920. ptr[0] = t.columns[0][0];
  921. ptr[1] = t.columns[1][0];
  922. ptr[2] = 0;
  923. ptr[3] = t.columns[2][0];
  924. ptr[4] = t.columns[0][1];
  925. ptr[5] = t.columns[1][1];
  926. ptr[6] = 0;
  927. ptr[7] = t.columns[2][1];
  928. } else {
  929. memset(ptr, 0, sizeof(float) * 8);
  930. }
  931. Color c = r[idx].color;
  932. ptr[8] = c.r;
  933. ptr[9] = c.g;
  934. ptr[10] = c.b;
  935. ptr[11] = c.a;
  936. ptr[12] = r[idx].custom[0];
  937. ptr[13] = r[idx].custom[1];
  938. ptr[14] = r[idx].custom[2];
  939. ptr[15] = r[idx].custom[3];
  940. ptr += 16;
  941. }
  942. }
  943. void CPUParticles2D::_set_do_redraw(bool p_do_redraw) {
  944. if (do_redraw == p_do_redraw) {
  945. return;
  946. }
  947. do_redraw = p_do_redraw;
  948. {
  949. MutexLock lock(update_mutex);
  950. if (do_redraw) {
  951. RS::get_singleton()->connect("frame_pre_draw", callable_mp(this, &CPUParticles2D::_update_render_thread));
  952. RS::get_singleton()->canvas_item_set_update_when_visible(get_canvas_item(), true);
  953. RS::get_singleton()->multimesh_set_visible_instances(multimesh, -1);
  954. } else {
  955. if (RS::get_singleton()->is_connected("frame_pre_draw", callable_mp(this, &CPUParticles2D::_update_render_thread))) {
  956. RS::get_singleton()->disconnect("frame_pre_draw", callable_mp(this, &CPUParticles2D::_update_render_thread));
  957. }
  958. RS::get_singleton()->canvas_item_set_update_when_visible(get_canvas_item(), false);
  959. RS::get_singleton()->multimesh_set_visible_instances(multimesh, 0);
  960. }
  961. }
  962. queue_redraw(); // redraw to update render list
  963. }
  964. void CPUParticles2D::_update_render_thread() {
  965. MutexLock lock(update_mutex);
  966. RS::get_singleton()->multimesh_set_buffer(multimesh, particle_data);
  967. }
  968. void CPUParticles2D::_notification(int p_what) {
  969. switch (p_what) {
  970. case NOTIFICATION_ENTER_TREE: {
  971. set_process_internal(emitting);
  972. _refresh_interpolation_state();
  973. set_physics_process_internal(emitting && _interpolation_data.interpolated_follow);
  974. // If we are interpolated following, then reset physics interpolation
  975. // when first appearing. This won't be called by canvas item, as in the
  976. // following mode, is_physics_interpolated() is actually FALSE.
  977. if (_interpolation_data.interpolated_follow) {
  978. notification(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
  979. }
  980. } break;
  981. case NOTIFICATION_EXIT_TREE: {
  982. _set_do_redraw(false);
  983. } break;
  984. case NOTIFICATION_DRAW: {
  985. // first update before rendering to avoid one frame delay after emitting starts
  986. if (emitting && (time == 0)) {
  987. _update_internal();
  988. }
  989. if (!do_redraw) {
  990. return; // don't add to render list
  991. }
  992. RID texrid;
  993. if (texture.is_valid()) {
  994. texrid = texture->get_rid();
  995. }
  996. RS::get_singleton()->canvas_item_add_multimesh(get_canvas_item(), multimesh, texrid);
  997. } break;
  998. case NOTIFICATION_INTERNAL_PROCESS: {
  999. _update_internal();
  1000. } break;
  1001. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  1002. if (_interpolation_data.interpolated_follow) {
  1003. // Keep the interpolated follow target updated.
  1004. _interpolation_data.global_xform_prev = _interpolation_data.global_xform_curr;
  1005. _interpolation_data.global_xform_curr = get_global_transform();
  1006. }
  1007. } break;
  1008. case NOTIFICATION_TRANSFORM_CHANGED: {
  1009. if (_interpolation_data.interpolated_follow) {
  1010. // If the transform has been updated AFTER the physics tick, keep data flowing.
  1011. if (Engine::get_singleton()->is_in_physics_frame()) {
  1012. _interpolation_data.global_xform_curr = get_global_transform();
  1013. }
  1014. }
  1015. } break;
  1016. case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
  1017. // Make sure current is up to date with any pending global transform changes.
  1018. _interpolation_data.global_xform_curr = get_global_transform_const();
  1019. _interpolation_data.global_xform_prev = _interpolation_data.global_xform_curr;
  1020. } break;
  1021. }
  1022. }
  1023. void CPUParticles2D::convert_from_particles(Node *p_particles) {
  1024. GPUParticles2D *gpu_particles = Object::cast_to<GPUParticles2D>(p_particles);
  1025. ERR_FAIL_NULL_MSG(gpu_particles, "Only GPUParticles2D nodes can be converted to CPUParticles2D.");
  1026. set_emitting(gpu_particles->is_emitting());
  1027. set_amount(gpu_particles->get_amount());
  1028. set_lifetime(gpu_particles->get_lifetime());
  1029. set_one_shot(gpu_particles->get_one_shot());
  1030. set_pre_process_time(gpu_particles->get_pre_process_time());
  1031. set_explosiveness_ratio(gpu_particles->get_explosiveness_ratio());
  1032. set_randomness_ratio(gpu_particles->get_randomness_ratio());
  1033. set_use_local_coordinates(gpu_particles->get_use_local_coordinates());
  1034. set_fixed_fps(gpu_particles->get_fixed_fps());
  1035. set_fractional_delta(gpu_particles->get_fractional_delta());
  1036. set_speed_scale(gpu_particles->get_speed_scale());
  1037. set_draw_order(DrawOrder(gpu_particles->get_draw_order()));
  1038. set_texture(gpu_particles->get_texture());
  1039. Ref<Material> mat = gpu_particles->get_material();
  1040. if (mat.is_valid()) {
  1041. set_material(mat);
  1042. }
  1043. Ref<ParticleProcessMaterial> proc_mat = gpu_particles->get_process_material();
  1044. if (proc_mat.is_null()) {
  1045. return;
  1046. }
  1047. Vector3 dir = proc_mat->get_direction();
  1048. set_direction(Vector2(dir.x, dir.y));
  1049. set_spread(proc_mat->get_spread());
  1050. set_color(proc_mat->get_color());
  1051. Ref<GradientTexture1D> gt = proc_mat->get_color_ramp();
  1052. if (gt.is_valid()) {
  1053. set_color_ramp(gt->get_gradient());
  1054. }
  1055. Ref<GradientTexture1D> gti = proc_mat->get_color_initial_ramp();
  1056. if (gti.is_valid()) {
  1057. set_color_initial_ramp(gti->get_gradient());
  1058. }
  1059. set_particle_flag(PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY, proc_mat->get_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY));
  1060. set_emission_shape(EmissionShape(proc_mat->get_emission_shape()));
  1061. set_emission_sphere_radius(proc_mat->get_emission_sphere_radius());
  1062. Vector2 rect_extents = Vector2(proc_mat->get_emission_box_extents().x, proc_mat->get_emission_box_extents().y);
  1063. set_emission_rect_extents(rect_extents);
  1064. Ref<CurveXYZTexture> scale3D = proc_mat->get_param_texture(ParticleProcessMaterial::PARAM_SCALE);
  1065. if (scale3D.is_valid()) {
  1066. split_scale = true;
  1067. scale_curve_x = scale3D->get_curve_x();
  1068. scale_curve_y = scale3D->get_curve_y();
  1069. }
  1070. set_gravity(Vector2(proc_mat->get_gravity().x, proc_mat->get_gravity().y));
  1071. set_lifetime_randomness(proc_mat->get_lifetime_randomness());
  1072. #define CONVERT_PARAM(m_param) \
  1073. set_param_min(m_param, proc_mat->get_param_min(ParticleProcessMaterial::m_param)); \
  1074. { \
  1075. Ref<CurveTexture> ctex = proc_mat->get_param_texture(ParticleProcessMaterial::m_param); \
  1076. if (ctex.is_valid()) \
  1077. set_param_curve(m_param, ctex->get_curve()); \
  1078. } \
  1079. set_param_max(m_param, proc_mat->get_param_max(ParticleProcessMaterial::m_param));
  1080. CONVERT_PARAM(PARAM_INITIAL_LINEAR_VELOCITY);
  1081. CONVERT_PARAM(PARAM_ANGULAR_VELOCITY);
  1082. CONVERT_PARAM(PARAM_ORBIT_VELOCITY);
  1083. CONVERT_PARAM(PARAM_LINEAR_ACCEL);
  1084. CONVERT_PARAM(PARAM_RADIAL_ACCEL);
  1085. CONVERT_PARAM(PARAM_TANGENTIAL_ACCEL);
  1086. CONVERT_PARAM(PARAM_DAMPING);
  1087. CONVERT_PARAM(PARAM_ANGLE);
  1088. CONVERT_PARAM(PARAM_SCALE);
  1089. CONVERT_PARAM(PARAM_HUE_VARIATION);
  1090. CONVERT_PARAM(PARAM_ANIM_SPEED);
  1091. CONVERT_PARAM(PARAM_ANIM_OFFSET);
  1092. #undef CONVERT_PARAM
  1093. }
  1094. void CPUParticles2D::_bind_methods() {
  1095. ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &CPUParticles2D::set_emitting);
  1096. ClassDB::bind_method(D_METHOD("set_amount", "amount"), &CPUParticles2D::set_amount);
  1097. ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &CPUParticles2D::set_lifetime);
  1098. ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &CPUParticles2D::set_one_shot);
  1099. ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &CPUParticles2D::set_pre_process_time);
  1100. ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &CPUParticles2D::set_explosiveness_ratio);
  1101. ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &CPUParticles2D::set_randomness_ratio);
  1102. ClassDB::bind_method(D_METHOD("set_lifetime_randomness", "random"), &CPUParticles2D::set_lifetime_randomness);
  1103. ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &CPUParticles2D::set_use_local_coordinates);
  1104. ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &CPUParticles2D::set_fixed_fps);
  1105. ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &CPUParticles2D::set_fractional_delta);
  1106. ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &CPUParticles2D::set_speed_scale);
  1107. ClassDB::bind_method(D_METHOD("request_particles_process", "process_time"), &CPUParticles2D::request_particles_process);
  1108. ClassDB::bind_method(D_METHOD("is_emitting"), &CPUParticles2D::is_emitting);
  1109. ClassDB::bind_method(D_METHOD("get_amount"), &CPUParticles2D::get_amount);
  1110. ClassDB::bind_method(D_METHOD("get_lifetime"), &CPUParticles2D::get_lifetime);
  1111. ClassDB::bind_method(D_METHOD("get_one_shot"), &CPUParticles2D::get_one_shot);
  1112. ClassDB::bind_method(D_METHOD("get_pre_process_time"), &CPUParticles2D::get_pre_process_time);
  1113. ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &CPUParticles2D::get_explosiveness_ratio);
  1114. ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &CPUParticles2D::get_randomness_ratio);
  1115. ClassDB::bind_method(D_METHOD("get_lifetime_randomness"), &CPUParticles2D::get_lifetime_randomness);
  1116. ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &CPUParticles2D::get_use_local_coordinates);
  1117. ClassDB::bind_method(D_METHOD("get_fixed_fps"), &CPUParticles2D::get_fixed_fps);
  1118. ClassDB::bind_method(D_METHOD("get_fractional_delta"), &CPUParticles2D::get_fractional_delta);
  1119. ClassDB::bind_method(D_METHOD("get_speed_scale"), &CPUParticles2D::get_speed_scale);
  1120. ClassDB::bind_method(D_METHOD("set_use_fixed_seed", "use_fixed_seed"), &CPUParticles2D::set_use_fixed_seed);
  1121. ClassDB::bind_method(D_METHOD("get_use_fixed_seed"), &CPUParticles2D::get_use_fixed_seed);
  1122. ClassDB::bind_method(D_METHOD("set_seed", "seed"), &CPUParticles2D::set_seed);
  1123. ClassDB::bind_method(D_METHOD("get_seed"), &CPUParticles2D::get_seed);
  1124. ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &CPUParticles2D::set_draw_order);
  1125. ClassDB::bind_method(D_METHOD("get_draw_order"), &CPUParticles2D::get_draw_order);
  1126. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &CPUParticles2D::set_texture);
  1127. ClassDB::bind_method(D_METHOD("get_texture"), &CPUParticles2D::get_texture);
  1128. ClassDB::bind_method(D_METHOD("restart", "keep_seed"), &CPUParticles2D::restart, DEFVAL(false));
  1129. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting", PROPERTY_HINT_ONESHOT), "set_emitting", "is_emitting");
  1130. ADD_PROPERTY(PropertyInfo(Variant::INT, "amount", PROPERTY_HINT_RANGE, "1,1000000,1,exp"), "set_amount", "get_amount"); // FIXME: Evaluate support for `exp` in integer properties, or remove this.
  1131. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  1132. ADD_GROUP("Time", "");
  1133. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime", PROPERTY_HINT_RANGE, "0.01,600.0,0.01,or_greater,exp,suffix:s"), "set_lifetime", "get_lifetime");
  1134. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
  1135. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "preprocess", PROPERTY_HINT_RANGE, "0.00,10.0,0.01,or_greater,exp,suffix:s"), "set_pre_process_time", "get_pre_process_time");
  1136. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
  1137. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
  1138. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
  1139. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_fixed_seed"), "set_use_fixed_seed", "get_use_fixed_seed");
  1140. ADD_PROPERTY(PropertyInfo(Variant::INT, "seed", PROPERTY_HINT_RANGE, "0," + itos(UINT32_MAX) + ",1"), "set_seed", "get_seed");
  1141. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime_randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_lifetime_randomness", "get_lifetime_randomness");
  1142. ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1,suffix:FPS"), "set_fixed_fps", "get_fixed_fps");
  1143. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
  1144. ADD_GROUP("Drawing", "");
  1145. // No visibility_rect property contrarily to Particles2D, it's updated automatically.
  1146. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
  1147. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime"), "set_draw_order", "get_draw_order");
  1148. BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
  1149. BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
  1150. ADD_PROPERTY_DEFAULT("seed", 0);
  1151. ////////////////////////////////
  1152. ClassDB::bind_method(D_METHOD("set_direction", "direction"), &CPUParticles2D::set_direction);
  1153. ClassDB::bind_method(D_METHOD("get_direction"), &CPUParticles2D::get_direction);
  1154. ClassDB::bind_method(D_METHOD("set_spread", "spread"), &CPUParticles2D::set_spread);
  1155. ClassDB::bind_method(D_METHOD("get_spread"), &CPUParticles2D::get_spread);
  1156. ClassDB::bind_method(D_METHOD("set_param_min", "param", "value"), &CPUParticles2D::set_param_min);
  1157. ClassDB::bind_method(D_METHOD("get_param_min", "param"), &CPUParticles2D::get_param_min);
  1158. ClassDB::bind_method(D_METHOD("set_param_max", "param", "value"), &CPUParticles2D::set_param_max);
  1159. ClassDB::bind_method(D_METHOD("get_param_max", "param"), &CPUParticles2D::get_param_max);
  1160. ClassDB::bind_method(D_METHOD("set_param_curve", "param", "curve"), &CPUParticles2D::set_param_curve);
  1161. ClassDB::bind_method(D_METHOD("get_param_curve", "param"), &CPUParticles2D::get_param_curve);
  1162. ClassDB::bind_method(D_METHOD("set_color", "color"), &CPUParticles2D::set_color);
  1163. ClassDB::bind_method(D_METHOD("get_color"), &CPUParticles2D::get_color);
  1164. ClassDB::bind_method(D_METHOD("set_color_ramp", "ramp"), &CPUParticles2D::set_color_ramp);
  1165. ClassDB::bind_method(D_METHOD("get_color_ramp"), &CPUParticles2D::get_color_ramp);
  1166. ClassDB::bind_method(D_METHOD("set_color_initial_ramp", "ramp"), &CPUParticles2D::set_color_initial_ramp);
  1167. ClassDB::bind_method(D_METHOD("get_color_initial_ramp"), &CPUParticles2D::get_color_initial_ramp);
  1168. ClassDB::bind_method(D_METHOD("set_particle_flag", "particle_flag", "enable"), &CPUParticles2D::set_particle_flag);
  1169. ClassDB::bind_method(D_METHOD("get_particle_flag", "particle_flag"), &CPUParticles2D::get_particle_flag);
  1170. ClassDB::bind_method(D_METHOD("set_emission_shape", "shape"), &CPUParticles2D::set_emission_shape);
  1171. ClassDB::bind_method(D_METHOD("get_emission_shape"), &CPUParticles2D::get_emission_shape);
  1172. ClassDB::bind_method(D_METHOD("set_emission_sphere_radius", "radius"), &CPUParticles2D::set_emission_sphere_radius);
  1173. ClassDB::bind_method(D_METHOD("get_emission_sphere_radius"), &CPUParticles2D::get_emission_sphere_radius);
  1174. ClassDB::bind_method(D_METHOD("set_emission_rect_extents", "extents"), &CPUParticles2D::set_emission_rect_extents);
  1175. ClassDB::bind_method(D_METHOD("get_emission_rect_extents"), &CPUParticles2D::get_emission_rect_extents);
  1176. ClassDB::bind_method(D_METHOD("set_emission_points", "array"), &CPUParticles2D::set_emission_points);
  1177. ClassDB::bind_method(D_METHOD("get_emission_points"), &CPUParticles2D::get_emission_points);
  1178. ClassDB::bind_method(D_METHOD("set_emission_normals", "array"), &CPUParticles2D::set_emission_normals);
  1179. ClassDB::bind_method(D_METHOD("get_emission_normals"), &CPUParticles2D::get_emission_normals);
  1180. ClassDB::bind_method(D_METHOD("set_emission_colors", "array"), &CPUParticles2D::set_emission_colors);
  1181. ClassDB::bind_method(D_METHOD("get_emission_colors"), &CPUParticles2D::get_emission_colors);
  1182. ClassDB::bind_method(D_METHOD("get_gravity"), &CPUParticles2D::get_gravity);
  1183. ClassDB::bind_method(D_METHOD("set_gravity", "accel_vec"), &CPUParticles2D::set_gravity);
  1184. ClassDB::bind_method(D_METHOD("get_split_scale"), &CPUParticles2D::get_split_scale);
  1185. ClassDB::bind_method(D_METHOD("set_split_scale", "split_scale"), &CPUParticles2D::set_split_scale);
  1186. ClassDB::bind_method(D_METHOD("get_scale_curve_x"), &CPUParticles2D::get_scale_curve_x);
  1187. ClassDB::bind_method(D_METHOD("set_scale_curve_x", "scale_curve"), &CPUParticles2D::set_scale_curve_x);
  1188. ClassDB::bind_method(D_METHOD("get_scale_curve_y"), &CPUParticles2D::get_scale_curve_y);
  1189. ClassDB::bind_method(D_METHOD("set_scale_curve_y", "scale_curve"), &CPUParticles2D::set_scale_curve_y);
  1190. ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &CPUParticles2D::convert_from_particles);
  1191. ADD_SIGNAL(MethodInfo("finished"));
  1192. ADD_GROUP("Emission Shape", "emission_");
  1193. ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_shape", PROPERTY_HINT_ENUM, "Point,Sphere,Sphere Surface,Rectangle,Points,Directed Points", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_emission_shape", "get_emission_shape");
  1194. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_sphere_radius", PROPERTY_HINT_RANGE, "0.01,128,0.01,suffix:px"), "set_emission_sphere_radius", "get_emission_sphere_radius");
  1195. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "emission_rect_extents", PROPERTY_HINT_NONE, "suffix:px"), "set_emission_rect_extents", "get_emission_rect_extents");
  1196. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "emission_points"), "set_emission_points", "get_emission_points");
  1197. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "emission_normals"), "set_emission_normals", "get_emission_normals");
  1198. ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "emission_colors"), "set_emission_colors", "get_emission_colors");
  1199. ADD_GROUP("Particle Flags", "particle_flag_");
  1200. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_align_y"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY);
  1201. ADD_GROUP("Direction", "");
  1202. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "direction"), "set_direction", "get_direction");
  1203. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "spread", PROPERTY_HINT_RANGE, "0,180,0.01"), "set_spread", "get_spread");
  1204. ADD_GROUP("Gravity", "");
  1205. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "gravity", PROPERTY_HINT_NONE, U"suffix:px/s\u00B2"), "set_gravity", "get_gravity");
  1206. ADD_GROUP("Initial Velocity", "initial_");
  1207. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "initial_velocity_min", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater,suffix:px/s"), "set_param_min", "get_param_min", PARAM_INITIAL_LINEAR_VELOCITY);
  1208. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "initial_velocity_max", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater,suffix:px/s"), "set_param_max", "get_param_max", PARAM_INITIAL_LINEAR_VELOCITY);
  1209. ADD_GROUP("Angular Velocity", "angular_");
  1210. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angular_velocity_min", PROPERTY_HINT_RANGE, "-720,720,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_ANGULAR_VELOCITY);
  1211. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angular_velocity_max", PROPERTY_HINT_RANGE, "-720,720,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_ANGULAR_VELOCITY);
  1212. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angular_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGULAR_VELOCITY);
  1213. ADD_GROUP("Orbit Velocity", "orbit_");
  1214. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "orbit_velocity_min", PROPERTY_HINT_RANGE, "-1000,1000,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_ORBIT_VELOCITY);
  1215. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "orbit_velocity_max", PROPERTY_HINT_RANGE, "-1000,1000,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_ORBIT_VELOCITY);
  1216. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "orbit_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ORBIT_VELOCITY);
  1217. ADD_GROUP("Linear Accel", "linear_");
  1218. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "linear_accel_min", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_LINEAR_ACCEL);
  1219. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "linear_accel_max", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_LINEAR_ACCEL);
  1220. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "linear_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_LINEAR_ACCEL);
  1221. ADD_GROUP("Radial Accel", "radial_");
  1222. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "radial_accel_min", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_RADIAL_ACCEL);
  1223. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "radial_accel_max", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_RADIAL_ACCEL);
  1224. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "radial_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_RADIAL_ACCEL);
  1225. ADD_GROUP("Tangential Accel", "tangential_");
  1226. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "tangential_accel_min", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_TANGENTIAL_ACCEL);
  1227. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "tangential_accel_max", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_TANGENTIAL_ACCEL);
  1228. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "tangential_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_TANGENTIAL_ACCEL);
  1229. ADD_GROUP("Damping", "");
  1230. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "damping_min", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_param_min", "get_param_min", PARAM_DAMPING);
  1231. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "damping_max", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_param_max", "get_param_max", PARAM_DAMPING);
  1232. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "damping_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_DAMPING);
  1233. ADD_GROUP("Angle", "");
  1234. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angle_min", PROPERTY_HINT_RANGE, "-720,720,0.1,or_less,or_greater,degrees"), "set_param_min", "get_param_min", PARAM_ANGLE);
  1235. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angle_max", PROPERTY_HINT_RANGE, "-720,720,0.1,or_less,or_greater,degrees"), "set_param_max", "get_param_max", PARAM_ANGLE);
  1236. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angle_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGLE);
  1237. ADD_GROUP("Scale", "");
  1238. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "scale_amount_min", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param_min", "get_param_min", PARAM_SCALE);
  1239. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "scale_amount_max", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param_max", "get_param_max", PARAM_SCALE);
  1240. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_amount_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_SCALE);
  1241. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "split_scale"), "set_split_scale", "get_split_scale");
  1242. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scale_curve_x", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_scale_curve_x", "get_scale_curve_x");
  1243. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scale_curve_y", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_scale_curve_y", "get_scale_curve_y");
  1244. ADD_GROUP("Color", "");
  1245. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  1246. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");
  1247. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_initial_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_initial_ramp", "get_color_initial_ramp");
  1248. ADD_GROUP("Hue Variation", "hue_");
  1249. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "hue_variation_min", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param_min", "get_param_min", PARAM_HUE_VARIATION);
  1250. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "hue_variation_max", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param_max", "get_param_max", PARAM_HUE_VARIATION);
  1251. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "hue_variation_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_HUE_VARIATION);
  1252. ADD_GROUP("Animation", "anim_");
  1253. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_speed_min", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater,or_less"), "set_param_min", "get_param_min", PARAM_ANIM_SPEED);
  1254. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_speed_max", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater,or_less"), "set_param_max", "get_param_max", PARAM_ANIM_SPEED);
  1255. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_speed_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_SPEED);
  1256. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_offset_min", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_param_min", "get_param_min", PARAM_ANIM_OFFSET);
  1257. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_offset_max", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_param_max", "get_param_max", PARAM_ANIM_OFFSET);
  1258. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_offset_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_OFFSET);
  1259. BIND_ENUM_CONSTANT(PARAM_INITIAL_LINEAR_VELOCITY);
  1260. BIND_ENUM_CONSTANT(PARAM_ANGULAR_VELOCITY);
  1261. BIND_ENUM_CONSTANT(PARAM_ORBIT_VELOCITY);
  1262. BIND_ENUM_CONSTANT(PARAM_LINEAR_ACCEL);
  1263. BIND_ENUM_CONSTANT(PARAM_RADIAL_ACCEL);
  1264. BIND_ENUM_CONSTANT(PARAM_TANGENTIAL_ACCEL);
  1265. BIND_ENUM_CONSTANT(PARAM_DAMPING);
  1266. BIND_ENUM_CONSTANT(PARAM_ANGLE);
  1267. BIND_ENUM_CONSTANT(PARAM_SCALE);
  1268. BIND_ENUM_CONSTANT(PARAM_HUE_VARIATION);
  1269. BIND_ENUM_CONSTANT(PARAM_ANIM_SPEED);
  1270. BIND_ENUM_CONSTANT(PARAM_ANIM_OFFSET);
  1271. BIND_ENUM_CONSTANT(PARAM_MAX);
  1272. BIND_ENUM_CONSTANT(PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY);
  1273. BIND_ENUM_CONSTANT(PARTICLE_FLAG_ROTATE_Y); // Unused, but exposed for consistency with 3D.
  1274. BIND_ENUM_CONSTANT(PARTICLE_FLAG_DISABLE_Z); // Unused, but exposed for consistency with 3D.
  1275. BIND_ENUM_CONSTANT(PARTICLE_FLAG_MAX);
  1276. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINT);
  1277. BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE);
  1278. BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE_SURFACE);
  1279. BIND_ENUM_CONSTANT(EMISSION_SHAPE_RECTANGLE);
  1280. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINTS);
  1281. BIND_ENUM_CONSTANT(EMISSION_SHAPE_DIRECTED_POINTS);
  1282. BIND_ENUM_CONSTANT(EMISSION_SHAPE_MAX);
  1283. }
  1284. CPUParticles2D::CPUParticles2D() {
  1285. mesh = RenderingServer::get_singleton()->mesh_create();
  1286. multimesh = RenderingServer::get_singleton()->multimesh_create();
  1287. RenderingServer::get_singleton()->multimesh_set_mesh(multimesh, mesh);
  1288. set_emitting(true);
  1289. set_amount(8);
  1290. set_use_local_coordinates(false);
  1291. set_seed(Math::rand());
  1292. rng.instantiate();
  1293. set_param_min(PARAM_INITIAL_LINEAR_VELOCITY, 0);
  1294. set_param_min(PARAM_ANGULAR_VELOCITY, 0);
  1295. set_param_min(PARAM_ORBIT_VELOCITY, 0);
  1296. set_param_min(PARAM_LINEAR_ACCEL, 0);
  1297. set_param_min(PARAM_RADIAL_ACCEL, 0);
  1298. set_param_min(PARAM_TANGENTIAL_ACCEL, 0);
  1299. set_param_min(PARAM_DAMPING, 0);
  1300. set_param_min(PARAM_ANGLE, 0);
  1301. set_param_min(PARAM_SCALE, 1);
  1302. set_param_min(PARAM_HUE_VARIATION, 0);
  1303. set_param_min(PARAM_ANIM_SPEED, 0);
  1304. set_param_min(PARAM_ANIM_OFFSET, 0);
  1305. set_param_max(PARAM_INITIAL_LINEAR_VELOCITY, 0);
  1306. set_param_max(PARAM_ANGULAR_VELOCITY, 0);
  1307. set_param_max(PARAM_ORBIT_VELOCITY, 0);
  1308. set_param_max(PARAM_LINEAR_ACCEL, 0);
  1309. set_param_max(PARAM_RADIAL_ACCEL, 0);
  1310. set_param_max(PARAM_TANGENTIAL_ACCEL, 0);
  1311. set_param_max(PARAM_DAMPING, 0);
  1312. set_param_max(PARAM_ANGLE, 0);
  1313. set_param_max(PARAM_SCALE, 1);
  1314. set_param_max(PARAM_HUE_VARIATION, 0);
  1315. set_param_max(PARAM_ANIM_SPEED, 0);
  1316. set_param_max(PARAM_ANIM_OFFSET, 0);
  1317. for (int i = 0; i < PARTICLE_FLAG_MAX; i++) {
  1318. particle_flags[i] = false;
  1319. }
  1320. set_color(Color(1, 1, 1, 1));
  1321. _update_mesh_texture();
  1322. // CPUParticles2D defaults to interpolation off.
  1323. // This is because the result often looks better when the particles are updated every frame.
  1324. // Note that children will need to explicitly turn back on interpolation if they want to use it,
  1325. // rather than relying on inherit mode.
  1326. set_physics_interpolation_mode(Node::PHYSICS_INTERPOLATION_MODE_OFF);
  1327. }
  1328. CPUParticles2D::~CPUParticles2D() {
  1329. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1330. RS::get_singleton()->free(multimesh);
  1331. RS::get_singleton()->free(mesh);
  1332. }