cpu_particles.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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. if (flags[FLAG_DISABLE_Z]) {
  456. float angle1_rad = (Math::randf() * 2.0 - 1.0) * Math_PI * spread / 180.0;
  457. Vector3 rot = Vector3(Math::cos(angle1_rad), Math::sin(angle1_rad), 0.0);
  458. p.velocity = rot * parameters[PARAM_INITIAL_LINEAR_VELOCITY] * Math::lerp(1.0f, float(Math::randf()), randomness[PARAM_INITIAL_LINEAR_VELOCITY]);
  459. } else {
  460. //initiate velocity spread in 3D
  461. float angle1_rad = (Math::randf() * 2.0 - 1.0) * Math_PI * spread / 180.0;
  462. float angle2_rad = (Math::randf() * 2.0 - 1.0) * (1.0 - flatness) * Math_PI * spread / 180.0;
  463. Vector3 direction_xz = Vector3(Math::sin(angle1_rad), 0, Math::cos(angle1_rad));
  464. Vector3 direction_yz = Vector3(0, Math::sin(angle2_rad), Math::cos(angle2_rad));
  465. direction_yz.z = direction_yz.z / MAX(0.0001, Math::sqrt(ABS(direction_yz.z))); //better uniform distribution
  466. Vector3 direction = Vector3(direction_xz.x * direction_yz.z, direction_yz.y, direction_xz.z * direction_yz.z);
  467. direction.normalize();
  468. p.velocity = direction * parameters[PARAM_INITIAL_LINEAR_VELOCITY] * Math::lerp(1.0f, float(Math::randf()), randomness[PARAM_INITIAL_LINEAR_VELOCITY]);
  469. }
  470. float base_angle = (parameters[PARAM_ANGLE] + tex_angle) * Math::lerp(1.0f, p.angle_rand, randomness[PARAM_ANGLE]);
  471. p.custom[0] = Math::deg2rad(base_angle); //angle
  472. p.custom[1] = 0.0; //phase
  473. 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)
  474. p.transform = Transform();
  475. p.time = 0;
  476. p.base_color = Color(1, 1, 1, 1);
  477. switch (emission_shape) {
  478. case EMISSION_SHAPE_POINT: {
  479. //do none
  480. } break;
  481. case EMISSION_SHAPE_SPHERE: {
  482. 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;
  483. } break;
  484. case EMISSION_SHAPE_BOX: {
  485. 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;
  486. } break;
  487. case EMISSION_SHAPE_POINTS:
  488. case EMISSION_SHAPE_DIRECTED_POINTS: {
  489. int pc = emission_points.size();
  490. if (pc == 0)
  491. break;
  492. int random_idx = Math::rand() % pc;
  493. p.transform.origin = emission_points.get(random_idx);
  494. if (emission_shape == EMISSION_SHAPE_DIRECTED_POINTS && emission_normals.size() == pc) {
  495. if (flags[FLAG_DISABLE_Z]) {
  496. /*
  497. mat2 rotm;
  498. ";
  499. rotm[0] = texelFetch(emission_texture_normal, emission_tex_ofs, 0).xy;
  500. rotm[1] = rotm[0].yx * vec2(1.0, -1.0);
  501. VELOCITY.xy = rotm * VELOCITY.xy;
  502. */
  503. } else {
  504. Vector3 normal = emission_normals.get(random_idx);
  505. Vector3 v0 = Math::abs(normal.z) < 0.999 ? Vector3(0.0, 0.0, 1.0) : Vector3(0, 1.0, 0.0);
  506. Vector3 tangent = v0.cross(normal).normalized();
  507. Vector3 bitangent = tangent.cross(normal).normalized();
  508. Basis m3;
  509. m3.set_axis(0, tangent);
  510. m3.set_axis(1, bitangent);
  511. m3.set_axis(2, normal);
  512. p.velocity = m3.xform(p.velocity);
  513. }
  514. }
  515. if (emission_colors.size() == pc) {
  516. p.base_color = emission_colors.get(random_idx);
  517. }
  518. } break;
  519. }
  520. if (!local_coords) {
  521. p.velocity = velocity_xform.xform(p.velocity);
  522. p.transform = emission_xform * p.transform;
  523. }
  524. if (flags[FLAG_DISABLE_Z]) {
  525. p.velocity.z = 0.0;
  526. p.transform.origin.z = 0.0;
  527. }
  528. } else if (!p.active) {
  529. continue;
  530. } else {
  531. uint32_t alt_seed = p.seed;
  532. p.time += local_delta;
  533. p.custom[1] = p.time / lifetime;
  534. float tex_linear_velocity = 0.0;
  535. if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  536. tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->interpolate(p.custom[1]);
  537. }
  538. /*
  539. float tex_orbit_velocity = 0.0;
  540. if (flags[FLAG_DISABLE_Z]) {
  541. if (curve_parameters[PARAM_INITIAL_ORBIT_VELOCITY].is_valid()) {
  542. tex_orbit_velocity = curve_parameters[PARAM_INITIAL_ORBIT_VELOCITY]->interpolate(p.custom[1]);
  543. }
  544. }
  545. */
  546. float tex_angular_velocity = 0.0;
  547. if (curve_parameters[PARAM_ANGULAR_VELOCITY].is_valid()) {
  548. tex_angular_velocity = curve_parameters[PARAM_ANGULAR_VELOCITY]->interpolate(p.custom[1]);
  549. }
  550. float tex_linear_accel = 0.0;
  551. if (curve_parameters[PARAM_LINEAR_ACCEL].is_valid()) {
  552. tex_linear_accel = curve_parameters[PARAM_LINEAR_ACCEL]->interpolate(p.custom[1]);
  553. }
  554. float tex_tangential_accel = 0.0;
  555. if (curve_parameters[PARAM_TANGENTIAL_ACCEL].is_valid()) {
  556. tex_tangential_accel = curve_parameters[PARAM_TANGENTIAL_ACCEL]->interpolate(p.custom[1]);
  557. }
  558. float tex_radial_accel = 0.0;
  559. if (curve_parameters[PARAM_RADIAL_ACCEL].is_valid()) {
  560. tex_radial_accel = curve_parameters[PARAM_RADIAL_ACCEL]->interpolate(p.custom[1]);
  561. }
  562. float tex_damping = 0.0;
  563. if (curve_parameters[PARAM_DAMPING].is_valid()) {
  564. tex_damping = curve_parameters[PARAM_DAMPING]->interpolate(p.custom[1]);
  565. }
  566. float tex_angle = 0.0;
  567. if (curve_parameters[PARAM_ANGLE].is_valid()) {
  568. tex_angle = curve_parameters[PARAM_ANGLE]->interpolate(p.custom[1]);
  569. }
  570. float tex_anim_speed = 0.0;
  571. if (curve_parameters[PARAM_ANIM_SPEED].is_valid()) {
  572. tex_anim_speed = curve_parameters[PARAM_ANIM_SPEED]->interpolate(p.custom[1]);
  573. }
  574. float tex_anim_offset = 0.0;
  575. if (curve_parameters[PARAM_ANIM_OFFSET].is_valid()) {
  576. tex_anim_offset = curve_parameters[PARAM_ANIM_OFFSET]->interpolate(p.custom[1]);
  577. }
  578. Vector3 force = gravity;
  579. Vector3 position = p.transform.origin;
  580. if (flags[FLAG_DISABLE_Z]) {
  581. position.z = 0.0;
  582. }
  583. //apply linear acceleration
  584. 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();
  585. //apply radial acceleration
  586. Vector3 org = emission_xform.origin;
  587. Vector3 diff = position - org;
  588. 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();
  589. //apply tangential acceleration;
  590. if (flags[FLAG_DISABLE_Z]) {
  591. Vector3 yx = Vector3(diff.y, 0, diff.x);
  592. 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();
  593. } else {
  594. Vector3 crossDiff = diff.normalized().cross(gravity.normalized());
  595. 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();
  596. }
  597. //apply attractor forces
  598. p.velocity += force * local_delta;
  599. //orbit velocity
  600. #if 0
  601. if (flags[FLAG_DISABLE_Z]) {
  602. float orbit_amount = (orbit_velocity + tex_orbit_velocity) * mix(1.0, rand_from_seed(alt_seed), orbit_velocity_random);
  603. if (orbit_amount != 0.0) {
  604. float ang = orbit_amount * DELTA * pi * 2.0;
  605. mat2 rot = mat2(vec2(cos(ang), -sin(ang)), vec2(sin(ang), cos(ang)));
  606. TRANSFORM[3].xy -= diff.xy;
  607. TRANSFORM[3].xy += rot * diff.xy;
  608. }
  609. }
  610. #endif
  611. if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  612. p.velocity = p.velocity.normalized() * tex_linear_velocity;
  613. }
  614. if (parameters[PARAM_DAMPING] + tex_damping > 0.0) {
  615. float v = p.velocity.length();
  616. float damp = (parameters[PARAM_DAMPING] + tex_damping) * Math::lerp(1.0f, rand_from_seed(alt_seed), randomness[PARAM_DAMPING]);
  617. v -= damp * local_delta;
  618. if (v < 0.0) {
  619. p.velocity = Vector3();
  620. } else {
  621. p.velocity = p.velocity.normalized() * v;
  622. }
  623. }
  624. float base_angle = (parameters[PARAM_ANGLE] + tex_angle) * Math::lerp(1.0f, p.angle_rand, randomness[PARAM_ANGLE]);
  625. 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]);
  626. p.custom[0] = Math::deg2rad(base_angle); //angle
  627. 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
  628. }
  629. //apply color
  630. //apply hue rotation
  631. float tex_scale = 1.0;
  632. if (curve_parameters[PARAM_SCALE].is_valid()) {
  633. tex_scale = curve_parameters[PARAM_SCALE]->interpolate(p.custom[1]);
  634. }
  635. float tex_hue_variation = 0.0;
  636. if (curve_parameters[PARAM_HUE_VARIATION].is_valid()) {
  637. tex_hue_variation = curve_parameters[PARAM_HUE_VARIATION]->interpolate(p.custom[1]);
  638. }
  639. 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]);
  640. float hue_rot_c = Math::cos(hue_rot_angle);
  641. float hue_rot_s = Math::sin(hue_rot_angle);
  642. Basis hue_rot_mat;
  643. {
  644. Basis mat1(0.299, 0.587, 0.114, 0.299, 0.587, 0.114, 0.299, 0.587, 0.114);
  645. Basis mat2(0.701, -0.587, -0.114, -0.299, 0.413, -0.114, -0.300, -0.588, 0.886);
  646. Basis mat3(0.168, 0.330, -0.497, -0.328, 0.035, 0.292, 1.250, -1.050, -0.203);
  647. for (int j = 0; j < 3; j++) {
  648. hue_rot_mat[j] = mat1[j] + mat2[j] * hue_rot_c + mat3[j] * hue_rot_s;
  649. }
  650. }
  651. if (color_ramp.is_valid()) {
  652. p.color = color_ramp->get_color_at_offset(p.custom[1]) * color;
  653. } else {
  654. p.color = color;
  655. }
  656. Vector3 color_rgb = hue_rot_mat.xform_inv(Vector3(p.color.r, p.color.g, p.color.b));
  657. p.color.r = color_rgb.x;
  658. p.color.g = color_rgb.y;
  659. p.color.b = color_rgb.z;
  660. p.color *= p.base_color;
  661. if (flags[FLAG_DISABLE_Z]) {
  662. if (flags[FLAG_ALIGN_Y_TO_VELOCITY]) {
  663. if (p.velocity.length() > 0.0) {
  664. p.transform.basis.set_axis(1, p.velocity.normalized());
  665. } else {
  666. p.transform.basis.set_axis(1, p.transform.basis.get_axis(1));
  667. }
  668. p.transform.basis.set_axis(0, p.transform.basis.get_axis(1).cross(p.transform.basis.get_axis(2)).normalized());
  669. p.transform.basis.set_axis(2, Vector3(0, 0, 1));
  670. } else {
  671. p.transform.basis.set_axis(0, Vector3(Math::cos(p.custom[0]), -Math::sin(p.custom[0]), 0.0));
  672. p.transform.basis.set_axis(1, Vector3(Math::sin(p.custom[0]), Math::cos(p.custom[0]), 0.0));
  673. p.transform.basis.set_axis(2, Vector3(0, 0, 1));
  674. }
  675. } else {
  676. //orient particle Y towards velocity
  677. if (flags[FLAG_ALIGN_Y_TO_VELOCITY]) {
  678. if (p.velocity.length() > 0.0) {
  679. p.transform.basis.set_axis(1, p.velocity.normalized());
  680. } else {
  681. p.transform.basis.set_axis(1, p.transform.basis.get_axis(1).normalized());
  682. }
  683. if (p.transform.basis.get_axis(1) == p.transform.basis.get_axis(0)) {
  684. p.transform.basis.set_axis(0, p.transform.basis.get_axis(1).cross(p.transform.basis.get_axis(2)).normalized());
  685. p.transform.basis.set_axis(2, p.transform.basis.get_axis(0).cross(p.transform.basis.get_axis(1)).normalized());
  686. } else {
  687. p.transform.basis.set_axis(2, p.transform.basis.get_axis(0).cross(p.transform.basis.get_axis(1)).normalized());
  688. p.transform.basis.set_axis(0, p.transform.basis.get_axis(1).cross(p.transform.basis.get_axis(2)).normalized());
  689. }
  690. } else {
  691. p.transform.basis.orthonormalize();
  692. }
  693. //turn particle by rotation in Y
  694. if (flags[FLAG_ROTATE_Y]) {
  695. Basis rot_y(Vector3(0, 1, 0), p.custom[0]);
  696. p.transform.basis = p.transform.basis * rot_y;
  697. }
  698. }
  699. //scale by scale
  700. float base_scale = Math::lerp(parameters[PARAM_SCALE] * tex_scale, 1.0f, p.scale_rand * randomness[PARAM_SCALE]);
  701. if (base_scale == 0.0) base_scale = 0.000001;
  702. p.transform.basis.scale(Vector3(1, 1, 1) * base_scale);
  703. if (flags[FLAG_DISABLE_Z]) {
  704. p.velocity.z = 0.0;
  705. p.transform.origin.z = 0.0;
  706. }
  707. p.transform.origin += p.velocity * local_delta;
  708. }
  709. }
  710. void CPUParticles::_update_particle_data_buffer() {
  711. #ifndef NO_THREADS
  712. update_mutex->lock();
  713. #endif
  714. {
  715. int pc = particles.size();
  716. PoolVector<int>::Write ow;
  717. int *order = NULL;
  718. PoolVector<float>::Write w = particle_data.write();
  719. PoolVector<Particle>::Read r = particles.read();
  720. float *ptr = w.ptr();
  721. Transform un_transform;
  722. if (!local_coords) {
  723. un_transform = get_global_transform().affine_inverse();
  724. }
  725. if (draw_order != DRAW_ORDER_INDEX) {
  726. ow = particle_order.write();
  727. order = ow.ptr();
  728. for (int i = 0; i < pc; i++) {
  729. order[i] = i;
  730. }
  731. if (draw_order == DRAW_ORDER_LIFETIME) {
  732. SortArray<int, SortLifetime> sorter;
  733. sorter.compare.particles = r.ptr();
  734. sorter.sort(order, pc);
  735. } else if (draw_order == DRAW_ORDER_VIEW_DEPTH) {
  736. Camera *c = get_viewport()->get_camera();
  737. if (c) {
  738. Vector3 dir = c->get_global_transform().basis.get_axis(2); //far away to close
  739. if (local_coords) {
  740. dir = un_transform.basis.xform(dir).normalized();
  741. }
  742. SortArray<int, SortAxis> sorter;
  743. sorter.compare.particles = r.ptr();
  744. sorter.compare.axis = dir;
  745. sorter.sort(order, pc);
  746. }
  747. }
  748. }
  749. for (int i = 0; i < pc; i++) {
  750. int idx = order ? order[i] : i;
  751. Transform t = r[idx].transform;
  752. if (!local_coords) {
  753. t = un_transform * t;
  754. }
  755. if (r[idx].active) {
  756. ptr[0] = t.basis.elements[0][0];
  757. ptr[1] = t.basis.elements[0][1];
  758. ptr[2] = t.basis.elements[0][2];
  759. ptr[3] = t.origin.x;
  760. ptr[4] = t.basis.elements[1][0];
  761. ptr[5] = t.basis.elements[1][1];
  762. ptr[6] = t.basis.elements[1][2];
  763. ptr[7] = t.origin.y;
  764. ptr[8] = t.basis.elements[2][0];
  765. ptr[9] = t.basis.elements[2][1];
  766. ptr[10] = t.basis.elements[2][2];
  767. ptr[11] = t.origin.z;
  768. } else {
  769. zeromem(ptr, sizeof(float) * 12);
  770. }
  771. Color c = r[idx].color;
  772. uint8_t *data8 = (uint8_t *)&ptr[12];
  773. data8[0] = CLAMP(c.r * 255.0, 0, 255);
  774. data8[1] = CLAMP(c.g * 255.0, 0, 255);
  775. data8[2] = CLAMP(c.b * 255.0, 0, 255);
  776. data8[3] = CLAMP(c.a * 255.0, 0, 255);
  777. ptr[13] = r[idx].custom[0];
  778. ptr[14] = r[idx].custom[1];
  779. ptr[15] = r[idx].custom[2];
  780. ptr[16] = r[idx].custom[3];
  781. ptr += 17;
  782. }
  783. can_update = true;
  784. }
  785. #ifndef NO_THREADS
  786. update_mutex->unlock();
  787. #endif
  788. }
  789. void CPUParticles::_update_render_thread() {
  790. #ifndef NO_THREADS
  791. update_mutex->lock();
  792. #endif
  793. if (can_update) {
  794. VS::get_singleton()->multimesh_set_as_bulk_array(multimesh, particle_data);
  795. can_update = false; //wait for next time
  796. }
  797. #ifndef NO_THREADS
  798. update_mutex->unlock();
  799. #endif
  800. }
  801. void CPUParticles::_notification(int p_what) {
  802. if (p_what == NOTIFICATION_ENTER_TREE) {
  803. if (is_processing_internal()) {
  804. #ifndef NO_THREADS
  805. update_mutex->lock();
  806. #endif
  807. VS::get_singleton()->connect("frame_pre_draw", this, "_update_render_thread");
  808. VS::get_singleton()->instance_geometry_set_flag(get_instance(), VS::INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE, true);
  809. #ifndef NO_THREADS
  810. update_mutex->unlock();
  811. #endif
  812. }
  813. }
  814. if (p_what == NOTIFICATION_EXIT_TREE) {
  815. if (is_processing_internal()) {
  816. #ifndef NO_THREADS
  817. update_mutex->lock();
  818. #endif
  819. VS::get_singleton()->disconnect("frame_pre_draw", this, "_update_render_thread");
  820. VS::get_singleton()->instance_geometry_set_flag(get_instance(), VS::INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE, false);
  821. #ifndef NO_THREADS
  822. update_mutex->unlock();
  823. #endif
  824. }
  825. }
  826. if (p_what == NOTIFICATION_PAUSED || p_what == NOTIFICATION_UNPAUSED) {
  827. }
  828. if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
  829. if (particles.size() == 0 || !is_visible_in_tree())
  830. return;
  831. float delta = get_process_delta_time();
  832. if (emitting) {
  833. inactive_time = 0;
  834. } else {
  835. inactive_time += delta;
  836. if (inactive_time > lifetime * 1.2) {
  837. set_process_internal(false);
  838. #ifndef NO_THREADS
  839. update_mutex->lock();
  840. #endif
  841. VS::get_singleton()->disconnect("frame_pre_draw", this, "_update_render_thread");
  842. VS::get_singleton()->instance_geometry_set_flag(get_instance(), VS::INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE, false);
  843. #ifndef NO_THREADS
  844. update_mutex->unlock();
  845. #endif
  846. //reset variables
  847. time = 0;
  848. inactive_time = 0;
  849. frame_remainder = 0;
  850. cycle = 0;
  851. return;
  852. }
  853. }
  854. bool processed = false;
  855. if (time == 0 && pre_process_time > 0.0) {
  856. float frame_time;
  857. if (fixed_fps > 0)
  858. frame_time = 1.0 / fixed_fps;
  859. else
  860. frame_time = 1.0 / 30.0;
  861. float todo = pre_process_time;
  862. while (todo >= 0) {
  863. _particles_process(frame_time);
  864. processed = true;
  865. todo -= frame_time;
  866. }
  867. }
  868. if (fixed_fps > 0) {
  869. float frame_time = 1.0 / fixed_fps;
  870. float decr = frame_time;
  871. float ldelta = delta;
  872. if (ldelta > 0.1) { //avoid recursive stalls if fps goes below 10
  873. ldelta = 0.1;
  874. } else if (ldelta <= 0.0) { //unlikely but..
  875. ldelta = 0.001;
  876. }
  877. float todo = frame_remainder + ldelta;
  878. while (todo >= frame_time) {
  879. _particles_process(frame_time);
  880. processed = true;
  881. todo -= decr;
  882. }
  883. frame_remainder = todo;
  884. } else {
  885. _particles_process(delta);
  886. processed = true;
  887. }
  888. if (processed) {
  889. _update_particle_data_buffer();
  890. }
  891. }
  892. }
  893. void CPUParticles::convert_from_particles(Node *p_particles) {
  894. Particles *particles = Object::cast_to<Particles>(p_particles);
  895. ERR_FAIL_COND(!particles);
  896. set_emitting(particles->is_emitting());
  897. set_amount(particles->get_amount());
  898. set_lifetime(particles->get_lifetime());
  899. set_one_shot(particles->get_one_shot());
  900. set_pre_process_time(particles->get_pre_process_time());
  901. set_explosiveness_ratio(particles->get_explosiveness_ratio());
  902. set_randomness_ratio(particles->get_randomness_ratio());
  903. set_use_local_coordinates(particles->get_use_local_coordinates());
  904. set_fixed_fps(particles->get_fixed_fps());
  905. set_fractional_delta(particles->get_fractional_delta());
  906. set_speed_scale(particles->get_speed_scale());
  907. set_draw_order(DrawOrder(particles->get_draw_order()));
  908. set_mesh(particles->get_draw_pass_mesh(0));
  909. Ref<ParticlesMaterial> material = particles->get_process_material();
  910. if (material.is_null())
  911. return;
  912. set_spread(material->get_spread());
  913. set_flatness(material->get_flatness());
  914. set_color(material->get_color());
  915. Ref<GradientTexture> gt = material->get_color_ramp();
  916. if (gt.is_valid()) {
  917. set_color_ramp(gt->get_gradient());
  918. }
  919. set_particle_flag(FLAG_ALIGN_Y_TO_VELOCITY, material->get_flag(ParticlesMaterial::FLAG_ALIGN_Y_TO_VELOCITY));
  920. set_particle_flag(FLAG_ROTATE_Y, material->get_flag(ParticlesMaterial::FLAG_ROTATE_Y));
  921. set_particle_flag(FLAG_DISABLE_Z, material->get_flag(ParticlesMaterial::FLAG_DISABLE_Z));
  922. set_emission_shape(EmissionShape(material->get_emission_shape()));
  923. set_emission_sphere_radius(material->get_emission_sphere_radius());
  924. set_emission_box_extents(material->get_emission_box_extents());
  925. set_gravity(material->get_gravity());
  926. #define CONVERT_PARAM(m_param) \
  927. set_param(m_param, material->get_param(ParticlesMaterial::m_param)); \
  928. { \
  929. Ref<CurveTexture> ctex = material->get_param_texture(ParticlesMaterial::m_param); \
  930. if (ctex.is_valid()) set_param_curve(m_param, ctex->get_curve()); \
  931. } \
  932. set_param_randomness(m_param, material->get_param_randomness(ParticlesMaterial::m_param));
  933. CONVERT_PARAM(PARAM_INITIAL_LINEAR_VELOCITY);
  934. CONVERT_PARAM(PARAM_ANGULAR_VELOCITY);
  935. // CONVERT_PARAM(PARAM_ORBIT_VELOCITY);
  936. CONVERT_PARAM(PARAM_LINEAR_ACCEL);
  937. CONVERT_PARAM(PARAM_RADIAL_ACCEL);
  938. CONVERT_PARAM(PARAM_TANGENTIAL_ACCEL);
  939. CONVERT_PARAM(PARAM_DAMPING);
  940. CONVERT_PARAM(PARAM_ANGLE);
  941. CONVERT_PARAM(PARAM_SCALE);
  942. CONVERT_PARAM(PARAM_HUE_VARIATION);
  943. CONVERT_PARAM(PARAM_ANIM_SPEED);
  944. CONVERT_PARAM(PARAM_ANIM_OFFSET);
  945. #undef CONVERT_PARAM
  946. }
  947. void CPUParticles::_bind_methods() {
  948. ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &CPUParticles::set_emitting);
  949. ClassDB::bind_method(D_METHOD("set_amount", "amount"), &CPUParticles::set_amount);
  950. ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &CPUParticles::set_lifetime);
  951. ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &CPUParticles::set_one_shot);
  952. ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &CPUParticles::set_pre_process_time);
  953. ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &CPUParticles::set_explosiveness_ratio);
  954. ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &CPUParticles::set_randomness_ratio);
  955. ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &CPUParticles::set_use_local_coordinates);
  956. ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &CPUParticles::set_fixed_fps);
  957. ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &CPUParticles::set_fractional_delta);
  958. ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &CPUParticles::set_speed_scale);
  959. ClassDB::bind_method(D_METHOD("is_emitting"), &CPUParticles::is_emitting);
  960. ClassDB::bind_method(D_METHOD("get_amount"), &CPUParticles::get_amount);
  961. ClassDB::bind_method(D_METHOD("get_lifetime"), &CPUParticles::get_lifetime);
  962. ClassDB::bind_method(D_METHOD("get_one_shot"), &CPUParticles::get_one_shot);
  963. ClassDB::bind_method(D_METHOD("get_pre_process_time"), &CPUParticles::get_pre_process_time);
  964. ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &CPUParticles::get_explosiveness_ratio);
  965. ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &CPUParticles::get_randomness_ratio);
  966. ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &CPUParticles::get_use_local_coordinates);
  967. ClassDB::bind_method(D_METHOD("get_fixed_fps"), &CPUParticles::get_fixed_fps);
  968. ClassDB::bind_method(D_METHOD("get_fractional_delta"), &CPUParticles::get_fractional_delta);
  969. ClassDB::bind_method(D_METHOD("get_speed_scale"), &CPUParticles::get_speed_scale);
  970. ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &CPUParticles::set_draw_order);
  971. ClassDB::bind_method(D_METHOD("get_draw_order"), &CPUParticles::get_draw_order);
  972. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &CPUParticles::set_mesh);
  973. ClassDB::bind_method(D_METHOD("get_mesh"), &CPUParticles::get_mesh);
  974. ClassDB::bind_method(D_METHOD("restart"), &CPUParticles::restart);
  975. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting"), "set_emitting", "is_emitting");
  976. ADD_PROPERTY(PropertyInfo(Variant::INT, "amount", PROPERTY_HINT_EXP_RANGE, "1,1000000,1"), "set_amount", "get_amount");
  977. ADD_GROUP("Time", "");
  978. ADD_PROPERTY(PropertyInfo(Variant::REAL, "lifetime", PROPERTY_HINT_EXP_RANGE, "0.01,600.0,0.01,or_greater"), "set_lifetime", "get_lifetime");
  979. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
  980. ADD_PROPERTY(PropertyInfo(Variant::REAL, "preprocess", PROPERTY_HINT_EXP_RANGE, "0.00,600.0,0.01"), "set_pre_process_time", "get_pre_process_time");
  981. ADD_PROPERTY(PropertyInfo(Variant::REAL, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
  982. ADD_PROPERTY(PropertyInfo(Variant::REAL, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
  983. ADD_PROPERTY(PropertyInfo(Variant::REAL, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
  984. ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1"), "set_fixed_fps", "get_fixed_fps");
  985. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
  986. ADD_GROUP("Drawing", "");
  987. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
  988. ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime,View Depth"), "set_draw_order", "get_draw_order");
  989. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  990. BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
  991. BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
  992. BIND_ENUM_CONSTANT(DRAW_ORDER_VIEW_DEPTH);
  993. ////////////////////////////////
  994. ClassDB::bind_method(D_METHOD("set_spread", "degrees"), &CPUParticles::set_spread);
  995. ClassDB::bind_method(D_METHOD("get_spread"), &CPUParticles::get_spread);
  996. ClassDB::bind_method(D_METHOD("set_flatness", "amount"), &CPUParticles::set_flatness);
  997. ClassDB::bind_method(D_METHOD("get_flatness"), &CPUParticles::get_flatness);
  998. ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &CPUParticles::set_param);
  999. ClassDB::bind_method(D_METHOD("get_param", "param"), &CPUParticles::get_param);
  1000. ClassDB::bind_method(D_METHOD("set_param_randomness", "param", "randomness"), &CPUParticles::set_param_randomness);
  1001. ClassDB::bind_method(D_METHOD("get_param_randomness", "param"), &CPUParticles::get_param_randomness);
  1002. ClassDB::bind_method(D_METHOD("set_param_curve", "param", "curve"), &CPUParticles::set_param_curve);
  1003. ClassDB::bind_method(D_METHOD("get_param_curve", "param"), &CPUParticles::get_param_curve);
  1004. ClassDB::bind_method(D_METHOD("set_color", "color"), &CPUParticles::set_color);
  1005. ClassDB::bind_method(D_METHOD("get_color"), &CPUParticles::get_color);
  1006. ClassDB::bind_method(D_METHOD("set_color_ramp", "ramp"), &CPUParticles::set_color_ramp);
  1007. ClassDB::bind_method(D_METHOD("get_color_ramp"), &CPUParticles::get_color_ramp);
  1008. ClassDB::bind_method(D_METHOD("set_particle_flag", "flag", "enable"), &CPUParticles::set_particle_flag);
  1009. ClassDB::bind_method(D_METHOD("get_particle_flag", "flag"), &CPUParticles::get_particle_flag);
  1010. ClassDB::bind_method(D_METHOD("set_emission_shape", "shape"), &CPUParticles::set_emission_shape);
  1011. ClassDB::bind_method(D_METHOD("get_emission_shape"), &CPUParticles::get_emission_shape);
  1012. ClassDB::bind_method(D_METHOD("set_emission_sphere_radius", "radius"), &CPUParticles::set_emission_sphere_radius);
  1013. ClassDB::bind_method(D_METHOD("get_emission_sphere_radius"), &CPUParticles::get_emission_sphere_radius);
  1014. ClassDB::bind_method(D_METHOD("set_emission_box_extents", "extents"), &CPUParticles::set_emission_box_extents);
  1015. ClassDB::bind_method(D_METHOD("get_emission_box_extents"), &CPUParticles::get_emission_box_extents);
  1016. ClassDB::bind_method(D_METHOD("set_emission_points", "array"), &CPUParticles::set_emission_points);
  1017. ClassDB::bind_method(D_METHOD("get_emission_points"), &CPUParticles::get_emission_points);
  1018. ClassDB::bind_method(D_METHOD("set_emission_normals", "array"), &CPUParticles::set_emission_normals);
  1019. ClassDB::bind_method(D_METHOD("get_emission_normals"), &CPUParticles::get_emission_normals);
  1020. ClassDB::bind_method(D_METHOD("set_emission_colors", "array"), &CPUParticles::set_emission_colors);
  1021. ClassDB::bind_method(D_METHOD("get_emission_colors"), &CPUParticles::get_emission_colors);
  1022. ClassDB::bind_method(D_METHOD("get_gravity"), &CPUParticles::get_gravity);
  1023. ClassDB::bind_method(D_METHOD("set_gravity", "accel_vec"), &CPUParticles::set_gravity);
  1024. ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &CPUParticles::convert_from_particles);
  1025. ClassDB::bind_method(D_METHOD("_update_render_thread"), &CPUParticles::_update_render_thread);
  1026. ADD_GROUP("Emission Shape", "emission_");
  1027. ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_shape", PROPERTY_HINT_ENUM, "Point,Sphere,Box,Points,Directed Points"), "set_emission_shape", "get_emission_shape");
  1028. ADD_PROPERTY(PropertyInfo(Variant::REAL, "emission_sphere_radius", PROPERTY_HINT_RANGE, "0.01,128,0.01"), "set_emission_sphere_radius", "get_emission_sphere_radius");
  1029. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "emission_box_extents"), "set_emission_box_extents", "get_emission_box_extents");
  1030. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR3_ARRAY, "emission_points"), "set_emission_points", "get_emission_points");
  1031. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR3_ARRAY, "emission_normals"), "set_emission_normals", "get_emission_normals");
  1032. ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "emission_colors"), "set_emission_colors", "get_emission_colors");
  1033. ADD_GROUP("Flags", "flag_");
  1034. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flag_align_y"), "set_particle_flag", "get_particle_flag", FLAG_ALIGN_Y_TO_VELOCITY);
  1035. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flag_rotate_y"), "set_particle_flag", "get_particle_flag", FLAG_ROTATE_Y);
  1036. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flag_disable_z"), "set_particle_flag", "get_particle_flag", FLAG_DISABLE_Z);
  1037. ADD_GROUP("Spread", "");
  1038. ADD_PROPERTY(PropertyInfo(Variant::REAL, "spread", PROPERTY_HINT_RANGE, "0,180,0.01"), "set_spread", "get_spread");
  1039. ADD_PROPERTY(PropertyInfo(Variant::REAL, "flatness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_flatness", "get_flatness");
  1040. ADD_GROUP("Gravity", "");
  1041. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "gravity"), "set_gravity", "get_gravity");
  1042. ADD_GROUP("Initial Velocity", "initial_");
  1043. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "initial_velocity", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param", "get_param", PARAM_INITIAL_LINEAR_VELOCITY);
  1044. 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);
  1045. ADD_GROUP("Angular Velocity", "angular_");
  1046. 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);
  1047. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANGULAR_VELOCITY);
  1048. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angular_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGULAR_VELOCITY);
  1049. /*
  1050. ADD_GROUP("Orbit Velocity", "orbit_");
  1051. 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);
  1052. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "orbit_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ORBIT_VELOCITY);
  1053. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "orbit_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ORBIT_VELOCITY);
  1054. */
  1055. ADD_GROUP("Linear Accel", "linear_");
  1056. 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);
  1057. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_LINEAR_ACCEL);
  1058. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "linear_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_LINEAR_ACCEL);
  1059. ADD_GROUP("Radial Accel", "radial_");
  1060. 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);
  1061. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "radial_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_RADIAL_ACCEL);
  1062. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "radial_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_RADIAL_ACCEL);
  1063. ADD_GROUP("Tangential Accel", "tangential_");
  1064. 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);
  1065. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "tangential_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_TANGENTIAL_ACCEL);
  1066. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "tangential_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_TANGENTIAL_ACCEL);
  1067. ADD_GROUP("Damping", "");
  1068. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "damping", PROPERTY_HINT_RANGE, "0,100,0.01"), "set_param", "get_param", PARAM_DAMPING);
  1069. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "damping_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_DAMPING);
  1070. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "damping_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_DAMPING);
  1071. ADD_GROUP("Angle", "");
  1072. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angle", PROPERTY_HINT_RANGE, "-720,720,0.1,or_lesser,or_greater"), "set_param", "get_param", PARAM_ANGLE);
  1073. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angle_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANGLE);
  1074. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angle_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGLE);
  1075. ADD_GROUP("Scale", "");
  1076. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale_amount", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param", "get_param", PARAM_SCALE);
  1077. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale_amount_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_SCALE);
  1078. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_amount_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_SCALE);
  1079. ADD_GROUP("Color", "");
  1080. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  1081. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");
  1082. ADD_GROUP("Hue Variation", "hue_");
  1083. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "hue_variation", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param", "get_param", PARAM_HUE_VARIATION);
  1084. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "hue_variation_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_HUE_VARIATION);
  1085. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "hue_variation_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_HUE_VARIATION);
  1086. ADD_GROUP("Animation", "anim_");
  1087. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_speed", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater"), "set_param", "get_param", PARAM_ANIM_SPEED);
  1088. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_speed_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANIM_SPEED);
  1089. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_speed_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_SPEED);
  1090. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_offset", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param", "get_param", PARAM_ANIM_OFFSET);
  1091. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_offset_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANIM_OFFSET);
  1092. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_offset_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_OFFSET);
  1093. BIND_ENUM_CONSTANT(PARAM_INITIAL_LINEAR_VELOCITY);
  1094. BIND_ENUM_CONSTANT(PARAM_ANGULAR_VELOCITY);
  1095. //BIND_ENUM_CONSTANT(PARAM_ORBIT_VELOCITY);
  1096. BIND_ENUM_CONSTANT(PARAM_LINEAR_ACCEL);
  1097. BIND_ENUM_CONSTANT(PARAM_RADIAL_ACCEL);
  1098. BIND_ENUM_CONSTANT(PARAM_TANGENTIAL_ACCEL);
  1099. BIND_ENUM_CONSTANT(PARAM_DAMPING);
  1100. BIND_ENUM_CONSTANT(PARAM_ANGLE);
  1101. BIND_ENUM_CONSTANT(PARAM_SCALE);
  1102. BIND_ENUM_CONSTANT(PARAM_HUE_VARIATION);
  1103. BIND_ENUM_CONSTANT(PARAM_ANIM_SPEED);
  1104. BIND_ENUM_CONSTANT(PARAM_ANIM_OFFSET);
  1105. BIND_ENUM_CONSTANT(PARAM_MAX);
  1106. BIND_ENUM_CONSTANT(FLAG_ALIGN_Y_TO_VELOCITY);
  1107. BIND_ENUM_CONSTANT(FLAG_ROTATE_Y);
  1108. BIND_ENUM_CONSTANT(FLAG_MAX);
  1109. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINT);
  1110. BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE);
  1111. BIND_ENUM_CONSTANT(EMISSION_SHAPE_BOX);
  1112. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINTS);
  1113. BIND_ENUM_CONSTANT(EMISSION_SHAPE_DIRECTED_POINTS);
  1114. }
  1115. CPUParticles::CPUParticles() {
  1116. time = 0;
  1117. inactive_time = 0;
  1118. frame_remainder = 0;
  1119. cycle = 0;
  1120. multimesh = VisualServer::get_singleton()->multimesh_create();
  1121. set_base(multimesh);
  1122. set_emitting(true);
  1123. set_one_shot(false);
  1124. set_amount(8);
  1125. set_lifetime(1);
  1126. set_fixed_fps(0);
  1127. set_fractional_delta(true);
  1128. set_pre_process_time(0);
  1129. set_explosiveness_ratio(0);
  1130. set_randomness_ratio(0);
  1131. set_use_local_coordinates(true);
  1132. set_draw_order(DRAW_ORDER_INDEX);
  1133. set_speed_scale(1);
  1134. set_spread(45);
  1135. set_flatness(0);
  1136. set_param(PARAM_INITIAL_LINEAR_VELOCITY, 1);
  1137. //set_param(PARAM_ORBIT_VELOCITY, 0);
  1138. set_param(PARAM_LINEAR_ACCEL, 0);
  1139. set_param(PARAM_RADIAL_ACCEL, 0);
  1140. set_param(PARAM_TANGENTIAL_ACCEL, 0);
  1141. set_param(PARAM_DAMPING, 0);
  1142. set_param(PARAM_ANGLE, 0);
  1143. set_param(PARAM_SCALE, 1);
  1144. set_param(PARAM_HUE_VARIATION, 0);
  1145. set_param(PARAM_ANIM_SPEED, 0);
  1146. set_param(PARAM_ANIM_OFFSET, 0);
  1147. set_emission_shape(EMISSION_SHAPE_POINT);
  1148. set_emission_sphere_radius(1);
  1149. set_emission_box_extents(Vector3(1, 1, 1));
  1150. set_gravity(Vector3(0, -9.8, 0));
  1151. for (int i = 0; i < PARAM_MAX; i++) {
  1152. set_param_randomness(Parameter(i), 0);
  1153. }
  1154. for (int i = 0; i < FLAG_MAX; i++) {
  1155. flags[i] = false;
  1156. }
  1157. can_update = false;
  1158. set_color(Color(1, 1, 1, 1));
  1159. #ifndef NO_THREADS
  1160. update_mutex = Mutex::create();
  1161. #endif
  1162. }
  1163. CPUParticles::~CPUParticles() {
  1164. VS::get_singleton()->free(multimesh);
  1165. #ifndef NO_THREADS
  1166. memdelete(update_mutex);
  1167. #endif
  1168. }