Surface constructor

Surface({
  1. required List<Vertex> vertices,
  2. required List<int> indices,
  3. Material? material,
  4. Map<int, int>? jointMap,
  5. bool calculateNormals = true,
})

Base surface Resource, it describes a single surface to be rendered.

Implementation

Surface({
  required List<Vertex> vertices,
  required List<int> indices,
  Material? material,
  this.jointMap,
  /**
   * If `true`, the normals will be calculated if they are not provided.
   */
  bool calculateNormals = true,
}) : material = material ?? Material.defaultMaterial {
  final normalizedVertices = _normalize(
    vertices: vertices,
    indices: indices,
    calculateNormals: calculateNormals,
  );
  // `TODO`(bdero): This should have an attribute map instead and be fully SoA
  // but vertex attributes in Impeller aren't flexible enough yet.
  // See also https://github.com/flutter/flutter/issues/116168.
  _vertices = Float32List.fromList(
    normalizedVertices.fold([], (p, v) => p..addAll(v.storage)),
  ).buffer;
  _vertexCount = normalizedVertices.length;

  final positions = Float32List(normalizedVertices.length * 3);
  for (var i = 0; i < normalizedVertices.length; i++) {
    final p = normalizedVertices[i].position;
    positions[i * 3] = p.x;
    positions[i * 3 + 1] = p.y;
    positions[i * 3 + 2] = p.z;
  }
  this.positions = positions;

  final indexList = Uint16List.fromList(indices);
  _indices = indexList.buffer;
  _indexCount = indices.length;
  this.indices = indexList;

  _calculateAabb(normalizedVertices);
}