Back to Drift City
SOURCE / PINNED RELEASE

Made of little things.

Drift City

Release
098731aa0f0f…
Author-recorded commit
2822ea0a4f5b…
License
LICENSE
Author’s source reference
nostr://npub12lm3rxsva8kfempyjkxupt09e02n3fezurkyadyqfejdc5e0r2kscqty6r/wss%3A%2F%2Fgit.napplet.soy%2F/n-a4c0039c855

Archive hash verified: b3323e408235502b…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

src/game/facades.ts
import * as THREE from 'three';
import { WALL_MATERIALS } from './geo.js';
import { LAMP, LAMP_GLSL, NIGHT, NIGHT_GLSL } from './night.js';
import { JOINT_COLOR, SURFACE_SIZE, wallSurfaces } from './wall-surfaces.js';

/**
 * The buildings' walls: the façade atlas (the window and door styles, one bay by one
 * storey each) and the walls' material, which tiles it across them.
 *
 * Three levels, for the graphics settings. Low (0): the atlas at 64 px a cell. Medium (1):
 * the same atlas drawn at 128 px (the windows exactly where they were, with crisper frames
 * and glazing), curtains and blinds that vary window to window, and each wall's material
 * (brick, stone, plaster, concrete, siding, corrugated metal, timber, or a glass tower's
 * metal) at its real size on the plain wall between the windows, its relief catching the
 * light. High (2): as Medium, and a room behind every window (interior mapping: a box
 * the view looks into, so it shifts as you go by), lit at night in the windows with
 * their lights on.
 */

/** The window and door styles in the façade atlas. */
export const FACADE = {
  classic: 0,
  arched: 1,
  twin: 2,
  ribbon: 3,
  curtain: 4,
  spandrel: 5,
  shop: 6,
  balcony: 7,
  // The doors (each in place of one bay of a ground storey).
  shopDoor: 8,
  entrance: 9,
  officeDoor: 10,
  lobby: 11,
  houseDoor: 12,
  roller: 13,
  churchDoor: 14,
  blank: 15,
} as const;

/**
 * The façade atlas: eight window styles and eight door styles, each one bay of wall by one
 * storey (a 64 px cell, the storey's top at the top of the cell). Opaque pixels are wall
 * and trim, which the wall colour tints; half-transparent pixels are glass, drawn in their
 * own colours (and lit at night); nearly transparent pixels are solid things in their own
 * colours (doors). Every cell keeps an opaque strip along its foot, where untextured faces
 * sample (and where a door's step is).
 */
function facadeAtlas(S = 64): THREE.Texture {
  // (Drawn in 64ths of a cell whatever the size, so the windows are in the same places.)
  const k = S / 64;
  const sharp = S > 64;
  const c = document.createElement('canvas');
  c.width = S * 4;
  c.height = S * 4;
  const g = c.getContext('2d')!;
  g.fillStyle = '#ffffff';
  g.fillRect(0, 0, c.width, c.height);
  for (let f = 0; f < 16; f++) {
    const ox = (f % 4) * S;
    const oy = (3 - Math.floor(f / 4)) * S;
    const rect = (x: number, y: number, w: number, h: number, color: string) => {
      g.fillStyle = color;
      g.fillRect(ox + x * k, oy + y * k, w * k, h * k);
    };
    // Glass: cleared, then painted half see-through, a little lighter at the top. Sharp:
    // with a fine dark frame round it and a glint along its top.
    const glass = (x: number, y: number, w: number, h: number, top: string, bottom: string) => {
      g.clearRect(ox + x * k, oy + y * k, w * k, h * k);
      const grad = g.createLinearGradient(0, oy + y * k, 0, oy + (y + h) * k);
      grad.addColorStop(0, top);
      grad.addColorStop(1, bottom);
      g.fillStyle = grad;
      g.fillRect(ox + x * k, oy + y * k, w * k, h * k);
      if (sharp) {
        g.strokeStyle = 'rgba(40,44,52,0.55)';
        g.lineWidth = 1;
        g.strokeRect(ox + x * k + 0.5, oy + y * k + 0.5, w * k - 1, h * k - 1);
        g.fillStyle = 'rgba(235,242,248,0.5)';
        g.fillRect(ox + x * k + 1, oy + y * k + 1, w * k - 2, 1);
      }
    };
    // A solid thing in its own colour (a door), not tinted and never lit.
    const solid = (x: number, y: number, w: number, h: number, color: string) => {
      g.clearRect(ox + x * k, oy + y * k, w * k, h * k);
      g.fillStyle = `rgb(${color} / 0.2)`;
      g.fillRect(ox + x * k, oy + y * k, w * k, h * k);
    };
    const blue = ['rgba(122,150,184,0.5)', 'rgba(52,68,96,0.5)'] as const;
    const teal = ['rgba(150,190,196,0.5)', 'rgba(58,92,104,0.5)'] as const;
    const trim = '#d4d0c8';
    const dark = '#5c5f66';
    switch (f) {
      case FACADE.classic:
        // A sash window with a stone lintel and sill.
        rect(14, 10, 36, 5, trim);
        glass(17, 15, 30, 30, ...blue);
        rect(31, 15, 2, 30, trim);
        rect(17, 29, 30, 2, trim);
        rect(13, 45, 38, 4, trim);
        break;
      case FACADE.arched:
        // A tall round-headed window with a keystone.
        g.clearRect(ox + 20 * k, oy + 22 * k, 24 * k, 30 * k);
        g.beginPath();
        g.arc(ox + 32 * k, oy + 22 * k, 12 * k, Math.PI, 0);
        g.rect(ox + 20 * k, oy + 22 * k, 24 * k, 30 * k);
        g.save();
        g.clip();
        glass(18, 8, 28, 46, ...teal);
        g.restore();
        rect(30, 6, 4, 6, trim);
        rect(31, 18, 2, 34, trim);
        rect(18, 52, 28, 3, trim);
        break;
      case FACADE.twin:
        // Two narrow windows with shutters.
        for (const x of [9, 37]) {
          rect(x - 4, 16, 4, 28, dark);
          rect(x + 18, 16, 4, 28, dark);
          glass(x, 16, 18, 28, ...blue);
          rect(x - 1, 44, 20, 3, trim);
        }
        break;
      case FACADE.ribbon:
        // A continuous band of glass between concrete floor strips.
        glass(0, 18, 64, 26, ...blue);
        rect(0, 18, 64, 2, trim);
        for (const x of [0, 31]) rect(x, 18, 2, 26, dark);
        break;
      case FACADE.curtain:
        // Floor-to-ceiling glass on slim mullions.
        glass(0, 0, 64, 58, 'rgba(170,205,222,0.5)', 'rgba(64,104,136,0.5)');
        rect(0, 0, 2, 58, '#c9ced4');
        rect(31, 0, 2, 58, '#c9ced4');
        rect(0, 54, 64, 4, '#9aa3ad');
        break;
      case FACADE.spandrel:
        // Tall dark glass with a solid panel at each floor.
        glass(0, 0, 64, 42, 'rgba(96,120,140,0.5)', 'rgba(30,40,54,0.5)');
        rect(0, 0, 3, 42, dark);
        rect(30, 0, 4, 42, dark);
        rect(0, 42, 64, 3, dark);
        break;
      case FACADE.shop:
        // A shop window under a sign band, on a low riser.
        rect(0, 4, 64, 12, '#bdb8ae');
        glass(4, 20, 56, 34, 'rgba(200,214,222,0.5)', 'rgba(70,84,96,0.5)');
        rect(2, 18, 60, 2, dark);
        rect(31, 20, 2, 34, dark);
        rect(2, 54, 60, 7, dark);
        break;
      case FACADE.shopDoor:
        // The shop's door, glazed, with a narrow window beside it, under the sign band.
        rect(0, 4, 64, 12, '#bdb8ae');
        rect(2, 18, 60, 2, dark);
        glass(4, 20, 18, 34, 'rgba(200,214,222,0.5)', 'rgba(70,84,96,0.5)');
        rect(2, 54, 22, 7, dark);
        solid(26, 20, 34, 41, '58 62 70');
        glass(30, 24, 26, 30, 'rgba(210,222,230,0.5)', 'rgba(80,96,108,0.5)');
        rect(42, 24, 2, 30, dark);
        break;
      case FACADE.entrance:
        // A block's front door: double doors with a fanlight in a stone surround.
        rect(14, 12, 36, 49, trim);
        glass(20, 16, 24, 8, ...blue);
        solid(20, 25, 24, 36, '74 54 40');
        rect(31, 25, 2, 36, '#3a2a20');
        rect(12, 9, 40, 4, dark);
        break;
      case FACADE.officeDoor:
        // Glass doors in a glass front, a dark frame round them.
        glass(0, 0, 64, 61, 'rgba(170,205,222,0.5)', 'rgba(64,104,136,0.5)');
        rect(0, 0, 2, 61, '#c9ced4');
        rect(62, 0, 2, 61, '#c9ced4');
        solid(14, 18, 36, 43, '46 52 60');
        glass(17, 21, 14, 40, 'rgba(190,215,228,0.5)', 'rgba(80,110,132,0.5)');
        glass(33, 21, 14, 40, 'rgba(190,215,228,0.5)', 'rgba(80,110,132,0.5)');
        rect(0, 15, 64, 3, '#9aa3ad');
        break;
      case FACADE.lobby:
        // A tower's lobby: tall glass, and a revolving door in the middle.
        glass(2, 4, 60, 57, 'rgba(180,210,226,0.5)', 'rgba(60,96,126,0.5)');
        for (const x of [2, 16, 46, 60]) rect(x, 4, 2, 57, '#c9ced4');
        solid(20, 24, 24, 37, '52 58 66');
        glass(23, 27, 18, 34, 'rgba(200,222,234,0.5)', 'rgba(90,118,140,0.5)');
        rect(31, 27, 2, 34, '#c9ced4');
        rect(18, 20, 28, 4, '#c9ced4');
        break;
      case FACADE.houseDoor:
        // A panelled front door with a little window over it, in a painted frame.
        rect(20, 16, 24, 45, trim);
        glass(24, 19, 16, 6, ...blue);
        solid(24, 26, 16, 35, '94 62 44');
        rect(26, 30, 5, 10, '#4e3424');
        rect(33, 30, 5, 10, '#4e3424');
        rect(26, 45, 5, 12, '#4e3424');
        rect(33, 45, 5, 12, '#4e3424');
        break;
      case FACADE.roller:
        // A roller door, and a door for people beside it.
        solid(4, 12, 42, 49, '128 134 140');
        for (let y = 15; y < 61; y += 4) rect(4, y, 42, 1, '#6a6e74');
        rect(2, 9, 46, 3, dark);
        solid(51, 26, 10, 35, '70 78 88');
        break;
      case FACADE.churchDoor:
        // An arched double door of oak in a stone surround.
        g.save();
        g.beginPath();
        g.arc(ox + 32 * k, oy + 28 * k, 16 * k, Math.PI, 0);
        g.rect(ox + 16 * k, oy + 28 * k, 32 * k, 33 * k);
        g.clip();
        g.clearRect(ox + 14 * k, oy + 10 * k, 36 * k, 51 * k);
        g.fillStyle = 'rgb(84 56 36 / 0.2)';
        g.fillRect(ox + 14 * k, oy + 10 * k, 36 * k, 51 * k);
        g.restore();
        rect(31, 14, 2, 47, '#3a2a20');
        rect(12, 26, 4, 35, trim);
        rect(48, 26, 4, 35, trim);
        break;
      case FACADE.blank:
        // Plain wall (under a door built on, say).
        break;
      case FACADE.balcony:
        // French windows onto a little balcony with railings.
        glass(16, 8, 32, 40, ...blue);
        rect(31, 8, 2, 40, trim);
        rect(8, 44, 48, 3, dark);
        rect(8, 52, 48, 4, trim);
        for (let x = 9; x < 56; x += 5) rect(x, 44, 2, 9, dark);
        break;
    }
    // The opaque foot every cell keeps.
    rect(0, 61, 64, 3, '#ffffff');
  }
  const t = new THREE.CanvasTexture(c);
  // (Low's pixels crisp blocks; the sharper atlas smooth.)
  t.magFilter = sharp ? THREE.LinearFilter : THREE.NearestFilter;
  t.colorSpace = THREE.SRGBColorSpace;
  t.anisotropy = sharp ? 8 : 4;
  return t;
}

/**
 * The walls' material: each face samples its own façade from the atlas, tiling bay by
 * bay and storey by storey. The wall colour tints the wall but not the glass, and each
 * window gets its own shade: a few have the blinds down, a few have the lights on.
 * The façade attribute is the style plus a fraction that varies building to building.
 */
export function wallMaterial(level: 0 | 1 | 2 = 0): THREE.MeshLambertMaterial {
  if (level !== 0) return detailedWallMaterial(level);
  const m = new THREE.MeshLambertMaterial({ vertexColors: true, map: facadeAtlas() });
  m.onBeforeCompile = (shader) => {
    shader.uniforms.nightLights = NIGHT;
    Object.assign(shader.uniforms, LAMP);
    shader.vertexShader = shader.vertexShader
      .replace('#include <common>', '#include <common>\nattribute float facade;\nflat varying float vFacade;\nvarying vec3 vLampPos;\nvarying vec3 vLampN;')
      .replace('#include <uv_vertex>', '#include <uv_vertex>\nvFacade = facade;')
      .replace('#include <project_vertex>', '#include <project_vertex>\nvLampPos = (modelMatrix * vec4(transformed, 1.0)).xyz;\nvLampN = normal;');
    shader.fragmentShader = shader.fragmentShader
      .replace('#include <common>', `#include <common>\nflat varying float vFacade;\nvarying vec3 vLampPos;\nvarying vec3 vLampN;\n${NIGHT_GLSL}\n${LAMP_GLSL}`)
      .replace(
        '#include <map_fragment>',
        `{
          float style = floor(vFacade + 0.001);
          // Whole numbers only into the hash: it magnifies the least difference.
          float seed = floor(fract(vFacade) * 16.0 + 0.5);
          vec2 cell = vec2(mod(style, 4.0), floor(style / 4.0));
          const vec2 atlas = vec2(4.0, 4.0);
          vec2 uv = (cell + 0.03 + fract(vMapUv) * 0.94) / atlas;
          vec4 tex = textureGrad(map, uv, dFdx(vMapUv) * 0.94 / atlas, dFdy(vMapUv) * 0.94 / atlas);
          // Glass (half see-through in the atlas), or a solid thing in its own colour (nearly see-through).
          float own = 1.0 - smoothstep(0.24, 0.36, tex.a);
          float isGlass = (1.0 - smoothstep(0.6, 0.9, tex.a)) * (1.0 - own);
          vec2 win = mod(floor(vMapUv) + vec2(seed * 7.0, style * 13.0), 289.0);
          float h = fract(sin(dot(win, vec2(12.9898, 78.233))) * 43758.5453);
          vec3 glass = tex.rgb * (0.85 + 0.3 * h);
          glass = mix(glass, vec3(1.0, 0.84, 0.52), step(0.97, h) * 0.75);
          glass = mix(glass, vec3(0.8, 0.8, 0.78), step(h, 0.05) * 0.6);
          diffuseColor.rgb *= mix(mix(tex.rgb * vColor, glass, isGlass), tex.rgb, own);
          // At night, about a third of the windows lit: most warm, some cool, each its own brightness.
          float lit = step(0.62, fract(h * 13.7));
          vec3 lamp = mix(vec3(1.0, 0.62, 0.32), vec3(0.62, 0.74, 1.0), step(0.85, fract(h * 31.1)));
          totalEmissiveRadiance += lamp * (0.35 + 0.55 * fract(h * 5.3)) * lit * isGlass * nightLights;
          if (nightLights > 0.001) totalEmissiveRadiance += lampLight(vLampPos, vLampN, diffuseColor.rgb) * nightLights;
        }`,
      )
      .replace('#include <color_fragment>', '');
  };
  return m;
}

/**
 * The walls on Medium (1) and High (2): the sharper atlas; the wall's material at its
 * real size on the plain wall (not the trim, frames or glass), its relief tilting the
 * light; curtains and blinds in some windows; and on High, a room behind each window.
 * The vertices carry each wall's material (wallMat) and its window cell (cell: the bay's
 * width and the storey's height, m, and whether it's a tower's), from Soup.facadeWall.
 */
function detailedWallMaterial(level: 1 | 2): THREE.MeshLambertMaterial {
  const m = new THREE.MeshLambertMaterial({ vertexColors: true, map: facadeAtlas(128) });
  // (Any material takes defines; three's types only list them on some.)
  (m as unknown as { defines: Record<string, number> }).defines = { FACADE_LEVEL: level };
  const surfaces = wallSurfaces();
  const sizes = WALL_MATERIALS.map((k) => SURFACE_SIZE[k]);
  const joints = WALL_MATERIALS.map((k) => new THREE.Color(JOINT_COLOR[k]));
  m.onBeforeCompile = (shader) => {
    shader.uniforms.nightLights = NIGHT;
    Object.assign(shader.uniforms, LAMP);
    shader.uniforms.surfaceTex = { value: surfaces };
    shader.uniforms.surfaceSize = { value: sizes };
    shader.uniforms.jointColor = { value: joints };
    shader.vertexShader = shader.vertexShader
      .replace(
        '#include <common>',
        '#include <common>\nattribute float facade;\nattribute float wallMat;\nattribute vec3 cell;\nflat varying float vFacade;\nflat varying float vWallMat;\nvarying vec3 vCell;\nvarying vec3 vLampPos;\nvarying vec3 vLampN;',
      )
      .replace('#include <uv_vertex>', '#include <uv_vertex>\nvFacade = facade;\nvWallMat = wallMat;\nvCell = cell;')
      .replace('#include <project_vertex>', '#include <project_vertex>\nvLampPos = (modelMatrix * vec4(transformed, 1.0)).xyz;\nvLampN = normal;');
    shader.fragmentShader = shader.fragmentShader
      .replace(
        '#include <common>',
        /* glsl */ `#include <common>
        flat varying float vFacade;
        flat varying float vWallMat;
        varying vec3 vCell;
        varying vec3 vLampPos;
        varying vec3 vLampN;
        uniform highp sampler2DArray surfaceTex;
        uniform float surfaceSize[8];
        uniform vec3 jointColor[8];
        // The wall's relief (slope across and up) and how much of this texel is plain wall, for the lighting.
        vec2 gRelief = vec2(0.0);
        float gWall = 0.0;
        ${NIGHT_GLSL}
        ${LAMP_GLSL}`,
      )
      .replace(
        '#include <map_fragment>',
        /* glsl */ `{
          float style = floor(vFacade + 0.001);
          // Whole numbers only into the hash: it magnifies the least difference.
          float seed = floor(fract(vFacade) * 16.0 + 0.5);
          vec2 cellAt = vec2(mod(style, 4.0), floor(style / 4.0));
          const vec2 atlas = vec2(4.0, 4.0);
          vec2 f = fract(vMapUv);
          vec2 uv = (cellAt + 0.03 + f * 0.94) / atlas;
          vec4 tex = textureGrad(map, uv, dFdx(vMapUv) * 0.94 / atlas, dFdy(vMapUv) * 0.94 / atlas);
          // Glass (half see-through in the atlas), or a solid thing in its own colour (nearly see-through).
          float own = 1.0 - smoothstep(0.24, 0.36, tex.a);
          float isGlass = (1.0 - smoothstep(0.6, 0.9, tex.a)) * (1.0 - own);
          vec2 win = mod(floor(vMapUv) + vec2(seed * 7.0, style * 13.0), 289.0);
          float h = fract(sin(dot(win, vec2(12.9898, 78.233))) * 43758.5453);
          vec3 glass = tex.rgb * (0.85 + 0.3 * h);
          glass = mix(glass, vec3(1.0, 0.84, 0.52), step(0.97, h) * 0.75);
          // At night, about a third of the windows lit: most warm, some cool, each its own brightness.
          float lit = step(0.62, fract(h * 13.7));
          vec3 lamp = mix(vec3(1.0, 0.62, 0.32), vec3(0.62, 0.74, 1.0), step(0.85, fract(h * 31.1)));
          vec3 glow = lamp * (0.35 + 0.55 * fract(h * 5.3));
          vec3 N = normalize(vLampN);
          // Along the wall (as its windows count across), and up.
          vec3 T = normalize(vec3(N.z, 0.0, -N.x) + 1e-5);
          #if FACADE_LEVEL >= 2
            // A room behind the window: a box a bay wide, a storey high and most of a bay
            // deep, looked into from where the camera is.
            if (isGlass > 0.01 && vCell.x > 0.1 && abs(N.y) < 0.5) {
              vec3 dir = normalize(vLampPos - cameraPosition);
              vec3 rd = vec3(dot(dir, T), dir.y, dot(dir, N));
              if (rd.z < -0.02) {
                vec2 size = vCell.xy;
                float depth = size.x * 0.85;
                vec3 p = vec3(f.x * size.x, f.y * size.y, 0.0);
                // (Never dividing by nothing: a ray along the wall keeps its sign, so it never
                // goes backwards; the distance kept positive, so the room never blows up.)
                vec2 dxy = vec2(rd.x >= 0.0 ? max(rd.x, 1e-4) : min(rd.x, -1e-4), rd.y >= 0.0 ? max(rd.y, 1e-4) : min(rd.y, -1e-4));
                float tx = ((dxy.x > 0.0 ? size.x : 0.0) - p.x) / dxy.x;
                float ty = ((dxy.y > 0.0 ? size.y : 0.0) - p.y) / dxy.y;
                float tz = -depth / rd.z;
                float t = clamp(min(min(tx, ty), tz), 0.0, 100.0);
                vec3 hit = p + rd * t;
                // Its walls a colour of their own; a floor, a ceiling; a dark band of furniture along the back.
                float pick = fract(h * 17.3);
                vec3 paint = pick < 0.25 ? vec3(0.93, 0.86, 0.72) : pick < 0.5 ? vec3(0.72, 0.82, 0.9) : pick < 0.75 ? vec3(0.76, 0.85, 0.72) : vec3(0.92, 0.74, 0.64);
                // Side walls a shade darker than the back; the floor boards; a pale ceiling; a dark band of furniture along the back.
                vec3 col = paint * 0.78;
                if (t == tz) col = paint * mix(0.55, 1.0, step(size.y * 0.32, hit.y));
                else if (t == ty) col = rd.y < 0.0 ? vec3(0.6, 0.43, 0.3) : vec3(0.97, 0.96, 0.92);
                // Darker further in.
                vec3 room = col * (0.55 + 0.45 * exp(-t * 0.25));
                // By day the room's a little dimmer than the street, the glass's reflection over it (more at a glance).
                // (Offices' and shops' glass reflects more than a home's window; and every window more far off.)
                float shiny = (style >= 3.0 && style <= 6.0) || style == 10.0 || style == 11.0 ? 0.55 : 0.3;
                shiny += 0.35 * smoothstep(40.0, 160.0, distance(vLampPos, cameraPosition));
                float fres = min(0.95, shiny + 0.5 * pow(1.0 - abs(rd.z), 2.0));
                glass = mix(room * 0.8, glass, fres);
                // At night the lit rooms glow, in their light.
                glow = room * lamp * 1.6 * (0.6 + 0.4 * fract(h * 5.3));
              }
            }
          #endif
          // Curtains drawn to the sides in some windows (homes' and flats': classic, arched,
          // twin and balcony), blinds part down in others (not the shops' or the doors'):
          // how many of each in a hundred. On High, half the windows dressed (homes' three
          // in ten with curtains, two with blinds; offices' and sheds' half with blinds),
          // and none of the towers'.
          float kind = fract(h * 7.31);
          bool home = style < 3.0 || style == 7.0;
          #if FACADE_LEVEL >= 2
            float curtains = home ? 0.3 : 0.0;
            float blinds = 0.5;
            if (vCell.z > 0.5) kind = 1.0;
          #else
            float curtains = home ? 0.26 : 0.0;
            float blinds = 0.42;
          #endif
          if (style == 6.0 || style > 7.5) kind = 1.0;
          if (kind < curtains) {
            float cw = 0.1 + 0.16 * fract(h * 3.7);
            float folds = 0.86 + 0.14 * sin(f.x * 90.0);
            vec3 cloth = mix(vec3(0.86, 0.8, 0.68), vec3(0.66, 0.34, 0.3), step(0.6, fract(h * 11.3)));
            float c = 1.0 - step(cw, f.x) * step(f.x, 1.0 - cw);
            glass = mix(glass, cloth * folds * 0.85, c * 0.9);
            glow *= 1.0 - c * 0.5;
          } else if (kind < blinds) {
            float down = 0.25 + 0.5 * fract(h * 5.1);
            float b = step(1.0 - down, f.y);
            float slat = 0.84 + 0.16 * step(0.5, fract(f.y * vCell.y * 14.0));
            glass = mix(glass, vec3(0.88, 0.86, 0.82) * slat, b * 0.92);
            glow *= 1.0 - b * 0.4;
          }
          // The plain wall (not the trim, the frames or the glass): its material at its real size.
          vec3 wall = tex.rgb * vColor;
          float plain = (1.0 - isGlass) * (1.0 - own) * step(0.95, min(tex.r, min(tex.g, tex.b)));
          if (vWallMat > 0.5 && abs(N.y) < 0.5) {
            int layer = int(vWallMat + 0.5) - 1;
            vec2 suv = vec2(dot(vLampPos, T), vLampPos.y) / surfaceSize[layer];
            vec4 sf = texture(surfaceTex, vec3(suv, float(layer)));
            vec3 surf = mix(vColor * (0.6 + sf.a * 0.8), jointColor[layer], sf.b * 0.85);
            wall = mix(wall, surf, plain);
            gRelief = (sf.rg * 2.0 - 1.0) * plain;
            gWall = plain;
          }
          diffuseColor.rgb *= mix(mix(wall, glass, isGlass), tex.rgb, own);
          totalEmissiveRadiance += glow * lit * isGlass * nightLights;
          if (nightLights > 0.001) totalEmissiveRadiance += lampLight(vLampPos, vLampN, diffuseColor.rgb) * nightLights;
        }`,
      )
      .replace(
        '#include <normal_fragment_maps>',
        /* glsl */ `#include <normal_fragment_maps>
        // The wall's relief: its normal tilted across and up the wall.
        if (gWall > 0.01) {
          vec3 Nw = normalize(vLampN);
          vec3 Tw = normalize(vec3(Nw.z, 0.0, -Nw.x) + 1e-5);
          vec3 Tv = normalize((viewMatrix * vec4(Tw, 0.0)).xyz);
          vec3 Bv = normalize((viewMatrix * vec4(0.0, 1.0, 0.0, 0.0)).xyz);
          normal = normalize(normal + Tv * gRelief.x + Bv * gRelief.y);
        }`,
      )
      .replace('#include <color_fragment>', '');
  };
  return m;
}