Particles.hx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. package h2d;
  2. /**
  3. See `ParticleGroup.sortMode` - not used anywhere.
  4. **/
  5. @:dox(hide)
  6. enum PartSortMode {
  7. /**
  8. Particles are not sorted.
  9. **/
  10. None;
  11. /**
  12. Particles are sorted back-to-front every frame based on their current position.
  13. **/
  14. Dynamic;
  15. }
  16. /**
  17. The particle emission pattern modes. See `ParticleGroup.emitMode`.
  18. **/
  19. enum PartEmitMode {
  20. /**
  21. A single Point, that emits in all directions.
  22. **/
  23. Point;
  24. /**
  25. A cone, parametrized with `emitAngle` and `emitDistance`.
  26. **/
  27. Cone;
  28. /**
  29. A box, parametrized with `emitDist` and `emitDistY`.
  30. **/
  31. Box;
  32. /**
  33. A box, parametrized with `emitAngle` and `emitDistance`.
  34. **/
  35. Direction;
  36. }
  37. private class ParticleShader extends hxsl.Shader {
  38. static var SRC = {
  39. @input var input : { color : Vec4 };
  40. @const var hasGradient : Bool;
  41. @const var has2DGradient : Bool;
  42. @param var gradient : Sampler2D;
  43. var pixelColor : Vec4;
  44. var textureColor : Vec4;
  45. function fragment() {
  46. pixelColor = textureColor; // ignore input.color RGB
  47. pixelColor.a *= input.color.a;
  48. if( has2DGradient ) {
  49. var g = gradient.get(vec2(input.color.r, textureColor.r));
  50. pixelColor.rgb = g.rgb;
  51. pixelColor.a *= g.a;
  52. } else if( hasGradient )
  53. pixelColor *= gradient.get(input.color.rg);
  54. }
  55. }
  56. }
  57. @:access(h2d.ParticleGroup)
  58. private class Particle extends h2d.SpriteBatch.BatchElement {
  59. var group : ParticleGroup;
  60. public var vx : Float;
  61. public var vy : Float;
  62. public var vSize : Float;
  63. public var vr : Float;
  64. public var maxLife : Float;
  65. public var life : Float;
  66. public var delay : Float;
  67. public function new(group) {
  68. super(null);
  69. this.group = group;
  70. }
  71. override function update(et:Float) {
  72. if( delay > 0 ) {
  73. delay -= et;
  74. if( delay <= 0 )
  75. visible = true;
  76. else {
  77. visible = false;
  78. return true;
  79. }
  80. }
  81. var dv = Math.pow(1 + group.speedIncr, et);
  82. vx *= dv;
  83. vy *= dv;
  84. vx += group.gravity * et * group.sinGravityAngle;
  85. vy += group.gravity * et * group.cosGravityAngle;
  86. x += vx * et;
  87. y += vy * et;
  88. life += et;
  89. if( group.rotAuto )
  90. rotation = Math.atan2(vy, vx) + life * vr + group.rotInit * Math.PI;
  91. else
  92. rotation += vr * et;
  93. if (group.incrX)
  94. scaleX *= Math.pow(1 + vSize, et);
  95. if (group.incrY)
  96. scaleY *= Math.pow(1 + vSize, et);
  97. var t = life / maxLife;
  98. if( t < group.fadeIn )
  99. alpha = Math.pow(t / group.fadeIn, group.fadePower);
  100. else if( t > group.fadeOut )
  101. alpha = Math.pow((1 - t) / (1 - group.fadeOut), group.fadePower);
  102. else
  103. alpha = 1;
  104. r = t; // pass to pshader for colorGradient
  105. if( group.animationRepeat > 0 )
  106. this.t = group.tiles[Std.int(t * group.tiles.length * group.animationRepeat) % group.tiles.length];
  107. if( t > 1 ) {
  108. if( group.emitLoop ) {
  109. @:privateAccess group.init(this);
  110. delay = 0;
  111. } else
  112. return false;
  113. }
  114. return true;
  115. }
  116. }
  117. /**
  118. An emitter of a single particle group. Part of `Particles` simulation system.
  119. **/
  120. @:access(h2d.SpriteBatch)
  121. @:access(h2d.Object)
  122. class ParticleGroup {
  123. static var FIELDS = null;
  124. static function getFields( inst : ParticleGroup ) {
  125. if( FIELDS != null )
  126. return FIELDS;
  127. FIELDS = Type.getInstanceFields(ParticleGroup);
  128. for( f in ["parts", "pshader", "batch", "needRebuild", "emitMode", "sortMode", "blendMode", "texture", "colorGradient", "tiles"] )
  129. FIELDS.remove(f);
  130. for( f in FIELDS.copy() )
  131. if( Reflect.isFunction(Reflect.field(inst, f)) )
  132. FIELDS.remove(f);
  133. FIELDS.sort(Reflect.compare);
  134. return FIELDS;
  135. }
  136. var parts : Particles;
  137. var batch : SpriteBatch;
  138. var needRebuild = true;
  139. var tiles : Array<h2d.Tile>;
  140. /**
  141. The group name.
  142. **/
  143. public var name : String;
  144. /**
  145. Disabling the group immediately removes it from rendering and resets it's state.
  146. **/
  147. public var enable(default, set) : Bool = true;
  148. /**
  149. Does nothing.
  150. **/
  151. public var sortMode(default, set) : PartSortMode = None;
  152. /**
  153. Configures blending mode for this group.
  154. **/
  155. public var blendMode(default, set) : BlendMode = Alpha;
  156. /**
  157. Maximum number of particles alive at a time.
  158. **/
  159. public var nparts(default, set) : Int = 100;
  160. /**
  161. Initial particle X offset.
  162. **/
  163. public var dx(default, set) : Int = 0;
  164. /**
  165. Initial particle Y offset.
  166. **/
  167. public var dy(default, set) : Int = 0;
  168. /**
  169. If enabled, group will emit new particles indefinitely maintaining number of particles at `ParticleGroup.nparts`.
  170. **/
  171. public var emitLoop(default, set) : Bool = true;
  172. /**
  173. The pattern in which particles are emitted. See individual `PartEmitMode` values for more details.
  174. **/
  175. public var emitMode(default, set):PartEmitMode = Point;
  176. /**
  177. Initial particle position distance from emission point.
  178. **/
  179. public var emitStartDist(default, set) : Float = 0.;
  180. /**
  181. Additional random particle position distance from emission point.
  182. **/
  183. public var emitDist(default, set) : Float = 50.;
  184. /**
  185. Secondary random position distance modifier (used by `Box` emitMode)
  186. **/
  187. public var emitDistY(default, set) : Float = 50.;
  188. /**
  189. Normalized particle emission direction angle.
  190. **/
  191. public var emitAngle(default, set) : Float = -0.5;
  192. /**
  193. When enabled, particle rotation will match the particle movement direction angle.
  194. **/
  195. public var emitDirectionAsAngle(default, set) : Bool = false;
  196. /**
  197. Randomized synchronization delay before particle appears after being emitted.
  198. Usage note for non-relative mode: Particle will use configuration that was happened at time of emission, not when delay timer runs out.
  199. **/
  200. public var emitSync(default, set) : Float = 0;
  201. /**
  202. Fixed delay before particle appears after being emitted.
  203. Usage note for non-relative mode: Particle will use configuration that was happened at time of emission, not when delay timer runs out.
  204. **/
  205. public var emitDelay(default, set) : Float = 0;
  206. /**
  207. Initial particle size.
  208. **/
  209. public var size(default, set) : Float = 1;
  210. /**
  211. If set, particle will change it's size with time.
  212. **/
  213. public var sizeIncr(default, set) : Float = 0;
  214. /**
  215. If enabled, particle will increase on X-axis with `sizeIncr`.
  216. **/
  217. public var incrX(default, set) : Bool = true;
  218. /**
  219. If enabled, particle will increase on Y-axis with `sizeIncr`.
  220. **/
  221. public var incrY(default, set) : Bool = true;
  222. /**
  223. Additional random size increase when particle is created.
  224. **/
  225. public var sizeRand(default, set) : Float = 0;
  226. /**
  227. Initial particle lifetime.
  228. **/
  229. public var life(default, set) : Float = 1;
  230. /**
  231. Additional random lifetime increase when particle is created.
  232. **/
  233. public var lifeRand(default, set) : Float = 0;
  234. /**
  235. Initial particle velocity.
  236. **/
  237. public var speed(default, set) : Float = 50.;
  238. /**
  239. Additional random velocity increase when particle is created.
  240. **/
  241. public var speedRand(default, set) : Float = 0;
  242. /**
  243. If set, particle velocity will change over time.
  244. **/
  245. public var speedIncr(default, set) : Float = 0;
  246. /**
  247. Gravity applied to the particle.
  248. **/
  249. public var gravity(default, set) : Float = 0;
  250. /**
  251. The gravity angle in radians. `0` points down.
  252. **/
  253. public var gravityAngle(default, set) : Float = 0;
  254. @:noCompletion @:dox(hide) public var cosGravityAngle : Float;
  255. @:noCompletion @:dox(hide) public var sinGravityAngle : Float;
  256. /**
  257. Initial particle rotation.
  258. **/
  259. public var rotInit(default, set) : Float = 0;
  260. /**
  261. Initial rotation speed of the particle.
  262. **/
  263. public var rotSpeed(default, set) : Float = 0;
  264. /**
  265. Additional random rotation speed when particle is created.
  266. **/
  267. public var rotSpeedRand(default, set):Float = 0;
  268. /**
  269. If enabled, particles will be automatically rotated in the direction of particle velocity.
  270. **/
  271. public var rotAuto = false;
  272. /**
  273. The time in seconds during which particle alpha fades in after being emitted.
  274. **/
  275. public var fadeIn : Float = 0.2;
  276. /**
  277. The time in seconds at which particle will start to fade out before dying. Fade out time can be calculated with `lifetime - fadeOut`.
  278. **/
  279. public var fadeOut : Float = 0.8;
  280. /**
  281. The exponent of the alpha transition speed on fade in and fade out.
  282. **/
  283. public var fadePower : Float = 1;
  284. /**
  285. Total count of frames used by the group.
  286. When 0, amount of frames in a group calculated by `frameDivisionX * frameDivisionY`.
  287. Otherwise it's `min(frameDivisionX * frameDivisionY, frameCount)`.
  288. **/
  289. public var frameCount(default,set) : Int = 0;
  290. /**
  291. Horizontal frame divisor.
  292. **/
  293. public var frameDivisionX(default,set) : Int = 1;
  294. /**
  295. Vertical frame divisor.
  296. **/
  297. public var frameDivisionY(default,set) : Int = 1;
  298. /**
  299. The amount of times the animations will loop during lifetime.
  300. Settings it to 0 will stop the animation playback and each particle will have a random frame assigned at emission time.
  301. **/
  302. public var animationRepeat(default,set) : Float = 1;
  303. /**
  304. The texture used to render particles.
  305. **/
  306. public var texture(default,set) : h3d.mat.Texture;
  307. /**
  308. Optional color gradient texture for tinting.
  309. **/
  310. public var colorGradient(default,set) : h3d.mat.Texture;
  311. /**
  312. When enabled, causes particles to always render relative to the emitter position, moving along with it.
  313. Otherwise, once emitted, particles won't follow the emitter, and will render relative to the scene origin.
  314. Non-relative mode is useful for simulating something like a smoke coming from a moving object,
  315. while relative mode things like jet flame that have to stick to its emission source.
  316. **/
  317. public var isRelative(default, set) : Bool = true;
  318. /**
  319. Should group rebuild on parameters change.
  320. Note that some parameters take immediate effect on the existing particles, and some would force rebuild regardless of this setting.
  321. Parameters that take immediate effect:
  322. `speedIncr`, `gravity`, `gravityAngle`, `fadeIn`, `fadeOut`, `fadePower`, `rotAuto`, `rotInit`, `incrX`, `incrY`, `emitLoop` and `blendMode`
  323. Parameters that will always force rebuild:
  324. `enable`, `sortMode`, `isRelative`, `texture`, `frameCount`, `frameDivisionX`, `frameDivisionY` and `nparts`
  325. Parameters that newer cause rebuild:
  326. `blendMode`, `colorGradient` and `animationRepeat`
  327. **/
  328. public var rebuildOnChange : Bool = true;
  329. inline function set_enable(v) { enable = v; if( !v ) { batch.clear(); needRebuild = true; }; return v; }
  330. inline function set_sortMode(v) { needRebuild = true; return sortMode = v; }
  331. inline function set_blendMode(v) { batch.blendMode = v; return blendMode = v; }
  332. inline function set_size(v) { if (rebuildOnChange) needRebuild = true; return size = v; }
  333. inline function set_sizeRand(v) { if (rebuildOnChange) needRebuild = true; return sizeRand = v; }
  334. inline function set_sizeIncr(v) { if (rebuildOnChange) needRebuild = true; return sizeIncr = v; }
  335. inline function set_incrX(v) { if (rebuildOnChange) needRebuild = true; return incrX = v; }
  336. inline function set_incrY(v) { if (rebuildOnChange) needRebuild = true; return incrY = v; }
  337. inline function set_speed(v) { if (rebuildOnChange) needRebuild = true; return speed = v; }
  338. inline function set_speedIncr(v) { if (rebuildOnChange) needRebuild = true; return speedIncr = v; }
  339. inline function set_gravity(v) { if (rebuildOnChange) needRebuild = true; return gravity = v; }
  340. inline function set_gravityAngle(v : Float) {
  341. if (rebuildOnChange) needRebuild = true;
  342. cosGravityAngle = Math.cos(v * Math.PI * 0.5);
  343. sinGravityAngle = Math.sin(v * Math.PI * 0.5);
  344. return gravityAngle = v;
  345. }
  346. inline function set_speedRand(v) { if (rebuildOnChange) needRebuild = true; return speedRand = v; }
  347. inline function set_life(v) { if (rebuildOnChange) needRebuild = true; return life = v; }
  348. inline function set_lifeRand(v) { if (rebuildOnChange) needRebuild = true; return lifeRand = v; }
  349. inline function set_nparts(n) { needRebuild = true; return nparts = n; }
  350. inline function set_dx(v) { if (rebuildOnChange) needRebuild = true; return dx = v; }
  351. inline function set_dy(v) { if (rebuildOnChange) needRebuild = true; return dy = v; }
  352. inline function set_emitLoop(v) { if (rebuildOnChange) needRebuild = true; return emitLoop = v; }
  353. inline function set_emitMode(v) { if (rebuildOnChange) needRebuild = true; return emitMode = v; }
  354. inline function set_emitStartDist(v) { if (rebuildOnChange) needRebuild = true; return emitStartDist = v; }
  355. inline function set_emitDist(v) { if (rebuildOnChange) needRebuild = true; return emitDist = v; }
  356. inline function set_emitDistY(v) { if (rebuildOnChange) needRebuild = true; return emitDistY = v; }
  357. inline function set_emitAngle(v) { if (rebuildOnChange) needRebuild = true; return emitAngle = v; }
  358. inline function set_emitDirectionAsAngle(v) { if (rebuildOnChange) needRebuild = true; return emitDirectionAsAngle = v; }
  359. inline function set_emitSync(v) { if (rebuildOnChange) needRebuild = true; return emitSync = v; }
  360. inline function set_emitDelay(v) { if (rebuildOnChange) needRebuild = true; return emitDelay = v; }
  361. inline function set_rotInit(v) { if (rebuildOnChange) needRebuild = true; return rotInit = v; }
  362. inline function set_rotSpeed(v) { if (rebuildOnChange) needRebuild = true; return rotSpeed = v; }
  363. inline function set_rotSpeedRand(v) { if (rebuildOnChange) needRebuild = true; return rotSpeedRand = v; }
  364. inline function set_texture(t) { texture = t; makeTiles(); return t; }
  365. inline function set_colorGradient(t) { colorGradient = t; return t; }
  366. inline function set_frameCount(v) { frameCount = v; makeTiles(); return v; }
  367. inline function set_frameDivisionX(v) { frameDivisionX = v; makeTiles(); return v; }
  368. inline function set_frameDivisionY(v) { frameDivisionY = v; makeTiles(); return v; }
  369. inline function set_animationRepeat(v) return animationRepeat = v;
  370. inline function set_isRelative(v) { needRebuild = true; return isRelative = v; }
  371. /**
  372. Create a new particle group instance.
  373. @param p The parent Particles instance. Group does not automatically adds itself to the Particles.
  374. **/
  375. public function new(p) {
  376. this.parts = p;
  377. batch = new SpriteBatch(null, p);
  378. batch.visible = false;
  379. batch.hasRotationScale = true;
  380. batch.hasUpdate = true;
  381. this.texture = null;
  382. }
  383. function makeTiles() {
  384. var t : h2d.Tile;
  385. // hide : create default particle
  386. if( h3d.Engine.getCurrent() == null ) {
  387. tiles = [];
  388. needRebuild = true;
  389. return;
  390. }
  391. if( texture == null )
  392. t = h2d.Tile.fromColor(0xFFFFFF, 16, 16);
  393. else
  394. t = h2d.Tile.fromTexture(texture);
  395. batch.tile = t;
  396. var dx = Std.int(t.width / frameDivisionX);
  397. var dy = Std.int(t.height / frameDivisionY);
  398. tiles = [for( y in 0...frameDivisionY ) for( x in 0...frameDivisionX ) if( frameCount == 0 || y * frameDivisionX + x < frameCount ) t.sub(x * dx, y * dy, dx, dy, -dx >> 1, -dy >> 1)];
  399. needRebuild = true;
  400. }
  401. /**
  402. Reset current state of particle group and re-emit all particles.
  403. **/
  404. public function rebuild() {
  405. needRebuild = false;
  406. batch.clear();
  407. for( i in 0...nparts ) {
  408. var p = new Particle(this);
  409. batch.add(p);
  410. init(p);
  411. }
  412. }
  413. function init( p : Particle ) {
  414. inline function srand() return hxd.Math.srand();
  415. inline function rand() return hxd.Math.random();
  416. inline function getAngleFromNormalized(a : Float, rand : Float = 1.) : Float {
  417. var newAngle = a * 0.5 * Math.PI * rand;
  418. if (a < 0) newAngle += Math.PI;
  419. return newAngle;
  420. };
  421. var g = this;
  422. var size = g.size * (1 + srand() * g.sizeRand);
  423. var rot = srand() * Math.PI * g.rotInit;
  424. var vrot = g.rotSpeed * (1 + rand() * g.rotSpeedRand) * (srand() < 0 ? -1 : 1);
  425. var life = g.life * (1 + srand() * g.lifeRand);
  426. var delay = rand() * life * (1 - g.emitSync) + g.emitDelay;
  427. var speed = g.speed * (1 + srand() * g.speedRand);
  428. if( g.life == 0 )
  429. life = 1e10;
  430. p.x = dx;
  431. p.y = dy;
  432. switch( g.emitMode ) {
  433. case Point:
  434. p.vx = srand();
  435. p.vy = srand();
  436. speed *= 1 / hxd.Math.sqrt(p.vx * p.vx + p.vy * p.vy);
  437. var r = g.emitStartDist + g.emitDist * rand();
  438. p.x += p.vx * r;
  439. p.y += p.vy * r;
  440. case Cone:
  441. if (g.emitAngle == 0) {
  442. p.vx = 0.;
  443. p.vy = 0.;
  444. }
  445. else {
  446. var theta = rand() * Math.PI * 2;
  447. var phi = getAngleFromNormalized(g.emitAngle, srand());
  448. p.vx = Math.sin(phi) * Math.cos(theta);
  449. p.vy = Math.cos(phi);
  450. }
  451. var r = g.emitStartDist + g.emitDist * rand();
  452. p.x += p.vx * r;
  453. p.y += p.vy * r;
  454. case Box:
  455. p.vx = srand();
  456. p.vy = srand();
  457. p.x += g.emitDist * srand();
  458. p.y += g.emitDistY * srand();
  459. var a = getAngleFromNormalized(g.emitAngle);
  460. var cosA = Math.cos(a);
  461. var sinA = Math.sin(a);
  462. var xx = cosA * (p.x - dx) - sinA * (p.y - dy) + dx;
  463. var yy = sinA * (p.x - dx) + cosA * (p.y - dy) + dy;
  464. p.x = xx;
  465. p.y = yy;
  466. case Direction:
  467. speed = Math.abs(speed);
  468. p.vx = Math.cos(g.emitAngle);
  469. p.vy = Math.sin(g.emitAngle);
  470. var r = g.emitStartDist + g.emitDist * rand();
  471. p.x += r * Math.cos(g.emitAngle - Math.PI / 2);
  472. p.y += r * Math.sin(g.emitAngle - Math.PI / 2);
  473. }
  474. p.scale = size;
  475. p.rotation = rot;
  476. p.vSize = g.sizeIncr;
  477. p.vr = vrot;
  478. p.t = animationRepeat == 0 ? tiles[Std.random(tiles.length)] : tiles[0];
  479. p.delay = delay;
  480. p.vx *= speed;
  481. p.vy *= speed;
  482. p.life = 0;
  483. p.maxLife = life;
  484. var rot = emitDirectionAsAngle ? Math.atan2(p.vy, p.vx) : srand() * Math.PI * g.rotInit;
  485. p.rotation = rot;
  486. if ( !isRelative ) {
  487. // Less this.parts access
  488. var parts = this.parts;
  489. // calcAbsPos() was already called, because during both rebuild() and Particle.update()
  490. // called during sync() call which calls this function if required before any of this happens.
  491. //parts.syncPos();
  492. var px = p.x;
  493. p.x = px * parts.matA + p.y * parts.matC + parts.absX;
  494. p.y = px * parts.matB + p.y * parts.matD + parts.absY;
  495. p.scaleX = Math.sqrt((parts.matA * parts.matA) + (parts.matC * parts.matC)) * size;
  496. p.scaleY = Math.sqrt((parts.matB * parts.matB) + (parts.matD * parts.matD)) * size;
  497. var rot = Math.atan2(parts.matB / p.scaleY, parts.matA / p.scaleX);
  498. p.rotation += rot;
  499. // Also rotate velocity.
  500. var cos = Math.cos(rot);
  501. var sin = Math.sin(rot);
  502. px = p.vx;
  503. p.vx = px * cos - p.vy * sin;
  504. p.vy = px * sin + p.vy * cos;
  505. }
  506. }
  507. /**
  508. Saves the particle group configuration into a `Dynamic` object.
  509. **/
  510. public function save() {
  511. var o : Dynamic = {};
  512. for( f in getFields(this) )
  513. Reflect.setField(o, f, Reflect.field(this, f));
  514. o.emitMode = emitMode.getName();
  515. o.sortMode = sortMode.getName();
  516. o.blendMode = blendMode.getName();
  517. if( texture != null ) o.texture = texture.name;
  518. if( colorGradient != null ) o.colorGradient = colorGradient.name;
  519. return o;
  520. }
  521. /**
  522. Loads the particle group configuration from a given object.
  523. @param version The version of Particles that were used to save the configuration.
  524. @param o The previously saved configuration data to load.
  525. **/
  526. public function load( version : Int, o : Dynamic ) {
  527. for( f in getFields(this) )
  528. if( Reflect.hasField(o,f) )
  529. Reflect.setProperty(this, f, Reflect.field(o, f));
  530. emitMode = PartEmitMode.createByName(o.emitMode);
  531. sortMode = PartSortMode.createByName(o.sortMode);
  532. blendMode = BlendMode.createByName(o.blendMode);
  533. if( o.texture != null ) texture = @:privateAccess parts.loadTexture(o.texture);
  534. if( o.colorGradient != null ) colorGradient = @:privateAccess parts.loadTexture(o.colorGradient);
  535. }
  536. }
  537. /**
  538. A 2D particle system with wide range of customizability.
  539. The Particles instance can contain multiple `ParticleGroup` instances - each of which works independently from one another.
  540. To simplify designing of the particles [HIDE](https://github.com/HeapsIO/hide/) contains a dedicated 2D particle editor and
  541. stores the particle data in a JSON format, which then can be loaded with the `Particles.load` method:
  542. ```haxe
  543. var part = new h2d.Particles();
  544. part.load(haxe.Json.parse(hxd.Res.my_parts_file.entry.getText()), hxd.Res.my_parts_file.entry.path);
  545. ```
  546. **/
  547. @:access(h2d.ParticleGroup)
  548. class Particles extends Drawable {
  549. static inline var VERSION = 1;
  550. var groups : Array<ParticleGroup>;
  551. var resourcePath : String;
  552. var hideProps : Dynamic;
  553. var pshader : ParticleShader;
  554. /**
  555. Create a new Particles instance.
  556. @param parent An optional parent `h2d.Object` instance to which Particles adds itself if set.
  557. **/
  558. public function new( ?parent ) {
  559. super(parent);
  560. groups = [];
  561. pshader = new ParticleShader();
  562. addShader(pshader);
  563. }
  564. function loadTexture( path : String ) {
  565. return hxd.res.Loader.currentInstance.load(path).toTexture();
  566. }
  567. /**
  568. Sent when all particle groups stopped playback.
  569. Restarts all groups by default.
  570. **/
  571. public dynamic function onEnd() {
  572. for( g in groups )
  573. g.needRebuild = true;
  574. }
  575. /**
  576. Saves Particles settings and returns an object that can be saved into a file and then loaded with a `Particles.load` method.
  577. **/
  578. public function save() : Dynamic {
  579. var obj : Dynamic = { type : "particles2D", version : VERSION, groups : [for( g in groups ) g.save()] };
  580. if( hideProps != null ) obj.hide = hideProps;
  581. return obj;
  582. }
  583. /**
  584. Loads previously saved Particles settings.
  585. @param o The saved Particles settings.
  586. @param resourcePath An optional path of the configuration file. May be safely omitted.
  587. **/
  588. public function load( o : Dynamic, ?resourcePath : String ) {
  589. this.resourcePath = resourcePath;
  590. if( o.version == 0 || o.version > VERSION ) throw "Unsupported version " + o.version;
  591. for( g in (o.groups:Array<Dynamic>) )
  592. addGroup().load(o.version, g);
  593. hideProps = o.hide;
  594. }
  595. /**
  596. Add new particle group to the Particles.
  597. @param g Particle group to add. If null, will create an empty ParticleGroup.
  598. Note that when passing existing group, it should be created with this Particles instanceas the constructor argument,
  599. otherwise it may lead to undefined behavior.
  600. @param index Optional insertion index at which the group should be inserted.
  601. @returns Added ParticleGroup instance.
  602. **/
  603. public function addGroup( ?g : ParticleGroup, ?index ) {
  604. if( g == null )
  605. g = new ParticleGroup(this);
  606. if( g.name == null )
  607. g.name = "Group#" + (groups.length + 1);
  608. if( index == null )
  609. index = groups.length;
  610. groups.insert(index, g);
  611. return g;
  612. }
  613. /**
  614. Removes the group from the Particles.
  615. **/
  616. public function removeGroup( g : ParticleGroup ) {
  617. var idx = groups.indexOf(g);
  618. if( idx < 0 ) return;
  619. groups.splice(idx,1);
  620. }
  621. /**
  622. Returns a group with a specified name or `null` if none found.
  623. **/
  624. public function getGroup( name : String ) {
  625. for( g in groups )
  626. if( g.name == name )
  627. return g;
  628. return null;
  629. }
  630. override function sync(ctx:RenderContext) {
  631. super.sync(ctx);
  632. var hasPart = false;
  633. for( g in groups ) {
  634. if ( g.needRebuild && g.enable )
  635. g.rebuild();
  636. if( @:privateAccess g.batch.first != null )
  637. hasPart = true;
  638. }
  639. if( !hasPart )
  640. onEnd();
  641. }
  642. override function draw(ctx:RenderContext) {
  643. var old = blendMode;
  644. var realX : Float = absX;
  645. var realY : Float = absY;
  646. var realA : Float = matA;
  647. var realB : Float = matB;
  648. var realC : Float = matC;
  649. var realD : Float = matD;
  650. for( g in groups )
  651. if( g.enable ) {
  652. pshader.gradient = g.colorGradient;
  653. pshader.hasGradient = g.colorGradient != null && g.colorGradient.height == 1;
  654. pshader.has2DGradient = g.colorGradient != null && g.colorGradient.height > 1;
  655. blendMode = g.batch.blendMode;
  656. if ( g.isRelative ) {
  657. g.batch.drawWith(ctx, this);
  658. } else {
  659. matA = 1;
  660. matB = 0;
  661. matC = 0;
  662. matD = 1;
  663. absX = 0;
  664. absY = 0;
  665. g.batch.drawWith(ctx, this);
  666. matA = realA;
  667. matB = realB;
  668. matC = realC;
  669. matD = realD;
  670. absX = realX;
  671. absY = realY;
  672. }
  673. }
  674. blendMode = old;
  675. }
  676. /**
  677. Returns an Iterator of particle groups within Particles.
  678. **/
  679. public inline function getGroups() {
  680. return groups.iterator();
  681. }
  682. }