src/primitives.js
function createCubeVertices(size) {
  size = size || 1;
  const k = size / 2;

  const cornerVertices = [
    [-k, -k, -k],
    [+k, -k, -k],
    [-k, +k, -k],
    [+k, +k, -k],
    [-k, -k, +k],
    [+k, -k, +k],
    [-k, +k, +k],
    [+k, +k, +k],
  ];

  const faceNormals = [
    [+1, +0, +0],
    [-1, +0, +0],
    [+0, +1, +0],
    [+0, -1, +0],
    [+0, +0, +1],
    [+0, +0, -1],
  ];

  const uvCoords = [
    [1, 0],
    [0, 0],
    [0, 1],
    [1, 1],
  ];

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

  for (let f = 0; f < 6; ++f) {
    const faceIndices = CUBE_FACE_INDICES[f];
    for (let v = 0; v < 4; ++v) {
      const position = cornerVertices[faceIndices[v]];
      const normal = faceNormals[f];
      const uv = uvCoords[v];

      // Each face needs all four vertices because the normals and texture
      // coordinates are not all the same.
      positions.push(position);
      normals.push(normal);
      texcoords.push(uv);

    }
    // Two triangles make a square face.
    const offset = 4 * f;
    indices.push(offset + 0, offset + 1, offset + 2);
    indices.push(offset + 0, offset + 2, offset + 3);
  }

  return {
    position: positions,
    normal: normals,
    texcoord: texcoords,
    indices: indices,
  };
}
function createCubeVertices(size) {
  size = size || 1;
  const k = size / 2;

  const cornerVertices = [
    [-k, -k, -k],
    [+k, -k, -k],
    [-k, +k, -k],
    [+k, +k, -k],
    [-k, -k, +k],
    [+k, -k, +k],
    [-k, +k, +k],
    [+k, +k, +k],
  ];

  const faceNormals = [
    [+1, +0, +0],
    [-1, +0, +0],
    [+0, +1, +0],
    [+0, -1, +0],
    [+0, +0, +1],
    [+0, +0, -1],
  ];

  const uvCoords = [
    [1, 0],
    [0, 0],
    [0, 1],
    [1, 1],
  ];

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

  for (let f = 0; f < 6; ++f) {
    const faceIndices = CUBE_FACE_INDICES[f];
    for (let v = 0; v < 4; ++v) {
      const position = cornerVertices[faceIndices[v]];
      const normal = faceNormals[f];
      const uv = uvCoords[v];

      // Each face needs all four vertices because the normals and texture
      // coordinates are not all the same.
      positions.push(position);
      normals.push(normal);
      texcoords.push(uv);

    }
    // Two triangles make a square face.
    const offset = 4 * f;
    indices.push(offset + 0, offset + 1, offset + 2);
    indices.push(offset + 0, offset + 2, offset + 3);
  }

  return {
    position: positions,
    normal: normals,
    texcoord: texcoords,
    indices: indices,
  };
}
function createCubeVertices ( size ) { size = size || 1 ; const k = size / 2 ; const cornerVertices = [ [ - k , - k , - k ] , [ + k , - k , - k ] , [ - k , + k , - k ] , [ + k , + k , - k ] , [ - k , - k , + k ] , [ + k , - k , + k ] , [ - k , + k , + k ] , [ + k , + k , + k ] , ] ; const faceNormals = [ [ + 1 , + 0 , + 0 ] , [ - 1 , + 0 , + 0 ] , [ + 0 , + 1 , + 0 ] , [ + 0 , - 1 , + 0 ] , [ + 0 , + 0 , + 1 ] , [ + 0 , + 0 , - 1 ] , ] ; const uvCoords = [ [ 1 , 0 ] , [ 0 , 0 ] , [ 0 , 1 ] , [ 1 , 1 ] , ] ; const numVertices = 6 * 4 ; const positions = createAugmentedTypedArray ( 3 , numVertices ) ; const normals = createAugmentedTypedArray ( 3 , numVertices ) ; const texcoords = createAugmentedTypedArray ( 2 , numVertices ) ; const indices = createAugmentedTypedArray ( 3 , 6 * 2 , Uint16Array ) ; for ( let f = 0 ; f < 6 ; ++ f ) { const faceIndices = CUBE_FACE_INDICES [ f ] ; for ( let v = 0 ; v < 4 ; ++ v ) { const position = cornerVertices [ faceIndices [ v ] ] ; const normal = faceNormals [ f ] ; const uv = uvCoords [ v ] ; // Each face needs all four vertices because the normals and texture // coordinates are not all the same. positions . push ( position ) ; normals . push ( normal ) ; texcoords . push ( uv ) ; } // Two triangles make a square face. const offset = 4 * f ; indices . push ( offset + 0 , offset + 1 , offset + 2 ) ; indices . push ( offset + 0 , offset + 2 , offset + 3 ) ; } return { position : positions , normal : normals , texcoord : texcoords , indices : indices , } ; }
Creates the buffers and indices for a cube.

The cube is created around the origin. (-size / 2, size / 2).

@param {WebGLRenderingContext} gl The WebGLRenderingContext.
@param {number} [size] width, height and depth of the cube.
@return {Object.<string, WebGLBuffer>} The created buffers.
@memberOf module:twgl/primitives
@function createCubeBuffers
 
Creates the vertices and indices for a cube.

The cube is created around the origin. (-size / 2, size / 2).

@param {number} [size] width, height and depth of the cube.
@return {Object.<string, TypedArray>} The created vertices.
@memberOf module:twgl/primitives
Creates the buffers and indices for a cube .