Log inGet started
Package

fluidSurface.package

Newest version

This package holds more than this page lists. See all of its files in its world.

README.md

fluid_shading

What a fluid surface's shading response is handed, and the function a response module defines. A surface fluidSurface.attach draws takes the response its spec names in response (or fluidSurface.update sets): a .shaderModule that includes this module and defines

  • fluid_shade(s: FluidSample) -> vec4<f32>: the colour the surface shows at a pixel (rgb) and how much of the scene behind it that colour covers (a, 0 to 1). vec4<f32>(s.physical, s.coverage) keeps the engine's own shading.

The splat, the filter and the resolve run as for any surface; the response decides only how each pixel the liquid covers is coloured, and pixels the scene's own depth puts in front of the liquid show the scene.

What a sample carries

FluidSample:

  • physical, coverage: the engine's own colour of the surface and how much of the scene it covers (0 at the thin edge, 1 from a sixth of a unit of thickness up).
  • scene: the scene behind the pixel. refracted: the scene seen through the surface, bent by its normal. transmitted: refracted after the liquid absorbs along the ray and its deep colour takes over with thickness.
  • reflected, fresnel: the light the surface mirrors (the reflection march's hit, or the sky colour) and the share of it the angle shows.
  • highlight (0 to 1) and specular (its colour and strength applied): the sun's highlight.
  • liquid: the mean particle colour along the ray, 0 when the particles carry none. deep: the colour the liquid takes where it is deep.
  • foam, foam_color: the foam the engine lays (thin-edge foam and the particles' foam weight) and its colour.
  • thickness: the liquid the view ray crosses, in world units (held to 6). edge: 1 at the body's thinnest edge, 0 from a third of a unit of thickness. depth: the surface's depth along the view axis. behind: how far behind the surface the scene stands along that axis (1e9 under open sky), small where the liquid meets geometry, for a shore line. depth_below: how far the surface point stands above the scene point seen behind it along the view ray, in world height (0 at least, 1e9 under open sky): over level ground the depth of liquid there, the same from any camera, for a shallow and a deep tone; over sloped ground or before a wall it moves with the camera. point: the surface's world position.
  • boundary: the share of the four points outline_px screen pixels to either side, across and down, that lie outside the liquid: above 0 in a band that wide inside the body's outline, at any resolution. The surface's shading = { outline_px = 5 } widens it (3 unless set; the band reads about a pixel narrower than the number).
  • silhouette: 1 where the surface faces the camera, toward 0 on rows seen edge-on.
  • normal, toward_sun: the surface normal and the direction toward the sun, in the composite's view frame (z toward the camera).
  • uv: the screen coordinate. params: the surface's responseParams, four vec4s a, b, c, d: each table in the list (a colour { r, g, b } or { r, g, b, a }, or up to four numbers) fills the next of them, each bare number the next of the sixteen, and a number it leaves out reads 0. A palette, band thresholds and an outline colour travel there, so one response module draws many looks.

fluid_luma(c) is the luminance of a colour.

These values are stated in shaders.fluid_types, which this module includes. This module also states FLUID_AUTHORED true: the composite builds a sample and hands it to fluid_shade only in a program drawn with a response.

The colour fluid_shade returns is light, like every colour in the scene: it reaches the screen through the frame's display transform (__display__'s curve, agx unless set), which softens and desaturates a flat palette. A palette meant to read as written takes a neutral curve: postprocess.setProperty("__display__", "curve", "pbrNeutral"), or clamp for the colours exactly.

Cost

A response's program runs everything the engine's own shading runs, then the response. The reflection march is the composite's largest cost; a response that draws no reflection sets the surface's shading = { ssr_int = 0 }, and the march does not run.

A two-tone response with a foam outline

#include "@builtin::systems.fluidSurface.shaders.fluid_shading"

fn fluid_shade(s: FluidSample) -> vec4<f32> {
    // The shallow tone where the ground lies less than a.w below the
    // surface, the deep tone elsewhere.
    var c = select(s.params.b.rgb, s.params.a.rgb, s.depth_below < s.params.a.w);
    // A flat foam line on the outline and the thin edge.
    let line = max(s.boundary, step(s.params.b.w, s.edge));
    c = mix(c, s.params.c.rgb, step(0.01, line));
    // Solid wherever the liquid is.
    return vec4<f32>(c, 1.0);
}
fluidSurface.attach("lake", {
    particles = layout, radius = 0.3,
    response = "@root::looks.toonWater",
    responseParams = { { 0.35, 0.75, 0.95, 0.6 }, { 0.1, 0.35, 0.7, 0.5 }, { 1, 1, 1, 1 } },
    shading = { ssr_int = 0 },
})

fluidSurface.status(name) reads whether the response is drawn; a response that names no module or fails to compile reads failed with the reason, the log says why once, and the surface draws the engine's own shading until the response is edited into one that compiles.