PROCITY/web/js/world/weather.js
m3ultra ee16545941 Lane B round 17 (v3.2): wind sway SHIPS -- weather-driven gum-tree sway (ledger #5, parked since R7)
Verdict: shipped, not killed. The R7 park reason ("touches shared materials") is a non-blocker under
R16's measurement-over-brief standard: the gum-tree billboard material is B-owned (furniture.js), so
this is entirely Lane B -- no C/E material seam. Verified fresh Chromium seed 20261990.

- New web/js/world/wind.js: shared uniforms (uWindTime/uWindAmp) + applyWind(material) (onBeforeCompile
  vertex patch: top-weighted horizontal sway, per-instance world-position phase, USE_INSTANCING-guarded)
  + updateWind(dt,intensity). furniture.js applies it to the tree material; weather.js drives it from
  update(dt) (base breeze 0.05 clear -> 0.05+intensity*0.30 ~0.35 rain gusts).
- Trees only. Awnings excluded by MEASUREMENT: rigid posted-verandah box slabs don't flutter (fabric
  canopy ripple = separate v3.3, shares the rigid awning InstancedMesh today).
- Byte-identical when weather off (the covenant): updateWind runs only inside weather.update, and the
  weather system isn't constructed under ?weather=0 / ?classic=1 -> uWindAmp stays 0 -> sin(...)*0.0==0.0
  -> identical vertices. Proven: amp=0 two times = 0/180000 px differ; ?weather=0 boot = 0 tree-motion px;
  ?classic=1 = amp 0, trees render, 0 errors. No plan/fetch/rng change -> F's classic gate untouched.
- Sway works: rain amp 0.305 moves ~14100/180000 region px over 0.8s (exaggerated 2.5 -> 101k); trees
  render clean, no glitch. ~0 draws (ALU ops in the existing tree InstancedMesh vertex program), 0 errors.

-> Lane F (B->F handshake): SWAY SHIPPED. In old frames under the default boot; ?classic=1/?weather=0
   sway-free + byte-identical. Procedural (no asset -> E has nothing to do).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-16 14:30:09 +10:00

134 lines
6.8 KiB
JavaScript

// PROCITY Lane B — weather.js
// v2 weather (?weather=1), default-off. Seeded per (citySeed, day): clear / overcast / rain, with
// AU-plausible weights. Rain is ONE draw — a camera-following Points layer with a procedural streak
// sprite + a falling/wind vertex shader — plus a wet-ground response (roughness/darken on the shared
// ground materials) and a matching rainy sky dome (lighting.setSky). Exposes { state, intensity } at
// window.PROCITY.weather for other lanes (Lane D reads it for weather-reactive crowds).
//
// Fully procedural (no fetch beyond the sky JPEG the town already loads). Flag-off is byte-identical:
// the shell simply never constructs this, so the default path is untouched.
import * as THREE from 'three';
import { rng, frange, pick } from '../core/prng.js';
import { updateWind } from './wind.js'; // R17: weather drives the tree-sway uniform (off ⇒ never called ⇒ calm)
const RAIN_SKIES = ['summer-storm', 'monsoon', 'cold-front'];
const OVERCAST_SKIES = ['grey-drizzle', 'cold-front', 'high-cirrus'];
/**
* weatherFor(citySeed, day=0) → { state:'clear'|'overcast'|'rain', intensity:0..1 }
* Deterministic. Mostly clear, some overcast, occasional rain (AU coastal-town plausible).
* `day` lets a future day-counter roll new weather; today the town has one day, so day=0.
*/
export function weatherFor(citySeed, day = 0) {
const r = rng(citySeed >>> 0, 'weather', day | 0);
const x = r();
if (x < 0.55) return { state: 'clear', intensity: 0 };
if (x < 0.80) return { state: 'overcast', intensity: +frange(r, 0.3, 0.6).toFixed(2) };
return { state: 'rain', intensity: +frange(r, 0.45, 1.0).toFixed(2) };
}
// procedural vertical rain-streak sprite (canvas — no fetch)
function streakTexture() {
const c = document.createElement('canvas'); c.width = 8; c.height = 64;
const x = c.getContext('2d');
const g = x.createLinearGradient(0, 0, 0, 64);
g.addColorStop(0, 'rgba(255,255,255,0)'); g.addColorStop(0.5, 'rgba(255,255,255,0.95)'); g.addColorStop(1, 'rgba(255,255,255,0)');
x.fillStyle = g; x.fillRect(3, 0, 2, 64);
const t = new THREE.CanvasTexture(c); t.colorSpace = THREE.SRGBColorSpace; t.generateMipmaps = false; t.minFilter = THREE.LinearFilter;
return t;
}
/**
* createWeather({ scene, plan, skins, lighting, camera, force }) → { group, state, update, dispose }
* The shell constructs this only under ?weather; `force` (a state string) overrides the seed for
* testing/demos. Call update(dt) each street frame. Sets window.PROCITY.weather to the live state.
*/
export function createWeather({ scene, plan, skins, lighting, camera, force = null }) {
const state = force && /^(clear|overcast|rain)$/.test(force)
? { state: force, intensity: force === 'clear' ? 0 : (force === 'rain' ? 0.85 : 0.5) }
: weatherFor(plan.citySeed);
const group = new THREE.Group(); group.name = 'weather';
scene.add(group);
const r = rng(plan.citySeed >>> 0, 'weathersky', 0);
// ── matching sky dome ──
if (state.state === 'rain') lighting.setSky(pick(r, RAIN_SKIES));
else if (state.state === 'overcast') lighting.setSky(pick(r, OVERCAST_SKIES));
// ── wet ground (rain only): darken + drop roughness on the shared ground materials ──
const groundRestore = [];
if (state.state === 'rain') {
const gg = scene.getObjectByName('ground');
if (gg) gg.traverse((o) => {
if (!o.isMesh || !o.material || !o.material.isMeshStandardMaterial) return;
const m = o.material;
if (m.userData._wet) return; // shared material — touch once, restore once
m.userData._wet = true;
groundRestore.push({ m, color: m.color.clone(), roughness: m.roughness, metalness: m.metalness });
m.color.multiplyScalar(0.6); m.roughness = Math.min(m.roughness, 0.35); m.metalness = 0.18; m.needsUpdate = true;
});
}
// ── rain particles: ONE draw (Points), camera-following, procedural streaks ──
let rainMat = null;
if (state.state === 'rain') {
const COUNT = Math.round(700 + state.intensity * 1500);
const BOX = 48, BOXH = 28;
const pos = new Float32Array(COUNT * 3), phase = new Float32Array(COUNT);
for (let i = 0; i < COUNT; i++) {
pos[i * 3] = frange(r, -BOX / 2, BOX / 2);
pos[i * 3 + 1] = frange(r, -BOXH / 2, BOXH / 2);
pos[i * 3 + 2] = frange(r, -BOX / 2, BOX / 2);
phase[i] = r() * BOXH;
}
const geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.BufferAttribute(pos, 3));
geo.setAttribute('aPhase', new THREE.BufferAttribute(phase, 1));
rainMat = new THREE.ShaderMaterial({
transparent: true, depthWrite: false,
uniforms: {
uTime: { value: 0 }, uTex: { value: streakTexture() }, uBoxH: { value: BOXH },
uSpeed: { value: 32 }, uSize: { value: 5.5 }, uWind: { value: 0.18 }, uOpacity: { value: 0.34 + state.intensity * 0.28 },
},
vertexShader: `
uniform float uTime, uBoxH, uSpeed, uSize, uWind; attribute float aPhase;
void main(){
vec3 p = position;
float fall = mod(uTime * uSpeed + aPhase, uBoxH);
p.y = position.y - fall; if (p.y < -uBoxH * 0.5) p.y += uBoxH; // fall + wrap
p.x += (uBoxH * 0.5 - p.y) * uWind; // wind slant
vec4 mv = modelViewMatrix * vec4(p, 1.0);
gl_Position = projectionMatrix * mv;
gl_PointSize = clamp(uSize * 30.0 / max(-mv.z, 1.0), 2.0, 22.0); // capped so near drops stay streaks, not blobs
}`,
fragmentShader: `
uniform sampler2D uTex; uniform float uOpacity;
void main(){ vec4 t = texture2D(uTex, gl_PointCoord); if (t.a < 0.04) discard; gl_FragColor = vec4(vec3(0.72, 0.77, 0.86), t.a * uOpacity); }`,
});
const pts = new THREE.Points(geo, rainMat);
pts.frustumCulled = false; // the box always surrounds the camera
group.add(pts);
}
function update(dt) {
group.position.copy(camera.position); // the rain box follows the player
if (rainMat) rainMat.uniforms.uTime.value += dt;
updateWind(dt, state.intensity); // sway the gum trees — calm breeze → rain gusts (weather-on only)
}
function dispose() {
for (const g of groundRestore) { g.m.color.copy(g.color); g.m.roughness = g.roughness; g.m.metalness = g.metalness; g.m.userData._wet = false; g.m.needsUpdate = true; }
group.traverse((o) => {
if (o.geometry) o.geometry.dispose();
if (o.material) { if (o.material.uniforms && o.material.uniforms.uTex) o.material.uniforms.uTex.value.dispose(); o.material.dispose(); }
});
scene.remove(group);
}
// publish the contract for other lanes (Lane D reads window.PROCITY.weather)
if (typeof window !== 'undefined') { window.PROCITY = window.PROCITY || {}; window.PROCITY.weather = state; }
return { group, state, update, dispose };
}