particles_2d.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. /*************************************************************************/
  2. /* particles_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "particles_2d.h"
  30. void ParticleAttractor2D::_notification(int p_what) {
  31. switch(p_what) {
  32. case NOTIFICATION_ENTER_TREE: {
  33. _update_owner();
  34. } break;
  35. case NOTIFICATION_DRAW: {
  36. if (!get_tree()->is_editor_hint())
  37. return;
  38. Vector2 pv;
  39. float dr = MIN(disable_radius,radius);
  40. for(int i=0;i<=32;i++) {
  41. Vector2 v(Math::sin(i/32.0*Math_PI*2),Math::cos(i/32.0*Math_PI*2));
  42. if (i>0) {
  43. draw_line(pv*radius,v*radius,Color(0,0,0.5,0.9));
  44. if (dr>0) {
  45. draw_line(pv*dr,v*dr,Color(0.5,0,0.0,0.9));
  46. }
  47. }
  48. pv=v;
  49. }
  50. } break;
  51. case NOTIFICATION_EXIT_TREE: {
  52. if (owner) {
  53. _set_owner(NULL);
  54. }
  55. } break;
  56. }
  57. }
  58. void ParticleAttractor2D::_owner_exited() {
  59. ERR_FAIL_COND(!owner);
  60. owner->attractors.erase(this);
  61. owner=NULL;
  62. }
  63. void ParticleAttractor2D::_update_owner() {
  64. if (!is_inside_tree() || !has_node(path)) {
  65. _set_owner(NULL);
  66. return;
  67. }
  68. Node *n = get_node(path);
  69. ERR_FAIL_COND(!n);
  70. Particles2D *pn = n->cast_to<Particles2D>();
  71. if (!pn) {
  72. _set_owner(NULL);
  73. return;
  74. }
  75. _set_owner(pn);
  76. }
  77. void ParticleAttractor2D::_set_owner(Particles2D* p_owner) {
  78. if (owner==p_owner)
  79. return;
  80. if (owner) {
  81. owner->disconnect("exit_tree",this,"_owner_exited");
  82. owner->attractors.erase(this);
  83. owner=NULL;
  84. }
  85. owner=p_owner;
  86. if (owner) {
  87. owner->connect("exit_tree",this,"_owner_exited",varray(),CONNECT_ONESHOT);
  88. owner->attractors.insert(this);
  89. }
  90. }
  91. void ParticleAttractor2D::_bind_methods() {
  92. ObjectTypeDB::bind_method(_MD("set_enabled","enabled"),&ParticleAttractor2D::set_enabled);
  93. ObjectTypeDB::bind_method(_MD("is_enabled"),&ParticleAttractor2D::is_enabled);
  94. ObjectTypeDB::bind_method(_MD("set_radius","radius"),&ParticleAttractor2D::set_radius);
  95. ObjectTypeDB::bind_method(_MD("get_radius"),&ParticleAttractor2D::get_radius);
  96. ObjectTypeDB::bind_method(_MD("set_disable_radius","radius"),&ParticleAttractor2D::set_disable_radius);
  97. ObjectTypeDB::bind_method(_MD("get_disable_radius"),&ParticleAttractor2D::get_disable_radius);
  98. ObjectTypeDB::bind_method(_MD("set_gravity","gravity"),&ParticleAttractor2D::set_gravity);
  99. ObjectTypeDB::bind_method(_MD("get_gravity"),&ParticleAttractor2D::get_gravity);
  100. ObjectTypeDB::bind_method(_MD("set_absorption","absorption"),&ParticleAttractor2D::set_absorption);
  101. ObjectTypeDB::bind_method(_MD("get_absorption"),&ParticleAttractor2D::get_absorption);
  102. ObjectTypeDB::bind_method(_MD("set_particles_path","path"),&ParticleAttractor2D::set_particles_path);
  103. ObjectTypeDB::bind_method(_MD("get_particles_path"),&ParticleAttractor2D::get_particles_path);
  104. ADD_PROPERTY(PropertyInfo(Variant::BOOL,"enabled"),_SCS("set_enabled"),_SCS("is_enabled"));
  105. ADD_PROPERTY(PropertyInfo(Variant::REAL,"radius",PROPERTY_HINT_RANGE,"0.1,16000,0.1"),_SCS("set_radius"),_SCS("get_radius"));
  106. ADD_PROPERTY(PropertyInfo(Variant::REAL,"disable_radius",PROPERTY_HINT_RANGE,"0.1,16000,0.1"),_SCS("set_disable_radius"),_SCS("get_disable_radius"));
  107. ADD_PROPERTY(PropertyInfo(Variant::REAL,"gravity",PROPERTY_HINT_RANGE,"-512,512,0.01"),_SCS("set_gravity"),_SCS("get_gravity"));
  108. ADD_PROPERTY(PropertyInfo(Variant::REAL,"absorption",PROPERTY_HINT_RANGE,"0,512,0.01"),_SCS("set_absorption"),_SCS("get_absorption"));
  109. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH,"particles_path",PROPERTY_HINT_RESOURCE_TYPE,"Particles2D"),_SCS("set_particles_path"),_SCS("get_particles_path"));
  110. }
  111. void ParticleAttractor2D::set_enabled(bool p_enabled) {
  112. enabled=p_enabled;
  113. }
  114. bool ParticleAttractor2D::is_enabled() const{
  115. return enabled;
  116. }
  117. void ParticleAttractor2D::set_radius(float p_radius) {
  118. radius = p_radius;
  119. update();
  120. }
  121. float ParticleAttractor2D::get_radius() const {
  122. return radius;
  123. }
  124. void ParticleAttractor2D::set_disable_radius(float p_disable_radius) {
  125. disable_radius = p_disable_radius;
  126. update();
  127. }
  128. float ParticleAttractor2D::get_disable_radius() const {
  129. return disable_radius;
  130. }
  131. void ParticleAttractor2D::set_gravity(float p_gravity) {
  132. gravity=p_gravity;
  133. }
  134. float ParticleAttractor2D::get_gravity() const {
  135. return gravity;
  136. }
  137. void ParticleAttractor2D::set_absorption(float p_absorption) {
  138. absorption=p_absorption;
  139. }
  140. float ParticleAttractor2D::get_absorption() const {
  141. return absorption;
  142. }
  143. void ParticleAttractor2D::set_particles_path(NodePath p_path) {
  144. path=p_path;
  145. _update_owner();
  146. }
  147. NodePath ParticleAttractor2D::get_particles_path() const {
  148. return path;
  149. }
  150. ParticleAttractor2D::ParticleAttractor2D() {
  151. owner=NULL;
  152. radius=50;
  153. disable_radius=0;
  154. gravity=100;
  155. absorption=0;
  156. path=String("..");
  157. enabled=true;
  158. }
  159. /****************************************/
  160. _FORCE_INLINE_ static float _rand_from_seed(uint32_t *seed) {
  161. uint32_t k;
  162. uint32_t s = (*seed);
  163. if (s == 0)
  164. s = 0x12345987;
  165. k = s / 127773;
  166. s = 16807 * (s - k * 127773) - 2836 * k;
  167. if (s < 0)
  168. s += 2147483647;
  169. (*seed) = s;
  170. float v=((float)((*seed) & 0xFFFFF))/(float)0xFFFFF;
  171. v=v*2.0-1.0;
  172. return v;
  173. }
  174. void Particles2D::_process_particles(float p_delta) {
  175. if (particles.size()==0 || lifetime==0)
  176. return;
  177. p_delta*=time_scale;
  178. float frame_time=p_delta;
  179. if (emit_timeout > 0) {
  180. time_to_live -= frame_time;
  181. if (time_to_live < 0) {
  182. emitting = false;
  183. _change_notify("config/emitting");
  184. };
  185. };
  186. float next_time = time+frame_time;
  187. if (next_time > lifetime)
  188. next_time=Math::fmod(next_time,lifetime);
  189. Particle *pdata=&particles[0];
  190. int particle_count=particles.size();
  191. Matrix32 xform;
  192. if (!local_space)
  193. xform=get_global_transform();
  194. active_count=0;
  195. DVector<Point2>::Read r;
  196. int emission_point_count=0;
  197. if (emission_points.size()) {
  198. emission_point_count=emission_points.size();
  199. r=emission_points.read();
  200. }
  201. int attractor_count=0;
  202. AttractorCache *attractor_ptr=NULL;
  203. if (attractors.size()) {
  204. if (attractors.size()!=attractor_cache.size()) {
  205. attractor_cache.resize(attractors.size());
  206. }
  207. int idx=0;
  208. Matrix32 m;
  209. if (local_space) {
  210. m= get_global_transform().affine_inverse();
  211. }
  212. for (Set<ParticleAttractor2D*>::Element *E=attractors.front();E;E=E->next()) {
  213. attractor_cache[idx].pos=m.xform( E->get()->get_global_pos() );
  214. attractor_cache[idx].attractor=E->get();
  215. idx++;
  216. }
  217. attractor_ptr=attractor_cache.ptr();
  218. attractor_count=attractor_cache.size();
  219. }
  220. for(int i=0;i<particle_count;i++) {
  221. Particle &p=pdata[i];
  222. float restart_time = (i * lifetime / particle_count) * explosiveness;
  223. bool restart=false;
  224. if ( next_time < time ) {
  225. if (restart_time > time || restart_time < next_time )
  226. restart=true;
  227. } else if (restart_time > time && restart_time < next_time ) {
  228. restart=true;
  229. }
  230. if (restart) {
  231. if (emitting) {
  232. p.pos=emissor_offset;
  233. if (emission_point_count) {
  234. Vector2 ep = r[Math::rand()%emission_point_count];
  235. if (!local_space) {
  236. p.pos=xform.xform(p.pos+ep*extents);
  237. } else {
  238. p.pos+=ep*extents;
  239. }
  240. } else {
  241. if (!local_space) {
  242. p.pos=xform.xform(p.pos+Vector2(Math::random(-extents.x,extents.x),Math::random(-extents.y,extents.y)));
  243. } else {
  244. p.pos+=Vector2(Math::random(-extents.x,extents.x),Math::random(-extents.y,extents.y));
  245. }
  246. }
  247. p.seed=Math::rand() % 12345678;
  248. uint32_t rand_seed=p.seed*(i+1);
  249. float angle = Math::deg2rad(param[PARAM_DIRECTION]+_rand_from_seed(&rand_seed)*param[PARAM_SPREAD]);
  250. p.velocity=Vector2( Math::sin(angle), Math::cos(angle) );
  251. if (!local_space) {
  252. p.velocity = xform.basis_xform(p.velocity).normalized();
  253. }
  254. p.velocity*=param[PARAM_LINEAR_VELOCITY]+param[PARAM_LINEAR_VELOCITY]*_rand_from_seed(&rand_seed)*randomness[PARAM_LINEAR_VELOCITY];
  255. p.velocity+=initial_velocity;
  256. p.active=true;
  257. p.rot=Math::deg2rad(param[PARAM_INITIAL_ANGLE]+param[PARAM_INITIAL_ANGLE]*randomness[PARAM_INITIAL_ANGLE]*_rand_from_seed(&rand_seed));
  258. active_count++;
  259. p.frame=Math::fmod(param[PARAM_ANIM_INITIAL_POS]+randomness[PARAM_ANIM_INITIAL_POS]*_rand_from_seed(&rand_seed),1.0);
  260. } else {
  261. p.active=false;
  262. }
  263. } else {
  264. if (!p.active)
  265. continue;
  266. uint32_t rand_seed=p.seed*(i+1);
  267. Vector2 force;
  268. //apply gravity
  269. float gravity_dir = Math::deg2rad( param[PARAM_GRAVITY_DIRECTION]+180*randomness[PARAM_GRAVITY_DIRECTION]*_rand_from_seed(&rand_seed));
  270. force+=Vector2( Math::sin(gravity_dir), Math::cos(gravity_dir) ) * (param[PARAM_GRAVITY_STRENGTH]+param[PARAM_GRAVITY_STRENGTH]*randomness[PARAM_GRAVITY_STRENGTH]*_rand_from_seed(&rand_seed));
  271. //apply radial
  272. Vector2 rvec = (p.pos - emissor_offset).normalized();
  273. force+=rvec*(param[PARAM_RADIAL_ACCEL]+param[PARAM_RADIAL_ACCEL]*randomness[PARAM_RADIAL_ACCEL]*_rand_from_seed(&rand_seed));
  274. //apply orbit
  275. float orbitvel = (param[PARAM_ORBIT_VELOCITY]+param[PARAM_ORBIT_VELOCITY]*randomness[PARAM_ORBIT_VELOCITY]*_rand_from_seed(&rand_seed));
  276. if (orbitvel!=0) {
  277. Vector2 rel = p.pos - xform.elements[2];
  278. Matrix32 rot(orbitvel*frame_time,Vector2());
  279. p.pos = rot.xform(rel) + xform.elements[2];
  280. }
  281. Vector2 tvec=rvec.tangent();
  282. force+=tvec*(param[PARAM_TANGENTIAL_ACCEL]+param[PARAM_TANGENTIAL_ACCEL]*randomness[PARAM_TANGENTIAL_ACCEL]*_rand_from_seed(&rand_seed));
  283. for(int j=0;j<attractor_count;j++) {
  284. Vector2 vec = (attractor_ptr[j].pos - p.pos);
  285. float vl = vec.length();
  286. if (!attractor_ptr[j].attractor->enabled || vl==0 || vl > attractor_ptr[j].attractor->radius)
  287. continue;
  288. force+=vec*attractor_ptr[j].attractor->gravity;
  289. float fvl = p.velocity.length();
  290. if (fvl && attractor_ptr[j].attractor->absorption) {
  291. Vector2 target = vec.normalized();
  292. p.velocity = p.velocity.normalized().linear_interpolate(target,MIN(frame_time*attractor_ptr[j].attractor->absorption,1))*fvl;
  293. }
  294. if (attractor_ptr[j].attractor->disable_radius && vl < attractor_ptr[j].attractor->disable_radius) {
  295. p.active=false;
  296. }
  297. }
  298. p.velocity+=force*frame_time;
  299. if (param[PARAM_DAMPING]) {
  300. float dmp = param[PARAM_DAMPING]+param[PARAM_DAMPING]*randomness[PARAM_DAMPING]*_rand_from_seed(&rand_seed);
  301. float v = p.velocity.length();
  302. v -= dmp * frame_time;
  303. if (v<=0) {
  304. p.velocity=Vector2();
  305. } else {
  306. p.velocity=p.velocity.normalized() * v;
  307. }
  308. }
  309. p.pos+=p.velocity*frame_time;
  310. p.rot+=Math::lerp(param[PARAM_SPIN_VELOCITY],param[PARAM_SPIN_VELOCITY]*randomness[PARAM_SPIN_VELOCITY]*_rand_from_seed(&rand_seed),randomness[PARAM_SPIN_VELOCITY])*frame_time;
  311. float anim_spd=param[PARAM_ANIM_SPEED_SCALE]+param[PARAM_ANIM_SPEED_SCALE]*randomness[PARAM_ANIM_SPEED_SCALE]*_rand_from_seed(&rand_seed);
  312. p.frame=Math::fposmod(p.frame+(frame_time/lifetime)*anim_spd,1.0);
  313. active_count++;
  314. }
  315. }
  316. time=Math::fmod( time+frame_time, lifetime );
  317. if (!emitting && active_count==0) {
  318. set_process(false);
  319. }
  320. update();
  321. }
  322. void Particles2D::_notification(int p_what) {
  323. switch(p_what) {
  324. case NOTIFICATION_PROCESS: {
  325. _process_particles( get_process_delta_time() );
  326. } break;
  327. case NOTIFICATION_ENTER_TREE: {
  328. float ppt=preprocess;
  329. while(ppt>0) {
  330. _process_particles(0.1);
  331. ppt-=0.1;
  332. }
  333. } break;
  334. case NOTIFICATION_DRAW: {
  335. if (particles.size()==0 || lifetime==0)
  336. return;
  337. RID ci=get_canvas_item();
  338. Size2 size(1,1);
  339. Point2 center;
  340. int total_frames=1;
  341. if (!texture.is_null()) {
  342. size=texture->get_size();
  343. size.x/=h_frames;
  344. size.y/=v_frames;
  345. total_frames=h_frames*v_frames;
  346. }
  347. float time_pos=(time/lifetime);
  348. Particle *pdata=&particles[0];
  349. int particle_count=particles.size();
  350. RID texrid;
  351. if (texture.is_valid())
  352. texrid = texture->get_rid();
  353. Matrix32 invxform;
  354. if (!local_space)
  355. invxform=get_global_transform().affine_inverse();
  356. int start_particle = (int)(time * (float)particle_count / lifetime);
  357. for (int id=0;id<particle_count;++id) {
  358. int i = start_particle + id;
  359. if (i >= particle_count) {
  360. i -= particle_count;
  361. }
  362. Particle &p=pdata[i];
  363. if (!p.active)
  364. continue;
  365. float ptime = ((float)i / particle_count)*explosiveness;
  366. if (ptime<time_pos)
  367. ptime=time_pos-ptime;
  368. else
  369. ptime=(1.0-ptime)+time_pos;
  370. uint32_t rand_seed=p.seed*(i+1);
  371. Color color;
  372. if(color_ramp.is_valid())
  373. {
  374. color = color_ramp->get_color_at_offset(ptime);
  375. } else
  376. {
  377. color = default_color;
  378. }
  379. {
  380. float huerand=_rand_from_seed(&rand_seed);
  381. float huerot = param[PARAM_HUE_VARIATION] + randomness[PARAM_HUE_VARIATION] * huerand;
  382. if (Math::abs(huerot) > CMP_EPSILON) {
  383. float h=color.get_h();
  384. float s=color.get_s();
  385. float v=color.get_v();
  386. float a=color.a;
  387. //float preh=h;
  388. h+=huerot;
  389. h=Math::abs(Math::fposmod(h,1.0));
  390. //print_line("rand: "+rtos(randomness[PARAM_HUE_VARIATION])+" rand: "+rtos(huerand));
  391. //print_line(itos(i)+":hue: "+rtos(preh)+" + "+rtos(huerot)+" = "+rtos(h));
  392. color.set_hsv(h,s,v);
  393. color.a=a;
  394. }
  395. }
  396. float initial_size = param[PARAM_INITIAL_SIZE]+param[PARAM_INITIAL_SIZE]*_rand_from_seed(&rand_seed)*randomness[PARAM_FINAL_SIZE];
  397. float final_size = param[PARAM_FINAL_SIZE]+param[PARAM_FINAL_SIZE]*_rand_from_seed(&rand_seed)*randomness[PARAM_FINAL_SIZE];
  398. float size_mult=initial_size*(1.0-ptime) + final_size*ptime;
  399. //Size2 rectsize=size * size_mult;
  400. //rectsize=rectsize.floor();
  401. //Rect2 r = Rect2(Vecto,rectsize);
  402. Matrix32 xform;
  403. if (p.rot) {
  404. xform.set_rotation(p.rot);
  405. xform.translate(-size*size_mult/2.0);
  406. xform.elements[2]+=p.pos;
  407. } else {
  408. xform.elements[2]=-size*size_mult/2.0;
  409. xform.elements[2]+=p.pos;
  410. }
  411. if (!local_space) {
  412. xform = invxform * xform;
  413. }
  414. xform.scale_basis(Size2(size_mult,size_mult));
  415. VisualServer::get_singleton()->canvas_item_add_set_transform(ci,xform);
  416. if (texrid.is_valid()) {
  417. Rect2 src_rect;
  418. src_rect.size=size;
  419. if (total_frames>1) {
  420. int frame = Math::fast_ftoi(Math::floor(p.frame*total_frames)) % total_frames;
  421. src_rect.pos.x = size.x * (frame%h_frames);
  422. src_rect.pos.y = size.y * (frame/h_frames);
  423. }
  424. texture->draw_rect_region(ci,Rect2(Point2(),size),src_rect,color);
  425. //VisualServer::get_singleton()->canvas_item_add_texture_rect(ci,r,texrid,false,color);
  426. } else {
  427. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2(),size),color);
  428. }
  429. }
  430. } break;
  431. }
  432. }
  433. static const char* _particlesframe_property_names[Particles2D::PARAM_MAX]={
  434. "params/direction",
  435. "params/spread",
  436. "params/linear_velocity",
  437. "params/spin_velocity",
  438. "params/orbit_velocity",
  439. "params/gravity_direction",
  440. "params/gravity_strength",
  441. "params/radial_accel",
  442. "params/tangential_accel",
  443. "params/damping",
  444. "params/initial_angle",
  445. "params/initial_size",
  446. "params/final_size",
  447. "params/hue_variation",
  448. "params/anim_speed_scale",
  449. "params/anim_initial_pos",
  450. };
  451. static const char* _particlesframe_property_rnames[Particles2D::PARAM_MAX]={
  452. "randomness/direction",
  453. "randomness/spread",
  454. "randomness/linear_velocity",
  455. "randomness/spin_velocity",
  456. "randomness/orbit_velocity",
  457. "randomness/gravity_direction",
  458. "randomness/gravity_strength",
  459. "randomness/radial_accel",
  460. "randomness/tangential_accel",
  461. "randomness/damping",
  462. "randomness/initial_angle",
  463. "randomness/initial_size",
  464. "randomness/final_size",
  465. "randomness/hue_variation",
  466. "randomness/anim_speed_scale",
  467. "randomness/anim_initial_pos",
  468. };
  469. static const char* _particlesframe_property_ranges[Particles2D::PARAM_MAX]={
  470. "0,360,0.01",
  471. "0,180,0.01",
  472. "-1024,1024,0.01",
  473. "-1024,1024,0.01",
  474. "-1024,1024,0.01",
  475. "0,360,0.01",
  476. "0,1024,0.01",
  477. "-128,128,0.01",
  478. "-128,128,0.01",
  479. "0,1024,0.001",
  480. "0,360,0.01",
  481. "0,1024,0.01",
  482. "0,1024,0.01",
  483. "0,1,0.01",
  484. "0,128,0.01",
  485. "0,1,0.01",
  486. };
  487. void Particles2D::set_emitting(bool p_emitting) {
  488. if (emitting==p_emitting)
  489. return;
  490. if (p_emitting) {
  491. if (active_count==0)
  492. time=0;
  493. set_process(true);
  494. time_to_live = emit_timeout;
  495. };
  496. emitting=p_emitting;
  497. _change_notify("config/emitting");
  498. }
  499. bool Particles2D::is_emitting() const {
  500. return emitting;
  501. }
  502. void Particles2D::set_amount(int p_amount) {
  503. ERR_FAIL_INDEX(p_amount,1024+1);
  504. particles.resize(p_amount);
  505. }
  506. int Particles2D::get_amount() const {
  507. return particles.size();
  508. }
  509. void Particles2D::set_emit_timeout(float p_timeout) {
  510. emit_timeout = p_timeout;
  511. time_to_live = p_timeout;
  512. };
  513. float Particles2D::get_emit_timeout() const {
  514. return emit_timeout;
  515. };
  516. void Particles2D::set_lifetime(float p_lifetime) {
  517. ERR_FAIL_INDEX(p_lifetime,3600+1);
  518. lifetime=p_lifetime;
  519. }
  520. float Particles2D::get_lifetime() const {
  521. return lifetime;
  522. }
  523. void Particles2D::set_time_scale(float p_time_scale) {
  524. time_scale=p_time_scale;
  525. }
  526. float Particles2D::get_time_scale() const {
  527. return time_scale;
  528. }
  529. void Particles2D::set_pre_process_time(float p_pre_process_time) {
  530. preprocess=p_pre_process_time;
  531. }
  532. float Particles2D::get_pre_process_time() const{
  533. return preprocess;
  534. }
  535. void Particles2D::set_param(Parameter p_param, float p_value) {
  536. ERR_FAIL_INDEX(p_param,PARAM_MAX);
  537. param[p_param]=p_value;
  538. }
  539. float Particles2D::get_param(Parameter p_param) const {
  540. ERR_FAIL_INDEX_V(p_param,PARAM_MAX,0);
  541. return param[p_param];
  542. }
  543. void Particles2D::set_randomness(Parameter p_param, float p_value) {
  544. ERR_FAIL_INDEX(p_param,PARAM_MAX);
  545. randomness[p_param]=p_value;
  546. }
  547. float Particles2D::get_randomness(Parameter p_param) const {
  548. ERR_FAIL_INDEX_V(p_param,PARAM_MAX,0);
  549. return randomness[p_param];
  550. }
  551. void Particles2D::set_texture(const Ref<Texture>& p_texture) {
  552. texture=p_texture;
  553. }
  554. Ref<Texture> Particles2D::get_texture() const {
  555. return texture;
  556. }
  557. void Particles2D::set_color(const Color& p_color) {
  558. default_color = p_color;
  559. }
  560. Color Particles2D::get_color() const {
  561. return default_color;
  562. }
  563. void Particles2D::set_color_ramp(const Ref<ColorRamp>& p_color_ramp) {
  564. color_ramp=p_color_ramp;
  565. }
  566. Ref<ColorRamp> Particles2D::get_color_ramp() const {
  567. return color_ramp;
  568. }
  569. void Particles2D::set_emissor_offset(const Point2& p_offset) {
  570. emissor_offset=p_offset;
  571. }
  572. Point2 Particles2D::get_emissor_offset() const {
  573. return emissor_offset;
  574. }
  575. void Particles2D::set_use_local_space(bool p_use) {
  576. local_space=p_use;
  577. }
  578. bool Particles2D::is_using_local_space() const {
  579. return local_space;
  580. }
  581. //Deprecated. Converts color phases to color ramp
  582. void Particles2D::set_color_phases(int p_phases) {
  583. //Create color ramp if we have 2 or more phases.
  584. //Otherwise first phase phase will be assigned to default color.
  585. if(p_phases > 1 && color_ramp.is_null())
  586. {
  587. color_ramp = Ref<ColorRamp>(memnew (ColorRamp()));
  588. }
  589. if(color_ramp.is_valid())
  590. {
  591. color_ramp->get_points().resize(p_phases);
  592. }
  593. }
  594. //Deprecated.
  595. int Particles2D::get_color_phases() const {
  596. if(color_ramp.is_valid())
  597. {
  598. return color_ramp->get_points_count();
  599. }
  600. return 0;
  601. }
  602. //Deprecated. Converts color phases to color ramp
  603. void Particles2D::set_color_phase_color(int p_phase,const Color& p_color) {
  604. ERR_FAIL_INDEX(p_phase,MAX_COLOR_PHASES);
  605. if(color_ramp.is_valid())
  606. {
  607. if(color_ramp->get_points_count() > p_phase)
  608. color_ramp->set_color(p_phase, p_color);
  609. } else
  610. {
  611. if(p_phase == 0)
  612. default_color = p_color;
  613. }
  614. }
  615. //Deprecated.
  616. Color Particles2D::get_color_phase_color(int p_phase) const {
  617. ERR_FAIL_INDEX_V(p_phase,MAX_COLOR_PHASES,Color());
  618. if(color_ramp.is_valid())
  619. {
  620. return color_ramp->get_color(p_phase);
  621. }
  622. return Color(0,0,0,1);
  623. }
  624. //Deprecated. Converts color phases to color ramp
  625. void Particles2D::set_color_phase_pos(int p_phase,float p_pos) {
  626. ERR_FAIL_INDEX(p_phase,MAX_COLOR_PHASES);
  627. ERR_FAIL_COND(p_pos<0.0 || p_pos>1.0);
  628. if(color_ramp.is_valid() && color_ramp->get_points_count() > p_phase)
  629. {
  630. return color_ramp->set_offset(p_phase, p_pos);
  631. }
  632. }
  633. //Deprecated.
  634. float Particles2D::get_color_phase_pos(int p_phase) const {
  635. ERR_FAIL_INDEX_V(p_phase,MAX_COLOR_PHASES,0);
  636. if(color_ramp.is_valid())
  637. {
  638. return color_ramp->get_offset(p_phase);
  639. }
  640. return 0;
  641. }
  642. void Particles2D::set_emission_half_extents(const Vector2& p_extents) {
  643. extents=p_extents;
  644. }
  645. Vector2 Particles2D::get_emission_half_extents() const {
  646. return extents;
  647. }
  648. void Particles2D::testee(int a, int b, int c, int d, int e) {
  649. print_line(itos(a));
  650. print_line(itos(b));
  651. print_line(itos(c));
  652. print_line(itos(d));
  653. print_line(itos(e));
  654. }
  655. void Particles2D::set_initial_velocity(const Vector2& p_velocity) {
  656. initial_velocity=p_velocity;
  657. }
  658. Vector2 Particles2D::get_initial_velocity() const{
  659. return initial_velocity;
  660. }
  661. void Particles2D::pre_process(float p_delta) {
  662. _process_particles(p_delta);
  663. }
  664. void Particles2D::set_explosiveness(float p_value) {
  665. explosiveness=p_value;
  666. }
  667. float Particles2D::get_explosiveness() const{
  668. return explosiveness;
  669. }
  670. void Particles2D::set_flip_h(bool p_flip) {
  671. flip_h=p_flip;
  672. }
  673. bool Particles2D::is_flipped_h() const{
  674. return flip_h;
  675. }
  676. void Particles2D::set_flip_v(bool p_flip){
  677. flip_v=p_flip;
  678. }
  679. bool Particles2D::is_flipped_v() const{
  680. return flip_v;
  681. }
  682. void Particles2D::set_h_frames(int p_frames) {
  683. ERR_FAIL_COND(p_frames<1);
  684. h_frames=p_frames;
  685. }
  686. int Particles2D::get_h_frames() const{
  687. return h_frames;
  688. }
  689. void Particles2D::set_v_frames(int p_frames){
  690. ERR_FAIL_COND(p_frames<1);
  691. v_frames=p_frames;
  692. }
  693. int Particles2D::get_v_frames() const{
  694. return v_frames;
  695. }
  696. void Particles2D::set_emission_points(const DVector<Vector2>& p_points) {
  697. emission_points=p_points;
  698. }
  699. DVector<Vector2> Particles2D::get_emission_points() const{
  700. return emission_points;
  701. }
  702. void Particles2D::reset() {
  703. for(int i=0;i<particles.size();i++) {
  704. particles[i].active=false;
  705. }
  706. time=0;
  707. active_count=0;
  708. }
  709. void Particles2D::_bind_methods() {
  710. ObjectTypeDB::bind_method(_MD("set_emitting","active"),&Particles2D::set_emitting);
  711. ObjectTypeDB::bind_method(_MD("is_emitting"),&Particles2D::is_emitting);
  712. ObjectTypeDB::bind_method(_MD("set_amount","amount"),&Particles2D::set_amount);
  713. ObjectTypeDB::bind_method(_MD("get_amount"),&Particles2D::get_amount);
  714. ObjectTypeDB::bind_method(_MD("set_lifetime","lifetime"),&Particles2D::set_lifetime);
  715. ObjectTypeDB::bind_method(_MD("get_lifetime"),&Particles2D::get_lifetime);
  716. ObjectTypeDB::bind_method(_MD("set_time_scale","time_scale"),&Particles2D::set_time_scale);
  717. ObjectTypeDB::bind_method(_MD("get_time_scale"),&Particles2D::get_time_scale);
  718. ObjectTypeDB::bind_method(_MD("set_pre_process_time","time"),&Particles2D::set_pre_process_time);
  719. ObjectTypeDB::bind_method(_MD("get_pre_process_time"),&Particles2D::get_pre_process_time);
  720. ObjectTypeDB::bind_method(_MD("set_emit_timeout","value"),&Particles2D::set_emit_timeout);
  721. ObjectTypeDB::bind_method(_MD("get_emit_timeout"),&Particles2D::get_emit_timeout);
  722. ObjectTypeDB::bind_method(_MD("set_param","param","value"),&Particles2D::set_param);
  723. ObjectTypeDB::bind_method(_MD("get_param","param"),&Particles2D::get_param);
  724. ObjectTypeDB::bind_method(_MD("set_randomness","param","value"),&Particles2D::set_randomness);
  725. ObjectTypeDB::bind_method(_MD("get_randomness","param"),&Particles2D::get_randomness);
  726. ObjectTypeDB::bind_method(_MD("set_texture:Texture","texture"),&Particles2D::set_texture);
  727. ObjectTypeDB::bind_method(_MD("get_texture:Texture"),&Particles2D::get_texture);
  728. ObjectTypeDB::bind_method(_MD("set_color","color"),&Particles2D::set_color);
  729. ObjectTypeDB::bind_method(_MD("get_color"),&Particles2D::get_color);
  730. ObjectTypeDB::bind_method(_MD("set_color_ramp:ColorRamp","color_ramp"),&Particles2D::set_color_ramp);
  731. ObjectTypeDB::bind_method(_MD("get_color_ramp:ColorRamp"),&Particles2D::get_color_ramp);
  732. ObjectTypeDB::bind_method(_MD("set_emissor_offset","offset"),&Particles2D::set_emissor_offset);
  733. ObjectTypeDB::bind_method(_MD("get_emissor_offset"),&Particles2D::get_emissor_offset);
  734. ObjectTypeDB::bind_method(_MD("set_flip_h","enable"),&Particles2D::set_flip_h);
  735. ObjectTypeDB::bind_method(_MD("is_flipped_h"),&Particles2D::is_flipped_h);
  736. ObjectTypeDB::bind_method(_MD("set_flip_v","enable"),&Particles2D::set_flip_v);
  737. ObjectTypeDB::bind_method(_MD("is_flipped_v"),&Particles2D::is_flipped_v);
  738. ObjectTypeDB::bind_method(_MD("set_h_frames","enable"),&Particles2D::set_h_frames);
  739. ObjectTypeDB::bind_method(_MD("get_h_frames"),&Particles2D::get_h_frames);
  740. ObjectTypeDB::bind_method(_MD("set_v_frames","enable"),&Particles2D::set_v_frames);
  741. ObjectTypeDB::bind_method(_MD("get_v_frames"),&Particles2D::get_v_frames);
  742. ObjectTypeDB::bind_method(_MD("set_emission_half_extents","extents"),&Particles2D::set_emission_half_extents);
  743. ObjectTypeDB::bind_method(_MD("get_emission_half_extents"),&Particles2D::get_emission_half_extents);
  744. ObjectTypeDB::bind_method(_MD("set_color_phases","phases"),&Particles2D::set_color_phases);
  745. ObjectTypeDB::bind_method(_MD("get_color_phases"),&Particles2D::get_color_phases);
  746. ObjectTypeDB::bind_method(_MD("set_color_phase_color","phase","color"),&Particles2D::set_color_phase_color);
  747. ObjectTypeDB::bind_method(_MD("get_color_phase_color","phase"),&Particles2D::get_color_phase_color);
  748. ObjectTypeDB::bind_method(_MD("set_color_phase_pos","phase","pos"),&Particles2D::set_color_phase_pos);
  749. ObjectTypeDB::bind_method(_MD("get_color_phase_pos","phase"),&Particles2D::get_color_phase_pos);
  750. ObjectTypeDB::bind_method(_MD("pre_process","time"),&Particles2D::pre_process);
  751. ObjectTypeDB::bind_method(_MD("reset"),&Particles2D::reset);
  752. ObjectTypeDB::bind_method(_MD("set_use_local_space","enable"),&Particles2D::set_use_local_space);
  753. ObjectTypeDB::bind_method(_MD("is_using_local_space"),&Particles2D::is_using_local_space);
  754. ObjectTypeDB::bind_method(_MD("set_initial_velocity","velocity"),&Particles2D::set_initial_velocity);
  755. ObjectTypeDB::bind_method(_MD("get_initial_velocity"),&Particles2D::get_initial_velocity);
  756. ObjectTypeDB::bind_method(_MD("set_explosiveness","amount"),&Particles2D::set_explosiveness);
  757. ObjectTypeDB::bind_method(_MD("get_explosiveness"),&Particles2D::get_explosiveness);
  758. ObjectTypeDB::bind_method(_MD("set_emission_points","points"),&Particles2D::set_emission_points);
  759. ObjectTypeDB::bind_method(_MD("get_emission_points"),&Particles2D::get_emission_points);
  760. ADD_PROPERTY(PropertyInfo(Variant::INT,"config/amount",PROPERTY_HINT_EXP_RANGE,"1,1024"),_SCS("set_amount"),_SCS("get_amount") );
  761. ADD_PROPERTY(PropertyInfo(Variant::REAL,"config/lifetime",PROPERTY_HINT_EXP_RANGE,"0.1,3600,0.1"),_SCS("set_lifetime"),_SCS("get_lifetime") );
  762. ADD_PROPERTYNO(PropertyInfo(Variant::REAL,"config/time_scale",PROPERTY_HINT_EXP_RANGE,"0.01,128,0.01"),_SCS("set_time_scale"),_SCS("get_time_scale") );
  763. ADD_PROPERTYNZ(PropertyInfo(Variant::REAL,"config/preprocess",PROPERTY_HINT_EXP_RANGE,"0.1,3600,0.1"),_SCS("set_pre_process_time"),_SCS("get_pre_process_time") );
  764. ADD_PROPERTYNZ(PropertyInfo(Variant::REAL,"config/emit_timeout",PROPERTY_HINT_RANGE,"0,3600,0.1"),_SCS("set_emit_timeout"),_SCS("get_emit_timeout") );
  765. ADD_PROPERTYNO(PropertyInfo(Variant::BOOL,"config/emitting"),_SCS("set_emitting"),_SCS("is_emitting") );
  766. ADD_PROPERTYNZ(PropertyInfo(Variant::VECTOR2,"config/offset"),_SCS("set_emissor_offset"),_SCS("get_emissor_offset"));
  767. ADD_PROPERTYNZ(PropertyInfo(Variant::VECTOR2,"config/half_extents"),_SCS("set_emission_half_extents"),_SCS("get_emission_half_extents"));
  768. ADD_PROPERTYNO(PropertyInfo(Variant::BOOL,"config/local_space"),_SCS("set_use_local_space"),_SCS("is_using_local_space"));
  769. ADD_PROPERTYNO(PropertyInfo(Variant::REAL,"config/explosiveness",PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_explosiveness"),_SCS("get_explosiveness"));
  770. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL,"config/flip_h"),_SCS("set_flip_h"),_SCS("is_flipped_h"));
  771. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL,"config/flip_v"),_SCS("set_flip_v"),_SCS("is_flipped_v"));
  772. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"config/texture",PROPERTY_HINT_RESOURCE_TYPE,"Texture"),_SCS("set_texture"),_SCS("get_texture"));
  773. ADD_PROPERTYNO(PropertyInfo(Variant::INT,"config/h_frames",PROPERTY_HINT_RANGE,"1,512,1"),_SCS("set_h_frames"),_SCS("get_h_frames"));
  774. ADD_PROPERTYNO(PropertyInfo(Variant::INT,"config/v_frames",PROPERTY_HINT_RANGE,"1,512,1"),_SCS("set_v_frames"),_SCS("get_v_frames"));
  775. for(int i=0;i<PARAM_MAX;i++) {
  776. ADD_PROPERTYI(PropertyInfo(Variant::REAL,_particlesframe_property_names[i],PROPERTY_HINT_RANGE,_particlesframe_property_ranges[i]),_SCS("set_param"),_SCS("get_param"),i);
  777. }
  778. for(int i=0;i<PARAM_MAX;i++) {
  779. ADD_PROPERTYINZ(PropertyInfo(Variant::REAL,_particlesframe_property_rnames[i],PROPERTY_HINT_RANGE,"-1,1,0.01"),_SCS("set_randomness"),_SCS("get_randomness"),i);
  780. }
  781. ADD_PROPERTYNZ( PropertyInfo( Variant::INT, "color_phases/count",PROPERTY_HINT_RANGE,"0,4,1", 0), _SCS("set_color_phases"), _SCS("get_color_phases"));
  782. //Backward compatibility. They will be converted to color ramp
  783. for(int i=0;i<MAX_COLOR_PHASES;i++) {
  784. String phase="phase_"+itos(i)+"/";
  785. ADD_PROPERTYI( PropertyInfo( Variant::REAL, phase+"pos", PROPERTY_HINT_RANGE,"0,1,0.01", 0),_SCS("set_color_phase_pos"),_SCS("get_color_phase_pos"),i );
  786. ADD_PROPERTYI( PropertyInfo( Variant::COLOR, phase+"color", PROPERTY_HINT_NONE, "", 0),_SCS("set_color_phase_color"),_SCS("get_color_phase_color"),i );
  787. }
  788. ADD_PROPERTYNO(PropertyInfo(Variant::COLOR, "color/color"),_SCS("set_color"),_SCS("get_color"));
  789. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"color/color_ramp",PROPERTY_HINT_RESOURCE_TYPE,"ColorRamp"),_SCS("set_color_ramp"),_SCS("get_color_ramp"));
  790. ADD_PROPERTYNZ(PropertyInfo(Variant::VECTOR2_ARRAY,"emission_points",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),_SCS("set_emission_points"),_SCS("get_emission_points"));
  791. BIND_CONSTANT( PARAM_DIRECTION );
  792. BIND_CONSTANT( PARAM_SPREAD );
  793. BIND_CONSTANT( PARAM_LINEAR_VELOCITY );
  794. BIND_CONSTANT( PARAM_SPIN_VELOCITY );
  795. BIND_CONSTANT( PARAM_ORBIT_VELOCITY );
  796. BIND_CONSTANT( PARAM_GRAVITY_DIRECTION );
  797. BIND_CONSTANT( PARAM_GRAVITY_STRENGTH );
  798. BIND_CONSTANT( PARAM_RADIAL_ACCEL );
  799. BIND_CONSTANT( PARAM_TANGENTIAL_ACCEL );
  800. BIND_CONSTANT( PARAM_DAMPING );
  801. BIND_CONSTANT( PARAM_INITIAL_ANGLE );
  802. BIND_CONSTANT( PARAM_INITIAL_SIZE );
  803. BIND_CONSTANT( PARAM_FINAL_SIZE );
  804. BIND_CONSTANT( PARAM_HUE_VARIATION );
  805. BIND_CONSTANT( PARAM_ANIM_SPEED_SCALE );
  806. BIND_CONSTANT( PARAM_ANIM_INITIAL_POS );
  807. BIND_CONSTANT( PARAM_MAX );
  808. BIND_CONSTANT( MAX_COLOR_PHASES );
  809. }
  810. Particles2D::Particles2D() {
  811. for(int i=0;i<PARAM_MAX;i++) {
  812. param[i]=0;
  813. randomness[i]=0;
  814. }
  815. set_param(PARAM_SPREAD,10);
  816. set_param(PARAM_LINEAR_VELOCITY,20);
  817. set_param(PARAM_GRAVITY_STRENGTH,9.8);
  818. set_param(PARAM_RADIAL_ACCEL,0);
  819. set_param(PARAM_TANGENTIAL_ACCEL,0);
  820. set_param(PARAM_INITIAL_ANGLE,0.0);
  821. set_param(PARAM_INITIAL_SIZE,1.0);
  822. set_param(PARAM_FINAL_SIZE,1.0);
  823. set_param(PARAM_ANIM_SPEED_SCALE,1.0);
  824. set_color(Color(1,1,1,1));
  825. time=0;
  826. lifetime=2;
  827. emitting=false;
  828. particles.resize(32);
  829. active_count=-1;
  830. set_emitting(true);
  831. local_space=true;
  832. preprocess=0;
  833. time_scale=1.0;
  834. flip_h=false;
  835. flip_v=false;
  836. v_frames=1;
  837. h_frames=1;
  838. emit_timeout = 0;
  839. time_to_live = 0;
  840. explosiveness=1.0;
  841. }