cpu_particles_2d.cpp 58 KB

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