cpu_particles_2d.cpp 60 KB

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