@@ -24,7 +24,7 @@ SOFTWARE.
24
24
25
25
'use strict' ;
26
26
27
- // Promo code
27
+ // Mobile promo section
28
28
29
29
const promoPopup = document . getElementsByClassName ( 'promo' ) [ 0 ] ;
30
30
const promoPopupClose = document . getElementsByClassName ( 'promo-close' ) [ 0 ] ;
@@ -51,7 +51,7 @@ googleLink.addEventListener('click', e => {
51
51
window . open ( 'https://play.google.com/store/apps/details?id=games.paveldogreat.fluidsimfree' ) ;
52
52
} ) ;
53
53
54
- // Simulation code
54
+ // Simulation section
55
55
56
56
const canvas = document . getElementsByTagName ( 'canvas' ) [ 0 ] ;
57
57
resizeCanvas ( ) ;
@@ -918,8 +918,22 @@ const blit = (() => {
918
918
gl . vertexAttribPointer ( 0 , 2 , gl . FLOAT , false , 0 , 0 ) ;
919
919
gl . enableVertexAttribArray ( 0 ) ;
920
920
921
- return ( destination ) => {
922
- gl . bindFramebuffer ( gl . FRAMEBUFFER , destination ) ;
921
+ return ( target , clear = false ) => {
922
+ if ( target == null )
923
+ {
924
+ gl . viewport ( 0 , 0 , gl . drawingBufferWidth , gl . drawingBufferHeight ) ;
925
+ gl . bindFramebuffer ( gl . FRAMEBUFFER , null ) ;
926
+ }
927
+ else
928
+ {
929
+ gl . viewport ( 0 , 0 , target . width , target . height ) ;
930
+ gl . bindFramebuffer ( gl . FRAMEBUFFER , target . fbo ) ;
931
+ }
932
+ if ( clear )
933
+ {
934
+ gl . clearColor ( 0.0 , 0.0 , 0.0 , 1.0 ) ;
935
+ gl . clear ( gl . COLOR_BUFFER_BIT ) ;
936
+ }
923
937
gl . drawElements ( gl . TRIANGLES , 6 , gl . UNSIGNED_SHORT , 0 ) ;
924
938
}
925
939
} ) ( ) ;
@@ -966,6 +980,8 @@ function initFramebuffers () {
966
980
const r = ext . formatR ;
967
981
const filtering = ext . supportLinearFiltering ? gl . LINEAR : gl . NEAREST ;
968
982
983
+ gl . disable ( gl . BLEND ) ;
984
+
969
985
if ( dye == null )
970
986
dye = createDoubleFBO ( dyeRes . width , dyeRes . height , rgba . internalFormat , rgba . format , texType , filtering ) ;
971
987
else
@@ -1084,7 +1100,7 @@ function resizeFBO (target, w, h, internalFormat, format, type, param) {
1084
1100
let newFBO = createFBO ( w , h , internalFormat , format , type , param ) ;
1085
1101
copyProgram . bind ( ) ;
1086
1102
gl . uniform1i ( copyProgram . uniforms . uTexture , target . attach ( 0 ) ) ;
1087
- blit ( newFBO . fbo ) ;
1103
+ blit ( newFBO ) ;
1088
1104
return newFBO ;
1089
1105
}
1090
1106
@@ -1205,47 +1221,46 @@ function applyInputs () {
1205
1221
1206
1222
function step ( dt ) {
1207
1223
gl . disable ( gl . BLEND ) ;
1208
- gl . viewport ( 0 , 0 , velocity . width , velocity . height ) ;
1209
1224
1210
1225
curlProgram . bind ( ) ;
1211
1226
gl . uniform2f ( curlProgram . uniforms . texelSize , velocity . texelSizeX , velocity . texelSizeY ) ;
1212
1227
gl . uniform1i ( curlProgram . uniforms . uVelocity , velocity . read . attach ( 0 ) ) ;
1213
- blit ( curl . fbo ) ;
1228
+ blit ( curl ) ;
1214
1229
1215
1230
vorticityProgram . bind ( ) ;
1216
1231
gl . uniform2f ( vorticityProgram . uniforms . texelSize , velocity . texelSizeX , velocity . texelSizeY ) ;
1217
1232
gl . uniform1i ( vorticityProgram . uniforms . uVelocity , velocity . read . attach ( 0 ) ) ;
1218
1233
gl . uniform1i ( vorticityProgram . uniforms . uCurl , curl . attach ( 1 ) ) ;
1219
1234
gl . uniform1f ( vorticityProgram . uniforms . curl , config . CURL ) ;
1220
1235
gl . uniform1f ( vorticityProgram . uniforms . dt , dt ) ;
1221
- blit ( velocity . write . fbo ) ;
1236
+ blit ( velocity . write ) ;
1222
1237
velocity . swap ( ) ;
1223
1238
1224
1239
divergenceProgram . bind ( ) ;
1225
1240
gl . uniform2f ( divergenceProgram . uniforms . texelSize , velocity . texelSizeX , velocity . texelSizeY ) ;
1226
1241
gl . uniform1i ( divergenceProgram . uniforms . uVelocity , velocity . read . attach ( 0 ) ) ;
1227
- blit ( divergence . fbo ) ;
1242
+ blit ( divergence ) ;
1228
1243
1229
1244
clearProgram . bind ( ) ;
1230
1245
gl . uniform1i ( clearProgram . uniforms . uTexture , pressure . read . attach ( 0 ) ) ;
1231
1246
gl . uniform1f ( clearProgram . uniforms . value , config . PRESSURE ) ;
1232
- blit ( pressure . write . fbo ) ;
1247
+ blit ( pressure . write ) ;
1233
1248
pressure . swap ( ) ;
1234
1249
1235
1250
pressureProgram . bind ( ) ;
1236
1251
gl . uniform2f ( pressureProgram . uniforms . texelSize , velocity . texelSizeX , velocity . texelSizeY ) ;
1237
1252
gl . uniform1i ( pressureProgram . uniforms . uDivergence , divergence . attach ( 0 ) ) ;
1238
1253
for ( let i = 0 ; i < config . PRESSURE_ITERATIONS ; i ++ ) {
1239
1254
gl . uniform1i ( pressureProgram . uniforms . uPressure , pressure . read . attach ( 1 ) ) ;
1240
- blit ( pressure . write . fbo ) ;
1255
+ blit ( pressure . write ) ;
1241
1256
pressure . swap ( ) ;
1242
1257
}
1243
1258
1244
1259
gradienSubtractProgram . bind ( ) ;
1245
1260
gl . uniform2f ( gradienSubtractProgram . uniforms . texelSize , velocity . texelSizeX , velocity . texelSizeY ) ;
1246
1261
gl . uniform1i ( gradienSubtractProgram . uniforms . uPressure , pressure . read . attach ( 0 ) ) ;
1247
1262
gl . uniform1i ( gradienSubtractProgram . uniforms . uVelocity , velocity . read . attach ( 1 ) ) ;
1248
- blit ( velocity . write . fbo ) ;
1263
+ blit ( velocity . write ) ;
1249
1264
velocity . swap ( ) ;
1250
1265
1251
1266
advectionProgram . bind ( ) ;
@@ -1257,17 +1272,15 @@ function step (dt) {
1257
1272
gl . uniform1i ( advectionProgram . uniforms . uSource , velocityId ) ;
1258
1273
gl . uniform1f ( advectionProgram . uniforms . dt , dt ) ;
1259
1274
gl . uniform1f ( advectionProgram . uniforms . dissipation , config . VELOCITY_DISSIPATION ) ;
1260
- blit ( velocity . write . fbo ) ;
1275
+ blit ( velocity . write ) ;
1261
1276
velocity . swap ( ) ;
1262
1277
1263
- gl . viewport ( 0 , 0 , dye . width , dye . height ) ;
1264
-
1265
1278
if ( ! ext . supportLinearFiltering )
1266
1279
gl . uniform2f ( advectionProgram . uniforms . dyeTexelSize , dye . texelSizeX , dye . texelSizeY ) ;
1267
1280
gl . uniform1i ( advectionProgram . uniforms . uVelocity , velocity . read . attach ( 0 ) ) ;
1268
1281
gl . uniform1i ( advectionProgram . uniforms . uSource , dye . read . attach ( 1 ) ) ;
1269
1282
gl . uniform1f ( advectionProgram . uniforms . dissipation , config . DENSITY_DISSIPATION ) ;
1270
- blit ( dye . write . fbo ) ;
1283
+ blit ( dye . write ) ;
1271
1284
dye . swap ( ) ;
1272
1285
}
1273
1286
@@ -1287,31 +1300,29 @@ function render (target) {
1287
1300
gl . disable ( gl . BLEND ) ;
1288
1301
}
1289
1302
1290
- let width = target == null ? gl . drawingBufferWidth : target . width ;
1291
- let height = target == null ? gl . drawingBufferHeight : target . height ;
1292
- gl . viewport ( 0 , 0 , width , height ) ;
1293
-
1294
- let fbo = target == null ? null : target . fbo ;
1295
1303
if ( ! config . TRANSPARENT )
1296
- drawColor ( fbo , normalizeColor ( config . BACK_COLOR ) ) ;
1304
+ drawColor ( target , normalizeColor ( config . BACK_COLOR ) ) ;
1297
1305
if ( target == null && config . TRANSPARENT )
1298
- drawCheckerboard ( fbo ) ;
1299
- drawDisplay ( fbo , width , height ) ;
1306
+ drawCheckerboard ( target ) ;
1307
+ drawDisplay ( target ) ;
1300
1308
}
1301
1309
1302
- function drawColor ( fbo , color ) {
1310
+ function drawColor ( target , color ) {
1303
1311
colorProgram . bind ( ) ;
1304
1312
gl . uniform4f ( colorProgram . uniforms . color , color . r , color . g , color . b , 1 ) ;
1305
- blit ( fbo ) ;
1313
+ blit ( target ) ;
1306
1314
}
1307
1315
1308
- function drawCheckerboard ( fbo ) {
1316
+ function drawCheckerboard ( target ) {
1309
1317
checkerboardProgram . bind ( ) ;
1310
1318
gl . uniform1f ( checkerboardProgram . uniforms . aspectRatio , canvas . width / canvas . height ) ;
1311
- blit ( fbo ) ;
1319
+ blit ( target ) ;
1312
1320
}
1313
1321
1314
- function drawDisplay ( fbo , width , height ) {
1322
+ function drawDisplay ( target ) {
1323
+ let width = target == null ? gl . drawingBufferWidth : target . width ;
1324
+ let height = target == null ? gl . drawingBufferHeight : target . height ;
1325
+
1315
1326
displayMaterial . bind ( ) ;
1316
1327
if ( config . SHADING )
1317
1328
gl . uniform2f ( displayMaterial . uniforms . texelSize , 1.0 / width , 1.0 / height ) ;
@@ -1324,7 +1335,7 @@ function drawDisplay (fbo, width, height) {
1324
1335
}
1325
1336
if ( config . SUNRAYS )
1326
1337
gl . uniform1i ( displayMaterial . uniforms . uSunrays , sunrays . attach ( 3 ) ) ;
1327
- blit ( fbo ) ;
1338
+ blit ( target ) ;
1328
1339
}
1329
1340
1330
1341
function applyBloom ( source , destination ) {
@@ -1342,16 +1353,14 @@ function applyBloom (source, destination) {
1342
1353
gl . uniform3f ( bloomPrefilterProgram . uniforms . curve , curve0 , curve1 , curve2 ) ;
1343
1354
gl . uniform1f ( bloomPrefilterProgram . uniforms . threshold , config . BLOOM_THRESHOLD ) ;
1344
1355
gl . uniform1i ( bloomPrefilterProgram . uniforms . uTexture , source . attach ( 0 ) ) ;
1345
- gl . viewport ( 0 , 0 , last . width , last . height ) ;
1346
- blit ( last . fbo ) ;
1356
+ blit ( last ) ;
1347
1357
1348
1358
bloomBlurProgram . bind ( ) ;
1349
1359
for ( let i = 0 ; i < bloomFramebuffers . length ; i ++ ) {
1350
1360
let dest = bloomFramebuffers [ i ] ;
1351
1361
gl . uniform2f ( bloomBlurProgram . uniforms . texelSize , last . texelSizeX , last . texelSizeY ) ;
1352
1362
gl . uniform1i ( bloomBlurProgram . uniforms . uTexture , last . attach ( 0 ) ) ;
1353
- gl . viewport ( 0 , 0 , dest . width , dest . height ) ;
1354
- blit ( dest . fbo ) ;
1363
+ blit ( dest ) ;
1355
1364
last = dest ;
1356
1365
}
1357
1366
@@ -1363,7 +1372,7 @@ function applyBloom (source, destination) {
1363
1372
gl . uniform2f ( bloomBlurProgram . uniforms . texelSize , last . texelSizeX , last . texelSizeY ) ;
1364
1373
gl . uniform1i ( bloomBlurProgram . uniforms . uTexture , last . attach ( 0 ) ) ;
1365
1374
gl . viewport ( 0 , 0 , baseTex . width , baseTex . height ) ;
1366
- blit ( baseTex . fbo ) ;
1375
+ blit ( baseTex ) ;
1367
1376
last = baseTex ;
1368
1377
}
1369
1378
@@ -1372,34 +1381,31 @@ function applyBloom (source, destination) {
1372
1381
gl . uniform2f ( bloomFinalProgram . uniforms . texelSize , last . texelSizeX , last . texelSizeY ) ;
1373
1382
gl . uniform1i ( bloomFinalProgram . uniforms . uTexture , last . attach ( 0 ) ) ;
1374
1383
gl . uniform1f ( bloomFinalProgram . uniforms . intensity , config . BLOOM_INTENSITY ) ;
1375
- gl . viewport ( 0 , 0 , destination . width , destination . height ) ;
1376
- blit ( destination . fbo ) ;
1384
+ blit ( destination ) ;
1377
1385
}
1378
1386
1379
1387
function applySunrays ( source , mask , destination ) {
1380
1388
gl . disable ( gl . BLEND ) ;
1381
1389
sunraysMaskProgram . bind ( ) ;
1382
1390
gl . uniform1i ( sunraysMaskProgram . uniforms . uTexture , source . attach ( 0 ) ) ;
1383
- gl . viewport ( 0 , 0 , mask . width , mask . height ) ;
1384
- blit ( mask . fbo ) ;
1391
+ blit ( mask ) ;
1385
1392
1386
1393
sunraysProgram . bind ( ) ;
1387
1394
gl . uniform1f ( sunraysProgram . uniforms . weight , config . SUNRAYS_WEIGHT ) ;
1388
1395
gl . uniform1i ( sunraysProgram . uniforms . uTexture , mask . attach ( 0 ) ) ;
1389
- gl . viewport ( 0 , 0 , destination . width , destination . height ) ;
1390
- blit ( destination . fbo ) ;
1396
+ blit ( destination ) ;
1391
1397
}
1392
1398
1393
1399
function blur ( target , temp , iterations ) {
1394
1400
blurProgram . bind ( ) ;
1395
1401
for ( let i = 0 ; i < iterations ; i ++ ) {
1396
1402
gl . uniform2f ( blurProgram . uniforms . texelSize , target . texelSizeX , 0.0 ) ;
1397
1403
gl . uniform1i ( blurProgram . uniforms . uTexture , target . attach ( 0 ) ) ;
1398
- blit ( temp . fbo ) ;
1404
+ blit ( temp ) ;
1399
1405
1400
1406
gl . uniform2f ( blurProgram . uniforms . texelSize , 0.0 , target . texelSizeY ) ;
1401
1407
gl . uniform1i ( blurProgram . uniforms . uTexture , temp . attach ( 0 ) ) ;
1402
- blit ( target . fbo ) ;
1408
+ blit ( target ) ;
1403
1409
}
1404
1410
}
1405
1411
@@ -1424,20 +1430,18 @@ function multipleSplats (amount) {
1424
1430
}
1425
1431
1426
1432
function splat ( x , y , dx , dy , color ) {
1427
- gl . viewport ( 0 , 0 , velocity . width , velocity . height ) ;
1428
1433
splatProgram . bind ( ) ;
1429
1434
gl . uniform1i ( splatProgram . uniforms . uTarget , velocity . read . attach ( 0 ) ) ;
1430
1435
gl . uniform1f ( splatProgram . uniforms . aspectRatio , canvas . width / canvas . height ) ;
1431
1436
gl . uniform2f ( splatProgram . uniforms . point , x , y ) ;
1432
1437
gl . uniform3f ( splatProgram . uniforms . color , dx , dy , 0.0 ) ;
1433
1438
gl . uniform1f ( splatProgram . uniforms . radius , correctRadius ( config . SPLAT_RADIUS / 100.0 ) ) ;
1434
- blit ( velocity . write . fbo ) ;
1439
+ blit ( velocity . write ) ;
1435
1440
velocity . swap ( ) ;
1436
1441
1437
- gl . viewport ( 0 , 0 , dye . width , dye . height ) ;
1438
1442
gl . uniform1i ( splatProgram . uniforms . uTarget , dye . read . attach ( 0 ) ) ;
1439
1443
gl . uniform3f ( splatProgram . uniforms . color , color . r , color . g , color . b ) ;
1440
- blit ( dye . write . fbo ) ;
1444
+ blit ( dye . write ) ;
1441
1445
dye . swap ( ) ;
1442
1446
}
1443
1447
0 commit comments