Windows, canvases and scenesSlide 79 of 86
79sceneWindows, canvases and scenes

Three dimensions

hive.ui.scene(attrs, shapes) is the one widget only a window can draw: a camera, a sky and a vector of shapes, rebuilt from the state by an ordinary func every frame. Nothing is a handle (no mesh to keep, no material to allocate), so a program says what is there now, never what changed. Its shapes are hive.ui calls like every other widget. It is the one part of the library that carries a download: Hive's own scene engine, built on Filament, fetched on the first build that draws a scene, checked against a pinned SHA-256, cached, and built into the executable. In a window the program draws itself, on Windows and Linux amd64, the engine runs as a process beside the program; in a window that is a page, it runs as WebAssembly. A scene looks the same either way.

Shapes take their dimensions in metres as their payload, the arguments after attrs, because a sphere without a radius is not a sphere with a default one:

  • box(attrs, width, height, depth), sphere(attrs, radius), cylinder(attrs, radius, height), and cone(attrs, top, bottom, height): a top of 0 makes a point, and a top equal to the bottom makes a cylinder.
  • roof(attrs, wide, tall, long), a ridge along its length with a gable at each end; ground(attrs, width, depth), already lying flat.
  • label(attrs, words), always facing the viewer; sprite(attrs, image, wide, tall), a picture from the program's assets/ that also faces the viewer. A sprite stands on its foot rather than its middle, is drawn unfiltered, and is untinted unless painted.
  • line(attrs, fromX, fromY, fromZ, toX, toY, toZ), which has no position of its own.

at(x, y, z) is where a shape's middle is. turn(x, y, z) is its rotation in radians, applied as a heading: yaw (y) first, then pitch (x) about the shape's own side-to-side axis, then roll (z) about its own length. So a shape turned to face somewhere and then pitched tips nose-up in the direction it faces. paint(Tone) is its colour, and a colour with alpha (Tone.RGBA, or a four- or eight-digit Tone.HEX) is see-through.

surface(Surface) is what a shape is made of, drawn over its colour. From the ground up: Turf, Gravel, Tarmac, Kerb; Concrete, Brick, Panes, Planks, Staves; Bark, Leaves, Crowd; Mesh; and Speckle, Metal, Carbon, Rubber, Glass, Gloss.

  • A finish repeats about every two metres, on a box face by face.
  • Every finish has relief except Gloss, Glass and Mesh. Mesh has real holes and shows from both sides, and Panes paints its own glass.
  • A shape that names no surface gets one from its kind: a ground is Turf, a cylinder or a cone Staves, a sphere Speckle, anything else Planks.
  • A photograph at assets/textures/<surface>.jpg replaces a finish's drawing, with a -normal.jpg beside it for relief. It is laid over the colour, so it should average mid-grey; if it is missing, the drawing is used.
A hive.ui window running the example above, photographedDrawn by hive.ui, in a window
A 3D scene, in a window
type Msg { Tick }

func view(model: Int): hive.ui.View {
	shapes := [
		hive.ui.ground([hive.ui.paint(hive.ui.Tone.HEX("#2d5c34"))], 20.0, 20.0),
		hive.ui.box(
			[
				hive.ui.at(0.0, 0.5, -3.0),
				hive.ui.paint(hive.ui.Tone.HEX("#b5651d")),
				hive.ui.surface(hive.ui.Surface.Planks()),
			],
			1.0, 1.0, 1.0,
		),
		hive.ui.sphere([hive.ui.at(2.0, 1.0, -4.0), hive.ui.paint(hive.ui.Tone.HEX("#e0c341"))], 1.0),
		// A cone takes both radii: nought at the top is the pointed one.
		hive.ui.cone(
			[
				hive.ui.at(-2.0, 0.4, -3.0),
				hive.ui.paint(hive.ui.Tone.HEX("#ff5a1f")),
				hive.ui.surface(hive.ui.Surface.Gloss()),
			],
			0.0, 0.4, 0.8,
		),
		// And a colour with an alpha is drawn see-through.
		hive.ui.box(
			[hive.ui.at(0.0, 1.2, -1.5), hive.ui.paint(hive.ui.Tone.RGBA(120, 200, 255, 96))],
			3.0, 2.0, 0.2,
		),
	]
	return hive.ui.scene(
		[hive.ui.grow(1), hive.ui.eye(0.0, 1.6, 4.0), hive.ui.aim(0.0, 0.0), hive.ui.lens(70), hive.ui.fog(60)],
		shapes,
	)
}

proc update(model: mut Int, msg: Msg): Msg {
	return msg
}

proc main(): void {
	spawn(hive.ui.window("A 3D scene", view, update), 0)
}