Rendering

Writing shaders for FluXY

When writing your own fluid rendering shaders, include the /FluXY/Resources/Shaders/FluidUtils.hlsl file at the top of your shader. This will make a variety of helper functions and samplers available to you:

FluXY solvers expose 2 textures to all shaders:

_MainTex
This is the density texture. RGB channels store density, and A channel stores temperature.
_Velocity
This is the velocity texture. RG channels store velocity, B stores divergence, A stores pressure.

You can use them anyway you like. Generally, the RGB channels of the density texture are interpreted as a color, and A interpreted as opacity. The velocity texture's A channel (which stores pressure) can be interpreted as fluid "height", useful for simulating waves, specially when using the iterative pressure solver.

When accessing the data in these textures, you should keep in mind that they store data for all containers in the solver. To access the region of the textures specific to a container, use the TileToUV() function included in FluidUtils.hlsl:

// convert texture coordinates from tile to normalized uvs:
float2 uv = TileToUV(i.uv,_TileIndex);

// use them to access velocity and density
float4 density = tex2D(_MainTex, uv);
float4 velocity = tex2D(_Velocity, uv);
								

If you're using Shader Graph to create your shaders, the TileToUV subgraph node you'll find in /Samples/Resources/Shaders/URP accomplishes the same goal.

Basic fluid shader

FluXY includes a basic unlit fluid shader, compatible with all rendering pipelines. This is mostly used in conjunction with coontainers using "Plane" as their shape:


You will find this shader at FluXY/Rendering/BasicFluid:


Let's take a look at what this shader offers:


Detail

This is a grayscale texture that will be advected (carried) using the fluid's velocity texture input, and used to modulate fluid density. You can use this to cheaply add fine detail to your fluid:

Fluid using a "clouds" detail texture.
Fluid using a "voronoi" detail texture.

Gradient

If set, the shader will map the fluid's density to the U (horizontal) coordinate of this texture.

Fluid using a blue gradient, and "clouds" detail texture.
Fluid using a multicolored gradient, and "voronoi" detail texture.

Detail advection

Amount of advection applied to the detail texture. This is a multiplier of the fluid's velocity vector.

"Voronoi" detail texture using zero advection.
"Voronoi" detail texture using maximum advection: the texture is smeared upwards, following fluid flow.

Additiveness

A value of 0 will use alpha blending. A value of 1 will use additive blending. Any value in-between will interpolate between the two.

Fire with additiveness set to 0.
Fire with additiveness set to 1.

Edge falloff

Opacity falloff applied at the edges of the container.

Edge falloff set to 0: container's edges are clearly visible.
Edge falloff set to 0.6: rendering fades out when approaching the edges of the container.

Soft particles factor

Softness at the intersection of the fluid container with other objects in the scene. Useful for smoke/fire effects.