cpu_particles_2d.cpp 54 KB

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