/
ParticleModules.js
157 lines (130 loc) · 4.45 KB
/
ParticleModules.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
import { structElementChunk } from "./ParticleSystem.js";
import { FeedbackUseMode } from "./TransformFeedback.js";
var PM = {};
//##################################################################
// Position
class ModPosition{
static run(psys, params, fbData){
if(params == null){ console.log("Position Module needs params"); return false; }
if(params.placement == undefined){ console.log("Position Module needs params.placement"); return false; }
psys.addFeedbackWrite(ModPosition.varName,"vec3", ModPosition.mainCodeFeedback, ModPosition.mainCodeDraw);
fbData.add("Position",
ModPosition.placement[ params.placement ]( psys, params, fbData ),
structElementChunk(ModPosition.varName, 3, psys._locFeedback, psys._locDraw, FeedbackUseMode.WriteDraw)
);
return true;
}
}
PM.Position = ModPosition;
ModPosition.varName = "particlePos";
ModPosition.mainCodeFeedback = "v_particlePos = a_particlePos;";
ModPosition.mainCodeDraw = "localPos += a_particlePos;";
ModPosition.placement = {
//...........................................
circleSegment : function(psys, params){
var aLen = psys.particleCount * 3, //Three floats per particle (x,y,z)
ary = new Array(aLen),
radInc = Math.PI * 2 / psys.particleCount,
radius = params.radius || 1;
var ii,rad;
for(var i=0; i < psys.particleCount; i++){
rad = i * radInc;
ii = i * 3;
ary[ii] = radius * Math.cos(rad);
ary[ii+1] = 0;
ary[ii+2] = radius * Math.sin(rad);
}
return ary;
},
//...........................................
test : function(psys, params){
var aLen = psys.particleCount * 3, //Three floats per particle (x,y,z)
ary = new Array(aLen),
v = 1 / 3 * 0.4;
//For test data just move the vertices on the x axis
for(var i=0; i < aLen; i += 3){
ary[i] = i * v;
ary[i+1] = ary[i+2] = 0;
}
return ary;
}
}
//endregion
//##################################################################
// Velocity
class ModVelocity{
static run(psys, params, fbData, fbInfo){
psys.addFeedbackRead(ModVelocity.varName,"vec3",ModVelocity.mainCodeFeedback,ModVelocity.mainCodeDraw);
fbData.add("Velocity",
ModVelocity.placement[ params.placement ]( psys, params, fbData ),
structElementChunk(ModVelocity.varName, 3, psys._locFeedback, -1, FeedbackUseMode.Read)
);
return true;
}
}
PM.Velocity = ModVelocity;
ModVelocity.varName = "particleVelocity";
ModVelocity.mainCodeFeedback = "v_particlePos += a_particleVelocity;";
ModVelocity.placement = {
//...........................................
test : function(psys, params){
var aLen = psys.particleCount * 3, //Three floats per particle (x,y,z)
ary = new Array(aLen),
v = 1 / 3 * (params.speed || 0.001);
//For test data just move the vertices on the x axis
for(var i=0; i < aLen; i += 3){
ary[i+1] = (i+3) * v;
ary[i] = ary[i+2] = 0;
}
return ary;
},
//...........................................
normalizePos : function(psys, params, fbData){
var aLen = psys.particleCount * 3, //Three floats per particle (x,y,z)
ary = new Array(aLen),
pos = fbData.getData("Position"),
t = 0,
speed = params.speed || 0.01;
for(var i=0; i < aLen; i += 3){
t = 1 / ( pos[i] * pos[i] + pos[i+1] * pos[i+1] + pos[i+2] * pos[i+2] ) * speed;
ary[i] = pos[i] * t;
ary[i+1] = pos[i+1] * t;
ary[i+2] = pos[i+2] * t;
}
return ary;
},
}
//endregion
//##################################################################
// Colors
class ModColor{
static run(psys, params, fbData, fbInfo){
psys.addDraw(ModColor.varName, "vec3", null, ModColor.mainCodeDrawFrag, true);
fbData.add("Color",
ModColor.placement[ params.placement ]( psys, params, fbData ),
structElementChunk(ModColor.varName, 3, -1, psys._locDraw, FeedbackUseMode.Draw)
);
return true;
}
}
PM.Color = ModColor;
ModColor.varName = "particleColor";
ModColor.mainCodeDrawFrag = "FragColor = vec4(v_particleColor,1.0);";
ModColor.placement = {
//...........................................
test : function(psys, params){
var aLen = psys.particleCount * 3, //Three floats per particle (x,y,z)
ary = new Array(aLen);
//For test data just move the vertices on the x axis
var ii,j;
for(var i=0; i < psys.particleCount; i++){
ii = i * 3;
j = i % 3;
ary[ii] = (j == 0)? 1:0;
ary[ii+1] = (j == 1)? 1:0;
ary[ii+2] = (j == 2)? 1:0;
}
return ary;
},
}
export default PM;