SphereGeometry constructor

SphereGeometry(
  1. double radius,
  2. int subdivisionsAxis,
  3. int subdivisionsHeight, [
  4. double startLatitudeInRadians = 0.0,
  5. double endLatitudeInRadians = math.pi,
  6. double startLongitudeInRadians = 0.0,
  7. double endLongitudeInRadians = (math.pi * 2),
])

Implementation

SphereGeometry(
  this.radius,
  this.subdivisionsAxis,
  this.subdivisionsHeight, [
  this.startLatitudeInRadians = 0.0,
  this.endLatitudeInRadians = math.pi,
  this.startLongitudeInRadians = 0.0,
  this.endLongitudeInRadians = (math.pi * 2),
]) {
  double latRange = endLatitudeInRadians - startLatitudeInRadians;
  double longRange = endLongitudeInRadians - startLongitudeInRadians;

  // int numVertices = (subdivisionsAxis + 1) * (subdivisionsHeight + 1);

  for (int y = 0; y <= subdivisionsHeight; y++) {
    for (int x = 0; x <= subdivisionsAxis; x++) {
      // Generate a vertex based on its spherical coordinates
      var u = x / subdivisionsAxis;
      var v = y / subdivisionsHeight;
      var theta = longRange * u;
      var phi = latRange * v;
      var sinTheta = math.sin(theta);
      var cosTheta = math.cos(theta);
      var sinPhi = math.sin(phi);
      var cosPhi = math.cos(phi);
      var ux = cosTheta * sinPhi;
      var uy = cosPhi;
      var uz = sinTheta * sinPhi;

      // vertices.push([radius * ux, radius * uy, radius * uz]);
      // normals.push([ux, uy, uz]);
      // uvs.push([1 - u, v]);

      vertices.addAll([radius * ux, radius * uy, radius * uz]);
      normals.addAll([ux, uy, uz]);
      uvs.addAll([1 - u, v]);
    }
  }

  int numVertsAround = subdivisionsAxis + 1;

  for (int x = 0; x < subdivisionsAxis; x++) {
    for (int y = 0; y < subdivisionsHeight; y++) {
      // Make triangle 1 of quad.
      indices.addAll([
        (y + 0) * numVertsAround + x,
        (y + 0) * numVertsAround + x + 1,
        (y + 1) * numVertsAround + x,
      ]);

      // Make triangle 2 of quad.
      indices.addAll([
        (y + 1) * numVertsAround + x,
        (y + 0) * numVertsAround + x + 1,
        (y + 1) * numVertsAround + x + 1,
      ]);
    }
  }

  // Set index buffers and attributes.
  setIndex(indices);
  setAttribute('position', Float32BufferAttribute(vertices, 3));
  setAttribute('normal', Float32BufferAttribute(normals, 3));
  setAttribute('uv', Float32BufferAttribute(uvs, 2));
}