-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdixieApi.js
2980 lines (2516 loc) · 85.4 KB
/
dixieApi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Guillem Martínez Jiménez
* In this file you can find the Api for JS
* It's possible that you have to rewrite the shaders depending on how your framework works
*/
//The globals for the API
var DixieGlobals = {
blending_factors : ["Zero", "One", "Source Color", "One minus source color", "Destination color", "One minus destination color", "Source alpha", "One minus source alpha", "Destination alpha", "One minus destination alpha"],
possible_origins : ["Point", "Mesh"],
spawn_modes : ["Linear", "Waves"],
DEG2RAD : Math.PI / 180,
force_types : ["gravity", "vortex", "magnet"],
cond_type : ["condition", "merged conditions"],
cond_oper : ["Equals", "Greater than", "Less than", "Greater than or equals", "Less than or equals", "No equals"],
cond_prop : ["Speed", "Life time", "Size"],
cond_mode : ["And", "Or"],
mod_prop : ["Speed", "Size", "Color", "Life time"],
mod_appl : ["Equalization", "Addition", "Subtraction"],
mod_modi : ["Along life time", "User defined"],
defaultSrcbValue : "Source alpha",
defaultDstbValue : "One",
defaultOrigin : "Point",
defaultPosition : [0,0,0],
defaultAtlasName : "None",
defaultUvs : [],
defaultTexture : {
id : -1,
prop : {
subtextures : false,
textures_x : 1,
textures_y : 1,
animated : false,
anim_loop : false,
anim_duration : 0
}
},
defaultParticleData : {
max_speed: [1,1,1],
min_speed: [-1,-1,-1],
max_size: 0.25,
min_size: 0.10,
max_life_time: 10,
min_life_time: 5,
color: [1,1,1,1]
},
defaultMesh : {
name : "None",
modal : []
},
identity: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
defaultSubEmittors : [],
defaultForces : [],
defaultGravity : {
direction: [0,-1,0],
strength: 1
},
defaultVortex : {
position: [0,0,0],
angular_speed: [1,1,1],
scale: 10,
color: [1,1,1,1]
},
defaultMagnet : {
position: [0,0,0],
strength: 10,
scale: 10,
color: [1,1,1,1]
},
defaultModifications : [],
defaultConditions : [],
default_centers : [-1.0,-1.0,1.0, 1.0,-1.0,1.0, 1.0,1.0,1.0, 1.0,1.0,1.0, -1.0,1.0,1.0, -1.0,-1.0,1.0],
default_coords : [1,1, 0,1, 1,0, 0,0, 1,0, 0,1],
square_vertices : [0.5,0.5, -0.5,0.5, 0.5,-0.5, -0.5,-0.5, 0.5,-0.5, -0.5,0.5],
default_color : [1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1],
default_sizes : [0.25,0.25, 0.25,0.25, 0.25,0.25, 0.25,0.25, 0.25,0.25, 0.25,0.25],
default_visibility : [0, 0, 0, 0, 0, 0],
/*
* Make a linear interpolation between two numbers
* @method lerp
* @params {Number} The start of the interpolation
* @params {Number} The end of the interpolation
* @params {Number} The value to be interpolated
*/
lerp :
function(s, e, x)
{
return s * ( 1 - x ) + e * x;
},
/*
* This method returns a random number
* @method randomNumber
* @params {Number} the minimum value of the random number
* @params {Number} the maximum value of the random number
*/
randomNumber:
function(min, max)
{
return Math.random() * (max - min) + min;
},
/*
* This method returns the cross product of two vectors
* @method cross
* @params {vector3} the first vector
* @params {vector3} the second vector
*/
cross:
function(a, b)
{
var c = new Float32Array(3);
c[0] = a[1]*b[2] - a[2]*b[1];
c[1] = a[2]*b[0] - a[0]*b[2];
c[2] = a[0]*b[1] - a[1]*b[0];
return c;
},
/*
* If a equation is gived then this function calculates the factor of the new value using it
* @method computeChangeEquation
* @params {Number} The value of the X
* @params {List} The number in front of the X
*/
computeEquation:
function(equation_, factor_)
{
let value = 0;
let length = equation_.length;
for (let i = 0; i < length; ++i)
value += equation_[i]*Math.pow(factor_, length-1-i);
if(value >= 0.99)
value = 1;
return value;
},
/*
* Given a matrix4x4 and a vector3 this function multiplies both
* @method vec3MultMatrix4
* @params {List} The matrix4x4
* @params {List} The vector3
*/
vec3MultMatrix4 :
function(m_, vector3_)
{
let x = vector3_[0], y = vector3_[1], z = vector3_[2];
vector3_[0] = m_[0] * x + m_[1] * y + m_[2] * z + m_[3];
vector3_[1] = m_[4] * x + m_[5] * y + m_[6] * z + m_[7];
vector3_[2] = m_[8] * x + m_[9] * y + m_[10] * z + m_[11];
},
//For doing the billboard I follow the next tutorial
//http://www.opengl-tutorial.org/intermediate-tutorials/billboards-particles/billboards/
vs_particles : '\
precision highp float;\
\
in vec3 vertices;\
in vec3 a_normal;\
in vec2 coords;\
in vec2 icoord;\
in vec4 colors;\
in vec2 size;\
in float visible;\
\
out vec4 v_color;\
out vec3 v_normal;\
out vec3 v_pos;\
out vec2 v_coord;\
out float v_visible;\
\
uniform mat4 u_viewprojection;\
uniform mat4 u_mvp;\
uniform mat4 u_model;\
uniform vec3 u_up;\
uniform vec3 u_right;\
\
\
void main() {\
v_visible = visible;\
v_coord = coords;\
v_color = colors;\
v_normal = (u_model * vec4(a_normal, 0.0)).xyz;\
v_pos = vertices + u_right * icoord.x * size.x + u_up * icoord.y * size.y;\
gl_Position = u_mvp * (u_model * vec4(v_pos, 1.0));\
\
}',
//The fragment shader for plain particles
fs_flat_p : '\
precision highp float;\
in vec4 v_color;\
in float v_visible;\
\
void main() {\
if (v_visible == 0.0) discard;\
pc_fragColor = v_color;\
}',
//The fragment shader for textured particles
fs_texture : '\
\
precision highp float;\
in vec4 v_color;\
in vec2 v_coord;\
in float v_visible;\
uniform sampler2D u_texture;\
\
void main() {\
if (v_visible == 0.0) discard;\
vec4 color = v_color * texture(u_texture, v_coord);\
if (color.a < 0.1)\
discard;\
pc_fragColor = color;\
}'
}
/*
* This class is for save information about every particle and control his properties along his lifetime
* @class DixieParticle
*/
class DixieParticle {
/*
* The constructor for the particle
* @method constructor
*/
constructor() {}
/*
* The inicializer of the particle, given all the data the particle information is filled
* @method fill
* @params {Object} The data of the particle (Speed, lifetime...)
* @params {Object} The inforation about the texture
* @params {List} The uvs of the atlas
* @params {String} The origin of the particle (subemitter or emitter)
* @params {Number} The id of the particle
*/
fill(data_, texture_, uvs_, origin_ = undefined, id_ = undefined) {
//If the origin and the id are diferents to undefined then a new particle is created
if(origin_ != undefined)
this.origin = origin_;
if(id_ != undefined)
this.id = id_;
//Set the position and the color
this.position = data_.position.slice(0);
this.iColor = data_.color.slice(0);
this.color = data_.color.slice(0);
//Get a random size for the particle
let s = DixieGlobals.randomNumber(data_.min_size, data_.max_size);
this.size = s;
this.iSize = s;
//Define a random speed for the particle
let speed = new Float32Array(3);
speed[0] = DixieGlobals.randomNumber(data_.min_speed[0], data_.max_speed[0]);
speed[1] = DixieGlobals.randomNumber(data_.min_speed[1], data_.max_speed[1]);
speed[2] = DixieGlobals.randomNumber(data_.min_speed[2], data_.max_speed[2]);
this.speed = speed;
this.aSpeed = [0,0,0];
this.iSpeed = speed.slice(0);
//Radom definition of the lifetime
let lifetime = DixieGlobals.randomNumber(data_.min_life_time, data_.max_life_time);
this.lifetime = lifetime;
this.iLifetime = lifetime;
this.c_lifetime = 0.0; //How many life time the particle lived
this.visibility = 1;
//Definition of the meeted conditions for the particle
this.conditions_meet = [];
//Set the texture information
this.texture_id = texture_.id;
this.subtextures = false; //If the texture is an atlas
this.textures_x = 0; //Number of textures in the component X
this.textures_y = 0; //Number of textures in the component Y
this.animated = false; //If is an animated texture
this.frameRate = 0; //The frameRate
this.c_frame = 0; //The curent frame (in ms)
this.frameX = 0; //In which frame is the particle
this.frameY = 0;
//If there is a texture then the data will be updated
if(texture_.id != -1)
{
let t_prop = texture_.prop;
//Get the uvs of the textured assigned to the particles (this can be not final depending if the texture is an atlas or animated)
this.uvs = uvs_[texture_.id];
this.subtextures = t_prop.subtextures; //Set if the texture is an atlas
//If is an atlas then the number of textures in x and y will be saved
if(t_prop.subtextures)
{
this.textures_x = t_prop.textures_x;
this.textures_y = t_prop.textures_y;
}
//If is a animated texture then all the data that depends on the animations
//will be inicialized
if(t_prop.animated)
{
this.animated = true;
let t = lifetime;
//The animation can be a loop or just "one time"
if(t_prop.anim_loop)
t = t_prop.anim_duration;
//Update the current frame in Y (The first one is on the last line)
this.frameY = t_prop.textures_y - 1;
//Calculate the frame rate of the particles
let frame_number = t_prop.textures_x * t_prop.textures_y;
this.frameRate = (t / frame_number);
}
}
//Calculate the texture coordinates of the particle
this.getCoords(true);
}
/*
* The lifetime, current frame and the visibility of the particles is updated
* @method updateLifetime
* @params {Number} The passed time (in ms) since the last frame
* @params {List} The list of particles to reset
*/
updateLifetime(dt_, to_reset_) {
//Update lifetime and frame
this.c_lifetime += dt_;
this.c_frame += dt_;
//If the current lifetime is bigger than the lifetime and is visible then the
//particle will be setted to a reset mode
if(this.c_lifetime >= this.lifetime && this.visibility == 1)
{
this.visibility = 0;
to_reset_.push(this.id);
}
}
/*
* Apply a basic movement (uniform rectilinear motion) to the particle depending on its speed
* @method move
* @params {Number} The passed time (in ms) since the last frame
*/
move(dt_) {
if(this.visibility == 1)
{
this.aSpeed = [0,0,0];
for(let i = 0; i < 3; ++i)
{
this.position[i] += this.speed[i] * dt_;
this.aSpeed[i] += this.speed[i];
}
}
}
/*
* Apply a serie of forces to the particle
* @method applyForces
* @params {Number} The passed time (in ms) since the last frame
* @params {List} The list of the forces that affects to the particle
*/
applyForces(dt_, forces_) {
let force, distance = [0,0,0], distance_factor;
let v_vortex;
for(let i = 0; i < forces_.length; ++i)
{
force = forces_[i];
if(!this.testCondition(force.condition))
continue;
switch (force.type) {
case "gravity":
for(let j = 0; j < 3; ++j)
this.position[j] += force.direction[j] * force.strength * dt_;
break;
case "vortex":
//First to all the distance between the particle and the vortex is calculated
for(let j = 0; j < 3; ++j)
distance[j] = this.position[j] - force.position[j];
//Then the cross product and the distance factor are computed
v_vortex = DixieGlobals.cross(force.angular_speed, distance);
//The distance factor uses a formula which is based on inverse square distance, avoiding singularity at the center
distance_factor = 1/(1+(distance[0]*distance[0]+distance[1]*distance[1]+distance[2]*distance[2])/force.scale);
for(let j = 0; j < 3; ++j)
this.position[j] += v_vortex[j] * distance_factor * dt_;
break;
case "magnet":
//First to all the distance between the particle and the vortex is calculated
for(let j = 0; j < 3; ++j)
distance[j] = this.position[j] - force.position[j];
//The distance factor uses a formula which is based on inverse square distance, avoiding singularity at the center
distance_factor = 1/(1+(distance[0]*distance[0]+distance[1]*distance[1]+distance[2]*distance[2])/force.scale);
distance_factor *= force.strength;
for(let j = 0; j < 3; ++j)
this.position[j] += distance[j] * distance_factor * dt_;
break;
}
}
}
/*
* Given a condition test if the particle meets it
* @method testCondition
* @params {Object} The condition
* @params {Number} The passed time (in ms) since the last frame
*/
testCondition(condition_, dt_ = 0.0) {
//If the condition is true always will be meeted (default conditon)
if(condition_ == true)
return true;
else if (condition_ == "On particle die") //The condition can be when a particle dies (default for subemitters)
{
//Test if the particle is recently "dead"
let diff = this.lifetime - this.c_lifetime;
if(diff <= dt_ && this.visibility != 0)
return true;
else
return false;
}
let meet = false;
let c;
//See if the condition is a merging of several conditions
switch (condition_.type) {
//If is a confition make the comprobation
case "condition":
let tested_value;
let value_to_test = condition_.value;
//If the condition only can be meted one time and is already
//meted then return false
if(condition_.one_time)
for(let i = 0; i < this.conditions_meet.length; ++i)
if(this.conditions_meet[i].id == condition_.id)
return false;
//See the condition to be tested
switch (condition_.property) {
case "Life time":
tested_value = this.lifetime;
break;
case "Speed":
tested_value = this.speed;
break;
case "Size":
tested_value = this.size;
break;
}
//See the operator for the condition
switch (condition_.operator) {
case "Equals":
if (tested_value == value_to_test)
meet = true;
break;
case "Greater than":
if (tested_value > value_to_test)
meet = true;
break;
case "Less than":
if (tested_value < value_to_test)
meet = true;
break;
case "Greater than or equals":
if (tested_value >= value_to_test)
meet = true;
break;
case "Less than or equals":
if (tested_value <= value_to_test)
meet = true;
break;
case "No equals":
if (tested_value != value_to_test)
meet = true;
break;
}
if(meet)
{
//See if the condition is meteed
c = this.getCondition(condition_.id)
//If is not meted then push it, instead just update when is meted
if(c == undefined)
this.conditions_meet.push({id: condition_.id, meet_at: this.c_lifetime})
else
c.meet_at = this.c_lifetime;
}
break;
//If is merged test both conditions
case "merged condition":
let conditions = condition_.conditions;
switch (condition_.mode) {
case "And":
return testCondition(conditions[0]) && testCondition(conditions[1]);
break;
case "Or":
return testCondition(conditions[0]) || testCondition(conditions[1]);
break;
}
break;
}
//return if the condition is meted
return meet;
}
/*
* See if a condition is already meted
* @method getCondition
* @params {Number} The id of the condition
*/
getCondition(id_) {
let conditions = this.conditions_meet;
let c;
for(let i = 0; i < conditions.length; ++i)
{
c = conditions[i];
if(c.id == id_)
return c;
}
return undefined;
}
/*
* Apply a serie of modifications to the particle
* @method applyModifications
* @params {Number} The passed time (in ms) since the last frame
* @params {List} The list of the modifications that affects to the particle
*/
applyModifications(dt_, modifications_) {
let modification, changed_value;
let final_value, new_value;
let application_mode, modification_mode, meet_at;
let x, e, factor;
x = this.c_lifetime;
for(let i = 0; i < modifications_.length; ++i)
{
modification = modifications_[i];
//See if the condition for the modification is meted
if(!this.testCondition(modification.condition))
continue;
new_value = modification.new_value;
application_mode = modification.application_mode;
modification_mode = modification.modification_mode;
if(modification_mode == "Along life time")
e = this.lifetime;
else if(modification_mode == "User defined")
{
//The conditions aren't meeted
if(x < modification.user_defined_start)
continue;
e = modification.user_defined_seconds + modification.user_defined_start;
//If is not true
if(!modification.condition)
{
meet_at = this.getCondition(modification.condition.id).meet_at;
e += meet_at;
}
}
//Compute the factor
factor = x / e;
if(modification.equation.length > 0)
factor = DixieGlobals.computeEquation(modification.equation, factor)
//Modify the property
switch (modification.changed_property) {
case "Color":
changed_value = this.color;
final_value = [0,0,0,0];
final_value[0] = new_value[0];
final_value[1] = new_value[1];
final_value[2] = new_value[2];
final_value[3] = new_value[3];
if(application_mode == "Addition")
for(let i = 0; i < 4; ++i)
final_value[i] = this.iColor[i] + final_value[i];
else if(application_mode == "Subtraction")
for(let i = 0; i < 4; ++i)
final_value[i] = this.iColor[i] - final_value[i];
for(let i = 0; i < 4 ; ++i)
this.color[i] = final_value[i] * factor + this.iColor[i] * (1.0 - factor);
break;
case "Life time":
changed_value = this.lifetime;
final_value = new_value;
if(application_mode == "Addition")
final_value += this.iLifetime;
else if(application_mode == "Subtraction")
final_value = Math.max(this.iLifetime - final_value, 0);
this.lifetime = final_value * factor + this.iLifetime * (1.0 - factor);
break;
case "Speed":
changed_value = this.speed;
final_value = [0,0,0];
final_value[0] = new_value[0];
final_value[1] = new_value[1];
final_value[2] = new_value[2];
if(application_mode == "Addition")
for(let i = 0; i < 3; ++i)
final_value[i] = this.iColor[i] + final_value[i];
else if(application_mode == "Subtraction")
for(let i = 0; i < 3; ++i)
final_value[i] = this.iColor[i] - final_value[i];
for(let i = 0; i < 4 ; ++i)
this.speed[i] = final_value[i] * factor + this.iSpeed[i] * (1.0 - factor);
break;
case "Size":
changed_value = this.size;
final_value = new_value;
if(application_mode == "Addition")
final_value += this.iSize;
else if(application_mode == "Subtraction")
final_value = Math.max(this.iSize - final_value, 0);
this.size = final_value * factor + this.iSize * (1.0 - factor);
break;
}
}
}
/*
* Get the next texture frame of the particle
* @method getNextFrame
*/
getNextFrame() {
//If it isn't animated just return
if(!this.animated || this.c_frame < this.frameRate)
return;
this.c_frame = 0;
this.frameX++;
if(this.frameX == this.textures_x)
{
this.frameY--;
this.frameX = 0;
if(this.frameY < 0)
this.frameY = this.textures_y - 1;
}
//With the updated frame get the coordinates
this.getCoords();
}
/*
* See if a condition is already meted
* @method getCondition
* @params {Boolean} If the particle is recently filled
*/
getCoords(fill = false) {
//If is not filled and is not animated just skip this...
if(!fill)
if(!this.animated)
return;
//In case there are no texture return the default coordinates
if(this.texture_id == -1)
{
this.coords = DixieGlobals.default_coords;
return;
}
let minX, minY, maxX, maxY;
let uvs = this.uvs;
let sizeX = this.textures_x, sizeY = this.textures_y;
if(sizeX == 0 && sizeY == 0 || !this.subtextures)
{
//In case there aren't subtexture of the user define both sizes as 0
minX = uvs[0];
minY = uvs[1];
maxX = uvs[2];
maxY = uvs[3];
}
else if(this.animated)
{
let iSx = 1/sizeX;
let iSy = 1/sizeY;
let frameX = this.frameX;
let frameY = this.frameY;
minX = sizeX != 1 ? frameX * iSx : 0;
minY = sizeY != 1 ? frameY * iSy : 0;
maxX = sizeX != 1 ? (frameX+1) * iSx : 1;
maxY = sizeY != 1 ? (frameY+1) * iSy : 1;
//Interpolation (in order to get the correct frame)
minX = DixieGlobals.lerp(uvs[0], uvs[2], minX);
minY = DixieGlobals.lerp(uvs[1], uvs[3], minY);
maxX = DixieGlobals.lerp(uvs[0], uvs[2], maxX);
maxY = DixieGlobals.lerp(uvs[1], uvs[3], maxY);
}
else if(this.subtextures)
{
//Get the basic Uvs in the case that the texture isn't animated or have subtextures
minX = sizeX != 1 ? Math.floor(Math.random() * sizeX)/sizeX : 0;
minY = sizeY != 1 ? Math.floor(Math.random() * sizeY)/sizeY : 0;
maxX = sizeX != 1 ? minX + (1/sizeX) : 1;
maxY = sizeY != 1 ? minY + (1/sizeY) : 1;
//Interpolation (in order to get the correct frame)
minX = DixieGlobals.lerp(uvs[0], uvs[2], minX);
minY = DixieGlobals.lerp(uvs[1], uvs[3], minY);
maxX = DixieGlobals.lerp(uvs[0], uvs[2], maxX);
maxY = DixieGlobals.lerp(uvs[1], uvs[3], maxY);
}
this.coords = [maxX,maxY, minX,maxY, maxX,minY, minX,minY, maxX,minY, minX,maxY];
}
/*
* The update logic of the particle
* @method update
* @params {Number} The passed time (in ms) since the last frame
* @params {List} The list of particles to reset
* @params {List} The list of forces
* @params {List} The list of modifications
*/
update(dt_, to_reset_, forces_, modifications_) {
//First to all the lifetime and frame of the particle is updated
this.updateLifetime(dt_, to_reset_);
//If the particle is not visible not do anything
if(this.visibility == 0)
return;
//Get the next frame
this.getNextFrame();
this.move(dt_); //Apply the movement
this.applyForces(dt_, forces_); //Apply the forces
this.applyModifications(dt_, modifications_); //Apply the modifications
}
}
/*
* This class is for save information about the particle systems and control the emission and position of the particles
* @class DixieParticleSystem
*/
class DixieParticleSystem {
/*
* The constructor of the ParticleSystem
* @method constructor
* @params {Object} The data of the particle system
* @params {String} The directory where the system is saved (where are the folders atlas and meshes)
* @params {Function} The function for create the particle mesh
* @params {Function} The function to load the meshes
* @params {Function} The function to load the textures
*/
constructor(data_, directory_, create_pmesh_f_, load_mesh_f = undefined, load_texture_f = undefined) {
Object.assign(this, data_);
this.getTotalParticles();
this.id;
this.transformModal = DixieGlobals.identity.slice(0);
this.rotation = [0, 0, 0];
this.scale = [1, 1, 1];
if(this.origin == "Mesh")
{
let m = this.origin_mesh.modal;
this.position[0] = m[12];
this.position[1] = m[13];
this.position[2] = m[14];
}
//Original position
this.o_position = this.position.slice(0);
//The transformed position
this.trans_position = this.position.slice(0);
//Every how many frames the particles are ordered
this.update_frame = 5;
this.frames_until_update = 0;
//Create the list for the particles and his ids
this.particles = [];
this.particles_ids = [];
this.particles_to_reset = [];
//Create an all_ids this is just for ordening the particles
this.all_ids = [];
//Create the sub emission info
this.sub_emissions_ids = [];
for(let i = 0; i < this.sub_emittors.length; ++i)
{
this.sub_emittors[i].particles_ids = [];
this.sub_emittors[i].particles_to_reset = [];
}
//Time variables
this.time_pased = 0;
this.spawn_period = 1.0 / this.spawn_rate;
//Shader definition
this.vertex_shader = DixieGlobals.vs_particles;
//The only shader that will change is the fragment
if(this.atlasName != DixieGlobals.defaultAtlasName)
this.fragment_shader = DixieGlobals.fs_texture;
else
this.fragment_shader = DixieGlobals.fs_flat_p;
//Creation of the render info and the particle mesh
this.createRenderInfo(directory_, load_mesh_f, load_texture_f);
this.createParticleMesh(create_pmesh_f_);
}
/*
* Set the id for the particle system (for ordening porpuses)
* @method setId
* @params {Number} The id of the particle system
*/
setId(id_) {
this.id = id_;
}
/*
* Change the update rate of the particle system
* @method changeUpdateRate
* @params {Number} The new update rate
*/
changeUpdateRate(new_rate_) {
if(Dixie.validPosInteger(new_rate_))
this.update_frame = new_rate_;
else
console.error("Dixie Error!! \n\n\t The new update rate must be a positive integer!! \n\n");
}
/*
* Displace all the particle system
* @method displace
* @params {List} A list of three elements, representing the displacement
*/
displace(pos_) {
if(pos_ != undefined)
{
if(this.origin == "Mesh")
{
let modal = this.renderInfo.modal;
//Apply the translation
modal[12] += pos_[0];
modal[13] += pos_[1];
modal[14] += pos_[2];
for(let i = 0; i < 3; ++i)
this.trans_position[i] += pos_[i];
}
else if (this.origin == "Point")
{
for(let i = 0; i < 3; ++i)
{
this.trans_position[i] += pos_[i];
this.position[i] += pos_[i];
}
}
}
}
/*
* Change the position of the particle system
* @method setDisplacement
* @params {List} A list of three elements, representing the new position
*/
setDisplacement(pos_) {
if(pos_ != undefined)
{
if(this.origin == "Mesh")
{
let modal = this.renderInfo.modal;
//Apply the translation
modal[12] = pos_[0];
modal[13] = pos_[1];
modal[14] = pos_[2];
for(let i = 0; i < 3; ++i)
this.trans_position[i] = pos_[i];
}
else if(this.origin == "Point")
{
for(let i = 0; i < 3; ++i)
{
this.trans_position[i] = pos_[i];
this.position[i] = pos_[i];
}
}
}
}
/*
* Get the id an position of the particle system
* @method getIdPosition
*/
getIdPosition() {
return {id: this.id, position: this.trans_position};
}
/*
* Reset the position of the particle system
* @method resetDisplacement
*/
resetDisplacement() {
if(this.origin == "Mesh")
{
let renderInfo = this.renderInfo;
let modal = renderInfo.modal;
let o_modal = renderInfo.o_modal;
for(let i = 0; i < modal.length; ++i)
modal[i] = o_modal[i];
}
else if(this.origin == "Point")
{