ConvexGeometry constructor

ConvexGeometry(
  1. List<Vector3> points
)

Implementation

ConvexGeometry(List<Vector3> points) : super() {
  List<double> vertices = [];
  List<double> normals = [];

  // buffers

  final convexHull = ConvexHull().setFromPoints(points);

  // generate vertices and normals

  final faces = convexHull.faces;

  for (int i = 0; i < faces.length; i++) {
    final face = faces[i];
    HalfEdge? edge = face.edge;

    // we move along a doubly-connected edge list to access all face points (see HalfEdge docs)

    do {
      final point = edge!.head().point;

      vertices.addAll(
          [point.x.toDouble(), point.y.toDouble(), point.z.toDouble()]);
      normals.addAll([
        face.normal.x.toDouble(),
        face.normal.y.toDouble(),
        face.normal.z.toDouble()
      ]);

      edge = edge.next;
    } while (edge != face.edge);
  }

  // build geometry

  setAttribute(Attribute.position,Float32BufferAttribute.fromList(vertices, 3, false));
  setAttribute(Attribute.normal,Float32BufferAttribute.fromList(normals, 3, false));
}