Skip to content

Geometry

Tube, Extrude and Lathe ​

Turn paths and outlines into solids: sweep a tube along a path, extrude a 2D outline into a slab, or spin a profile around an axis.

All three are geometries with position, normal, uv and index, so they light with LitProgram, take textures, and work with the CPU tools and CSG like any other shape. They're built from the paths and curves on the previous page, or from plain lists of points.

js
import {
  Renderer,
  Camera,
  Obj,
  Mesh,
  Lights,
  LitProgram,
  Curve,
  Tube,
  Extrude,
  Lathe,
  Vec2
} from 'wtc-gl'

const TAU = Math.PI * 2

// A rippling tube around a trefoil knot, a bevelled star with a hole, and a
// lathed vase
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 })
  const scene = new Obj()
  const lights = new Lights()

  const knot = new Curve({
    closed: true,
    points: Array.from({ length: 12 }, (_, i) => {
      const a = (i / 12) * TAU
      const x = Math.sin(a) + 2 * Math.sin(2 * a)
      const y = Math.cos(a) - 2 * Math.cos(2 * a)
      return [x * 0.27, y * 0.27 + 0.15, -Math.sin(3 * a) * 0.27]
    })
  })
  // Outlines as lists of [x, y] points
  const star = Array.from({ length: 10 }, (_, i) => {
    const a = (i / 10) * TAU + Math.PI / 2
    const r = i % 2 ? 0.38 : 0.85
    return [Math.cos(a) * r, Math.sin(a) * r]
  })
  const hole = Array.from({ length: 24 }, (_, i) => {
    const a = (i / 24) * TAU
    return [Math.cos(a) * 0.18, Math.sin(a) * 0.18]
  })
  // [distance from the axis, height], bottom to top, closed on the axis
  const vase = Array.from({ length: 16 }, (_, i) => {
    const t = i / 15
    return [0.32 + 0.2 * Math.sin(t * 5 + 0.3), t * 1.4 - 0.7]
  })

  const meshes = [
    [
      new Tube(gl, {
        path: knot,
        radius: (u) => 0.11 + 0.04 * Math.sin(u * TAU * 8),
        tubularSegments: 200,
        radialSegments: 12
      }),
      [0.45, 0.6, 0.95]
    ],
    [
      new Extrude(gl, { shape: star, holes: [hole], depth: 0.25, bevel: true }),
      [0.96, 0.78, 0.38]
    ],
    [
      new Lathe(gl, { points: [[0, -0.7], ...vase], segments: 40 }),
      [0.62, 0.8, 0.5]
    ]
  ].map(([geometry, colour], i) => {
    // The vase is open at the top, so light its inside too
    const program = new LitProgram(gl, {
      lights,
      colour,
      specular: 0.4,
      doubleSided: i === 2
    })
    const mesh = new Mesh(gl, { geometry, program })
    mesh.position.x = (i - 1) * 2.3
    mesh.setParent(scene)
    return mesh
  })

  return {
    frame(time) {
      for (const mesh of meshes) {
        mesh.rotation.x = 0.3
        mesh.rotation.y = time * 0.5
        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, 10 / (width / height))
    },
    dispose() {
      for (const mesh of meshes) {
        mesh.geometry.remove()
        mesh.program.remove()
      }
      renderer.dispose()
      gl.canvas.remove()
    }
  }
}

Tube ​

A Tube sweeps a circle along a Path or Curve. The rings are spaced evenly by distance along the path and oriented by its rotation-minimising frames, so the tube doesn't twist unless the path has a tilt.

js
import { Curve, Tube } from 'wtc-gl'

const path = new Curve({
  points: [
    [-2, 0, 0],
    [-1, 1, 0.5],
    [1, -1, -0.5],
    [2, 0, 0]
  ]
})
const tube = new Tube(gl, { path, radius: 0.15, tubularSegments: 128 })
OptionDefaultWhat it does
pathrequiredThe Path or Curve to sweep along
radius0.1The radius, or a function of u, the fraction of the way along
tubularSegments64Rings along the path
radialSegments8Divisions around the tube
closedpath.closedJoin the last ring onto the first

Tapers ​

Pass a function as radius to vary it along the path. It's given u, from 0 at the start to 1 at the end, and the normals lean to match the slope:

js
// Thick in the middle, to a point at each end
const spindle = new Tube(gl, {
  path,
  radius: (u) => 0.2 * Math.sin(u * Math.PI)
})

The ends of an open tube aren't capped. If they'll be seen, taper them to nothing as above, draw the tube two-sided (doubleSided: true on LitProgram), or cap them with another shape. u runs along the tube and v around it, so a texture wraps it like a label.

A tube is built once. To follow a path that moves, make a new tube and swap it onto the mesh, or for a line that only needs to look round, use a Polyline, which can update its points every frame.

Extrude ​

An Extrude takes a 2D outline, with optional holes, and extrudes it along z into a solid: letters, logos, cookie-cutter shapes.

js
import { Extrude } from 'wtc-gl'

const square = [
  [-1, -1],
  [1, -1],
  [1, 1],
  [-1, 1]
]
const hole = [
  [-0.4, -0.4],
  [0.4, -0.4],
  [0.4, 0.4],
  [-0.4, 0.4]
]
const frame = new Extrude(gl, { shape: square, holes: [hole], depth: 0.3 })

An outline is a list of points, or a Path or Curve, which is sampled with toPolyline(curveDivisions) so its corners stay sharp. Only x and y are used. Outlines can be wound either way, and don't need to repeat their first point. The solid keeps the outline's x and y and is centred on z, from -depth / 2 to depth / 2.

OptionDefaultWhat it does
shaperequiredThe outline
holes[]Outlines to cut out of the shape
depth0.2Length along z, not counting the bevel
curveDivisions12Points in each curved segment of a Path outline
bevelfalseRound the edges between the faces and the sides
bevelThickness0.05How far the bevel adds to each end along z
bevelSize0.04How far the bevel insets the faces
bevelSegments3Steps in the bevel; 1 is a chamfer
creaseAngleMath.PI / 6Sides turning more sharply than this, in radians, get a hard edge
uvs'projected'How the bevel is mapped: 'projected' or 'separate'

An outline needs at least three points and some area, or Extrude throws.

Curved outlines ​

Draw the outline as a Path to mix straight edges and curves. This is a rounded rectangle:

js
import { Path, Extrude } from 'wtc-gl'

const w = 1
const h = 0.6
const r = 0.2
const rounded = new Path()
  .moveTo([-w + r, -h])
  .lineTo([w - r, -h])
  .quadraticCurveTo([w, -h], [w, -h + r])
  .lineTo([w, h - r])
  .quadraticCurveTo([w, h], [w - r, h])
  .lineTo([-w + r, h])
  .quadraticCurveTo([-w, h], [-w, h - r])
  .lineTo([-w, -h + r])
  .quadraticCurveTo([-w, -h], [-w + r, -h])
  .close()

const tile = new Extrude(gl, { shape: rounded, depth: 0.1, bevel: true })

A Curve with closed: true is the easy way to get a smooth outline through a few points.

Bevels and creases ​

With bevel, each end grows by bevelThickness along z and the faces are inset by bevelSize, joined to the sides by a rounded edge of bevelSegments steps. A large bevelSize on a tight shape can make the inset outline cross itself, so keep it smaller than the narrowest part.

creaseAngle decides where the shading is smooth. Where the outline turns by less than the angle, as around a sampled curve, the normals are shared and the side looks round; at sharper corners, as on a star's points, they're split and the edge is hard. The same applies across the bevel.

Uvs ​

Uvs are in world units, so a texture keeps its scale whatever the shape's size; set the texture to repeat. The faces are mapped flat, as (x, y). On the sides, u is the distance around the outline and v the distance from the back. With uvs: 'projected', the default, the bevel is mapped like the faces, so a texture runs on from the face over the bevel without a seam. With uvs: 'separate' the bevel is mapped like the sides.

Lathe ​

A Lathe spins a 2D profile around the y axis. Each point is [r, y], its distance from the axis and its height, or a Vec2 with the same meaning. List the points from bottom to top so the faces point outwards.

js
import { Lathe, Vec2 } from 'wtc-gl'

// A bottle: closed on the axis at the bottom, open at the neck
const bottle = new Lathe(gl, {
  points: [
    new Vec2(0, 0),
    new Vec2(0.4, 0),
    new Vec2(0.4, 0.8),
    new Vec2(0.15, 1.1),
    new Vec2(0.15, 1.3)
  ],
  segments: 32
})
OptionDefaultWhat it does
pointsrequiredThe profile, bottom to top; at least two points
segments12Divisions around the axis
phiStart0Where to start, in radians, from +z towards +x
phiLengthMath.PI * 2How far to sweep

A point at r = 0 closes the shape on the axis. A profile that doesn't end on the axis leaves an opening, like the bottle's neck, so draw it two-sided if you can see in. A phiLength under a full turn leaves a slice out, open along both cut edges.

v runs along the profile by distance, so a texture doesn't stretch where the points are unevenly spaced, and u runs around the axis.

Hard edges ​

The lathe's normals are smooth all along the profile, even at its corners, so the bottle's shoulder looks rounded however sharp the points make it. For hard edges, make a flat-shaded copy:

js
const faceted = bottle.toNonIndexed()
faceted.computeVertexNormals()

That makes every face flat, the curved parts included. To keep some corners round and others sharp, repeat a point in the profile at each hard corner, or build the profile in parts.

Lathe.build ​

Lathe.build(points, segments, phiStart, phiLength, profileNormals) makes the arrays without making a geometry: position, normal, uv and index. profileNormals, one [r, y] per point, replaces the normals worked out from the profile, for shapes whose exact normals you know. Capsule is built this way.

js
const { position, normal, uv, index } = Lathe.build(points, 32, 0, Math.PI * 2)

See also ​