src/primitives.js
function createDiscVertices(
    radius,
    divisions,
    stacks,
    innerRadius,
    stackPower) {
  if (divisions < 3) {
    throw Error('divisions must be at least 3');
  }

  stacks = stacks ? stacks : 1;
  stackPower = stackPower ? stackPower : 1;
  innerRadius = innerRadius ? innerRadius : 0;

  // Note: We don't share the center vertex because that would
  // mess up texture coordinates.
  const numVertices = (divisions + 1) * (stacks + 1);

  const positions = createAugmentedTypedArray(3, numVertices);
  const normals   = createAugmentedTypedArray(3, numVertices);
  const texcoords = createAugmentedTypedArray(2, numVertices);
  const indices   = createAugmentedTypedArray(3, stacks * divisions * 2, Uint16Array);

  let firstIndex = 0;
  const radiusSpan = radius - innerRadius;
  const pointsPerStack = divisions + 1;

  // Build the disk one stack at a time.
  for (let stack = 0; stack <= stacks; ++stack) {
    const stackRadius = innerRadius + radiusSpan * Math.pow(stack / stacks, stackPower);

    for (let i = 0; i <= divisions; ++i) {
      const theta = 2.0 * Math.PI * i / divisions;
      const x = stackRadius * Math.cos(theta);
      const z = stackRadius * Math.sin(theta);

      positions.push(x, 0, z);
      normals.push(0, 1, 0);
      texcoords.push(1 - (i / divisions), stack / stacks);
      if (stack > 0 && i !== divisions) {
        // a, b, c and d are the indices of the vertices of a quad.  unless
        // the current stack is the one closest to the center, in which case
        // the vertices a and b connect to the center vertex.
        const a = firstIndex + (i + 1);
        const b = firstIndex + i;
        const c = firstIndex + i - pointsPerStack;
        const d = firstIndex + (i + 1) - pointsPerStack;

        // Make a quad of the vertices a, b, c, d.
        indices.push(a, b, c);
        indices.push(a, c, d);
      }
    }

    firstIndex += divisions + 1;
  }

  return {
    position: positions,
    normal: normals,
    texcoord: texcoords,
    indices: indices,
  };
}
function createDiscVertices(
    radius,
    divisions,
    stacks,
    innerRadius,
    stackPower) {
  if (divisions < 3) {
    throw Error('divisions must be at least 3');
  }

  stacks = stacks ? stacks : 1;
  stackPower = stackPower ? stackPower : 1;
  innerRadius = innerRadius ? innerRadius : 0;

  // Note: We don't share the center vertex because that would
  // mess up texture coordinates.
  const numVertices = (divisions + 1) * (stacks + 1);

  const positions = createAugmentedTypedArray(3, numVertices);
  const normals   = createAugmentedTypedArray(3, numVertices);
  const texcoords = createAugmentedTypedArray(2, numVertices);
  const indices   = createAugmentedTypedArray(3, stacks * divisions * 2, Uint16Array);

  let firstIndex = 0;
  const radiusSpan = radius - innerRadius;
  const pointsPerStack = divisions + 1;

  // Build the disk one stack at a time.
  for (let stack = 0; stack <= stacks; ++stack) {
    const stackRadius = innerRadius + radiusSpan * Math.pow(stack / stacks, stackPower);

    for (let i = 0; i <= divisions; ++i) {
      const theta = 2.0 * Math.PI * i / divisions;
      const x = stackRadius * Math.cos(theta);
      const z = stackRadius * Math.sin(theta);

      positions.push(x, 0, z);
      normals.push(0, 1, 0);
      texcoords.push(1 - (i / divisions), stack / stacks);
      if (stack > 0 && i !== divisions) {
        // a, b, c and d are the indices of the vertices of a quad.  unless
        // the current stack is the one closest to the center, in which case
        // the vertices a and b connect to the center vertex.
        const a = firstIndex + (i + 1);
        const b = firstIndex + i;
        const c = firstIndex + i - pointsPerStack;
        const d = firstIndex + (i + 1) - pointsPerStack;

        // Make a quad of the vertices a, b, c, d.
        indices.push(a, b, c);
        indices.push(a, c, d);
      }
    }

    firstIndex += divisions + 1;
  }

  return {
    position: positions,
    normal: normals,
    texcoord: texcoords,
    indices: indices,
  };
}
function createDiscVertices ( radius , divisions , stacks , innerRadius , stackPower ) { if ( divisions < 3 ) { throw Error ( 'divisions must be at least 3' ) ; } stacks = stacks ? stacks : 1 ; stackPower = stackPower ? stackPower : 1 ; innerRadius = innerRadius ? innerRadius : 0 ; // Note: We don't share the center vertex because that would // mess up texture coordinates. const numVertices = ( divisions + 1 ) * ( stacks + 1 ) ; const positions = createAugmentedTypedArray ( 3 , numVertices ) ; const normals = createAugmentedTypedArray ( 3 , numVertices ) ; const texcoords = createAugmentedTypedArray ( 2 , numVertices ) ; const indices = createAugmentedTypedArray ( 3 , stacks * divisions * 2 , Uint16Array ) ; let firstIndex = 0 ; const radiusSpan = radius - innerRadius ; const pointsPerStack = divisions + 1 ; // Build the disk one stack at a time. for ( let stack = 0 ; stack <= stacks ; ++ stack ) { const stackRadius = innerRadius + radiusSpan * Math . pow ( stack / stacks , stackPower ) ; for ( let i = 0 ; i <= divisions ; ++ i ) { const theta = 2.0 * Math . PI * i / divisions ; const x = stackRadius * Math . cos ( theta ) ; const z = stackRadius * Math . sin ( theta ) ; positions . push ( x , 0 , z ) ; normals . push ( 0 , 1 , 0 ) ; texcoords . push ( 1 - ( i / divisions ) , stack / stacks ) ; if ( stack > 0 && i !== divisions ) { // a, b, c and d are the indices of the vertices of a quad.  unless // the current stack is the one closest to the center, in which case // the vertices a and b connect to the center vertex. const a = firstIndex + ( i + 1 ) ; const b = firstIndex + i ; const c = firstIndex + i - pointsPerStack ; const d = firstIndex + ( i + 1 ) - pointsPerStack ; // Make a quad of the vertices a, b, c, d. indices . push ( a , b , c ) ; indices . push ( a , c , d ) ; } } firstIndex += divisions + 1 ; } return { position : positions , normal : normals , texcoord : texcoords , indices : indices , } ; }
Creates disc buffers. The disc will be in the xz plane, centered at
the origin. When creating, at least 3 divisions, or pie
pieces, need to be specified, otherwise the triangles making
up the disc will be degenerate. You can also specify the
number of radial pieces `stacks`. A value of 1 for
stacks will give you a simple disc of pie pieces.  If you
want to create an annulus you can set `innerRadius` to a
value > 0. Finally, `stackPower` allows you to have the widths
increase or decrease as you move away from the center. This
is particularly useful when using the disc as a ground plane
with a fixed camera such that you don't need the resolution
of small triangles near the perimeter. For example, a value
of 2 will produce stacks whose ouside radius increases with
the square of the stack index. A value of 1 will give uniform
stacks.

@param {WebGLRenderingContext} gl The WebGLRenderingContext.
@param {number} radius Radius of the ground plane.
@param {number} divisions Number of triangles in the ground plane (at least 3).
@param {number} [stacks] Number of radial divisions (default=1).
@param {number} [innerRadius] Default 0.
@param {number} [stackPower] Power to raise stack size to for decreasing width.
@return {Object.<string, WebGLBuffer>} The created buffers.
@memberOf module:twgl/primitives
@function createDiscBuffers
 
Creates disc vertices. The disc will be in the xz plane, centered at
the origin. When creating, at least 3 divisions, or pie
pieces, need to be specified, otherwise the triangles making
up the disc will be degenerate. You can also specify the
number of radial pieces `stacks`. A value of 1 for
stacks will give you a simple disc of pie pieces.  If you
want to create an annulus you can set `innerRadius` to a
value > 0. Finally, `stackPower` allows you to have the widths
increase or decrease as you move away from the center. This
is particularly useful when using the disc as a ground plane
with a fixed camera such that you don't need the resolution
of small triangles near the perimeter. For example, a value
of 2 will produce stacks whose ouside radius increases with
the square of the stack index. A value of 1 will give uniform
stacks.

@param {number} radius Radius of the ground plane.
@param {number} divisions Number of triangles in the ground plane (at least 3).
@param {number} [stacks] Number of radial divisions (default=1).
@param {number} [innerRadius] Default 0.
@param {number} [stackPower] Power to raise stack size to for decreasing width.
@return {Object.<string, TypedArray>} The created vertices.
@memberOf module:twgl/primitives
Creates disc buffers . The disc will be in the xz plane centered at the origin . When creating at least 3 divisions or pie pieces need to be specified otherwise the triangles making up the disc will be degenerate . You can also specify the number of radial pieces stacks . A value of 1 for stacks will give you a simple disc of pie pieces . If you want to create an annulus you can set innerRadius to a value > 0 . Finally stackPower allows you to have the widths increase or decrease as you move away from the center . This is particularly useful when using the disc as a ground plane with a fixed camera such that you don t need the resolution of small triangles near the perimeter . For example a value of 2 will produce stacks whose ouside radius increases with the square of the stack index . A value of 1 will give uniform stacks .