rebuild method

void rebuild({
  1. required Float32List positions,
  2. Float32List? normals,
  3. Float32List? texCoords,
  4. Float32List? colors,
  5. List<int>? indices,
})

Replaces all of this geometry's data, allowing the vertex and index counts to change.

Reuses the existing GPU buffers when the new data fits within their spare capacity, and reallocates with headroom only when it does not. The indexed-or-not state is fixed at construction: a geometry created with indices requires indices here, and one created without must omit them. Throws a StateError unless this geometry is GeometryStorage.updatable.

Implementation

void rebuild({
  required Float32List positions,
  Float32List? normals,
  Float32List? texCoords,
  Float32List? colors,
  List<int>? indices,
}) {
  _ensureUpdatable('rebuild');
  if (positions.length % 3 != 0) {
    throw ArgumentError(
      'positions has ${positions.length} floats; expected a multiple of '
      'three (one vec3 per vertex)',
    );
  }
  if (_indexed && indices == null) {
    throw ArgumentError(
      'This geometry was created with indices; rebuild requires them',
    );
  }
  if (!_indexed && indices != null) {
    throw ArgumentError(
      'This geometry was created without indices; rebuild must not '
      'supply them',
    );
  }
  final vertexCount = positions.length ~/ 3;
  final resolvedNormals =
      normals ??
      (vertexCount > 0 && primitiveType == gpu.PrimitiveType.triangle
          ? InterleavedLayoutAdapter.generateNormals(
            positions: positions,
            vertexCount: vertexCount,
            indices: indices,
          )
          : null);

  _setCpuStreams(positions, vertexCount, resolvedNormals, texCoords, colors);
  if (vertexCount > _vertexCapacity) {
    _vertexCapacity = nextBufferCapacity(vertexCount);
    _vertexBuffer = gpu.gpuContext.createDeviceBuffer(
      gpu.StorageMode.hostVisible,
      _vertexCapacity * kInterleavedVertexBytes,
    );
  }
  _liveVertexCount = vertexCount;
  _uploadVertexBytes();
  if (_indexed) _uploadIndices(indices!);
  _recomputeBounds();
}