cpu_particles_2d.cpp 56 KB

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