Ollin

Mensaje

A square of gilded metal punched in rows, by candlelight, after Goeritz's Mensajes and his Message (Mensaje) (1967).

Run
swift run --package-path Examples Example-Recreations-MathiasGoeritz-Mensaje
Source
Examples/Recreations/MathiasGoeritz/Mensaje/Sketch.swift
//  Recreation after Mathias Goeritz - the Mensajes (Messages), the gold
//  monochromes he began in the late 1950s. This one is read from Message
//  (Mensaje) of 1967, a gilded sheet punched in rows. It is wood, steel
//  sheet, and gilding, 69.85 x 69.85 x 6.35 cm. The Los Angeles County
//  Museum of Art holds it, a gift of the 2007 Collectors Committee. A
//  homage, not a reproduction, and not affiliated with or endorsed by the
//  artist's estate or the museum.
//  https://collections.lacma.org/node/213615
//  https://unframed.lacma.org/2008/11/10/meditating-on-mathias-goeritzs-message
//  https://muac.unam.mx/objeto/oro?lang=en
//
//  An original Ollin interpretation, written from the museum's record, from
//  its published description of the surface, and from pictures of the work.
//  The counts of rows and of punches to a row are read off the museum's
//  picture and are approximate. The candles come from the account of how
//  Mensajes were shown in 1960, in a dark room lit only by candles. Nothing
//  says this panel of 1967 was ever lit that way. Nothing was ported: the
//  sheet was punched by hand.

import Foundation
import Ollin

/// A *Mensaje* (Mathias Goeritz, Mexico City): a square of gilded metal
/// punched in rows, by candlelight. **Move the pointer** to carry a candle
/// past it.
///
/// The Mensajes are panels of one color, gold. Some are gold leaf on wood
/// and nothing else. In others the gold is a thin sheet of metal that he
/// punched thousands of times, until the flat sheet had a grain like cloth.
/// There is no picture on them. What they show is the light in the room,
/// and in 1960 they were hung in the dark, lit only by candles.
///
/// The museum's account of this one gives the rule the sketch follows. The
/// punches run in "rigid horizontal lines across the panel," and the lines
/// let up in places, which leaves bars of bare gilding. Some punches went
/// right through the sheet and show as "tiny black voids." Here `rows`
/// lines are ruled across the square, with about `perRow` punches to a full
/// line. Each punch is set down a little off its mark, as a hand does.
/// `letUp` is the share of them left out, in bars a line or a few lines
/// high. `through` is the share that pierced the sheet.
///
/// The light is worked out for every punch and every square of leaf. A
/// candle `distance` centimeters in front of the sheet lights the gold under
/// it and falls off to the sides. Each square of leaf lies at its own slight
/// tilt, so each takes the shine at a different place. Each punch is a small
/// dent. Its far wall faces the flame and shines, and its near wall is in
/// its own shadow. So every dent points at the candle that lights it.
/// `candles` stand in a row below the panel and `flicker` is how unsteady
/// their flames are. One more candle is carried past once every `seconds`,
/// or goes where the pointer goes. `tarnish` is how far the bare gilding has
/// turned toward gray and green.
///
/// Try it: a press deals another panel. `--export-svg` gives the sheet as a
/// punching plan, one circle for every punch, filled where it goes through.
@main
final class Mensaje: Sketch {
    /// The lines of punches ruled across the panel.
    @Param(50 ... 140, icon: "line.3.horizontal") var rows = 118
    /// The punches in a line that runs the whole width.
    @Param(40 ... 110, icon: "circle.grid.3x3") var perRow = 90
    /// The share of punches left out, where the lines let up.
    @Param(0 ... 0.5, icon: "rectangle.dashed") var letUp = 0.2
    /// The share of punches that went right through the sheet.
    @Param(0 ... 0.4, icon: "circle.fill") var through = 0.1
    /// The candles standing in a row below the panel.
    @Param(1 ... 5, icon: "flame") var candles = 3
    /// How far in front of the sheet the candles are, in centimeters.
    @Param(10 ... 60, icon: "arrow.left.and.right") var distance = 26.0
    /// How unsteady the flames are.
    @Param(0 ... 1, icon: "wind") var flicker = 0.6
    /// How far the bare gilding has turned toward gray and green.
    @Param(0 ... 1, icon: "drop") var tarnish = 0.5
    /// One pass of the carried candle, out and back, in seconds.
    @Param(12 ... 30, icon: "timer") var seconds = 24.0

    override var canvasSize: CanvasSize { .square(1080) }
    override var loopDuration: Double? { seconds }

    // The panel, in centimeters, and its place on the canvas.
    static let side = 69.85
    static let leaves = 9
    /// A punch's radius, in centimeters, before the hand varies it.
    static let punch = 0.17
    private var perCm: Double { 800 / Mensaje.side }
    private var corner: Vector2 { Vector2((width - 800) / 2, (height - 800) / 2 - 36) }

    struct Punch {
        /// Where it is, in centimeters from the panel's top left corner.
        var at: Vector2
        var radius: Double
        var pierced: Bool
    }

    /// One square of leaf: how it lies, and how far it has turned.
    struct Leaf {
        var tilt: Vector2
        var tone: Double
        var turned: Double
        var hue: Double
    }

    /// A flame: where it is over the panel's plane in centimeters, how far
    /// in front, and how bright it burns just now.
    struct Flame {
        var at: Vector2
        var height: Double
        var power: Double
    }

    private var punches: [Punch] = []
    private var leaf: [Leaf] = []
    /// The punches as circles on the canvas, each drawn through the middle
    /// of its rim.
    private var rings: [Circle] = []
    private var built = ""
    private var dealt = 0
    private var lastMoved = -100.0
    private var held = Vector2.zero
    private var holding = false

    override func mousePressed() {
        dealt += 1
    }

    // MARK: - The frame

    override func draw() {
        let key = "\(variation)/\(dealt)/\(rows)/\(perRow)/\(letUp)/\(through)"
        if key != built {
            (punches, leaf) = Mensaje.deal(seed: variation &+ dealt &* 7919, rows: rows, perRow: perRow,
                                           letUp: letUp, through: through)
            built = key
            rings = punches.map { Circle(center: toCanvas($0.at), radius: $0.radius * perCm * 1.175) }
        }

        if isVectorExporting {
            drawThePlan()
            return
        }

        let flames = lightTheCandles()
        background(Color(hex: 0x070605))
        drawTheWall(flames)
        drawTheGilding(flames)
        drawThePunches(flames)
        drawTheFlames(flames)
    }

    // MARK: - Dealing a panel

    /// Rule the lines, set a punch at every mark, and then let the lines up.
    /// Everything comes from the seed.
    static func deal(seed: Int, rows: Int, perRow: Int, letUp: Double, through: Double) -> ([Punch], [Leaf]) {
        var rng = SplitMix64(seed: UInt64(bitPattern: Int64(seed &* 1_000_003 &+ 1957)))
        func roll(_ low: Double, _ high: Double) -> Double { Double.random(in: low ... high, using: &rng) }

        var leaf: [Leaf] = []
        for _ in 0 ..< leaves * leaves {
            let angle = roll(0, 2 * Double.pi), lean = roll(0, 1).squareRoot() * 0.075
            leaf.append(Leaf(tilt: Vector2(cos(angle), sin(angle)) * lean, tone: roll(0.88, 1.0),
                             turned: roll(0.25, 1), hue: roll(0, 1)))
        }

        // Every mark first. Each line is stepped off on its own, from where
        // the hand happened to start and at its own stride. So the punches
        // of one line do not sit under those of the last.
        var lines: [[Punch]] = []
        let pitchY = side / Double(rows), pitchX = side / Double(perRow)
        let radius = min(punch, 0.3 * min(pitchX, pitchY))
        var total = 0
        for r in 0 ..< rows {
            // A line wanders a hair as the hand moves along it.
            let drift = roll(-0.1, 0.1) * pitchY
            let stride = pitchX * roll(0.95, 1.05)
            var x = roll(0.2, 0.9) * stride
            var line: [Punch] = []
            while x < side - 0.2 * stride {
                let size = radius * roll(0.85, 1.15)
                let reach = 1.5 * size
                let at = Vector2(min(max(x + roll(-0.12, 0.12) * stride, reach), side - reach),
                                 min(max((Double(r) + 0.5) * pitchY + drift + roll(-0.07, 0.07) * pitchY, reach), side - reach))
                line.append(Punch(at: at, radius: size, pierced: roll(0, 1) < through))
                x += stride
            }
            total += line.count
            lines.append(line)
        }

        // Then the lines let up, in bars a line or a few lines high, until
        // the share asked for is bare. Some bars are stacked like the rungs
        // of a ladder, with a punched line between each.
        var bare = lines.map { [Bool](repeating: false, count: $0.count) }
        var gone = 0
        let wanted = Int(letUp * Double(total))
        var tries = 0
        while gone < wanted && tries < 4000 {
            tries += 1
            var row = Int(roll(0, Double(rows)))
            let wide = roll(0.12, 0.7) * side
            var from = roll(-0.2, 1.0) * side
            let rungs = roll(0, 1) < 0.3 ? Int(roll(2, 5)) : 1
            for _ in 0 ..< rungs {
                let high = roll(0, 1) < 0.7 ? 1 : (roll(0, 1) < 0.85 ? 2 : 3)
                for r in row ..< row + high where r >= 0 && r < rows {
                    for (i, mark) in lines[r].enumerated() where !bare[r][i] && mark.at.x >= from && mark.at.x < from + wide {
                        bare[r][i] = true
                        gone += 1
                    }
                }
                row += high + 1
                from += roll(-0.06, 0.06) * side
            }
        }

        var punches: [Punch] = []
        for r in 0 ..< rows {
            for (i, mark) in lines[r].enumerated() where !bare[r][i] { punches.append(mark) }
        }
        return (punches, leaf)
    }

    // MARK: - The light

    /// The flames for this frame: the row below the panel, each flickering,
    /// and the one that is carried.
    private func lightTheCandles() -> [Flame] {
        let lap = time / seconds - (time / seconds).rounded(.down)
        var flames: [Flame] = []
        for k in 0 ..< candles {
            let x = Mensaje.side * (Double(k) + 0.5) / Double(candles)
            let waver = signedNoise(Double(k) * 9.1 + 3, loop: lap, radius: 26)
            let breath = signedNoise(Double(k) * 5.7 + 40, loop: lap, radius: 2.2)
            let power = max(0.15, 0.62 + 0.3 * flicker * waver + 0.32 * flicker * breath)
            let sway = signedNoise(Double(k) * 3.3 + 80, loop: lap, radius: 14) * 0.5 * flicker
            flames.append(Flame(at: Vector2(x + sway, Mensaje.side + 7.5), height: distance, power: power))
        }

        // The carried candle: out along the foot of the panel and back
        // across its face, or wherever the pointer has just been.
        if mouse != previousMouse { lastMoved = time }
        let turn = 2 * Double.pi * lap
        var carried = Vector2(Mensaje.side * (0.5 - 0.46 * cos(turn)), Mensaje.side * (0.74 - 0.3 * sin(turn)))
        if time - lastMoved < 4 {
            let pointer = (mouse - corner) / perCm
            held = holding ? held + (pointer - held) * (1 - exp(-deltaTime * 8)) : pointer
            holding = true
            carried = held
        } else {
            holding = false
        }
        let waver = signedNoise(71.3, loop: lap, radius: 26)
        flames.append(Flame(at: carried, height: distance * 0.8, power: max(0.2, 0.9 + 0.25 * flicker * waver)))
        return flames
    }

    /// The light a flame lays on a point of the panel's plane, as a share of
    /// what falls straight under it.
    private static func reach(_ flame: Flame, _ p: Vector2) -> (light: Double, toward: Vector2, graze: Double) {
        let off = flame.at - p
        let flat = off.length
        let far = (flat * flat + flame.height * flame.height).squareRoot()
        let cosine = flame.height / far
        return (flame.power * cosine * cosine * cosine, flat > 1e-9 ? off / flat : Vector2(0, -1), flat / far)
    }

    private static let candlelight = SIMD3<Double>(1.0, 0.62, 0.27)
    private static let gold = SIMD3<Double>(0.78, 0.50, 0.13)
    private static let brightGold = SIMD3<Double>(1.0, 0.74, 0.26)
    private static let room = SIMD3<Double>(0.012, 0.010, 0.009)

    private func toCanvas(_ p: Vector2) -> Vector2 { corner + p * perCm }

    /// A round pool of light as a gradient: the stops follow the same
    /// falloff the punches use, out to eight times the candle's distance.
    private func pool(_ flame: Flame, tint: SIMD3<Double>) -> Gradient {
        let stops = 28
        let colors = (0 ... stops).map { k -> Color in
            let flat = 8 * flame.height * Double(k) / Double(stops)
            let cosine = flame.height / (flat * flat + flame.height * flame.height).squareRoot()
            let light = k == stops ? 0 : flame.power * cosine * cosine * cosine
            return Color(linear: tint * Mensaje.candlelight * light)
        }
        return Gradient.radial(center: toCanvas(flame.at), radius: 8 * flame.height * perCm, colors)
    }

    /// The wall takes a little of every candle.
    private func drawTheWall(_ flames: [Flame]) {
        noStroke()
        blendMode(.add)
        for flame in flames {
            fill(pool(flame, tint: SIMD3<Double>(0.05, 0.045, 0.04)))
            drawRect(0, 0, width, height)
        }
        blendMode(.normal)
    }

    /// The gilded sheet. It takes the room's own dim light and a pool under
    /// every candle. Each square of leaf has turned its own amount, and takes
    /// the shine where its tilt puts it.
    private func drawTheGilding(_ flames: [Flame]) {
        let at = corner, size = Mensaje.side * perCm
        noStroke()
        // The panel's own edge, a box standing off the wall.
        fill(Color(hex: 0x050404))
        drawRect(at.x - 3, at.y - 3, size + 6, size + 9)
        fill(Color(linear: Mensaje.gold * Mensaje.room * 6))
        drawRect(at.x, at.y, size, size)

        blendMode(.add)
        for flame in flames {
            fill(pool(flame, tint: Mensaje.gold))
            drawRect(at.x, at.y, size, size)
        }

        // The shine. A square tilted by a small angle takes the flame's
        // glint twice that angle away. So the pool of shine breaks along the
        // joins of the leaf. One gradient per flame serves every square:
        // each square draws it moved over by its own tilt.
        let cell = size / Double(Mensaje.leaves)
        let sharpness = 70.0
        for flame in flames {
            let spread = 2 * flame.height / sharpness.squareRoot()
            let stops = 14
            let colors = (0 ... stops).map { k -> Color in
                let t = 3 * Double(k) / Double(stops)
                let flat = t * spread
                let cosine = flame.height / (flat * flat + flame.height * flame.height).squareRoot()
                let power = k == stops ? 0 : 0.55 * flame.power * cosine * cosine * exp(-t * t / 2)
                return Color(linear: Mensaje.brightGold * Mensaje.candlelight * power)
            }
            fill(Gradient.radial(center: toCanvas(flame.at), radius: 3 * spread * perCm, colors))
            for (i, square) in leaf.enumerated() {
                let shift = square.tilt * (-2 * flame.height * perCm)
                withState {
                    translate(shift)
                    drawRect(at.x + Double(i % Mensaje.leaves) * cell - shift.x,
                             at.y + Double(i / Mensaje.leaves) * cell - shift.y, cell, cell)
                }
            }
        }

        // Bare gilding turns: each square of leaf its own way, some toward
        // green and some toward a bruised purple. The shine dulls with it.
        blendMode(.multiply)
        for (i, square) in leaf.enumerated() {
            let turned = tarnish * square.turned
            let cast = square.hue < 0.65 ? SIMD3<Double>(0.62, 0.74, 0.52) : SIMD3<Double>(0.66, 0.56, 0.66)
            let tint = SIMD3<Double>(repeating: square.tone) * (SIMD3<Double>(repeating: 1 - turned) + cast * turned)
            fill(Color(linear: tint))
            drawRect(at.x + Double(i % Mensaje.leaves) * cell, at.y + Double(i / Mensaje.leaves) * cell, cell, cell)
        }
        blendMode(.normal)
    }

    /// Every punch has a raised rim, lit by the light that reaches it. Its
    /// dent is dark, or black where it went through, with a glint on the wall
    /// that faces a flame. A circle takes one flat color, so the punches are
    /// sorted by their light into a few dozen steps. Each step is drawn in
    /// one call, the dent as the circle's fill and the rim as its stroke.
    private func drawThePunches(_ flames: [Flame]) {
        let steps = 40
        var held = [[Circle]](repeating: [], count: steps)
        var gone = [[Circle]](repeating: [], count: steps)
        var glints = [[Circle]](repeating: [], count: steps)
        // Light is stepped evenly in what the eye sees, not in energy.
        func step(_ light: Double, top: Double) -> Int {
            min(steps - 1, Int((min(light, top) / top).squareRoot() * Double(steps)))
        }
        for (i, punch) in punches.enumerated() {
            var light = 0.0
            var first = (shine: 0.0, toward: Vector2.zero), second = first
            for flame in flames {
                let (share, toward, graze) = Mensaje.reach(flame, punch.at)
                light += share
                let shine = Mensaje.shine(share, graze)
                if shine > first.shine {
                    second = first
                    first = (shine, toward)
                } else if shine > second.shine {
                    second = (shine, toward)
                }
            }
            if punch.pierced { gone[step(light, top: 1.6)].append(rings[i]) } else { held[step(light, top: 1.6)].append(rings[i]) }
            // The far wall of a dent faces the flame, so the glint sits on
            // the side of the punch away from it. The lower the light comes
            // in, the more of it that wall takes. The two brightest flames
            // are enough: a third glint is under a pixel of light.
            let r = punch.radius * perCm
            for glint in [first, second] where glint.shine > 0.03 {
                glints[step(glint.shine, top: 1.1)].append(
                    Circle(center: rings[i].center - glint.toward * (0.55 * r), radius: 0.6 * r))
            }
        }
        strokeWeight(0.55 * Mensaje.punch * perCm)
        for k in 0 ..< steps {
            let light = pow((Double(k) + 0.5) / Double(steps), 2) * 1.6
            let rim = Mensaje.brightGold * (Mensaje.room * 5 + Mensaje.candlelight * light * 0.9)
            stroke(Color(linear: rim))
            if !held[k].isEmpty {
                fill(Color(linear: rim * SIMD3<Double>(0.2, 0.16, 0.12)))
                drawCircles(held[k])
            }
            if !gone[k].isEmpty {
                fill(Color(hex: 0x070203))
                drawCircles(gone[k])
            }
        }
        noStroke()
        blendMode(.add)
        for k in 0 ..< steps where !glints[k].isEmpty {
            let shine = pow((Double(k) + 0.5) / Double(steps), 2) * 1.1
            fill(Color(linear: Mensaje.brightGold * Mensaje.candlelight * (shine * 1.5)))
            drawCircles(glints[k])
        }
        blendMode(.normal)
    }

    /// How brightly the lit wall of a dent shines, from the light that
    /// reaches it and how low that light comes in.
    private static func shine(_ share: Double, _ graze: Double) -> Double {
        share * (0.35 + 1.9 * graze)
    }

    /// The flames themselves, each with its glow, and the wax under the
    /// ones that stand.
    private func drawTheFlames(_ flames: [Flame]) {
        noStroke()
        for (k, flame) in flames.enumerated() {
            let at = toCanvas(flame.at)
            let tall = 12 + 6 * flame.power
            if k < flames.count - 1 {
                fill(Gradient.linear(from: Vector2(at.x, at.y + tall * 0.5), to: Vector2(at.x, at.y + tall * 0.5 + 60),
                                     [Color(hex: 0xB89A6A), Color(hex: 0x4A3B28), Color(hex: 0x1A140E)]))
                drawRect(at.x - 6, at.y + tall * 0.5, 12, 70, cornerRadius: 2)
            }
            blendMode(.add)
            let glow = (0 ... 10).map { j -> Color in
                let t = Double(j) / 10
                return Color(linear: Mensaje.candlelight * (j == 10 ? 0 : 0.3 * flame.power * exp(-t * t * 9)))
            }
            fill(Gradient.radial(center: at, radius: 70, glow))
            drawCircle(at.x, at.y, 70)
            blendMode(.normal)
            fill(Color(hex: 0xFFAE42))
            drawEllipse(at.x, at.y, 3.8, tall * 0.5)
            fill(Color(hex: 0xFFF3D0))
            drawEllipse(at.x, at.y + tall * 0.14, 2.1, tall * 0.3)
        }
    }

    // MARK: - The plan

    /// The sheet as a punching plan: the square, and a circle for every
    /// punch, filled where it goes through.
    private func drawThePlan() {
        background(.white)
        let at = corner, size = Mensaje.side * perCm
        noFill()
        stroke(.black)
        strokeWeight(0.6)
        drawRect(at.x, at.y, size, size)
        strokeWeight(0.35)
        for punch in punches {
            let center = toCanvas(punch.at)
            if punch.pierced { fill(.black) } else { noFill() }
            drawCircle(center.x, center.y, punch.radius * perCm)
        }
    }
}

↑↓ move ↩ open