Vertices.raw constructor

Vertices.raw(
  1. VertexMode mode,
  2. Float32List positions, {
  3. Int32List? colors,
  4. Float32List? textureCoordinates,
  5. Uint16List? indices,
})

Creates a set of vertex data for use with Canvas.drawVertices, using the encoding expected by the Flutter engine.

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. Each point is represented as two numbers in the list, the first giving the x coordinate and the second giving the y coordinate. (As a result, the list must have an even number of entries.)

The colors parameter, if specified, provides the color for each point in positions. Each color is represented as ARGB with 8 bit color channels (like Color.value's internal representation), and the list, if specified, must therefore be half the length of 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. Each point is represented as two numbers in the list, the first giving the x coordinate and the second giving the y coordinate. This list, if specified, 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-2, where n is the number of pairs in positions (i.e. half the length of 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 pairs in positions. For example, if there are 12 numbers in positions (representing 6 coordinates), the indicies must be numbers in the range 0..5 inclusive.

The mode and positions parameters must not be null.

Implementation

Vertices.raw(
  VertexMode mode,
  Float32List positions, {
  Int32List? colors,
  Float32List? textureCoordinates,
  Uint16List? indices,
}) {
  if (positions.length % 2 != 0) {
    throw ArgumentError('"positions" must have an even number of entries (each coordinate is an x,y pair).');
  }
  if (colors != null && colors.length * 2 != 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.');
  }
  assert(() {
    if (indices != null) {
      for (int index = 0; index < indices.length; index += 1) {
        if (indices[index] * 2 >= positions.length) {
          throw ArgumentError(
            '"indices" values must be valid indices in the positions list '
            '(i.e. numbers in the range 0..${positions.length ~/ 2 - 1}), '
            'but indices[$index] is ${indices[index]}, which is too big.',
          );
        }
      }
    }
    return true;
  }());
  if (!_init(this, mode.index, positions, textureCoordinates, colors, indices)) {
    throw ArgumentError('Invalid configuration for vertices.');
  }
}