diff options
Experimental visualization of the velocity curl
Calculating the curl of our simulated velocity field requires an additional
compute shader step. Handling of buffer and shader switching depending on
the display mode is implemented rudimentarily for now.
Most of this commit is scaffolding, the actual computation is more or less
trivial:
```
const float dxvy = (getFluidVelocity(x+1,y).y - getFluidVelocity(x-1,y).y)
/ (2*convLength);
const float dyvx = (getFluidVelocity(x,y+1).x - getFluidVelocity(x,y-1).x)
/ (2*convLength);
setFluidExtra(x, y, dxvy - dyvx);
```
This implements the following discretization of the 2d curl operator:
Let $V : \mathbb{N}^2 \to \mathbb{R}^2$ be the simulated velocity field at
discrete lattice points spaced by $\Delta x \in \mathbb{R}_{\gt 0}$.
We want to approximate the $z$-component of the curl for visualization:
$$\omega := \partial_x F_y - \partial_y F_x$$
As we do not possess the actual function $F$ but only its values at a
set of discrete points we approximate the two partial derivatives using
a second order central difference scheme:
$$\overline{\omega}(i,j) := \frac{F_y(i+1,j) - F_y(i-1,j)}{2 \Delta x} - \frac{F_x(i,j+1) - F_x(i,j-1)}{2 \Delta x}$$
Note that the scene shader does some further rescaling of the curl to better
fit the color palette. One issue that irks me is the emergence of some
artefacts near boundaries as well as isolated "single-cell-vortices".
This might be caused by running the simulation too close to divergence
but as I am currently mostly interested in building an interactive fluid
playground it could be worth it to try running an additional smoothening
shader pass to straighten things out.
Diffstat (limited to 'src/shader/code/vertex.glsl')
-rw-r--r-- | src/shader/code/vertex.glsl | 52 |
1 files changed, 47 insertions, 5 deletions
diff --git a/src/shader/code/vertex.glsl b/src/shader/code/vertex.glsl index 0f0e07c..5136023 100644 --- a/src/shader/code/vertex.glsl +++ b/src/shader/code/vertex.glsl @@ -11,6 +11,7 @@ uniform uint nX; uniform uint nY; uniform bool show_fluid_quality; +uniform bool show_curl; uniform int palette_factor; float unit(float x) { @@ -65,6 +66,38 @@ vec3 trafficLightPalette(float x) { } } +vec3 blueBlackRedPalette(float x) { + if ( x < 0.5 ) { + return mix( + vec3(0.0, 0.0, 1.0), + vec3(0.0, 0.0, 0.0), + 2*x + ); + } else { + return mix( + vec3(0.0, 0.0, 0.0), + vec3(1.0, 0.0, 0.0), + 2*(x - 0.5) + ); + } +} + +vec3 blackGoldPalette(float x) { + return mix( + vec3(0.0, 0.0, 0.0), + vec3(0.5, 0.35, 0.05), + x + ); +} + +vec3 blueRedPalette(float x) { + return mix( + vec3(0.0, 0.0, 1.0), + vec3(1.0, 0.0, 0.0), + x + ); +} + void main() { const vec2 idx = fluidVertexAtIndex(gl_VertexID); @@ -83,12 +116,21 @@ void main() { vs_out.color = vec3(0.0, 0.0, 0.0); } else { if ( show_fluid_quality ) { - vs_out.color = trafficLightPalette(restrictedQuality(VertexPosition.y)); + vs_out.color = trafficLightPalette( + restrictedQuality(VertexPosition.y) + ); + } else if ( show_curl ) { + const float factor = 1.0 / float(100*palette_factor); + if ( abs(VertexPosition.x) > 1.0 ) { + vs_out.color = blueBlackRedPalette( + 0.5 + (VertexPosition.x * factor) + ); + } else { + vs_out.color = vec3(0.0,0.0,0.0); + } } else { - vs_out.color = mix( - vec3(-0.5, 0.0, 1.0), - vec3( 1.0, 0.0, 0.0), - norm(VertexPosition.xy) / float(palette_factor) + vs_out.color = blueRedPalette( + norm(VertexPosition.xy) / palette_factor ); } } |