MeshGradientRenderer


A renderer responsible for tessellating and drawing a 2D mesh gradient.

A mesh gradient is defined by a grid of vertices, where each vertex has a position, color, and four optional Bezier control points (tangents) that define the curvature of the edges connecting neighboring vertices. Colors can be interpolated using either bilinear or bicubic interpolation.

The renderer can be stateful (e.g. on Android) and maintain internal buffers (such as index and vertex buffers) to optimize rendering performance and avoid per-frame allocations.

See also
meshGradient

Summary

Public functions

Unit
DrawScope.draw(
    rows: Int,
    columns: Int,
    positions: FloatArray,
    colors: IntArray,
    leftBezierOffsets: FloatArray?,
    topBezierOffsets: FloatArray?,
    rightBezierOffsets: FloatArray?,
    bottomBezierOffsets: FloatArray?,
    hasBicubicColor: Boolean
)

Renders the mesh gradient onto the given DrawScope.

Cmn

Public functions

DrawScope.draw

fun DrawScope.draw(
    rows: Int,
    columns: Int,
    positions: FloatArray,
    colors: IntArray,
    leftBezierOffsets: FloatArray? = null,
    topBezierOffsets: FloatArray? = null,
    rightBezierOffsets: FloatArray? = null,
    bottomBezierOffsets: FloatArray? = null,
    hasBicubicColor: Boolean = false
): Unit

Renders the mesh gradient onto the given DrawScope.

Parameters
rows: Int

The number of rows in the mesh grid. Must be greater than 0.

columns: Int

The number of columns in the mesh grid. Must be greater than 0.

positions: FloatArray

A flattened FloatArray containing the (x, y) coordinates for each vertex in the mesh. The array must contain at least (rows + 1) * (columns + 1) * 2 elements. The coordinates should be normalized (0.0 to 1.0) relative to the DrawScope.size.

colors: IntArray

A flattened IntArray containing the ARGB colors in sRGB color space for each vertex. The array must contain at least (rows + 1) * (columns + 1) elements.

leftBezierOffsets: FloatArray? = null

Optional flattened FloatArray of (x, y) offsets for the left Bezier control points relative to the vertex position. If null, or if values are Offset.Unspecified, the renderer will infer smooth control points based on neighbors.

topBezierOffsets: FloatArray? = null

Optional FloatArray for top Bezier control point offsets.

rightBezierOffsets: FloatArray? = null

Optional FloatArray for right Bezier control point offsets.

bottomBezierOffsets: FloatArray? = null

Optional FloatArray for bottom Bezier control point offsets.

hasBicubicColor: Boolean = false

Whether to use bicubic interpolation for colors (Catmull-Rom) or bilinear interpolation. Bicubic provides smoother transitions but is more computationally expensive.

Note: If any Bezier offset is Unspecified, the renderer may modify the provided bezier offset arrays to store the inferred values instead of Offset.Unspecified, avoiding redundant calculations in subsequent calls.