forked from prideout/fluidsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fluid.glsl
262 lines (203 loc) · 7.2 KB
/
Fluid.glsl
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
-- Vertex
in vec4 Position;
out int vInstance;
void main()
{
gl_Position = Position;
vInstance = gl_InstanceID;
}
-- Fill
out vec3 FragColor;
void main()
{
FragColor = vec3(1, 0, 0);
}
-- PickLayer
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
in int vInstance[3];
out float gLayer;
void main()
{
gl_Layer = vInstance[0];
gLayer = float(gl_Layer) + 0.5;
gl_Position = gl_in[0].gl_Position;
EmitVertex();
gl_Position = gl_in[1].gl_Position;
EmitVertex();
gl_Position = gl_in[2].gl_Position;
EmitVertex();
EndPrimitive();
}
-- Advect
out vec4 FragColor;
uniform sampler3D VelocityTexture;
uniform sampler3D SourceTexture;
uniform sampler3D Obstacles;
uniform vec3 InverseSize;
uniform float TimeStep;
uniform float Dissipation;
in float gLayer;
void main()
{
vec3 fragCoord = vec3(gl_FragCoord.xy, gLayer);
float solid = texture(Obstacles, InverseSize * fragCoord).x;
if (solid > 0) {
FragColor = vec4(0);
return;
}
vec3 u = texture(VelocityTexture, InverseSize * fragCoord).xyz;
vec3 coord = InverseSize * (fragCoord - TimeStep * u);
FragColor = Dissipation * texture(SourceTexture, coord);
}
-- Jacobi
out vec4 FragColor;
uniform sampler3D Pressure;
uniform sampler3D Divergence;
uniform sampler3D Obstacles;
uniform float Alpha;
uniform float InverseBeta;
in float gLayer;
void main()
{
ivec3 T = ivec3(gl_FragCoord.xy, gLayer);
// Find neighboring pressure:
vec4 pN = texelFetchOffset(Pressure, T, 0, ivec3(0, 1, 0));
vec4 pS = texelFetchOffset(Pressure, T, 0, ivec3(0, -1, 0));
vec4 pE = texelFetchOffset(Pressure, T, 0, ivec3(1, 0, 0));
vec4 pW = texelFetchOffset(Pressure, T, 0, ivec3(-1, 0, 0));
vec4 pU = texelFetchOffset(Pressure, T, 0, ivec3(0, 0, 1));
vec4 pD = texelFetchOffset(Pressure, T, 0, ivec3(0, 0, -1));
vec4 pC = texelFetch(Pressure, T, 0);
// Find neighboring obstacles:
vec3 oN = texelFetchOffset(Obstacles, T, 0, ivec3(0, 1, 0)).xyz;
vec3 oS = texelFetchOffset(Obstacles, T, 0, ivec3(0, -1, 0)).xyz;
vec3 oE = texelFetchOffset(Obstacles, T, 0, ivec3(1, 0, 0)).xyz;
vec3 oW = texelFetchOffset(Obstacles, T, 0, ivec3(-1, 0, 0)).xyz;
vec3 oU = texelFetchOffset(Obstacles, T, 0, ivec3(0, 0, 1)).xyz;
vec3 oD = texelFetchOffset(Obstacles, T, 0, ivec3(0, 0, -1)).xyz;
// Use center pressure for solid cells:
if (oN.x > 0) pN = pC;
if (oS.x > 0) pS = pC;
if (oE.x > 0) pE = pC;
if (oW.x > 0) pW = pC;
if (oU.x > 0) pU = pC;
if (oD.x > 0) pD = pC;
vec4 bC = texelFetch(Divergence, T, 0);
FragColor = (pW + pE + pS + pN + pU + pD + Alpha * bC) * InverseBeta;
}
-- SubtractGradient
out vec3 FragColor;
uniform sampler3D Velocity;
uniform sampler3D Pressure;
uniform sampler3D Obstacles;
uniform float GradientScale;
in float gLayer;
void main()
{
ivec3 T = ivec3(gl_FragCoord.xy, gLayer);
vec3 oC = texelFetch(Obstacles, T, 0).xyz;
if (oC.x > 0) {
FragColor = oC.yzx;
return;
}
// Find neighboring pressure:
float pN = texelFetchOffset(Pressure, T, 0, ivec3(0, 1, 0)).r;
float pS = texelFetchOffset(Pressure, T, 0, ivec3(0, -1, 0)).r;
float pE = texelFetchOffset(Pressure, T, 0, ivec3(1, 0, 0)).r;
float pW = texelFetchOffset(Pressure, T, 0, ivec3(-1, 0, 0)).r;
float pU = texelFetchOffset(Pressure, T, 0, ivec3(0, 0, 1)).r;
float pD = texelFetchOffset(Pressure, T, 0, ivec3(0, 0, -1)).r;
float pC = texelFetch(Pressure, T, 0).r;
// Find neighboring obstacles:
vec3 oN = texelFetchOffset(Obstacles, T, 0, ivec3(0, 1, 0)).xyz;
vec3 oS = texelFetchOffset(Obstacles, T, 0, ivec3(0, -1, 0)).xyz;
vec3 oE = texelFetchOffset(Obstacles, T, 0, ivec3(1, 0, 0)).xyz;
vec3 oW = texelFetchOffset(Obstacles, T, 0, ivec3(-1, 0, 0)).xyz;
vec3 oU = texelFetchOffset(Obstacles, T, 0, ivec3(0, 0, 1)).xyz;
vec3 oD = texelFetchOffset(Obstacles, T, 0, ivec3(0, 0, -1)).xyz;
// Use center pressure for solid cells:
vec3 obstV = vec3(0);
vec3 vMask = vec3(1);
if (oN.x > 0) { pN = pC; obstV.y = oN.z; vMask.y = 0; }
if (oS.x > 0) { pS = pC; obstV.y = oS.z; vMask.y = 0; }
if (oE.x > 0) { pE = pC; obstV.x = oE.y; vMask.x = 0; }
if (oW.x > 0) { pW = pC; obstV.x = oW.y; vMask.x = 0; }
if (oU.x > 0) { pU = pC; obstV.z = oU.x; vMask.z = 0; }
if (oD.x > 0) { pD = pC; obstV.z = oD.x; vMask.z = 0; }
// Enforce the free-slip boundary condition:
vec3 oldV = texelFetch(Velocity, T, 0).xyz;
vec3 grad = vec3(pE - pW, pN - pS, pU - pD) * GradientScale;
vec3 newV = oldV - grad;
FragColor = (vMask * newV) + obstV;
}
-- ComputeDivergence
out float FragColor;
uniform sampler3D Velocity;
uniform sampler3D Obstacles;
uniform float HalfInverseCellSize;
in float gLayer;
void main()
{
ivec3 T = ivec3(gl_FragCoord.xy, gLayer);
// Find neighboring velocities:
vec3 vN = texelFetchOffset(Velocity, T, 0, ivec3(0, 1, 0)).xyz;
vec3 vS = texelFetchOffset(Velocity, T, 0, ivec3(0, -1, 0)).xyz;
vec3 vE = texelFetchOffset(Velocity, T, 0, ivec3(1, 0, 0)).xyz;
vec3 vW = texelFetchOffset(Velocity, T, 0, ivec3(-1, 0, 0)).xyz;
vec3 vU = texelFetchOffset(Velocity, T, 0, ivec3(0, 0, 1)).xyz;
vec3 vD = texelFetchOffset(Velocity, T, 0, ivec3(0, 0, -1)).xyz;
// Find neighboring obstacles:
vec3 oN = texelFetchOffset(Obstacles, T, 0, ivec3(0, 1, 0)).xyz;
vec3 oS = texelFetchOffset(Obstacles, T, 0, ivec3(0, -1, 0)).xyz;
vec3 oE = texelFetchOffset(Obstacles, T, 0, ivec3(1, 0, 0)).xyz;
vec3 oW = texelFetchOffset(Obstacles, T, 0, ivec3(-1, 0, 0)).xyz;
vec3 oU = texelFetchOffset(Obstacles, T, 0, ivec3(0, 0, 1)).xyz;
vec3 oD = texelFetchOffset(Obstacles, T, 0, ivec3(0, 0, -1)).xyz;
// Use obstacle velocities for solid cells:
if (oN.x > 0) vN = oN.yzx;
if (oS.x > 0) vS = oS.yzx;
if (oE.x > 0) vE = oE.yzx;
if (oW.x > 0) vW = oW.yzx;
if (oU.x > 0) vU = oU.yzx;
if (oD.x > 0) vD = oD.yzx;
FragColor = HalfInverseCellSize * (vE.x - vW.x + vN.y - vS.y + vU.z - vD.z);
}
-- Splat
out vec4 FragColor;
uniform vec3 Point;
uniform float Radius;
uniform vec3 FillColor;
in float gLayer;
void main()
{
float d = distance(Point, vec3(gl_FragCoord.xy, gLayer));
if (d < Radius) {
float a = (Radius - d) * 0.5;
a = min(a, 1.0);
FragColor = vec4(FillColor, a);
} else {
FragColor = vec4(0);
}
}
-- Buoyancy
out vec3 FragColor;
uniform sampler3D Velocity;
uniform sampler3D Temperature;
uniform sampler3D Density;
uniform float AmbientTemperature;
uniform float TimeStep;
uniform float Sigma;
uniform float Kappa;
in float gLayer;
void main()
{
ivec3 TC = ivec3(gl_FragCoord.xy, gLayer);
float T = texelFetch(Temperature, TC, 0).r;
vec3 V = texelFetch(Velocity, TC, 0).xyz;
FragColor = V;
if (T > AmbientTemperature) {
float D = texelFetch(Density, TC, 0).x;
FragColor += (TimeStep * (T - AmbientTemperature) * Sigma - D * Kappa ) * vec3(0, -1, 0);
}
}