-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathsimulator.js
591 lines (407 loc) · 27.6 KB
/
simulator.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
'use strict'
var Simulator = (function () {
//simulation grid dimensions and resolution
//all particles are in the world position space ([0, 0, 0], [GRID_WIDTH, GRID_HEIGHT, GRID_DEPTH])
//when doing most grid operations, we transform positions from world position space into the grid position space ([0, 0, 0], [GRID_RESOLUTION_X, GRID_RESOLUTION_Y, GRID_RESOLUTION_Z])
//in grid space, cell boundaries are simply at integer values
//we emulate 3D textures with tiled 2d textures
//so the z slices of a 3d texture are laid out along the x axis
//the 2d dimensions of a 3d texture are therefore [width * depth, height]
/*
we use a staggered MAC grid
this means the velocity grid width = grid width + 1 and velocity grid height = grid height + 1 and velocity grid depth = grid depth + 1
a scalar for cell [i, j, k] is positionally located at [i + 0.5, j + 0.5, k + 0.5]
x velocity for cell [i, j, k] is positionally located at [i, j + 0.5, k + 0.5]
y velocity for cell [i, j, k] is positionally located at [i + 0.5, j, k + 0.5]
z velocity for cell [i, j, k] is positionally located at [i + 0.5, j + 0.5, k]
*/
//the boundaries are the boundaries of the grid
//a grid cell can either be fluid, air (these are tracked by markTexture) or is a wall (implicit by position)
function Simulator (wgl, onLoaded) {
this.wgl = wgl;
this.particlesWidth = 0;
this.particlesHeight = 0;
this.gridWidth = 0;
this.gridHeight = 0;
this.gridDepth = 0;
this.gridResolutionX = 0;
this.gridResolutionY = 0;
this.gridResolutionZ = 0;
this.particleDensity = 0;
this.velocityTextureWidth = 0;
this.velocityTextureHeight = 0;
this.scalarTextureWidth = 0;
this.scalarTextureHeight = 0;
this.halfFloatExt = this.wgl.getExtension('OES_texture_half_float');
this.wgl.getExtension('OES_texture_half_float_linear');
this.simulationNumberType = this.halfFloatExt.HALF_FLOAT_OES;
///////////////////////////////////////////////////////
// simulation parameters
this.flipness = 0.99; //0 is full PIC, 1 is full FLIP
this.frameNumber = 0; //used for motion randomness
/////////////////////////////////////////////////
// simulation objects (most are filled in by reset)
this.quadVertexBuffer = wgl.createBuffer();
wgl.bufferData(this.quadVertexBuffer, wgl.ARRAY_BUFFER, new Float32Array([-1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0]), wgl.STATIC_DRAW);
this.simulationFramebuffer = wgl.createFramebuffer();
this.particleVertexBuffer = wgl.createBuffer();
this.particlePositionTexture = wgl.createTexture();
this.particlePositionTextureTemp = wgl.createTexture();
this.particleVelocityTexture = wgl.createTexture();
this.particleVelocityTextureTemp = wgl.createTexture();
this.particleRandomTexture = wgl.createTexture(); //contains a random normalized direction for each particle
////////////////////////////////////////////////////
// create simulation textures
this.velocityTexture = wgl.createTexture();
this.tempVelocityTexture = wgl.createTexture();
this.originalVelocityTexture = wgl.createTexture();
this.weightTexture = wgl.createTexture();
this.markerTexture = wgl.createTexture(); //marks fluid/air, 1 if fluid, 0 if air
this.divergenceTexture = wgl.createTexture();
this.pressureTexture = wgl.createTexture();
this.tempSimulationTexture = wgl.createTexture();
/////////////////////////////
// load programs
wgl.createProgramsFromFiles({
transferToGridProgram: {
vertexShader: 'shaders/transfertogrid.vert',
fragmentShader: ['shaders/common.frag', 'shaders/transfertogrid.frag'],
attributeLocations: { 'a_textureCoordinates': 0}
},
normalizeGridProgram: {
vertexShader: 'shaders/fullscreen.vert',
fragmentShader: 'shaders/normalizegrid.frag',
attributeLocations: { 'a_position': 0}
},
markProgram: {
vertexShader: 'shaders/mark.vert',
fragmentShader: 'shaders/mark.frag',
attributeLocations: { 'a_textureCoordinates': 0}
},
addForceProgram: {
vertexShader: 'shaders/fullscreen.vert',
fragmentShader: ['shaders/common.frag', 'shaders/addforce.frag'],
attributeLocations: { 'a_position': 0}
},
enforceBoundariesProgram: {
vertexShader: 'shaders/fullscreen.vert',
fragmentShader: ['shaders/common.frag', 'shaders/enforceboundaries.frag'],
attributeLocations: { 'a_textureCoordinates': 0 }
},
extendVelocityProgram: {
vertexShader: 'shaders/fullscreen.vert',
fragmentShader: 'shaders/extendvelocity.frag',
attributeLocations: { 'a_textureCoordinates': 0 }
},
transferToParticlesProgram: {
vertexShader: 'shaders/fullscreen.vert',
fragmentShader: ['shaders/common.frag', 'shaders/transfertoparticles.frag'],
attributeLocations: { 'a_position': 0}
},
divergenceProgram: {
vertexShader: 'shaders/fullscreen.vert',
fragmentShader: ['shaders/common.frag', 'shaders/divergence.frag'],
attributeLocations: { 'a_position': 0}
},
jacobiProgram: {
vertexShader: 'shaders/fullscreen.vert',
fragmentShader: ['shaders/common.frag', 'shaders/jacobi.frag'],
attributeLocations: { 'a_position': 0}
},
subtractProgram: {
vertexShader: 'shaders/fullscreen.vert',
fragmentShader: ['shaders/common.frag', 'shaders/subtract.frag'],
attributeLocations: { 'a_position': 0}
},
advectProgram: {
vertexShader: 'shaders/fullscreen.vert',
fragmentShader: ['shaders/common.frag', 'shaders/advect.frag'],
attributeLocations: { 'a_position': 0}
},
copyProgram: {
vertexShader: 'shaders/fullscreen.vert',
fragmentShader: 'shaders/copy.frag',
attributeLocations: { 'a_position': 0}
}
}, (function (programs) {
for (var programName in programs) {
this[programName] = programs[programName];
}
onLoaded();
}).bind(this));
}
//expects an array of [x, y, z] particle positions
//gridSize and gridResolution are both [x, y, z]
//particleDensity is particles per simulation grid cell
Simulator.prototype.reset = function (particlesWidth, particlesHeight, particlePositions, gridSize, gridResolution, particleDensity) {
this.particlesWidth = particlesWidth;
this.particlesHeight = particlesHeight;
this.gridWidth = gridSize[0];
this.gridHeight = gridSize[1];
this.gridDepth = gridSize[2];
this.gridResolutionX = gridResolution[0];
this.gridResolutionY = gridResolution[1];
this.gridResolutionZ = gridResolution[2];
this.particleDensity = particleDensity;
this.velocityTextureWidth = (this.gridResolutionX + 1) * (this.gridResolutionZ + 1);
this.velocityTextureHeight = (this.gridResolutionY + 1);
this.scalarTextureWidth = this.gridResolutionX * this.gridResolutionZ;
this.scalarTextureHeight = this.gridResolutionY;
///////////////////////////////////////////////////////////
// create particle data
var particleCount = this.particlesWidth * this.particlesHeight;
//fill particle vertex buffer containing the relevant texture coordinates
var particleTextureCoordinates = new Float32Array(this.particlesWidth * this.particlesHeight * 2);
for (var y = 0; y < this.particlesHeight; ++y) {
for (var x = 0; x < this.particlesWidth; ++x) {
particleTextureCoordinates[(y * this.particlesWidth + x) * 2] = (x + 0.5) / this.particlesWidth;
particleTextureCoordinates[(y * this.particlesWidth + x) * 2 + 1] = (y + 0.5) / this.particlesHeight;
}
}
wgl.bufferData(this.particleVertexBuffer, wgl.ARRAY_BUFFER, particleTextureCoordinates, wgl.STATIC_DRAW);
//generate initial particle positions amd create particle position texture for them
var particlePositionsData = new Float32Array(this.particlesWidth * this.particlesHeight * 4);
var particleRandoms = new Float32Array(this.particlesWidth * this.particlesHeight * 4);
for (var i = 0; i < this.particlesWidth * this.particlesHeight; ++i) {
particlePositionsData[i * 4] = particlePositions[i][0];
particlePositionsData[i * 4 + 1] = particlePositions[i][1];
particlePositionsData[i * 4 + 2] = particlePositions[i][2];
particlePositionsData[i * 4 + 3] = 0.0;
var theta = Math.random() * 2.0 * Math.PI;
var u = Math.random() * 2.0 - 1.0;
particleRandoms[i * 4] = Math.sqrt(1.0 - u * u) * Math.cos(theta);
particleRandoms[i * 4 + 1] = Math.sqrt(1.0 - u * u) * Math.sin(theta);
particleRandoms[i * 4 + 2] = u;
particleRandoms[i * 4 + 3] = 0.0;
}
wgl.rebuildTexture(this.particlePositionTexture, wgl.RGBA, wgl.FLOAT, this.particlesWidth, this.particlesHeight, particlePositionsData, wgl.CLAMP_TO_EDGE, wgl.CLAMP_TO_EDGE, wgl.NEAREST, wgl.NEAREST);
wgl.rebuildTexture(this.particlePositionTextureTemp, wgl.RGBA, wgl.FLOAT, this.particlesWidth, this.particlesHeight, null, wgl.CLAMP_TO_EDGE, wgl.CLAMP_TO_EDGE, wgl.NEAREST, wgl.NEAREST);
wgl.rebuildTexture(this.particleVelocityTexture, wgl.RGBA, this.simulationNumberType, this.particlesWidth, this.particlesHeight, null, wgl.CLAMP_TO_EDGE, wgl.CLAMP_TO_EDGE, wgl.NEAREST, wgl.NEAREST);
wgl.rebuildTexture(this.particleVelocityTextureTemp, wgl.RGBA, this.simulationNumberType, this.particlesWidth, this.particlesHeight, null, wgl.CLAMP_TO_EDGE, wgl.CLAMP_TO_EDGE, wgl.NEAREST, wgl.NEAREST);
wgl.rebuildTexture(this.particleRandomTexture, wgl.RGBA, wgl.FLOAT, this.particlesWidth, this.particlesHeight, particleRandoms, wgl.CLAMP_TO_EDGE, wgl.CLAMP_TO_EDGE, wgl.NEAREST, wgl.NEAREST); //contains a random normalized direction for each particle
////////////////////////////////////////////////////
// create simulation textures
wgl.rebuildTexture(this.velocityTexture, wgl.RGBA, this.simulationNumberType, this.velocityTextureWidth, this.velocityTextureHeight, null, wgl.CLAMP_TO_EDGE, wgl.CLAMP_TO_EDGE, wgl.LINEAR, wgl.LINEAR);
wgl.rebuildTexture(this.tempVelocityTexture, wgl.RGBA, this.simulationNumberType, this.velocityTextureWidth, this.velocityTextureHeight, null, wgl.CLAMP_TO_EDGE, wgl.CLAMP_TO_EDGE, wgl.LINEAR, wgl.LINEAR);
wgl.rebuildTexture(this.originalVelocityTexture, wgl.RGBA, this.simulationNumberType, this.velocityTextureWidth, this.velocityTextureHeight, null, wgl.CLAMP_TO_EDGE, wgl.CLAMP_TO_EDGE, wgl.LINEAR, wgl.LINEAR);
wgl.rebuildTexture(this.weightTexture, wgl.RGBA, this.simulationNumberType, this.velocityTextureWidth, this.velocityTextureHeight, null, wgl.CLAMP_TO_EDGE, wgl.CLAMP_TO_EDGE, wgl.LINEAR, wgl.LINEAR);
wgl.rebuildTexture(this.markerTexture, wgl.RGBA, wgl.UNSIGNED_BYTE, this.scalarTextureWidth, this.scalarTextureHeight, null, wgl.CLAMP_TO_EDGE, wgl.CLAMP_TO_EDGE, wgl.LINEAR, wgl.LINEAR); //marks fluid/air, 1 if fluid, 0 if air
wgl.rebuildTexture(this.divergenceTexture, wgl.RGBA, this.simulationNumberType, this.scalarTextureWidth, this.scalarTextureHeight, null, wgl.CLAMP_TO_EDGE, wgl.CLAMP_TO_EDGE, wgl.LINEAR, wgl.LINEAR);
wgl.rebuildTexture(this.pressureTexture, wgl.RGBA, this.simulationNumberType, this.scalarTextureWidth, this.scalarTextureHeight, null, wgl.CLAMP_TO_EDGE, wgl.CLAMP_TO_EDGE, wgl.LINEAR, wgl.LINEAR);
wgl.rebuildTexture(this.tempSimulationTexture, wgl.RGBA, this.simulationNumberType, this.scalarTextureWidth, this.scalarTextureHeight, null, wgl.CLAMP_TO_EDGE, wgl.CLAMP_TO_EDGE, wgl.LINEAR, wgl.LINEAR);
}
function swap (object, a, b) {
var temp = object[a];
object[a] = object[b];
object[b] = temp;
}
//you need to call reset() with correct parameters before simulating
//mouseVelocity, mouseRayOrigin, mouseRayDirection are all expected to be arrays of 3 values
Simulator.prototype.simulate = function (timeStep, mouseVelocity, mouseRayOrigin, mouseRayDirection) {
if (timeStep === 0.0) return;
this.frameNumber += 1;
var wgl = this.wgl;
/*
the simulation process
transfer particle velocities to velocity grid
save this velocity grid
solve velocity grid for non divergence
update particle velocities with new velocity grid
advect particles through the grid velocity field
*/
//////////////////////////////////////////////////////
//transfer particle velocities to grid
//we transfer particle velocities to the grid in two steps
//in the first step, we accumulate weight * velocity into tempVelocityTexture and then weight into weightTexture
//in the second step: velocityTexture = tempVelocityTexture / weightTexture
//we accumulate into velocityWeightTexture and then divide into velocityTexture
var transferToGridDrawState = wgl.createDrawState()
.bindFramebuffer(this.simulationFramebuffer)
.viewport(0, 0, this.velocityTextureWidth, this.velocityTextureHeight)
.vertexAttribPointer(this.particleVertexBuffer, 0, 2, wgl.FLOAT, wgl.FALSE, 0, 0)
.useProgram(this.transferToGridProgram)
.uniform3f('u_gridResolution', this.gridResolutionX, this.gridResolutionY, this.gridResolutionZ)
.uniform3f('u_gridSize', this.gridWidth, this.gridHeight, this.gridDepth)
.uniformTexture('u_positionTexture', 0, wgl.TEXTURE_2D, this.particlePositionTexture)
.uniformTexture('u_velocityTexture', 1, wgl.TEXTURE_2D, this.particleVelocityTexture)
.enable(wgl.BLEND)
.blendEquation(wgl.FUNC_ADD)
.blendFuncSeparate(wgl.ONE, wgl.ONE, wgl.ONE, wgl.ONE);
//accumulate weight
wgl.framebufferTexture2D(this.simulationFramebuffer, wgl.FRAMEBUFFER, wgl.COLOR_ATTACHMENT0, wgl.TEXTURE_2D, this.weightTexture, 0);
wgl.clear(
wgl.createClearState().bindFramebuffer(this.simulationFramebuffer).clearColor(0, 0, 0, 0),
wgl.COLOR_BUFFER_BIT);
transferToGridDrawState.uniform1i('u_accumulate', 0)
//each particle gets splatted layer by layer from z - (SPLAT_SIZE - 1) / 2 to z + (SPLAT_SIZE - 1) / 2
var SPLAT_DEPTH = 5;
for (var z = -(SPLAT_DEPTH - 1) / 2; z <= (SPLAT_DEPTH - 1) / 2; ++z) {
transferToGridDrawState.uniform1f('u_zOffset', z);
wgl.drawArrays(transferToGridDrawState, wgl.POINTS, 0, this.particlesWidth * this.particlesHeight);
}
//accumulate (weight * velocity)
wgl.framebufferTexture2D(this.simulationFramebuffer, wgl.FRAMEBUFFER, wgl.COLOR_ATTACHMENT0, wgl.TEXTURE_2D, this.tempVelocityTexture, 0);
wgl.clear(
wgl.createClearState().bindFramebuffer(this.simulationFramebuffer),
wgl.COLOR_BUFFER_BIT);
transferToGridDrawState.uniform1i('u_accumulate', 1)
for (var z = -(SPLAT_DEPTH - 1) / 2; z <= (SPLAT_DEPTH - 1) / 2; ++z) {
transferToGridDrawState.uniform1f('u_zOffset', z);
wgl.drawArrays(transferToGridDrawState, wgl.POINTS, 0, this.particlesWidth * this.particlesHeight);
}
//in the second step, we divide sum(weight * velocity) by sum(weight) (the two accumulated quantities from before)
wgl.framebufferTexture2D(this.simulationFramebuffer, wgl.FRAMEBUFFER, wgl.COLOR_ATTACHMENT0, wgl.TEXTURE_2D, this.velocityTexture, 0);
var normalizeDrawState = wgl.createDrawState()
.bindFramebuffer(this.simulationFramebuffer)
.viewport(0, 0, this.velocityTextureWidth, this.velocityTextureHeight)
.vertexAttribPointer(this.quadVertexBuffer, 0, 2, wgl.FLOAT, wgl.FALSE, 0, 0)
.useProgram(this.normalizeGridProgram)
.uniformTexture('u_weightTexture', 0, wgl.TEXTURE_2D, this.weightTexture)
.uniformTexture('u_accumulatedVelocityTexture', 1, wgl.TEXTURE_2D, this.tempVelocityTexture)
wgl.drawArrays(normalizeDrawState, wgl.TRIANGLE_STRIP, 0, 4);
//////////////////////////////////////////////////////
// mark cells with fluid
wgl.framebufferTexture2D(this.simulationFramebuffer, wgl.FRAMEBUFFER, wgl.COLOR_ATTACHMENT0, wgl.TEXTURE_2D, this.markerTexture, 0);
wgl.clear(
wgl.createClearState().bindFramebuffer(this.simulationFramebuffer),
wgl.COLOR_BUFFER_BIT);
var markDrawState = wgl.createDrawState()
.bindFramebuffer(this.simulationFramebuffer)
.viewport(0, 0, this.scalarTextureWidth, this.scalarTextureHeight)
.vertexAttribPointer(this.particleVertexBuffer, 0, 2, wgl.FLOAT, wgl.FALSE, 0, 0)
.useProgram(this.markProgram)
.uniform3f('u_gridResolution', this.gridResolutionX, this.gridResolutionY, this.gridResolutionZ)
.uniform3f('u_gridSize', this.gridWidth, this.gridHeight, this.gridDepth)
.uniformTexture('u_positionTexture', 0, wgl.TEXTURE_2D, this.particlePositionTexture);
wgl.drawArrays(markDrawState, wgl.POINTS, 0, this.particlesWidth * this.particlesHeight);
////////////////////////////////////////////////////
// save our original velocity grid
wgl.framebufferTexture2D(this.simulationFramebuffer, wgl.FRAMEBUFFER, wgl.COLOR_ATTACHMENT0, wgl.TEXTURE_2D, this.originalVelocityTexture, 0);
var copyDrawState = wgl.createDrawState()
.bindFramebuffer(this.simulationFramebuffer)
.viewport(0, 0, this.velocityTextureWidth, this.velocityTextureHeight)
.vertexAttribPointer(this.quadVertexBuffer, 0, 2, wgl.FLOAT, wgl.FALSE, 0, 0)
.useProgram(this.copyProgram)
.uniformTexture('u_texture', 0, wgl.TEXTURE_2D, this.velocityTexture)
wgl.drawArrays(copyDrawState, wgl.TRIANGLE_STRIP, 0, 4);
/////////////////////////////////////////////////////
// add forces to velocity grid
wgl.framebufferTexture2D(this.simulationFramebuffer, wgl.FRAMEBUFFER, wgl.COLOR_ATTACHMENT0, wgl.TEXTURE_2D, this.tempVelocityTexture, 0);
var addForceDrawState = wgl.createDrawState()
.bindFramebuffer(this.simulationFramebuffer)
.viewport(0, 0, this.velocityTextureWidth, this.velocityTextureHeight)
.vertexAttribPointer(this.quadVertexBuffer, 0, 2, wgl.FLOAT, wgl.FALSE, 0, 0)
.useProgram(this.addForceProgram)
.uniformTexture('u_velocityTexture', 0, wgl.TEXTURE_2D, this.velocityTexture)
.uniform1f('u_timeStep', timeStep)
.uniform3f('u_mouseVelocity', mouseVelocity[0], mouseVelocity[1], mouseVelocity[2])
.uniform3f('u_gridResolution', this.gridResolutionX, this.gridResolutionY, this.gridResolutionZ)
.uniform3f('u_gridSize', this.gridWidth, this.gridHeight, this.gridDepth)
.uniform3f('u_mouseRayOrigin', mouseRayOrigin[0], mouseRayOrigin[1], mouseRayOrigin[2])
.uniform3f('u_mouseRayDirection', mouseRayDirection[0], mouseRayDirection[1], mouseRayDirection[2])
wgl.drawArrays(addForceDrawState, wgl.TRIANGLE_STRIP, 0, 4);
swap(this, 'velocityTexture', 'tempVelocityTexture');
/////////////////////////////////////////////////////
// enforce boundary velocity conditions
wgl.framebufferTexture2D(this.simulationFramebuffer, wgl.FRAMEBUFFER, wgl.COLOR_ATTACHMENT0, wgl.TEXTURE_2D, this.tempVelocityTexture, 0);
var enforceBoundariesDrawState = wgl.createDrawState()
.bindFramebuffer(this.simulationFramebuffer)
.viewport(0, 0, this.velocityTextureWidth, this.velocityTextureHeight)
.vertexAttribPointer(this.quadVertexBuffer, 0, 2, wgl.FLOAT, wgl.FALSE, 0, 0)
.useProgram(this.enforceBoundariesProgram)
.uniformTexture('u_velocityTexture', 0, wgl.TEXTURE_2D, this.velocityTexture)
.uniform3f('u_gridResolution', this.gridResolutionX, this.gridResolutionY, this.gridResolutionZ);
wgl.drawArrays(enforceBoundariesDrawState, wgl.TRIANGLE_STRIP, 0, 4);
swap(this, 'velocityTexture', 'tempVelocityTexture');
/////////////////////////////////////////////////////
// update velocityTexture for non divergence
//compute divergence for pressure projection
var divergenceDrawState = wgl.createDrawState()
.bindFramebuffer(this.simulationFramebuffer)
.viewport(0, 0, this.scalarTextureWidth, this.scalarTextureHeight)
.useProgram(this.divergenceProgram)
.uniform3f('u_gridResolution', this.gridResolutionX, this.gridResolutionY, this.gridResolutionZ)
.uniformTexture('u_velocityTexture', 0, wgl.TEXTURE_2D, this.velocityTexture)
.uniformTexture('u_markerTexture', 1, wgl.TEXTURE_2D, this.markerTexture)
.uniformTexture('u_weightTexture', 2, wgl.TEXTURE_2D, this.weightTexture)
.uniform1f('u_maxDensity', this.particleDensity)
.vertexAttribPointer(this.quadVertexBuffer, 0, 2, wgl.FLOAT, false, 0, 0)
wgl.framebufferTexture2D(this.simulationFramebuffer, wgl.FRAMEBUFFER, wgl.COLOR_ATTACHMENT0, wgl.TEXTURE_2D, this.divergenceTexture, 0);
wgl.clear(
wgl.createClearState().bindFramebuffer(this.simulationFramebuffer),
wgl.COLOR_BUFFER_BIT);
wgl.drawArrays(divergenceDrawState, wgl.TRIANGLE_STRIP, 0, 4);
//compute pressure via jacobi iteration
var jacobiDrawState = wgl.createDrawState()
.bindFramebuffer(this.simulationFramebuffer)
.viewport(0, 0, this.scalarTextureWidth, this.scalarTextureHeight)
.useProgram(this.jacobiProgram)
.uniform3f('u_gridResolution', this.gridResolutionX, this.gridResolutionY, this.gridResolutionZ)
.uniformTexture('u_divergenceTexture', 1, wgl.TEXTURE_2D, this.divergenceTexture)
.uniformTexture('u_markerTexture', 2, wgl.TEXTURE_2D, this.markerTexture)
.vertexAttribPointer(this.quadVertexBuffer, 0, 2, wgl.FLOAT, false, 0, 0)
wgl.framebufferTexture2D(this.simulationFramebuffer, wgl.FRAMEBUFFER, wgl.COLOR_ATTACHMENT0, wgl.TEXTURE_2D, this.pressureTexture, 0);
wgl.clear(
wgl.createClearState().bindFramebuffer(this.simulationFramebuffer),
wgl.COLOR_BUFFER_BIT);
var PRESSURE_JACOBI_ITERATIONS = 50;
for (var i = 0; i < PRESSURE_JACOBI_ITERATIONS; ++i) {
wgl.framebufferTexture2D(this.simulationFramebuffer, wgl.FRAMEBUFFER, wgl.COLOR_ATTACHMENT0, wgl.TEXTURE_2D, this.tempSimulationTexture, 0);
jacobiDrawState.uniformTexture('u_pressureTexture', 0, wgl.TEXTURE_2D, this.pressureTexture);
wgl.drawArrays(jacobiDrawState, wgl.TRIANGLE_STRIP, 0, 4);
swap(this, 'pressureTexture', 'tempSimulationTexture');
}
//subtract pressure gradient from velocity
wgl.framebufferTexture2D(this.simulationFramebuffer, wgl.FRAMEBUFFER, wgl.COLOR_ATTACHMENT0, wgl.TEXTURE_2D, this.tempVelocityTexture, 0);
var subtractDrawState = wgl.createDrawState()
.bindFramebuffer(this.simulationFramebuffer)
.viewport(0, 0, this.velocityTextureWidth, this.velocityTextureHeight)
.useProgram(this.subtractProgram)
.uniform3f('u_gridResolution', this.gridResolutionX, this.gridResolutionY, this.gridResolutionZ)
.uniformTexture('u_pressureTexture', 0, wgl.TEXTURE_2D, this.pressureTexture)
.uniformTexture('u_velocityTexture', 1, wgl.TEXTURE_2D, this.velocityTexture)
.uniformTexture('u_markerTexture', 2, wgl.TEXTURE_2D, this.markerTexture)
.vertexAttribPointer(this.quadVertexBuffer, 0, 2, wgl.FLOAT, false, 0, 0)
wgl.drawArrays(subtractDrawState, wgl.TRIANGLE_STRIP, 0, 4);
swap(this, 'velocityTexture', 'tempVelocityTexture');
/////////////////////////////////////////////////////////////
// transfer velocities back to particles
wgl.framebufferTexture2D(this.simulationFramebuffer, wgl.FRAMEBUFFER, wgl.COLOR_ATTACHMENT0, wgl.TEXTURE_2D, this.particleVelocityTextureTemp, 0);
var transferToParticlesDrawState = wgl.createDrawState()
.bindFramebuffer(this.simulationFramebuffer)
.viewport(0, 0, this.particlesWidth, this.particlesHeight)
.vertexAttribPointer(this.quadVertexBuffer, 0, 2, wgl.FLOAT, wgl.FALSE, 0, 0)
.useProgram(this.transferToParticlesProgram)
.uniformTexture('u_particlePositionTexture', 0, wgl.TEXTURE_2D, this.particlePositionTexture)
.uniformTexture('u_particleVelocityTexture', 1, wgl.TEXTURE_2D, this.particleVelocityTexture)
.uniformTexture('u_gridVelocityTexture', 2, wgl.TEXTURE_2D, this.velocityTexture)
.uniformTexture('u_originalGridVelocityTexture', 3, wgl.TEXTURE_2D, this.originalVelocityTexture)
.uniform3f('u_gridResolution', this.gridResolutionX, this.gridResolutionY, this.gridResolutionZ)
.uniform3f('u_gridSize', this.gridWidth, this.gridHeight, this.gridDepth)
.uniform1f('u_flipness', this.flipness)
wgl.drawArrays(transferToParticlesDrawState, wgl.TRIANGLE_STRIP, 0, 4);
swap(this, 'particleVelocityTextureTemp', 'particleVelocityTexture');
///////////////////////////////////////////////
// advect particle positions with velocity grid using RK2
wgl.framebufferTexture2D(this.simulationFramebuffer, wgl.FRAMEBUFFER, wgl.COLOR_ATTACHMENT0, wgl.TEXTURE_2D, this.particlePositionTextureTemp, 0);
wgl.clear(
wgl.createClearState().bindFramebuffer(this.simulationFramebuffer),
wgl.COLOR_BUFFER_BIT);
var advectDrawState = wgl.createDrawState()
.bindFramebuffer(this.simulationFramebuffer)
.viewport(0, 0, this.particlesWidth, this.particlesHeight)
.vertexAttribPointer(this.quadVertexBuffer, 0, 2, wgl.FLOAT, wgl.FALSE, 0, 0)
.useProgram(this.advectProgram)
.uniformTexture('u_positionsTexture', 0, wgl.TEXTURE_2D, this.particlePositionTexture)
.uniformTexture('u_randomsTexture', 1, wgl.TEXTURE_2D, this.particleRandomTexture)
.uniformTexture('u_velocityGrid', 2, wgl.TEXTURE_2D, this.velocityTexture)
.uniform3f('u_gridResolution', this.gridResolutionX, this.gridResolutionY, this.gridResolutionZ)
.uniform3f('u_gridSize', this.gridWidth, this.gridHeight, this.gridDepth)
.uniform1f('u_timeStep', timeStep)
.uniform1f('u_frameNumber', this.frameNumber)
.uniform2f('u_particlesResolution', this.particlesWidth, this.particlesHeight);
wgl.drawArrays(advectDrawState, wgl.TRIANGLE_STRIP, 0, 4);
swap(this, 'particlePositionTextureTemp', 'particlePositionTexture');
}
return Simulator;
}());