• Stars
    star
    286
  • Rank 144,690 (Top 3 %)
  • Language
    JavaScript
  • License
    Other
  • Created over 8 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

2D liquid simulation in WebGL

2D Liquid Simulation in WebGL

This project is an implementation of 2D liquid simulation using Smoothed Particle Hydrodynamics in WebGL. Here is a demo. You can use the demo to place out emitters and capsules and create liquid simulations. You can see some simulations made using the demo below

Container Obstacles
container.json obstacles.json
Red Obstacles
red.json ramps.json
Simple Sprinkler
simple.json sprinkler.json

You can see the full simulations in this video. You can import the JSON files into the demo to run the simulations yourself, by copying the json and pressing the import button(if you are having problems, see the FAQ!). But note that these simulations were renderered offline, and they will probably not run in real-time for you. Note that the implementation is not very efficient, and using too many particles(about more than 1500) will slow down the GUI and the entire simulation.

Brief Technical Details

The water simulation is based on the paper Particle-based Viscoelastic Fluid Simulation, which describes a variation of Smoothed Particle Hydrodynamics. But not all details of the paper are implemented. The implemented parts are Double Density Relaxation(section 4), and Viscosity Impulses(section 5.3).

The environment is all represented by capsulses. This simplifies the code much, because it is trivial to check for collision with a capsule. To handle the collision, we use the formulas derived in section 4.4.2.2 of"Lagrangian Fluid Dynamics Using Smoothed Particle Hydrodynamics".

People wanting an easy-to-read introduction to the field of fluid simulation are referred to "Fluid Simulation -Siggraph 2007 Course Notes". People wanting an introduction to Smoothed Particle Hydrodynamics are referred to "Lagrangian Fluid Dynamics Using Smoothed Particle Hydrodynamics".

How to Record Offline Simulations

The below instructions are what works for me. But I have only tested this on my Mac, so this may or may not work for you. If you can't get it to work no matter what, make I pull request and I will help you.

If you press the record button, you will start recording the simulation. This functionality uses the FileSystems API, so it will only work on Chrome!

Once you press record, the program will start saving the frames of the simulation to some place on your filesystem. On my Mac, this location is /Users/eric/Library/Application Support/Google/Chrome/Default/File System. On windows, this location appears to be C:\Users\Eric\AppData\Local\Google\Chrome\User Data\Default\File System. At this location, there are folders named 001, 002, and so on. In one of these folders(probably the most newly created one) the recorded frames can be found. In that folder, enter the folder p/. In this folder, the recorded frames are saved in the TGA format, and they are spread out over directories named 00, 01, and so on. You will need to gather the frames of these folders into a single folder.

I provide the script gather.py for gathering up all the files in one folder. Set the variables in_path and out_path to specify the output and input folders, run the script, and if it works, it will copy all the frames to a single folder. In the process, it will convert the .tga files to .png files using imagemagick/graphicsmagick, because this makes it much faster to later convert the frames to a video.

If you have ffmpeg installed, you can now easily make a video. Go to out_path and run the command

ffmpeg -framerate 50 -i frame%08d.png -c:v libx264 -r 50 -pix_fmt yuv420p out.mp4

and it will create a video in out.mp4. If you want a GIF, you can make it using imagemagick by doing

convert -delay 2 -loop 0 frame*.png out.gif

or by using graphicsmagick

gm convert -delay 2 -loop 0 frame*.png out.gif

but be aware that this will take quite a while!

If you want to record another simulation after recording a first one, be sure to clear the cache before you do so! To do this, go to chrome://settings/cookies and delete the site data for the demo. The entry you should delete is named something like erkaman.github.io. If you do not clear the cache, the numbering of the frames of the new simulation will not start from zero, and gather.py will not handle this special case for you.

Build

To run the demo locally on your computer, first change your directory to the directory of the project, then run

npm install

To then run the demo, do

npm start

FAQ

Why does it say "Import Failed" when I try to import some json?

This means your json is invalid. Check the javascript console for more detailed error messages.

The most probable reason for failure is that you accidentaly got some garbage characters in the beginning of your json when your copied it(you can check this in a hex-editor). Be careful when copying.

More Repositories

1

vulkan_minimal_compute

Minimal Example of Using Vulkan for Compute Operations. Only ~400LOC.
C++
697
star
2

regl-cnn

Digit recognition with Convolutional Neural Networks in WebGL
JavaScript
502
star
3

Awesome-CUDA

This is a list of useful libraries and resources for CUDA development.
421
star
4

wireframe-world

An infinite wireframe world in WebGL
JavaScript
357
star
5

poisson_blend

Seamless copy-and-paste of images with Poisson Blending.
C++
335
star
6

hole_fixer

Demo implementation of smoothly filling holes in 3D meshes using surface fairing
C++
250
star
7

glsl-godrays

This module implements a volumetric light scattering effect(godrays)
JavaScript
190
star
8

cute-deferred-shading

Cute little deferred shading implementation.
C++
137
star
9

gl-catmull-clark

A javascript implementation of the Catmull-Clark subdivision surface algorithm
JavaScript
104
star
10

fluid_sim

Flashy 2D fluid simulations experiments.
C
102
star
11

gl-rock

Procedural Generation of Rocks in WebGL
JavaScript
97
star
12

glsl-worley

Worley noise implementation for WebGL shaders
GLSL
85
star
13

pnp-gui

Minimalistic Immediate Mode GUI toolkit for WebGL
JavaScript
83
star
14

sse-avx-rasterization

Triangle rasterization routines accelerated by SSE and AVX
C++
62
star
15

cloud_gen

Procedural Generation of Clouds with Vector Graphics
C++
49
star
16

webgl-rsm

Real-time Indirect lighting with Reflective Shadows Maps in WebGL
JavaScript
45
star
17

planar_proj_shadows

Demo of Planar Projected Shadows in regl
JavaScript
32
star
18

glsl-cos-palette

glsl function for making cosine palettes
JavaScript
31
star
19

teapot_shooter

Augmented Reality Teapot Shooter made using Unity and ARCore
C#
30
star
20

image-load-store-demo

A small demo and tutorial of the image load/store feature of OpenGL 4
C++
26
star
21

glsl-gradient-palette

Module for creating gradient palettes for usage in glsl.
JavaScript
19
star
22

regl-anim

Some weird animations made with regl and WebGL
JavaScript
17
star
23

regl-fire

Fire particle system made with regl
JavaScript
17
star
24

tess-opt

Demonstration of how we can use tessellation shaders to make faster fragment shaders.
C++
15
star
25

erkaman.github.io

The source code of my website.
HTML
13
star
26

regl-webvr-demo

Demo that shows how to use regl and WebVR together
JavaScript
11
star
27

ffmpeg-add-text-to-video-tutorial

Tutorial that shows how to add text to a video file with ffmpeg
9
star
28

parle-cuda

A reference implementation of RLE in CUDA
Cuda
9
star
29

NeoTextureEdit2

Fork of NeoTextureEdit that fixes several things.
Java
8
star
30

regl-stats-widget

Small widget for displaying statistics of regl
JavaScript
8
star
31

gl-quads-to-tris

WebGL helper module that converts an array of quad indices to an array of triangles indices
JavaScript
7
star
32

spiky-anim

The source code of some silly spiky animation I made
C++
7
star
33

smiley

:^) :^) :^) :^) :^) :^), :^) :^) :^) :^) :^) :^) :^) :^) :^) :^) :^).
JavaScript
6
star
34

particle-simd

SIMD-accelerated particle simulation in C++
C++
5
star
35

digital-image-formats

An exploration of the inner workings of digital image formats.
C++
3
star
36

gl-camera-pos-from-view-matrix

A helper module that allows you to recover the camera position from a view matrix
JavaScript
3
star
37

regl-cpp

Remaking regl in C++
C
3
star
38

font_creator_cpp

A simple program that creates a font atlas using FreeType
C++
2
star
39

scan_shift_jis.rb

Small utility for scanning Shift-JIS encoded strings in a file.
Ruby
1
star
40

smw-tools

Java
1
star
41

font_sheet.py

Python
1
star
42

cloth

C++
1
star
43

BrainfuckSharp

A Brainfuck compiler for the .NET framework.
C#
1
star
44

sculpture

C++
1
star
45

dotfiles

My miscellaneous configuration files.
Emacs Lisp
1
star
46

aabb_create

Small utility for computing an AABB from a wavefront object file.
C++
1
star
47

texture-editor

C++
1
star