Vertices constructor
Creates a set of vertex data for use with Canvas.drawVertices.
The mode
parameter describes how the points should be interpreted: as
independent triangles (VertexMode.triangles), as a sliding window of
points forming a chain of triangles each sharing one side with the next
(VertexMode.triangleStrip), or as a fan of triangles with a single
shared point (VertexMode.triangleFan).
The positions
parameter provides the points in the canvas space that
will be use to draw the triangles.
The colors
parameter, if specified, provides the color for each point in
positions
. Each triangle is painted as a gradient that blends between
the three colors at the three points of that triangle. (These colors are
then blended with the Paint specified in the call to
Canvas.drawVertices.)
The textureCoordinates
parameter, if specified, provides the points in
the Paint image to sample for the corresponding points in positions
.
If the colors
or textureCoordinates
parameters are specified, they must
be the same length as positions
.
The indices
parameter specifies the order in which the points should be
painted. If it is omitted (or present but empty), the points are processed
in the order they are given in positions
, as if the indices
was a list
from 0 to n-1, where n is the number of entries in positions
. The
indices
parameter, if present and non-empty, must have at least three
entries, but may be of any length beyond this. Indicies may refer to
offsets in the positions array multiple times, or may skip positions
entirely.
If the indices
parameter is specified, all values in the list must be
valid index values for positions
.
The mode
and positions
parameters must not be null.
This constructor converts its parameters into dart:typed_data lists (e.g. using Float32Lists for the coordinates) before sending them to the Flutter engine. If the data provided to this constructor is not already in List form, consider using the Vertices.raw constructor instead to avoid converting the data twice.
Implementation
Vertices(
VertexMode mode,
List<Offset> positions, {
List<Color>? colors,
List<Offset>? textureCoordinates,
List<int>? indices,
}) {
if (colors != null && colors.length != positions.length) {
throw ArgumentError('"positions" and "colors" lengths must match.');
}
if (textureCoordinates != null && textureCoordinates.length != positions.length) {
throw ArgumentError('"positions" and "textureCoordinates" lengths must match.');
}
if (indices != null) {
for (int index = 0; index < indices.length; index += 1) {
if (indices[index] >= positions.length) {
throw ArgumentError(
'"indices" values must be valid indices in the positions list '
'(i.e. numbers in the range 0..${positions.length - 1}), '
'but indices[$index] is ${indices[index]}, which is too big.',
);
}
}
}
final Float32List encodedPositions = _encodePointList(positions);
final Float32List? encodedTextureCoordinates = (textureCoordinates != null)
? _encodePointList(textureCoordinates)
: null;
final Int32List? encodedColors = colors != null
? _encodeColorList(colors)
: null;
final Uint16List? encodedIndices = indices != null
? Uint16List.fromList(indices)
: null;
if (!_init(this, mode.index, encodedPositions, encodedTextureCoordinates, encodedColors, encodedIndices)) {
throw ArgumentError('Invalid configuration for vertices.');
}
}