src/primitives.js
function createSphereVertices(
    radius,
    subdivisionsAxis,
    subdivisionsHeight,
    opt_startLatitudeInRadians,
    opt_endLatitudeInRadians,
    opt_startLongitudeInRadians,
    opt_endLongitudeInRadians) {
  if (subdivisionsAxis <= 0 || subdivisionsHeight <= 0) {
    throw Error('subdivisionAxis and subdivisionHeight must be > 0');
  }

  opt_startLatitudeInRadians = opt_startLatitudeInRadians || 0;
  opt_endLatitudeInRadians = opt_endLatitudeInRadians || Math.PI;
  opt_startLongitudeInRadians = opt_startLongitudeInRadians || 0;
  opt_endLongitudeInRadians = opt_endLongitudeInRadians || (Math.PI * 2);

  const latRange = opt_endLatitudeInRadians - opt_startLatitudeInRadians;
  const longRange = opt_endLongitudeInRadians - opt_startLongitudeInRadians;

  // We are going to generate our sphere by iterating through its
  // spherical coordinates and generating 2 triangles for each quad on a
  // ring of the sphere.
  const numVertices = (subdivisionsAxis + 1) * (subdivisionsHeight + 1);
  const positions = createAugmentedTypedArray(3, numVertices);
  const normals   = createAugmentedTypedArray(3, numVertices);
  const texcoords = createAugmentedTypedArray(2 , numVertices);

  // Generate the individual vertices in our vertex buffer.
  for (let y = 0; y <= subdivisionsHeight; y++) {
    for (let x = 0; x <= subdivisionsAxis; x++) {
      // Generate a vertex based on its spherical coordinates
      const u = x / subdivisionsAxis;
      const v = y / subdivisionsHeight;
      const theta = longRange * u + opt_startLongitudeInRadians;
      const phi = latRange * v + opt_startLatitudeInRadians;
      const sinTheta = Math.sin(theta);
      const cosTheta = Math.cos(theta);
      const sinPhi = Math.sin(phi);
      const cosPhi = Math.cos(phi);
      const ux = cosTheta * sinPhi;
      const uy = cosPhi;
      const uz = sinTheta * sinPhi;
      positions.push(radius * ux, radius * uy, radius * uz);
      normals.push(ux, uy, uz);
      texcoords.push(1 - u, v);
    }
  }

  const numVertsAround = subdivisionsAxis + 1;
  const indices = createAugmentedTypedArray(3, subdivisionsAxis * subdivisionsHeight * 2, Uint16Array);
  for (let x = 0; x < subdivisionsAxis; x++) {  // eslint-disable-line
    for (let y = 0; y < subdivisionsHeight; y++) {  // eslint-disable-line
      // Make triangle 1 of quad.
      indices.push(
          (y + 0) * numVertsAround + x,
          (y + 0) * numVertsAround + x + 1,
          (y + 1) * numVertsAround + x);

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

  return {
    position: positions,
    normal: normals,
    texcoord: texcoords,
    indices: indices,
  };
}
function createSphereVertices(
    radius,
    subdivisionsAxis,
    subdivisionsHeight,
    opt_startLatitudeInRadians,
    opt_endLatitudeInRadians,
    opt_startLongitudeInRadians,
    opt_endLongitudeInRadians) {
  if (subdivisionsAxis <= 0 || subdivisionsHeight <= 0) {
    throw Error('subdivisionAxis and subdivisionHeight must be > 0');
  }

  opt_startLatitudeInRadians = opt_startLatitudeInRadians || 0;
  opt_endLatitudeInRadians = opt_endLatitudeInRadians || Math.PI;
  opt_startLongitudeInRadians = opt_startLongitudeInRadians || 0;
  opt_endLongitudeInRadians = opt_endLongitudeInRadians || (Math.PI * 2);

  const latRange = opt_endLatitudeInRadians - opt_startLatitudeInRadians;
  const longRange = opt_endLongitudeInRadians - opt_startLongitudeInRadians;

  // We are going to generate our sphere by iterating through its
  // spherical coordinates and generating 2 triangles for each quad on a
  // ring of the sphere.
  const numVertices = (subdivisionsAxis + 1) * (subdivisionsHeight + 1);
  const positions = createAugmentedTypedArray(3, numVertices);
  const normals   = createAugmentedTypedArray(3, numVertices);
  const texcoords = createAugmentedTypedArray(2 , numVertices);

  // Generate the individual vertices in our vertex buffer.
  for (let y = 0; y <= subdivisionsHeight; y++) {
    for (let x = 0; x <= subdivisionsAxis; x++) {
      // Generate a vertex based on its spherical coordinates
      const u = x / subdivisionsAxis;
      const v = y / subdivisionsHeight;
      const theta = longRange * u + opt_startLongitudeInRadians;
      const phi = latRange * v + opt_startLatitudeInRadians;
      const sinTheta = Math.sin(theta);
      const cosTheta = Math.cos(theta);
      const sinPhi = Math.sin(phi);
      const cosPhi = Math.cos(phi);
      const ux = cosTheta * sinPhi;
      const uy = cosPhi;
      const uz = sinTheta * sinPhi;
      positions.push(radius * ux, radius * uy, radius * uz);
      normals.push(ux, uy, uz);
      texcoords.push(1 - u, v);
    }
  }

  const numVertsAround = subdivisionsAxis + 1;
  const indices = createAugmentedTypedArray(3, subdivisionsAxis * subdivisionsHeight * 2, Uint16Array);
  for (let x = 0; x < subdivisionsAxis; x++) {  // eslint-disable-line
    for (let y = 0; y < subdivisionsHeight; y++) {  // eslint-disable-line
      // Make triangle 1 of quad.
      indices.push(
          (y + 0) * numVertsAround + x,
          (y + 0) * numVertsAround + x + 1,
          (y + 1) * numVertsAround + x);

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

  return {
    position: positions,
    normal: normals,
    texcoord: texcoords,
    indices: indices,
  };
}
function createSphereVertices ( radius , subdivisionsAxis , subdivisionsHeight , opt_startLatitudeInRadians , opt_endLatitudeInRadians , opt_startLongitudeInRadians , opt_endLongitudeInRadians ) { if ( subdivisionsAxis <= 0 || subdivisionsHeight <= 0 ) { throw Error ( 'subdivisionAxis and subdivisionHeight must be > 0' ) ; } opt_startLatitudeInRadians = opt_startLatitudeInRadians || 0 ; opt_endLatitudeInRadians = opt_endLatitudeInRadians || Math . PI ; opt_startLongitudeInRadians = opt_startLongitudeInRadians || 0 ; opt_endLongitudeInRadians = opt_endLongitudeInRadians || ( Math . PI * 2 ) ; const latRange = opt_endLatitudeInRadians - opt_startLatitudeInRadians ; const longRange = opt_endLongitudeInRadians - opt_startLongitudeInRadians ; // We are going to generate our sphere by iterating through its // spherical coordinates and generating 2 triangles for each quad on a // ring of the sphere. const numVertices = ( subdivisionsAxis + 1 ) * ( subdivisionsHeight + 1 ) ; const positions = createAugmentedTypedArray ( 3 , numVertices ) ; const normals = createAugmentedTypedArray ( 3 , numVertices ) ; const texcoords = createAugmentedTypedArray ( 2 , numVertices ) ; // Generate the individual vertices in our vertex buffer. for ( let y = 0 ; y <= subdivisionsHeight ; y ++ ) { for ( let x = 0 ; x <= subdivisionsAxis ; x ++ ) { // Generate a vertex based on its spherical coordinates const u = x / subdivisionsAxis ; const v = y / subdivisionsHeight ; const theta = longRange * u + opt_startLongitudeInRadians ; const phi = latRange * v + opt_startLatitudeInRadians ; const sinTheta = Math . sin ( theta ) ; const cosTheta = Math . cos ( theta ) ; const sinPhi = Math . sin ( phi ) ; const cosPhi = Math . cos ( phi ) ; const ux = cosTheta * sinPhi ; const uy = cosPhi ; const uz = sinTheta * sinPhi ; positions . push ( radius * ux , radius * uy , radius * uz ) ; normals . push ( ux , uy , uz ) ; texcoords . push ( 1 - u , v ) ; } } const numVertsAround = subdivisionsAxis + 1 ; const indices = createAugmentedTypedArray ( 3 , subdivisionsAxis * subdivisionsHeight * 2 , Uint16Array ) ; for ( let x = 0 ; x < subdivisionsAxis ; x ++ ) { // eslint-disable-line for ( let y = 0 ; y < subdivisionsHeight ; y ++ ) { // eslint-disable-line // Make triangle 1 of quad. indices . push ( ( y + 0 ) * numVertsAround + x , ( y + 0 ) * numVertsAround + x + 1 , ( y + 1 ) * numVertsAround + x ) ; // Make triangle 2 of quad. indices . push ( ( y + 1 ) * numVertsAround + x , ( y + 0 ) * numVertsAround + x + 1 , ( y + 1 ) * numVertsAround + x + 1 ) ; } } return { position : positions , normal : normals , texcoord : texcoords , indices : indices , } ; }
Creates sphere buffers.

The created sphere has position, normal, and texcoord data

@param {WebGLRenderingContext} gl The WebGLRenderingContext.
@param {number} radius radius of the sphere.
@param {number} subdivisionsAxis number of steps around the sphere.
@param {number} subdivisionsHeight number of vertically on the sphere.
@param {number} [opt_startLatitudeInRadians] where to start the
top of the sphere. Default = 0.
@param {number} [opt_endLatitudeInRadians] Where to end the
bottom of the sphere. Default = Math.PI.
@param {number} [opt_startLongitudeInRadians] where to start
wrapping the sphere. Default = 0.
@param {number} [opt_endLongitudeInRadians] where to end
wrapping the sphere. Default = 2 * Math.PI.
@return {Object.<string, WebGLBuffer>} The created sphere buffers.
@memberOf module:twgl/primitives
@function createSphereBuffers
 
Creates sphere vertices.

The created sphere has position, normal, and texcoord data

@param {number} radius radius of the sphere.
@param {number} subdivisionsAxis number of steps around the sphere.
@param {number} subdivisionsHeight number of vertically on the sphere.
@param {number} [opt_startLatitudeInRadians] where to start the
top of the sphere. Default = 0.
@param {number} [opt_endLatitudeInRadians] Where to end the
bottom of the sphere. Default = Math.PI.
@param {number} [opt_startLongitudeInRadians] where to start
wrapping the sphere. Default = 0.
@param {number} [opt_endLongitudeInRadians] where to end
wrapping the sphere. Default = 2 * Math.PI.
@return {Object.<string, TypedArray>} The created sphere vertices.
@memberOf module:twgl/primitives
Creates sphere buffers .