A lantern should give light. On this page it is evening, the lantern has a lamp inside, its glass glows, and it lights up as you walk toward it, fading on over a second and a half and dimming again when you walk away. Two ideas are new: a light that is placed in the world, and a glowing material. A third, the chase, is an animator that follows a moving goal.

The whole world, changed

part hello                                              // the world
  part materials
    part steel: physical(color: srgb(60, 62, 66), roughness: 0.4, metal: 1)
    part amber: [plain(srgb(255, 205, 130)), translucent(covers: 0.5),
                 glowing(color: color(1, 0.45, 0.1), strength: glow)]           // new: it can glow
      field glow: nits = 0 nits                                                 // new
    part stone: plain(srgb(140, 140, 135))
  part floor: box(x: -5 ..= 5, y: -0.1 ..= 0, z: -5 ..= 5), material: materials.stone, at: (0, 0, 0)
    solid(self, derived)
  part lantern, at: (0, 0, 0)
    fn frame() shape { ... }                            // as on page 3
    part body : frame(), material: materials.steel
    part glass: box(x: -0.11 ..= 0.11, y: 0.94 ..= 1.2, z: -0.11 ..= 0.11), material: materials.amber
    part lamp : pointLight(color: color(1, 0.6, 0.25), source: 2 cm, reach: 4 m, castsShadow: true),
                at: (0, 1.07, 0)                        // new: a lamp in the middle of the glass
    field near : bool = false, lives: client
    field level: fraction = 0, within: 0 ..= 1, mutable: true, lives: client     // new: how lit it is
    area(self, sphere(radius: 2 m, at: (0, 1, 0)))
    derive(near, occupied(self, by: me))
    derive(lamp.intensity, level * 60 candela)          // new
    derive(materials.amber.glow, level * 40 nits)       // new
    on(created, start(chase(level, toward: if near { 1 } else { 0 },             // new
                            speed: rate(1, per: sim(1.5 s)))))
  part sun: sunLight(direction: (0.9, 0.12, 0.3), color: color(1, 0.6, 0.35), lux: 300 lux,
                castsShadow: true)                      // new: a low evening sun
  part arrivals
    part start, at: (0, 0, 3), facing: (0, 0, -1)

First, make it evening

A lamp is lost in full daylight, so the sun has moved. Its direction is now nearly level, (0.9, 0.12, 0.3): low on your right, just above the horizon. Its color is the orange of a setting sun, and its light is 300 lux instead of 80,000, about what is left half an hour before dark. Nothing else about the sun changed. (On the next page the sun moves by itself, with the time of day.)

A light with a place

The sun is a light by direction: it lights everything from one angle. A lamp is a light with a place: pointLight shines in every direction from a point, and grows fainter with distance, as a bulb does. It is a part like any other, placed with at:, here in the middle of the glass, 1.07 m up.

How bright it is, its intensity, is not written on its line, because it changes. A rule sets it: derive(lamp.intensity, level * 60 candela). A candela is the unit of how strong a light is; at level 1 the lamp is 60 candela, a bright bulb.

Glass that glows

A light lights other things, but the glass itself must look lit too. The amber material gains a third item, glowing(...): the surface gives out light of its own color, at a strength. The strength is a field on the material, glow, measured in nits, the unit of how bright a glowing surface looks. A rule keeps it at 40 nits when the lamp is fully lit.

Because glow belongs to the material, every part drawn in amber glows together. On the next page six lanterns share it.

Fading on: a chase

level is how lit the lantern is, from 0 (dark) to 1 (fully lit). within: 0 ..= 1 says it belongs in that range, and mutable: true says it may change while the world runs. The lamp's intensity and the glass's glow both follow it.

A chase is an animator that moves a value toward a goal at a set speed, and keeps following if the goal moves. Its goal here is 1 while you are near and 0 when you are not. speed: rate(1, per: sim(1.5 s)) moves it the whole way from 0 to 1 in a second and a half. Walk up, and the goal jumps to 1 and the lantern fades on; walk away, and it fades back off, from wherever it had got to. The chase is started once, when the world starts, and never stops.

Like near, level lives on each visitor's machine, so the lantern lights for whoever is walking up to it, on their own screen.

Try changing it

What you have now

A light can come from a direction, like the sun, or from a place, like a lamp. A glowing material gives out its own light, and a field on the material sets how strongly. A chase follows a goal that changes. The last page puts six of these lanterns along a garden walk, lights them all at dusk, and makes a world you can go into and out of.

← Guide 3: Grow it into a lantern | Contents | Guide 5: A place to visit →