cpu_particles_2d.cpp 51 KB

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