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.
import * as THREE from 'three';
/**
* The lights that come on as the sun goes down: one value, 0 by day to 1 at night (see
* Game.setTimeOfDay), shared as a uniform by the materials that glow with it: street and
* highway lamps (the map's glow attribute), lit windows, the traffic's headlights.
*/
export const NIGHT = { value: 0 };
/** GLSL: the night uniform. */
export const NIGHT_GLSL = 'uniform float nightLights;';
const blank = new THREE.DataTexture(new Uint8Array([0, 0, 0, 0]), 1, 1);
blank.needsUpdate = true;
/**
* The street lamps' light, baked (see lamp-bake.ts): a top-down map of the light all the
* lamps throw on the top surfaces under them (shadows and all), and of how high those
* surfaces are; where they lie on the map (x0, z0, x1, z1); and the lamps' colour.
*/
export const LAMP = {
lampMap: { value: blank as THREE.Texture },
lampHeight: { value: blank as THREE.Texture },
lampBounds: { value: new THREE.Vector4(0, 0, 1, 1) },
lampColor: { value: new THREE.Color(0xffc98a) },
};
/**
* GLSL: the baked lamp light at a world point with a world normal, as light to add to a
* surface of colour c: the map's value there (for a wall, the ground just in front of
* it, fading up the wall from it); nothing under a deck or a canopy (where the baked
* surface is well above).
*/
export const LAMP_GLSL = /* glsl */ `
uniform sampler2D lampMap;
uniform sampler2D lampHeight;
uniform vec4 lampBounds;
uniform vec3 lampColor;
vec3 lampLight(vec3 wp, vec3 wn, vec3 c) {
vec2 at = wp.xz + normalize(wn.xz + 1e-6) * 0.6 * (1.0 - abs(wn.y));
vec2 uv = (at - lampBounds.xy) / (lampBounds.zw - lampBounds.xy);
if (uv.x < 0.0 || uv.y < 0.0 || uv.x > 1.0 || uv.y > 1.0) return vec3(0.0);
float light = texture2D(lampMap, uv).r;
float surf = texture2D(lampHeight, uv).r * 64.0 - 8.0;
float above = wp.y - surf;
float fade = smoothstep(-2.5, -1.2, above) * (1.0 - smoothstep(0.6, 6.0, above));
return c * lampColor * light * fade;
}
`;
/**
* Make a material take the street lamps' baked light (by its world position and normal):
* for the cars and the traffic, which move about under the lamps. Keeps any shader edit
* the material already has.
*/
export function withLampLight<T extends THREE.Material>(m: T): T {
const src = m.onBeforeCompile.toString();
const before = m.onBeforeCompile.bind(m);
m.onBeforeCompile = (shader, renderer) => {
before(shader, renderer);
shader.uniforms.nightLights = NIGHT;
Object.assign(shader.uniforms, LAMP);
shader.vertexShader = shader.vertexShader
.replace('#include <common>', '#include <common>\nvarying vec3 vLampPos;\nvarying vec3 vLampN;')
.replace('#include <project_vertex>', '#include <project_vertex>\nvLampPos = (modelMatrix * vec4(transformed, 1.0)).xyz;\nvLampN = normalize(mat3(modelMatrix) * objectNormal);');
if (!shader.fragmentShader.includes('uniform float nightLights;')) shader.fragmentShader = shader.fragmentShader.replace('#include <common>', `#include <common>\n${NIGHT_GLSL}`);
shader.fragmentShader = shader.fragmentShader
.replace('#include <common>', `#include <common>\nvarying vec3 vLampPos;\nvarying vec3 vLampN;\n${LAMP_GLSL}`)
.replace('#include <emissivemap_fragment>', '#include <emissivemap_fragment>\nif (nightLights > 0.001) totalEmissiveRadiance += lampLight(vLampPos, vLampN, diffuseColor.rgb) * nightLights;');
};
const key = m.customProgramCacheKey.bind(m);
m.customProgramCacheKey = () => key() + '|lamp|' + src;
return m;
}
