createGeometry method

MeshGeometry createGeometry({
  1. GeometryGeneratorFlags? flags,
  2. List<GeometryFilter>? filters,
})

Implementation

MeshGeometry createGeometry(
    {GeometryGeneratorFlags? flags, List<GeometryFilter>? filters}) {
  flags ??= GeometryGeneratorFlags();

  VertexAttrib positionAttrib;
  VertexAttrib texCoordAttrib;
  VertexAttrib normalAttrib;
  VertexAttrib tangentAttrib;

  Vector2List? texCoordView;
  Vector3List? positionView;
  Vector3List? normalView;
  Vector4List tangentView;

  final attribs = <VertexAttrib>[];

  positionAttrib = VertexAttrib('POSITION', 3, 'float');
  attribs.add(positionAttrib);

  if (flags.texCoords || flags.tangents) {
    texCoordAttrib = VertexAttrib('TEXCOORD0', 2, 'float');
    attribs.add(texCoordAttrib);
  }

  if (flags.normals || flags.tangents) {
    normalAttrib = VertexAttrib('NORMAL', 3, 'float');
    attribs.add(normalAttrib);
  }

  if (flags.tangents) {
    tangentAttrib = VertexAttrib('TANGENT', 4, 'float');
    attribs.add(tangentAttrib);
  }

  var mesh = MeshGeometry(vertexCount, attribs)
    ..indices = Uint16List(indexCount);
  generateIndices(mesh.indices!);

  var view = mesh.getViewForAttrib('POSITION');
  if (view is Vector3List) {
    positionView = view;
    generateVertexPositions(positionView, mesh.indices!);
  }

  if (flags.texCoords || flags.tangents) {
    view = mesh.getViewForAttrib('TEXCOORD0');
    if (view is Vector2List) {
      texCoordView = view;
      generateVertexTexCoords(texCoordView, positionView!, mesh.indices!);
    }
  }

  if (flags.normals || flags.tangents) {
    view = mesh.getViewForAttrib('NORMAL');
    if (view is Vector3List) {
      normalView = view;
      generateVertexNormals(normalView, positionView!, mesh.indices!);
    }
  }

  if (flags.tangents) {
    view = mesh.getViewForAttrib('TANGENT');
    if (view is Vector4List) {
      tangentView = view;
      generateVertexTangents(tangentView, positionView!, normalView!,
          texCoordView!, mesh.indices!);
    }
  }

  if (filters != null) {
    for (var filter in filters) {
      if (filter.inplace && filter is InplaceGeometryFilter) {
        filter.filterInplace(mesh);
      } else {
        mesh = filter.filter(mesh);
      }
    }
  }

  return mesh;
}