Skip to content

Geometry

Geometry and attributes ​

A Geometry is a set of named vertex attributes, and optionally an index, uploaded to the GPU for a mesh to draw.

Each attribute is a GeometryAttribute: a typed array, and how many numbers make up one vertex's value. A vertex shader reads them by name, so a position attribute feeds in vec3 position. The built-in shapes in primitives are geometries with position, normal, uv and index already filled in; this page is about making and changing your own.

Making a geometry ​

Pass the attributes by name. This is a square in the xy plane, made of two triangles that share two of their corners through the index:

js
import { Geometry, GeometryAttribute } from 'wtc-gl'

const geometry = new Geometry(gl, {
  position: new GeometryAttribute({
    size: 3,
    data: new Float32Array([
      -0.5, -0.5, 0, 0.5, -0.5, 0, 0.5, 0.5, 0, -0.5, 0.5, 0
    ])
  }),
  uv: new GeometryAttribute({
    size: 2,
    data: new Float32Array([0, 0, 1, 0, 1, 1, 0, 1])
  }),
  index: new GeometryAttribute({
    data: new Uint16Array([0, 1, 2, 0, 2, 3])
  })
})

The data is uploaded when the geometry is made. Triangles are front-facing when their corners run anticlockwise as you look at them, and programs cull back faces by default, so a triangle wound the other way is invisible from the front.

Attribute options ​

OptionDefaultWhat it does
datarequiredA Float32Array, Uint16Array or Uint32Array of values
size1Numbers per vertex: 2 for a vec2, 3 for a vec3, 16 for a mat4
typefrom dataThe GL type: FLOAT for a Float32Array, UNSIGNED_SHORT for a Uint16Array, else UNSIGNED_INT
normalizedfalseRead integers as 0 to 1 in the shader
instanced0The divisor: how many instances share each value. 0 means one value per vertex
stride0Bytes from one vertex's value to the next, for interleaved data. 0 means tightly packed
offset0Bytes from the start of data to the first value
countfrom dataThe number of values, worked out from data, size and stride when left out
buffernullAn existing WebGL buffer to use instead of making one
externalfalseWhether something else, such as a TransformFeedback, owns buffer

WebGL has no 64-bit float attributes, so keep float data in a Float32Array.

Normalised integers ​

Colours stored as integers take less memory. With normalized, a Uint16Array of 0 to 65535 reads as 0 to 1 in the shader:

js
const colour = new GeometryAttribute({
  size: 3,
  data: new Uint16Array([65535, 0, 0, 0, 65535, 0, 0, 0, 65535]),
  normalized: true
})

Interleaved data ​

When several attributes come in one array, one vertex after another, give each the same data with a stride and offset in bytes. Here each vertex is five floats, x y z u v, so 20 bytes:

js
const data = new Float32Array([/* x, y, z, u, v for each vertex */])
const geometry = new Geometry(gl, {
  position: new GeometryAttribute({ size: 3, data, stride: 20, offset: 0 }),
  uv: new GeometryAttribute({ size: 2, data, stride: 20, offset: 12 })
})

Each attribute uploads the array to a buffer of its own, so this is for reading data that arrives interleaved rather than for saving GPU memory. The CPU tools below read interleaved attributes correctly.

Instanced attributes ​

An attribute with instanced: 1 has one value per instance rather than per vertex, and makes the geometry draw that many instances in one call. geometry.instancedCount is set from it: the attribute's count times its divisor.

js
import { Box, GeometryAttribute } from 'wtc-gl'

const offsets = new Float32Array(100 * 3).map(() => Math.random() * 10 - 5)

const geometry = new Box(gl, {
  attributes: {
    offset: new GeometryAttribute({ size: 3, instanced: 1, data: offsets })
  }
})

The vertex shader reads in vec3 offset and adds it to the position. If two instanced attributes give different instance counts, the geometry warns and draws the smaller. A mesh is frustum culled from its geometry's bounds, which don't know where the instances are, so give the mesh frustumCulled: false when they spread out.

For the usual case, many copies each with a matrix and colour, use InstancedMesh, which sets this up and culls each instance. The camera-and-instancing demo draws with an instanced mat4 attribute of its own.

External attributes ​

external: true marks an attribute whose buffer is owned by something else, such as a TransformFeedback writing positions on the GPU. The geometry never uploads, recreates or deletes an external buffer, and follows it when it's swapped. Its data is only the starting values, so bounds aren't measured from it and the CPU tools leave it out: set geometry.bounds yourself, or give the mesh frustumCulled: false.

The index ​

The index attribute lists the vertices of each triangle, so vertices can be shared. Use a Uint16Array for up to 65,536 vertices and a Uint32Array beyond that; the built-in shapes switch for you.

To replace the index after the geometry is made, use setIndex, with an attribute or a typed array. It resets drawRange.start to 0. The old index buffer isn't deleted, because attributes can be shared between geometries.

js
geometry.setIndex(new Uint16Array([0, 2, 1, 0, 3, 2]))

To change indices in place at the same length, edit geometry.attributes.index.data and set its needsUpdate.

Draw range and instance count ​

drawRange is which part of the geometry is drawn: start and count in index entries for an indexed geometry, or in vertices for one without. It starts as the whole geometry. Drawing fewer triangles is a cheap way to build a shape up over time:

js
const total = geometry.attributes.index.count
geometry.setDrawRange(0, Math.floor((total * progress) / 3) * 3)

instancedCount is how many instances an instanced geometry draws. setInstancedCount(n) draws fewer than the instanced attributes hold.

Updating data ​

Edit an attribute's data in place, then set needsUpdate. It's uploaded the next time a program that reads it draws the geometry:

js
const position = geometry.attributes.position
for (let i = 0; i < position.data.length; i += 3) {
  position.data[i + 1] = Math.sin(position.data[i] * 4 + time)
}
position.needsUpdate = true

For a different number of vertices, add a new attribute with addAttribute(name, attribute), which replaces one of the same name, then set the draw range to match. addAttribute also adds attributes the geometry doesn't have yet. The replaced attribute's buffer isn't deleted, as with setIndex.

Moving positions doesn't move the bounds. If the mesh is frustum culled or raycast, measure them again afterwards.

Bounds ​

geometry.bounds holds min, max, center and scale (the box's size), and radius, a sphere around the centre. The renderer uses it to skip meshes outside the camera, and Raycast to find what a ray might hit. It's measured from position the first time it's needed.

computeBoundingBox() measures the box and computeBoundingSphere() the sphere. Call both after changing positions on the CPU:

js
geometry.computeBoundingBox()
geometry.computeBoundingSphere()

When positions move in a shader, the bounds can't see it. Set geometry.bounds to something big enough, or give the mesh frustumCulled: false.

CPU tools ​

These work on the attribute data on the CPU, and assume the geometry is drawn as triangles. They skip external attributes, since their data can't be read back.

Normals ​

computeVertexNormals() works out normals from the positions, each face weighted by its area. On an indexed geometry, faces that share a vertex share its normal, so the surface looks smooth; on a non-indexed one, every triangle has its own vertices, so it looks faceted. It writes into the existing normal attribute when it can, and adds one when there isn't one.

Transforming ​

applyMatrix(matrix) moves the data itself: positions by the matrix, and normals by its normal matrix. Use it to place a shape once, rather than through a mesh's transform every frame, for example before merging:

js
import { Cylinder, Mat4, Vec3 } from 'wtc-gl'

// A cone standing on the origin rather than centred on it
const cone = new Cylinder(gl, { radiusTop: 0, radialSegments: 32 })
cone.applyMatrix(Mat4.fromTranslatingVec3(new Vec3(0, 0.5, 0)))

A mirroring matrix, with a negative scale, also reverses each triangle's winding, so the faces still face outwards. Attributes shared with other geometries change for them too. If the bounds have been measured, they're measured again.

Flat and smooth shading ​

toNonIndexed() returns a copy where every index entry is a vertex of its own. Computing normals on it gives each face its own normal: flat shading. mergeVertices(tolerance) goes the other way, welding vertices that match into an indexed copy. If the geometry had normals, they're recomputed, so they're smooth across the welds; the example works its normals out after moving the vertices.

js
import {
  Renderer,
  Camera,
  Obj,
  Mesh,
  Sphere,
  Geometry,
  GeometryAttribute,
  NormalProgram,
  Vec2
} from 'wtc-gl'

// The same rock twice, coloured by its normals: per face on the left, from
// toNonIndexed(), and smooth on the right, from mergeVertices().
export default function (container) {
  const renderer = new Renderer({
    dpr: Math.min(devicePixelRatio, 2),
    alpha: true,
    antialias: true
  })
  const { gl } = renderer
  container.appendChild(gl.canvas)

  const camera = new Camera({ fov: 35 })

  // A sphere's positions and index, without its uvs: mergeVertices keeps
  // vertices with different uvs apart, which would leave the seam open
  const sphere = new Sphere(gl, { radius: 1, widthSegments: 20 })
  const { position, index } = sphere.attributes
  const shape = new Geometry(gl, {
    position: new GeometryAttribute({ size: 3, data: position.data }),
    index: new GeometryAttribute({ data: index.data })
  })
  const smooth = shape.mergeVertices()
  sphere.remove()
  shape.remove()

  // Push each vertex in or out, then upload the change
  const p = smooth.attributes.position.data
  for (let i = 0; i < p.length; i += 3) {
    const [x, y, z] = [p[i], p[i + 1], p[i + 2]]
    const r =
      1 + 0.18 * Math.sin(3 * x + 1) * Math.sin(4 * y + 2) * Math.sin(5 * z)
    p[i] *= r
    p[i + 1] *= r
    p[i + 2] *= r
  }
  smooth.attributes.position.needsUpdate = true
  smooth.computeVertexNormals()

  // Every triangle with vertices of its own, so each gets its face's normal
  const flat = smooth.toNonIndexed()
  flat.computeVertexNormals()

  const program = new NormalProgram(gl)
  const scene = new Obj()
  const meshes = [flat, smooth].map((geometry, i) => {
    const mesh = new Mesh(gl, { geometry, program })
    mesh.position.x = i ? 1.4 : -1.4
    mesh.setParent(scene)
    return mesh
  })

  return {
    frame(time) {
      for (const mesh of meshes) {
        mesh.rotation.x = 0.4
        mesh.rotation.y = time * 0.4
        mesh.updateRotation()
      }
      renderer.render({ scene, camera })
    },
    resize(width, height) {
      renderer.dimensions = new Vec2(width, height)
      camera.perspective({ aspect: width / height })
      camera.position.z = Math.max(5.5, 9.5 / (width / height))
    },
    dispose() {
      flat.remove()
      smooth.remove()
      program.remove()
      renderer.dispose()
      gl.canvas.remove()
    }
  }
}

mergeVertices compares every attribute except normal, so vertices only weld when their uvs (and any other attributes) match too, within tolerance (by default 1e-4). That's why a Box keeps hard edges when welded: each face has its own uvs. It's also why the example copies only the sphere's positions first: the sphere's uvs differ across its seam and at the poles, which would leave creases there. Vertices the index never uses are dropped. Both methods leave the original alone; call remove() on it if it's no longer needed.

TIP

For flat shading without extra vertices, LitProgram and NormalProgram take flatShading: true, which shades each triangle from its face normal in the fragment shader.

Merging ​

mergeGeometries(geometries) combines geometries into one new geometry, so they draw in a single call. Every geometry needs the same attributes, with the same size, type and normalisation. If any is indexed, the result is indexed. Instanced and external attributes can't be merged, and draw ranges are ignored.

js
import { Box, Sphere, Mat4, Vec3, mergeGeometries } from 'wtc-gl'

const base = new Box(gl, { width: 1, height: 0.2, depth: 1 })
const ball = new Sphere(gl, { radius: 0.3 })
ball.applyMatrix(Mat4.fromTranslatingVec3(new Vec3(0, 0.4, 0)))

const merged = mergeGeometries([base, ball])
base.remove()
ball.remove()

There's no clone(); mergeGeometries([geometry]) makes a copy.

Cleaning up ​

geometry.remove() deletes its buffers and vertex arrays, except external ones. Attributes can be shared between geometries, so only remove a geometry once nothing else draws from its buffers. See cleaning up.

See also ​