cpu_particles.cpp 52 KB

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