-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
310 lines (225 loc) · 9.36 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="static/styles/main.css">
<title>SPH Three.JS</title>
</head>
<body>
<script type="x-shader/x-vertex" id="physicsVertexShader">
attribute vec2 reference;
attribute vec3 pos_offset;
uniform sampler2D texturePosition;
uniform sampler2D textureAcceleration;
uniform float scale;
#include <fog_pars_vertex>
varying vec4 vColor;
varying vec3 modelPos;
varying vec3 Normal;
varying vec3 Ka;
varying vec3 Kd;
void main(){
//vec4 tmpPos = scale * texture2D(texturePosition,reference);
vec4 tmpPos = texture2D(texturePosition,reference);
float rho = texture2D(textureAcceleration,reference).a;
float tmpX = scale * tmpPos.x;
float tmpY = scale * tmpPos.y;
float tmpZ = scale * tmpPos.z;
Ka = vec3(1.0,rho,0.0);
Kd = vec3(1.0,rho,0.0);
//gl_Position=projectionMatrix * modelViewMatrix * vec4(tmpX,tmpY,tmpZ,1.0);
//gl_PointSize= 3.0;
vec3 transformed = vec3(tmpX,tmpY,tmpZ);
vec4 mvPosition = vec4( position + transformed, 1.0 );
modelPos = vec3(modelViewMatrix * mvPosition);
Normal = normalize(normalMatrix * normal);
//mvPosition = instanceMatrix * mvPosition;
mvPosition = modelViewMatrix * mvPosition;
gl_Position = projectionMatrix * mvPosition;
#include <fog_vertex>
}
</script>
<script type="x-shader/x-fragment" id="pointFragmentShader">
varying vec3 Normal;
varying vec3 modelPos;
//uniform vec3 Ka;
//uniform vec3 Kd;
varying vec3 Ka;
varying vec3 Kd;
uniform vec3 Ks;
uniform vec4 LightPosition;
uniform vec3 LightIntensity;
uniform float Shininess;
#include <fog_pars_fragment>
varying vec4 vColor;
vec3 phong() {
vec3 n = normalize(Normal);
vec3 s = normalize(vec3(LightPosition) - modelPos);
vec3 v = normalize(vec3(-modelPos));
vec3 r = reflect(-s, n);
vec3 ambient = Ka;
vec3 diffuse = Kd * max(dot(s, n), 0.0);
vec3 specular = Ks * pow(max(dot(r, v), 0.0), Shininess);
return LightIntensity * (ambient + diffuse + specular);
}
void main(){
//gl_FragColor=vColor;
gl_FragColor = vec4(phong(), 1.0);
#include <fog_fragment>
}
</script>
<script type="x-shader/x-fragment" id="fragmentShaderAcceleration">
uniform float equationOfStateConst;
uniform float polytropicIndex;
uniform float smoothingLength;
uniform float particleMass;
uniform float lambda;
uniform float viscosity;
const float PI = 3.141592653589793;
const float width = resolution.x;
const float height = resolution.y;
vec3 evaluateKernelDerivative(float xDist, float yDist, float zDist){
float r = sqrt(xDist*xDist + yDist*yDist + zDist*zDist);
float n = -2.0 * exp(-(r * r) / (smoothingLength * smoothingLength));
n /= pow(smoothingLength,5.0);
n /= pow(PI, (3.0/2.0) );
return vec3(n * xDist, n * yDist, n * zDist);
}
float evaluatePressure(float rho){
return equationOfStateConst * pow(rho, 1.0 + (1.0 / polytropicIndex) );
}
void main(){
vec2 uv = gl_FragCoord.xy / resolution.xy;
vec4 tmpPos = texture2D( texturePosition, uv);
vec4 tmpVel = texture2D( textureVelocity, uv);
vec4 tmpRho = texture2D( textureDensity, uv);
float rho = tmpRho.x;
float pressure = evaluatePressure(rho);
float selfXPos = tmpPos.x;
float selfYPos = tmpPos.y;
float selfZPos = tmpPos.z;
vec4 otherPosition;
vec3 gradient;
float otherPressure;
float ax=0.0;
float ay=0.0;
float az=0.0;
float gradientZTest = 0.0;
float rhoMax = rho;
float rhoMin = rho;
for (float y = 0.0; y < height; y++){
for (float x = 0.0; x < width; x++){
vec2 ref = vec2( x + 0.5, y + 0.5 ) / resolution.xy;
otherPosition = texture2D(texturePosition, ref);
float otherRho = texture2D(textureDensity, ref).x;
float otherPressure = evaluatePressure(otherRho);
if (otherRho > rhoMax){
rhoMax = otherRho;
}
if (otherRho < rhoMin){
rhoMin = otherRho;
}
float xDist = selfXPos - otherPosition.x;
float yDist = selfYPos - otherPosition.y;
float zDist = selfZPos - otherPosition.z;
gradient = evaluateKernelDerivative(xDist,yDist,zDist);
float pressureAcc = (pressure/(rho*rho)) + (otherPressure/(otherRho*otherRho));
ax += particleMass * pressureAcc * gradient.x;
ay += particleMass * pressureAcc * gradient.y;
az += particleMass * pressureAcc * gradient.z;
gradientZTest += gradient.z;
}
}
ax = -ax - lambda * selfXPos - viscosity * tmpVel.x;
ay = -ay - lambda * selfYPos - viscosity * tmpVel.y;
az = -az - lambda * selfZPos - viscosity * tmpVel.z;
float colorRho = min(15.0, rho);
//float colorRho = rho;
rhoMax = min(15.0, rhoMax);
colorRho = (colorRho - rhoMin) / (rhoMax - rhoMin);
gl_FragColor = vec4(ax,ay,az,colorRho);
}
</script>
<script type="x-shader/x-fragment" id="fragmentShaderVelocity">
uniform float deltaT;
void main(){
vec2 uv = gl_FragCoord.xy / resolution.xy;
vec4 tmpAcc = texture2D( textureAcceleration, uv );
vec4 tmpVel = texture2D( textureVelocity, uv);
float xVel = tmpVel.x + tmpAcc.x * 0.5 * deltaT;
float yVel = tmpVel.y + tmpAcc.y * 0.5 * deltaT;
float zVel = tmpVel.z + tmpAcc.z * 0.5 * deltaT;
gl_FragColor = vec4(xVel,yVel,zVel,1.0);
}
</script>
<script type="x-shader/x-fragment" id="fragmentShaderPosition">
uniform float deltaT;
//uniform float smoothingLength;
//uniform float particleMass;
//const float PI = 3.141592653589793;
/*float evaluateKernel(float xDist, float yDist, float zDist){
float r = sqrt(xDist*xDist + yDist*yDist + zDist*zDist);
float w = (1.0 / (smoothingLength * sqrt(PI)));
w = w * w * w * exp( -(r*r) / (smoothingLength*smoothingLength));
return w;
}*/
void main(){
vec2 uv = gl_FragCoord.xy / resolution.xy;
vec4 tmpVel = texture2D( textureVelocity, uv);
vec4 tmpPos = texture2D( texturePosition, uv);
float selfXPos = tmpPos.x + tmpVel.x * deltaT;
float selfYPos = tmpPos.y + tmpVel.y * deltaT;
float selfZPos = tmpPos.z + tmpVel.z * deltaT;
gl_FragColor = vec4(selfXPos,selfYPos,selfZPos,0.0);
}
</script>
<script type="x-shader/x-fragment" id="fragmentShaderDensity">
uniform float smoothingLength;
uniform float particleMass;
const float PI = 3.141592653589793;
const float width = resolution.x;
const float height = resolution.y;
float evaluateKernel(float xDist, float yDist, float zDist){
float r = sqrt(xDist*xDist + yDist*yDist + zDist*zDist);
float w = (1.0 / (smoothingLength * sqrt(PI)));
w = w * w * w * exp( -(r*r) / (smoothingLength*smoothingLength));
return w;
}
void main(){
vec2 uv = gl_FragCoord.xy / resolution.xy;
vec4 selfPos = texture2D( texturePosition, uv);
vec3 otherPosition;
float rho = 0.0;
for (float y = 0.0; y < height; y++){
for (float x = 0.0; x < width; x++){
vec2 ref = vec2( x + 0.5, y + 0.5 ) / resolution.xy;
otherPosition = texture2D(texturePosition, ref).xyz;
float xDist = selfPos.x - otherPosition.x;
float yDist = selfPos.y - otherPosition.y;
float zDist = selfPos.z - otherPosition.z;
rho += particleMass * evaluateKernel(xDist, yDist, zDist);
}
}
gl_FragColor = vec4(rho,0.0,0.0,0.0);
}
</script>
<!-- Information modal -->
<a id="open-modal-btn" class="float">
<i class="icon-float material-icons">menu</i>
</a>
<div id="info-modal" class="modal">
<div class="modal-content">
<span class="close-modal-btn">×</span>
<h3>Smoothed particle hydrodynamics example with Three.js</h3>
<p>Three.js/GPU implementation of the <a href="https://philip-mocz.medium.com/create-your-own-smoothed-particle-hydrodynamics-simulation-with-python-76e1cec505f1">tutorial</a> by Philip Mocz.
GLSL GPU-based particle computation, rendered in 3D as InstancedMesh with Three.js. 4096 samples. GPUComputationRenderer use
demonstrates individual texture computation in sequence.</p>
<p>Built and deployed rapidly with <a href="https://github.com/aakatev/three-js-webpack">three-js-webpack</a></p>
<p><a href="https://github.com/myociss/threejs-star-sph">Code</a></p>
</div>
</div>
<!-- End information modal -->
<script src="public/bundle.js"></script>
</body>
</html>