Class: Geometry

Geometry

A Geometry is a low-level object that represents the structure of 2D shapes in terms of vertices and attributes. It's a crucial component for rendering as it describes the shape and format of the data that will go through the shaders. Essentially, a Geometry object holds the data you'd send to a GPU buffer.

A geometry is basically made of two components:
Attributes: These are essentially arrays that define properties of the vertices like position, color, texture coordinates, etc. They map directly to attributes in your vertex shaders.
Indices: An optional array that describes how the vertices are connected. If not provided, vertices will be interpreted in the sequence they're given.

new Geometry (options)

Create a new instance of a geometry

Name Type Description
options GeometryDescriptor

The options for the geometry.

Example

const geometry = new Geometry({
  attributes: {
    aPosition: [ // add some positions
      0, 0,
      0, 100,
      100, 100,
      100,   0,
    ],
    aUv: [ // add some uvs
      0, 0,
      0, 1,
      1, 1,
      1, 0,
    ]
  }
});

Members

attributes Record<string, Attribute> readonly

A record of the attributes of the geometry.

bounds Bounds

Returns the bounds of the geometry.

buffers Buffer[] readonly

The buffers that the attributes use

indexBuffer Buffer

The index buffer of the geometry

instanceCount number

the instance count of the geometry to draw

Default Value:
  • 1

topology Topology

The topology of the geometry.

uid number readonly

The unique id of the geometry.

Methods

addAttribute (name, attributeOption) void

Adds an attribute to the geometry.

Name Type Description
name string

The name of the attribute to add.

attributeOption AttributeOption

The attribute option to add.

addIndex (indexBuffer) void

Adds an index buffer to the geometry.

Name Type Description
indexBuffer Buffer | TypedArray | number[]

The index buffer to add. Can be a Buffer, TypedArray, or an array of numbers.

destroy (destroyBuffers) void

destroys the geometry.

Name Type Default Description
destroyBuffers boolean false

destroy the buffers associated with this geometry

getAttribute (id) Attribute

Returns the requested attribute.

Name Type Description
id string

The name of the attribute required

Returns:
Type Description
Attribute
  • The attribute requested.

getBuffer (id) Buffer

Returns the requested buffer.

Name Type Description
id string

The name of the buffer required.

Returns:
Type Description
Buffer
  • The buffer requested.

getIndex () Buffer

Returns the index buffer

Returns:
Type Description
Buffer
  • The index buffer.

getSize () number

Used to figure out how many vertices there are in this geometry

Returns:
Type Description
number the number of vertices in the geometry