twgl/primitives

Various functions to make simple primitives

note: Most primitive functions come in 3 styles

  • createSomeShapeBufferInfo

    These functions are almost always the functions you want to call. They
    create vertices then make WebGLBuffers and create module:twgl.AttribInfos
    returning a module:twgl.BufferInfo you can pass to module:twgl.setBuffersAndAttributes
    and module:twgl.drawBufferInfo etc...

  • createSomeShapeBuffers

    These create WebGLBuffers and put your data in them but nothing else.
    It's a shortcut to doing it yourself if you don't want to use
    the higher level functions.

  • createSomeShapeVertices

    These just create vertices, no buffers. This allows you to manipulate the vertices
    or add more data before generating a module:twgl.BufferInfo. Once you're finished
    manipulating the vertices call module:twgl.createBufferInfoFromArrays.

    example:

    const arrays = twgl.primitives.createPlaneVertices(1);
    twgl.primitives.reorientVertices(arrays, m4.rotationX(Math.PI * 0.5));
    const bufferInfo = twgl.createBufferInfoFromArrays(gl, arrays);

Methods

(static) concatVertices(arrays) → {module:twgl.Arrays}

Concatenates sets of vertices

Assumes the vertices match in composition. For example
if one set of vertices has positions, normals, and indices
all sets of vertices must have positions, normals, and indices
and of the same type.

Example:

 const cubeVertices = twgl.primitives.createCubeVertices(2);
 const sphereVertices = twgl.primitives.createSphereVertices(1, 10, 10);
 // move the sphere 2 units up
 twgl.primitives.reorientVertices(
     sphereVertices, twgl.m4.translation([0, 2, 0]));
 // merge the sphere with the cube
 const cubeSphereVertices = twgl.primitives.concatVertices(
     [cubeVertices, sphereVertices]);
 // turn them into WebGL buffers and attrib data
 const bufferInfo = twgl.createBufferInfoFromArrays(gl, cubeSphereVertices);
Parameters:
Name Type Description
arrays Array.<module:twgl.Arrays>

Array of arrays of vertices

Returns:
Type:
module:twgl.Arrays

The concatenated vertices.

(static) create3DFBufferInfo(gl) → {module:twgl.BufferInfo}

Creates 3D 'F' BufferInfo.
An 'F' is useful because you can easily tell which way it is oriented.
The created 'F' has position, normal, texcoord, and color buffers.

Parameters:
Name Type Description
gl WebGLRenderingContext

The WebGLRenderingContext.

Returns:
Type:
module:twgl.BufferInfo

The created BufferInfo.

(static) create3DFBuffers(gl) → {Object.<string, WebGLBuffer>}

Creates 3D 'F' buffers.
An 'F' is useful because you can easily tell which way it is oriented.
The created 'F' has position, normal, texcoord, and color buffers.

Parameters:
Name Type Description
gl WebGLRenderingContext

The WebGLRenderingContext.

Returns:
Type:
Object.<string, WebGLBuffer>

The created buffers.

(static) create3DFVertices() → {Object.<string, TypedArray>}

Creates 3D 'F' vertices.
An 'F' is useful because you can easily tell which way it is oriented.
The created 'F' has position, normal, texcoord, and color arrays.

Returns:
Type:
Object.<string, TypedArray>

The created vertices.

(static) createAugmentedTypedArray(numComponents, numElements, opt_type) → {ArrayBufferView}

creates a typed array with a push function attached
so that you can easily push values.

push can take multiple arguments. If an argument is an array each element
of the array will be added to the typed array.

Example:

const array = createAugmentedTypedArray(3, 2);  // creates a Float32Array with 6 values
array.push(1, 2, 3);
array.push([4, 5, 6]);
// array now contains [1, 2, 3, 4, 5, 6]

Also has numComponents and numElements properties.

Parameters:
Name Type Description
numComponents number

number of components

numElements number

number of elements. The total size of the array will be numComponents * numElements.

opt_type constructor

A constructor for the type. Default = Float32Array.

Returns:
Type:
ArrayBufferView

A typed array.

(static) createCrescentBufferInfo(gl, verticalRadius, outerRadius, innerRadius, thickness, subdivisionsDown, startOffsetopt, endOffsetopt) → {module:twgl.BufferInfo}

Creates crescent BufferInfo.

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

verticalRadius number

The vertical radius of the crescent.

outerRadius number

The outer radius of the crescent.

innerRadius number

The inner radius of the crescent.

thickness number

The thickness of the crescent.

subdivisionsDown number

number of steps around the crescent.

startOffset number <optional>

Where to start arc. Default 0.

endOffset number <optional>

Where to end arg. Default 1.

Returns:
Type:
module:twgl.BufferInfo

The created BufferInfo.

(static) createCrescentBuffers(gl, verticalRadius, outerRadius, innerRadius, thickness, subdivisionsDown, startOffsetopt, endOffsetopt) → {Object.<string, WebGLBuffer>}

Creates crescent buffers.

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

verticalRadius number

The vertical radius of the crescent.

outerRadius number

The outer radius of the crescent.

innerRadius number

The inner radius of the crescent.

thickness number

The thickness of the crescent.

subdivisionsDown number

number of steps around the crescent.

startOffset number <optional>

Where to start arc. Default 0.

endOffset number <optional>

Where to end arg. Default 1.

Returns:
Type:
Object.<string, WebGLBuffer>

The created buffers.

(static) createCrescentVertices(verticalRadius, outerRadius, innerRadius, thickness, subdivisionsDown, startOffsetopt, endOffsetopt) → {Object.<string, TypedArray>}

Creates crescent vertices.

Parameters:
Name Type Attributes Description
verticalRadius number

The vertical radius of the crescent.

outerRadius number

The outer radius of the crescent.

innerRadius number

The inner radius of the crescent.

thickness number

The thickness of the crescent.

subdivisionsDown number

number of steps around the crescent.

startOffset number <optional>

Where to start arc. Default 0.

endOffset number <optional>

Where to end arg. Default 1.

Returns:
Type:
Object.<string, TypedArray>

The created vertices.

(static) createCresentBufferInfo(gl, verticalRadius, outerRadius, innerRadius, thickness, subdivisionsDown, startOffsetopt, endOffsetopt) → {module:twgl.BufferInfo}

Creates crescent BufferInfo.

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

verticalRadius number

The vertical radius of the crescent.

outerRadius number

The outer radius of the crescent.

innerRadius number

The inner radius of the crescent.

thickness number

The thickness of the crescent.

subdivisionsDown number

number of steps around the crescent.

startOffset number <optional>

Where to start arc. Default 0.

endOffset number <optional>

Where to end arg. Default 1.

Returns:
Type:
module:twgl.BufferInfo

The created BufferInfo.

(static) createCresentBuffers(gl, verticalRadius, outerRadius, innerRadius, thickness, subdivisionsDown, startOffsetopt, endOffsetopt) → {Object.<string, WebGLBuffer>}

Creates crescent buffers.

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

verticalRadius number

The vertical radius of the crescent.

outerRadius number

The outer radius of the crescent.

innerRadius number

The inner radius of the crescent.

thickness number

The thickness of the crescent.

subdivisionsDown number

number of steps around the crescent.

startOffset number <optional>

Where to start arc. Default 0.

endOffset number <optional>

Where to end arg. Default 1.

Returns:
Type:
Object.<string, WebGLBuffer>

The created buffers.

(static) createCresentBuffers(verticalRadius, outerRadius, innerRadius, thickness, subdivisionsDown, startOffsetopt, endOffsetopt) → {Object.<string, TypedArray>}

Creates crescent vertices.

Parameters:
Name Type Attributes Description
verticalRadius number

The vertical radius of the crescent.

outerRadius number

The outer radius of the crescent.

innerRadius number

The inner radius of the crescent.

thickness number

The thickness of the crescent.

subdivisionsDown number

number of steps around the crescent.

startOffset number <optional>

Where to start arc. Default 0.

endOffset number <optional>

Where to end arg. Default 1.

Returns:
Type:
Object.<string, TypedArray>

The created vertices.

(static) createCubeBufferInfo(gl, sizeopt) → {module:twgl.BufferInfo}

Creates a BufferInfo for a cube.

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

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

size number <optional>

width, height and depth of the cube.

Returns:
Type:
module:twgl.BufferInfo

The created BufferInfo.

(static) createCubeBuffers(gl, sizeopt) → {Object.<string, WebGLBuffer>}

Creates the buffers and indices for a cube.

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

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

size number <optional>

width, height and depth of the cube.

Returns:
Type:
Object.<string, WebGLBuffer>

The created buffers.

(static) createCubeVertices(sizeopt) → {Object.<string, TypedArray>}

Creates the vertices and indices for a cube.

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

Parameters:
Name Type Attributes Description
size number <optional>

width, height and depth of the cube.

Returns:
Type:
Object.<string, TypedArray>

The created vertices.

(static) createCylinderBufferInfo(gl, radius, height, radialSubdivisions, verticalSubdivisions, topCapopt, bottomCapopt) → {module:twgl.BufferInfo}

Creates cylinder BufferInfo. The cylinder will be created around the origin
along the y-axis.

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

radius number

Radius of cylinder.

height number

Height of cylinder.

radialSubdivisions number

The number of subdivisions around the cylinder.

verticalSubdivisions number

The number of subdivisions down the cylinder.

topCap boolean <optional>

Create top cap. Default = true.

bottomCap boolean <optional>

Create bottom cap. Default = true.

Returns:
Type:
module:twgl.BufferInfo

The created BufferInfo.

(static) createCylinderBuffers(gl, radius, height, radialSubdivisions, verticalSubdivisions, topCapopt, bottomCapopt) → {Object.<string, WebGLBuffer>}

Creates cylinder buffers. The cylinder will be created around the origin
along the y-axis.

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

radius number

Radius of cylinder.

height number

Height of cylinder.

radialSubdivisions number

The number of subdivisions around the cylinder.

verticalSubdivisions number

The number of subdivisions down the cylinder.

topCap boolean <optional>

Create top cap. Default = true.

bottomCap boolean <optional>

Create bottom cap. Default = true.

Returns:
Type:
Object.<string, WebGLBuffer>

The created buffers.

(static) createCylinderVertices(radius, height, radialSubdivisions, verticalSubdivisions, topCapopt, bottomCapopt) → {Object.<string, TypedArray>}

Creates cylinder vertices. The cylinder will be created around the origin
along the y-axis.

Parameters:
Name Type Attributes Description
radius number

Radius of cylinder.

height number

Height of cylinder.

radialSubdivisions number

The number of subdivisions around the cylinder.

verticalSubdivisions number

The number of subdivisions down the cylinder.

topCap boolean <optional>

Create top cap. Default = true.

bottomCap boolean <optional>

Create bottom cap. Default = true.

Returns:
Type:
Object.<string, TypedArray>

The created vertices.

(static) createDiscBufferInfo(gl, radius, divisions, stacksopt, innerRadiusopt, stackPoweropt) → {module:twgl.BufferInfo}

Creates a disc BufferInfo. 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 outside radius increases with
the square of the stack index. A value of 1 will give uniform
stacks.

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

radius number

Radius of the ground plane.

divisions number

Number of triangles in the ground plane (at least 3).

stacks number <optional>

Number of radial divisions (default=1).

innerRadius number <optional>

Default 0.

stackPower number <optional>

Power to raise stack size to for decreasing width.

Returns:
Type:
module:twgl.BufferInfo

The created BufferInfo.

(static) createDiscBuffers(gl, radius, divisions, stacksopt, innerRadiusopt, stackPoweropt) → {Object.<string, WebGLBuffer>}

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 outside radius increases with
the square of the stack index. A value of 1 will give uniform
stacks.

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

radius number

Radius of the ground plane.

divisions number

Number of triangles in the ground plane (at least 3).

stacks number <optional>

Number of radial divisions (default=1).

innerRadius number <optional>

Default 0.

stackPower number <optional>

Power to raise stack size to for decreasing width.

Returns:
Type:
Object.<string, WebGLBuffer>

The created buffers.

(static) createDiscVertices(radius, divisions, stacksopt, innerRadiusopt, stackPoweropt) → {Object.<string, TypedArray>}

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 outside radius increases with
the square of the stack index. A value of 1 will give uniform
stacks.

Parameters:
Name Type Attributes Description
radius number

Radius of the ground plane.

divisions number

Number of triangles in the ground plane (at least 3).

stacks number <optional>

Number of radial divisions (default=1).

innerRadius number <optional>

Default 0.

stackPower number <optional>

Power to raise stack size to for decreasing width.

Returns:
Type:
Object.<string, TypedArray>

The created vertices.

(static) createPlaneBufferInfo(gl, widthopt, depthopt, subdivisionsWidthopt, subdivisionsDepthopt, matrixopt) → {module:twgl.BufferInfo}

Creates XZ plane BufferInfo.

The created plane has position, normal, and texcoord data

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

width number <optional>

Width of the plane. Default = 1

depth number <optional>

Depth of the plane. Default = 1

subdivisionsWidth number <optional>

Number of steps across the plane. Default = 1

subdivisionsDepth number <optional>

Number of steps down the plane. Default = 1

matrix module:twgl/m4.Mat4 <optional>

A matrix by which to multiply all the vertices.

Returns:
Type:
module:twgl.BufferInfo

The created plane BufferInfo.

(static) createPlaneBuffers(gl, widthopt, depthopt, subdivisionsWidthopt, subdivisionsDepthopt, matrixopt) → {Object.<string, WebGLBuffer>}

Creates XZ plane buffers.

The created plane has position, normal, and texcoord data

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

width number <optional>

Width of the plane. Default = 1

depth number <optional>

Depth of the plane. Default = 1

subdivisionsWidth number <optional>

Number of steps across the plane. Default = 1

subdivisionsDepth number <optional>

Number of steps down the plane. Default = 1

matrix module:twgl/m4.Mat4 <optional>

A matrix by which to multiply all the vertices.

Returns:
Type:
Object.<string, WebGLBuffer>

The created plane buffers.

(static) createPlaneVertices(widthopt, depthopt, subdivisionsWidthopt, subdivisionsDepthopt, matrixopt) → {Object.<string, TypedArray>}

Creates XZ plane vertices.

The created plane has position, normal, and texcoord data

Parameters:
Name Type Attributes Description
width number <optional>

Width of the plane. Default = 1

depth number <optional>

Depth of the plane. Default = 1

subdivisionsWidth number <optional>

Number of steps across the plane. Default = 1

subdivisionsDepth number <optional>

Number of steps down the plane. Default = 1

matrix module:twgl/m4.Mat4 <optional>

A matrix by which to multiply all the vertices.

Returns:
Type:
Object.<string, TypedArray>

The created plane vertices.

(static) createSphereBufferInfo(gl, radius, subdivisionsAxis, subdivisionsHeight, opt_startLatitudeInRadiansopt, opt_endLatitudeInRadiansopt, opt_startLongitudeInRadiansopt, opt_endLongitudeInRadiansopt) → {module:twgl.BufferInfo}

Creates sphere BufferInfo.

The created sphere has position, normal, and texcoord data

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

radius number

radius of the sphere.

subdivisionsAxis number

number of steps around the sphere.

subdivisionsHeight number

number of vertically on the sphere.

opt_startLatitudeInRadians number <optional>

where to start the
top of the sphere. Default = 0.

opt_endLatitudeInRadians number <optional>

Where to end the
bottom of the sphere. Default = Math.PI.

opt_startLongitudeInRadians number <optional>

where to start
wrapping the sphere. Default = 0.

opt_endLongitudeInRadians number <optional>

where to end
wrapping the sphere. Default = 2 * Math.PI.

Returns:
Type:
module:twgl.BufferInfo

The created sphere BufferInfo.

(static) createSphereBuffers(gl, radius, subdivisionsAxis, subdivisionsHeight, opt_startLatitudeInRadiansopt, opt_endLatitudeInRadiansopt, opt_startLongitudeInRadiansopt, opt_endLongitudeInRadiansopt) → {Object.<string, WebGLBuffer>}

Creates sphere buffers.

The created sphere has position, normal, and texcoord data

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

radius number

radius of the sphere.

subdivisionsAxis number

number of steps around the sphere.

subdivisionsHeight number

number of vertically on the sphere.

opt_startLatitudeInRadians number <optional>

where to start the
top of the sphere. Default = 0.

opt_endLatitudeInRadians number <optional>

Where to end the
bottom of the sphere. Default = Math.PI.

opt_startLongitudeInRadians number <optional>

where to start
wrapping the sphere. Default = 0.

opt_endLongitudeInRadians number <optional>

where to end
wrapping the sphere. Default = 2 * Math.PI.

Returns:
Type:
Object.<string, WebGLBuffer>

The created sphere buffers.

(static) createSphereVertices(radius, subdivisionsAxis, subdivisionsHeight, opt_startLatitudeInRadiansopt, opt_endLatitudeInRadiansopt, opt_startLongitudeInRadiansopt, opt_endLongitudeInRadiansopt) → {Object.<string, TypedArray>}

Creates sphere vertices.

The created sphere has position, normal, and texcoord data

Parameters:
Name Type Attributes Description
radius number

radius of the sphere.

subdivisionsAxis number

number of steps around the sphere.

subdivisionsHeight number

number of vertically on the sphere.

opt_startLatitudeInRadians number <optional>

where to start the
top of the sphere. Default = 0.

opt_endLatitudeInRadians number <optional>

Where to end the
bottom of the sphere. Default = Math.PI.

opt_startLongitudeInRadians number <optional>

where to start
wrapping the sphere. Default = 0.

opt_endLongitudeInRadians number <optional>

where to end
wrapping the sphere. Default = 2 * Math.PI.

Returns:
Type:
Object.<string, TypedArray>

The created sphere vertices.

(static) createTorusBufferInfo(gl, radius, thickness, radialSubdivisions, bodySubdivisions, startAngleopt, endAngleopt) → {module:twgl.BufferInfo}

Creates BufferInfo for a torus

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

radius number

radius of center of torus circle.

thickness number

radius of torus ring.

radialSubdivisions number

The number of subdivisions around the torus.

bodySubdivisions number

The number of subdivisions around the body torus.

startAngle boolean <optional>

start angle in radians. Default = 0.

endAngle boolean <optional>

end angle in radians. Default = Math.PI * 2.

Returns:
Type:
module:twgl.BufferInfo

The created BufferInfo.

(static) createTorusBuffers(gl, radius, thickness, radialSubdivisions, bodySubdivisions, startAngleopt, endAngleopt) → {Object.<string, WebGLBuffer>}

Creates buffers for a torus

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

radius number

radius of center of torus circle.

thickness number

radius of torus ring.

radialSubdivisions number

The number of subdivisions around the torus.

bodySubdivisions number

The number of subdivisions around the body torus.

startAngle boolean <optional>

start angle in radians. Default = 0.

endAngle boolean <optional>

end angle in radians. Default = Math.PI * 2.

Returns:
Type:
Object.<string, WebGLBuffer>

The created buffers.

(static) createTorusVertices(radius, thickness, radialSubdivisions, bodySubdivisions, startAngleopt, endAngleopt) → {Object.<string, TypedArray>}

Creates vertices for a torus

Parameters:
Name Type Attributes Description
radius number

radius of center of torus circle.

thickness number

radius of torus ring.

radialSubdivisions number

The number of subdivisions around the torus.

bodySubdivisions number

The number of subdivisions around the body torus.

startAngle boolean <optional>

start angle in radians. Default = 0.

endAngle boolean <optional>

end angle in radians. Default = Math.PI * 2.

Returns:
Type:
Object.<string, TypedArray>

The created vertices.

(static) createTruncatedConeBufferInfo(gl, bottomRadius, topRadius, height, radialSubdivisions, verticalSubdivisions, opt_topCapopt, opt_bottomCapopt) → {module:twgl.BufferInfo}

Creates a BufferInfo for a truncated cone, which is like a cylinder
except that it has different top and bottom radii. A truncated cone
can also be used to create cylinders and regular cones. The
truncated cone will be created centered about the origin, with the
y axis as its vertical axis.

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

bottomRadius number

Bottom radius of truncated cone.

topRadius number

Top radius of truncated cone.

height number

Height of truncated cone.

radialSubdivisions number

The number of subdivisions around the
truncated cone.

verticalSubdivisions number

The number of subdivisions down the
truncated cone.

opt_topCap boolean <optional>

Create top cap. Default = true.

opt_bottomCap boolean <optional>

Create bottom cap. Default = true.

Returns:
Type:
module:twgl.BufferInfo

The created cone BufferInfo.

(static) createTruncatedConeBuffers(gl, bottomRadius, topRadius, height, radialSubdivisions, verticalSubdivisions, opt_topCapopt, opt_bottomCapopt) → {Object.<string, WebGLBuffer>}

Creates buffers for a truncated cone, which is like a cylinder
except that it has different top and bottom radii. A truncated cone
can also be used to create cylinders and regular cones. The
truncated cone will be created centered about the origin, with the
y axis as its vertical axis.

Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

bottomRadius number

Bottom radius of truncated cone.

topRadius number

Top radius of truncated cone.

height number

Height of truncated cone.

radialSubdivisions number

The number of subdivisions around the
truncated cone.

verticalSubdivisions number

The number of subdivisions down the
truncated cone.

opt_topCap boolean <optional>

Create top cap. Default = true.

opt_bottomCap boolean <optional>

Create bottom cap. Default = true.

Returns:
Type:
Object.<string, WebGLBuffer>

The created cone buffers.

(static) createTruncatedConeVertices(bottomRadius, topRadius, height, radialSubdivisions, verticalSubdivisions, opt_topCapopt, opt_bottomCapopt) → {Object.<string, TypedArray>}

Creates vertices for a truncated cone, which is like a cylinder
except that it has different top and bottom radii. A truncated cone
can also be used to create cylinders and regular cones. The
truncated cone will be created centered about the origin, with the
y axis as its vertical axis. .

Parameters:
Name Type Attributes Description
bottomRadius number

Bottom radius of truncated cone.

topRadius number

Top radius of truncated cone.

height number

Height of truncated cone.

radialSubdivisions number

The number of subdivisions around the
truncated cone.

verticalSubdivisions number

The number of subdivisions down the
truncated cone.

opt_topCap boolean <optional>

Create top cap. Default = true.

opt_bottomCap boolean <optional>

Create bottom cap. Default = true.

Returns:
Type:
Object.<string, TypedArray>

The created cone vertices.

(static) createXYQuadBufferInfo(gl, sizeopt, xOffsetopt, yOffsetopt) → {module:twgl.BufferInfo}

Creates XY quad Buffers

The default with no parameters will return a 2x2 quad with values from -1 to +1.
If you want a unit quad with that goes from 0 to 1 you'd call it with

twgl.primitives.createXYQuadBufferInfo(gl, 1, 0.5, 0.5);

If you want a unit quad centered above 0,0 you'd call it with

twgl.primitives.createXYQuadBufferInfo(gl, 1, 0, 0.5);
Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

size number <optional>

the size across the quad. Defaults to 2 which means vertices will go from -1 to +1

xOffset number <optional>

the amount to offset the quad in X

yOffset number <optional>

the amount to offset the quad in Y

Returns:
Type:
module:twgl.BufferInfo

the created XY Quad buffers

(static) createXYQuadBuffers(gl, sizeopt, xOffsetopt, yOffsetopt) → {Object.<string, WebGLBuffer>}

Creates XY quad BufferInfo

The default with no parameters will return a 2x2 quad with values from -1 to +1.
If you want a unit quad with that goes from 0 to 1 you'd call it with

twgl.primitives.createXYQuadBufferInfo(gl, 1, 0.5, 0.5);

If you want a unit quad centered above 0,0 you'd call it with

twgl.primitives.createXYQuadBufferInfo(gl, 1, 0, 0.5);
Parameters:
Name Type Attributes Description
gl WebGLRenderingContext

The WebGLRenderingContext.

size number <optional>

the size across the quad. Defaults to 2 which means vertices will go from -1 to +1

xOffset number <optional>

the amount to offset the quad in X

yOffset number <optional>

the amount to offset the quad in Y

Returns:
Type:
Object.<string, WebGLBuffer>

the created XY Quad BufferInfo

(static) createXYQuadVertices(sizeopt, xOffsetopt, yOffsetopt) → {Object.<string, TypedArray>}

Creates XY quad vertices

The default with no parameters will return a 2x2 quad with values from -1 to +1.
If you want a unit quad with that goes from 0 to 1 you'd call it with

twgl.primitives.createXYQuadVertices(1, 0.5, 0.5);

If you want a unit quad centered above 0,0 you'd call it with

twgl.primitives.createXYQuadVertices(1, 0, 0.5);
Parameters:
Name Type Attributes Description
size number <optional>

the size across the quad. Defaults to 2 which means vertices will go from -1 to +1

xOffset number <optional>

the amount to offset the quad in X

yOffset number <optional>

the amount to offset the quad in Y

Returns:
Type:
Object.<string, TypedArray>

the created XY Quad vertices

(static) deindexVertices(vertices) → {Object.<string, TypedArray>}

Given indexed vertices creates a new set of vertices un-indexed by expanding the indexed vertices.

Parameters:
Name Type Description
vertices Object.<string, TypedArray>

The indexed vertices to deindex

Returns:
Type:
Object.<string, TypedArray>

The deindexed vertices

(static) duplicateVertices(arrays) → {module:twgl.Arrays}

Creates a duplicate set of vertices

This is useful for calling reorientVertices when you
also want to keep the original available

Parameters:
Name Type Description
arrays module:twgl.Arrays

of vertices

Returns:
Type:
module:twgl.Arrays

The duplicated vertices.

(static) flattenNormals(vertices) → {Object.<string, TypedArray>}

flattens the normals of deindexed vertices in place.

Parameters:
Name Type Description
vertices Object.<string, TypedArray>

The deindexed vertices who's normals to flatten

Returns:
Type:
Object.<string, TypedArray>

The flattened vertices (same as was passed in)

(static) makeRandomVertexColors(vertices, optionsopt) → {Object.<string, AugmentedTypedArray>}

Creates an augmentedTypedArray of random vertex colors.
If the vertices are indexed (have an indices array) then will
just make random colors. Otherwise assumes they are triangles
and makes one random color for every 3 vertices.

Parameters:
Name Type Attributes Description
vertices Object.<string, AugmentedTypedArray>

Vertices as returned from one of the createXXXVertices functions.

options module:twgl/primitives.RandomVerticesOptions <optional>

options.

Returns:
Type:
Object.<string, AugmentedTypedArray>

same vertices as passed in with color added.

(static) reorientDirections(array, matrix) → {Array.<number>|TypedArray}

Reorients directions by the given matrix..

Parameters:
Name Type Description
array Array.<number> | TypedArray

The array. Assumes value floats per element.

matrix module:twgl/m4.Mat4

A matrix to multiply by.

Returns:
Type:
Array.<number> | TypedArray

the same array that was passed in

(static) reorientNormals(array, matrix) → {Array.<number>|TypedArray}

Reorients normals by the inverse-transpose of the given
matrix..

Parameters:
Name Type Description
array Array.<number> | TypedArray

The array. Assumes value floats per element.

matrix module:twgl/m4.Mat4

A matrix to multiply by.

Returns:
Type:
Array.<number> | TypedArray

the same array that was passed in

(static) reorientPositions(array, matrix) → {Array.<number>|TypedArray}

Reorients positions by the given matrix. In other words, it
multiplies each vertex by the given matrix.

Parameters:
Name Type Description
array Array.<number> | TypedArray

The array. Assumes value floats per element.

matrix module:twgl/m4.Mat4

A matrix to multiply by.

Returns:
Type:
Array.<number> | TypedArray

the same array that was passed in

(static) reorientVertices(arrays, matrix) → {Object.<string, NativeArrayOrTypedArray>}

Reorients arrays by the given matrix. Assumes arrays have
names that contains 'pos' could be reoriented as positions,
'binorm' or 'tan' as directions, and 'norm' as normals.

Parameters:
Name Type Description
arrays Object.<string, NativeArrayOrTypedArray>

The vertices to reorient

matrix module:twgl/m4.Mat4

matrix to reorient by.

Returns:
Type:
Object.<string, NativeArrayOrTypedArray>

same arrays that were passed in.

Type Definitions

RandomColorFunc(ndx, channel) → {number}

Used to supply random colors

Parameters:
Name Type Description
ndx number

index of triangle/quad if unindexed or index of vertex if indexed

channel number

0 = red, 1 = green, 2 = blue, 3 = alpha

Returns:
Type:
number

a number from 0 to 255

RandomVerticesOptions

Properties:
Name Type Attributes Description
vertsPerColor number <optional>

Defaults to 3 for non-indexed vertices

rand module:twgl/primitives.RandomColorFunc <optional>

A function to generate random numbers

Type:
  • Object

NativeArrayOrTypedArray

Type:
  • Array.<number> | TypedArray

TypedArray

Type:
  • Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array