Files
Jules NényandClaude Opus 5.5 fa5b242f10 V0.1 : le fil des logos, les documents d'origine, le hors-champ en trois systèmes
Retour de Jules sur la version 4 (08/10) :
- « Chaleur » devient « Énergie » (chapitre, couleurs, menu, scène).
- Bulles du quartier en 2030 : le rond seul, sans la pointe (07_bulles.py ajuste le
  cercle par la plus grande distance au fond et redessine l'anneau).
- Les points de progression deviennent le fil des logos de la planche 4 du concours
  Habiter Demain (10_logos.py) : de la captation au résultat, cliquables.
- Chaque fiche ouvre son document d'origine dans le mur de son chapitre (mur.js,
  content/planches.md, 11_documents.py) : planches 1 à 8 entières, dessins du recueil
  seulement ; version haute définition chargée au zoom.
- Le cycle de l'eau se joue sur la planche 6 entière, avec son texte.
- Le hors-champ suit la planche 2 : systèmes hydrique, agraire, énergétique, chacun
  dans la couleur de son fil, trois fiches reliées à l'eau, au vivant, à l'énergie.
- Glisser-zoomer commun (plateau.js) ; contenu vérifié au build (documents, zones).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 15:11:24 +02:00

634 lines
54 KiB
JavaScript

// La scène 3D : le plan vectoriel du concours posé sur un socle, les volumes, le végétal, l'eau, la vie, le hors-champ.
// Caméra axonométrique orthographique. Les fiches appellent cadrer(nom) et effet(nom) : deux fiches qui partagent
// un cadrage ne bougent pas la caméra (c'est ce qui empêche les fiches de « se balader »).
import * as THREE from 'three';
import { lot, haiesEssences, vergers, arbresPlan, thuyas, plantesEau, potager, NOMS_AQUA, rnd } from './vegetal.js';
import { vieAnimale, velo, veloCargo, attelage, voiture, chien } from './animaux.js';
const PAL = {
fond: '#efeee2', papier: '#e7e5d3', socle: '#d9d3bc',
mur: '#f4f0e4', toit: '#c9bba8', paille: '#e6cf8a', toitT: '#b9a68e', grandMur: '#f1ebdc', grandToit: '#c99a7f',
trait: '#7d6a5c', tronc: '#8f7a62', thuya: '#6f8659', eau: '#3f8fb0', eauVive: '#2f7fa8', sentier: '#d6a83a',
gens: ['#c47f5a', '#6f8fa8', '#d3b45c', '#8aa36a', '#a7768e', '#7a7066', '#cbbfa6', '#8f7aa6'],
autos: ['#9aa1a6', '#a8675a', '#6d7f99', '#d8d2c4', '#6a6e70', '#8a9a72'],
etals: ['#d9a35b', '#e0c27a', '#c98466', '#9fb57a', '#85adc0'],
};
export function creerScene(canvas, { D, ESSENCES, BULLES, IMG, reduit, petit, onToucher }) {
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true, powerPreference: 'high-performance' });
if (!renderer.getContext()) return null;
renderer.setPixelRatio(Math.min(devicePixelRatio || 1, petit ? 1.5 : 2));
renderer.shadowMap.enabled = true; renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(PAL.fond);
const cam = new THREE.OrthographicCamera(-1, 1, 1, -1, 1, 4000);
const C = (h) => new THREE.Color(h);
// lumière douce : ciel chaud, soleil bas du sud-ouest (ombres légères, comme un lavis)
scene.add(new THREE.HemisphereLight(0xfffcf2, 0xd9d2bf, 2.35));
const soleil = new THREE.DirectionalLight(0xfff1dc, 1.05);
soleil.position.set(-110, 170, 120); soleil.castShadow = true; soleil.shadow.mapSize.set(petit ? 1024 : 2048, petit ? 1024 : 2048);
Object.assign(soleil.shadow.camera, { left: -170, right: 170, top: 130, bottom: -130, near: 20, far: 520 });
soleil.shadow.bias = -0.0006; soleil.shadow.normalBias = 0.35; soleil.shadow.intensity = 0.7;
scene.add(soleil, soleil.target);
// ---------------------------------------------------------------- sol : le plan, en deux états
let besoin = true;
const chargeur = new THREE.TextureLoader();
const tex = (url) => { const t = chargeur.load(url, () => (besoin = true)); t.colorSpace = THREE.SRGBColorSpace; t.anisotropy = renderer.capabilities.getMaxAnisotropy(); return t; };
const papier = new THREE.Mesh(new THREE.PlaneGeometry(5000, 5000).rotateX(-Math.PI / 2), new THREE.MeshLambertMaterial({ color: PAL.papier }));
papier.position.y = -2.3; papier.receiveShadow = true; scene.add(papier);
const socle = new THREE.Mesh(new THREE.BoxGeometry(D.Wm, 2.2, D.Hm), new THREE.MeshLambertMaterial({ color: PAL.socle }));
socle.position.y = -1.13; socle.receiveShadow = true; scene.add(socle);
const geoSol = new THREE.PlaneGeometry(D.Wm, D.Hm).rotateX(-Math.PI / 2);
const sol35 = new THREE.Mesh(geoSol, new THREE.MeshLambertMaterial({ map: tex(IMG.sol2035) }));
sol35.receiveShadow = true; scene.add(sol35);
const matAuj = new THREE.MeshLambertMaterial({ map: tex(IMG.solauj), transparent: true, opacity: 0, depthWrite: false, polygonOffset: true, polygonOffsetFactor: -2, polygonOffsetUnits: -2 });
const solAuj = new THREE.Mesh(geoSol, matAuj); solAuj.receiveShadow = true; solAuj.position.y = 0.02; scene.add(solAuj);
const G = { v2035: new THREE.Group(), vAuj: new THREE.Group() };
scene.add(G.v2035, G.vAuj);
const cliquables = [];
const anim = [];
// ---------------------------------------------------------------- maisons
const matTrait = new THREE.LineBasicMaterial({ color: PAL.trait, transparent: true, opacity: 0.42 });
const mat = {
mur: new THREE.MeshLambertMaterial({ color: PAL.mur }), toit: new THREE.MeshLambertMaterial({ color: PAL.toit, side: THREE.DoubleSide }),
paille: new THREE.MeshLambertMaterial({ color: PAL.paille, emissive: '#000000' }), toitT: new THREE.MeshLambertMaterial({ color: PAL.toitT, side: THREE.DoubleSide }),
verre: new THREE.MeshLambertMaterial({ color: '#d4ebe6', transparent: true, opacity: 0.55, depthWrite: false, emissive: '#000000' }), bord: new THREE.LineBasicMaterial({ color: '#86ada7' }),
chem: new THREE.MeshLambertMaterial({ color: '#a39a8c', emissive: '#000000' }), cuve: new THREE.MeshLambertMaterial({ color: '#7fb3cf', emissive: '#000000' }),
grandMur: new THREE.MeshLambertMaterial({ color: PAL.grandMur }), grandToit: new THREE.MeshLambertMaterial({ color: PAL.grandToit, side: THREE.DoubleSide, emissive: '#000000' }),
zinc: new THREE.MeshLambertMaterial({ color: '#9aa3a6', emissive: '#000000' }), pompe: new THREE.MeshLambertMaterial({ color: '#5f7f96', emissive: '#000000' }),
};
// la maison témoin a ses propres matériaux : ses effets n'allument qu'elle
const matT = { paille: mat.paille.clone(), verre: mat.verre.clone(), cuve: mat.cuve.clone(), chem: mat.chem.clone(), toitT: mat.toitT.clone() };
function toit(Lu, Lv, eave, rh, o) {
const hu = Lu / 2 + o, hv = Lv / 2 + o, ry = eave + rh, yb = eave - rh * (o / (Lv / 2));
const g = new THREE.BufferGeometry();
g.setAttribute('position', new THREE.Float32BufferAttribute([
-hu, yb, -hv, hu, yb, -hv, hu, ry, 0, -hu, yb, -hv, hu, ry, 0, -hu, ry, 0,
-hu, yb, hv, -hu, ry, 0, hu, ry, 0, -hu, yb, hv, hu, ry, 0, hu, yb, hv], 3));
g.computeVertexNormals(); return g;
}
function pignons(Lu, Lv, eave, rh) {
const g = new THREE.BufferGeometry(), x = Lu / 2, v = Lv / 2, ry = eave + rh;
g.setAttribute('position', new THREE.Float32BufferAttribute([-x, eave, -v, -x, eave, v, -x, ry, 0, x, eave, v, x, eave, -v, x, ry, 0], 3));
g.computeVertexNormals(); return g;
}
const transformees = []; // { g, c, eave, rh, Lu, Lv, ite, chem, cuve, temoin }
let indexT = 0, grand = null;
function maison(h, variante) {
const p = h.p.map(([x, z]) => new THREE.Vector2(x, z));
const c = p.reduce((a, b) => a.add(b), new THREE.Vector2()).multiplyScalar(0.25);
const e1 = p[1].clone().sub(p[0]), e2 = p[2].clone().sub(p[1]);
const fw = new THREE.Vector2(Math.cos(h.f), -Math.sin(h.f));
let u, Lu, Lv;
if (Math.abs(e1.clone().normalize().dot(fw)) >= Math.abs(e2.clone().normalize().dot(fw))) { u = e1.clone().normalize(); Lu = e1.length(); Lv = e2.length(); }
else { u = e2.clone().normalize(); Lu = e2.length(); Lv = e1.length(); }
const g = new THREE.Group(); g.position.set(c.x, 0, c.y); g.rotation.y = Math.atan2(-u.y, u.x);
const estGrand = h.t === 'grand', transfo = h.t === 'transformee' && variante === '2035';
const temoin = transfo && indexT === D.temoin.maison;
const M = temoin ? { ...mat, ...matT } : mat;
const eave = estGrand ? 7.4 : 5.4, rh = Math.min(estGrand ? 5 : 3.4, Lv * (estGrand ? 0.27 : 0.31)), ite = transfo ? 0.7 : 0;
const mMur = estGrand ? M.grandMur : transfo ? M.paille : M.mur, mToit = estGrand ? M.grandToit : transfo ? M.toitT : M.toit;
const murs = new THREE.Mesh(new THREE.BoxGeometry(Lu + ite, eave, Lv + ite), mMur); murs.position.y = eave / 2;
const t = new THREE.Mesh(toit(Lu + ite, Lv + ite, eave, rh, 0.55), mToit);
const pg = new THREE.Mesh(pignons(Lu + ite, Lv + ite, eave, rh), transfo ? M.paille : new THREE.MeshLambertMaterial({ color: mMur.color, side: THREE.DoubleSide }));
[murs, t, pg].forEach((m) => { m.castShadow = true; m.receiveShadow = true; g.add(m); });
// le trait du dessin : arêtes des volumes, discrètes
const ar = new THREE.LineSegments(new THREE.EdgesGeometry(murs.geometry), matTrait); ar.position.copy(murs.position); g.add(ar);
g.add(new THREE.LineSegments(new THREE.EdgesGeometry(t.geometry, 20), matTrait));
if (transfo) {
const th = g.rotation.y, faces = [[1, 0, Math.cos(th), -Math.sin(th)], [-1, 0, -Math.cos(th), Math.sin(th)], [0, 1, Math.sin(th), Math.cos(th)], [0, -1, -Math.sin(th), -Math.cos(th)]];
const f = faces.reduce((a, b) => (b[3] > a[3] ? b : a));
const sh = 3.3, d = 2.4, cote = f[0] !== 0 ? Lv : Lu;
const serre = new THREE.Mesh(new THREE.BoxGeometry(f[0] !== 0 ? d : cote * 0.7, sh, f[0] !== 0 ? cote * 0.7 : d), M.verre);
serre.position.set(f[0] * ((Lu + ite) / 2 + d / 2), sh / 2, f[1] * ((Lv + ite) / 2 + d / 2)); serre.renderOrder = 1; g.add(serre);
const as = new THREE.LineSegments(new THREE.EdgesGeometry(serre.geometry), mat.bord); as.position.copy(serre.position); g.add(as);
const ch = new THREE.Mesh(new THREE.BoxGeometry(0.7, 2.6, 0.7), M.chem); ch.position.set(Lu * 0.2, eave + rh * 0.62 + 0.7, -f[1] * Lv * 0.12); ch.castShadow = true; g.add(ch);
// la cuve, au pied de la descente, à l'opposé de la serre
const bout = (Lu + ite) / 2 + 1.1;
const cu = new THREE.Mesh(new THREE.CylinderGeometry(0.85, 0.85, 1.8, 16), M.cuve);
if (f[0] !== 0) cu.position.set(-f[0] * bout, 0.9, Lv / 2 - 1); else cu.position.set(bout, 0.9, -f[1] * (Lv / 2 - 1));
cu.castShadow = true; g.add(cu);
const info = { g, c, eave, rh, Lu, Lv, ite, temoin, f, cu, ch };
if (temoin) {
// gouttière, descente, filtre et pompe : le circuit de la chaîne de l'eau, sur la maison témoin seulement
const cote = cu.position.z >= 0 ? 1 : -1, ze = cote * ((Lv + ite) / 2 + 0.6);
const gout = new THREE.Mesh(new THREE.BoxGeometry(Lu + ite + 1.1, 0.16, 0.2), mat.zinc); gout.position.set(0, eave - 0.2, ze); g.add(gout);
const xd = cu.position.x;
const desc = new THREE.Mesh(new THREE.CylinderGeometry(0.08, 0.08, eave - 1.6, 8), mat.zinc); desc.position.set(Math.max(-(Lu + ite) / 2, Math.min((Lu + ite) / 2, xd)), (eave - 1.6) / 2 + 1.4, ze); g.add(desc);
const filtre = new THREE.Mesh(new THREE.BoxGeometry(0.6, 0.9, 0.2), mat.zinc); filtre.position.set(desc.position.x, 1.0, ze + cote * 0.05); g.add(filtre);
const pompe = new THREE.Mesh(new THREE.BoxGeometry(0.55, 0.45, 0.45), mat.pompe); pompe.position.set(cu.position.x + (cu.position.x > 0 ? -1.2 : 1.2), 0.23, cu.position.z); pompe.castShadow = true; g.add(pompe);
Object.assign(info, { gout, desc, filtre, pompe, ze, xd: desc.position.x });
}
g.updateMatrixWorld(true);
info.chemW = ch.getWorldPosition(new THREE.Vector3()).add(new THREE.Vector3(0, 1.4, 0));
info.cuveW = cu.getWorldPosition(new THREE.Vector3());
transformees.push(info); indexT++;
}
if (estGrand) grand = g;
return g;
}
for (const h of D.maisons) {
if (h.t === 'transformee') { G.v2035.add(maison(h, '2035')); G.vAuj.add(maison(h, 'auj')); }
else scene.add(maison(h, '2035'));
}
const T = transformees[D.temoin.maison];
transformees.forEach((m) => cliquables.push({ o: m.g, fiche: 'energie-paille' }));
if (grand) cliquables.push({ o: grand, fiche: 'quartier-communs' });
// ---------------------------------------------------------------- végétal (content/essences.md)
const E = ESSENCES;
const matsHaie = { couronne: new THREE.MeshLambertMaterial({ color: '#ffffff', emissive: '#000000' }), tronc: new THREE.MeshLambertMaterial({ color: PAL.tronc }), fruit: new THREE.MeshLambertMaterial({ color: '#ffffff' }) };
const matsForet = { couronne: new THREE.MeshLambertMaterial({ color: '#ffffff', emissive: '#000000' }), tronc: matsHaie.tronc, fruit: matsHaie.fruit };
const matsAutres = { couronne: new THREE.MeshLambertMaterial({ color: '#ffffff' }), tronc: matsHaie.tronc, fruit: matsHaie.fruit };
const itemsHaies = haiesEssences(D.haies, E);
G.v2035.add(lot(itemsHaies, matsHaie, E.reglages.fruits));
G.v2035.add(lot(vergers(D.vergers, E), matsHaie, E.reglages.fruits));
const parGenre = (k) => D.arbres.filter((a) => a[4] === k);
const med = (L) => { const v = L.map((a) => a[2]).sort((a, b) => a - b); return v[Math.floor(v.length / 2)] || 1; };
const foret = parGenre(1), exist = parGenre(2), buis = parGenre(3);
G.v2035.add(lot(arbresPlan(foret, E, 'foret', 1, med(foret)), matsForet, false));
scene.add(lot(arbresPlan(exist, E, 'arbre existant', 2, med(exist)), matsAutres, false));
scene.add(lot(arbresPlan(buis, E, 'buisson existant', 3, med(buis)), matsAutres, false));
G.vAuj.add(thuyas(D.haies, new THREE.MeshLambertMaterial({ color: PAL.thuya })));
const eauV = plantesEau(D.aqua); G.v2035.add(eauV.groupe);
// les haies du jardin témoin : toutes celles qui passent à moins de 28 m de la maison
const echantillon = (pl, pas = 2) => { const out = []; for (let i = 1; i < pl.length; i++) { const [a, b] = [pl[i - 1], pl[i]], L = Math.hypot(b[0] - a[0], b[1] - a[1]); for (let s = 0; s <= L; s += pas) out.push([a[0] + (b[0] - a[0]) * s / L, a[1] + (b[1] - a[1]) * s / L]); } return out; };
const pres2 = ([x, z]) => Math.hypot(x - T.c.x, z - T.c.y);
const haiesT = new Set(D.haies.map((h, i) => [i, echantillon(h, 1)]).filter(([, pts]) => pts.some((p) => pres2(p) < 28)).map(([i]) => i));
// le potager au pied de ces haies, côté jardin
const ptsPotager = [];
for (const k of haiesT) {
const haieT = D.haies[k];
for (let i = 1; i < haieT.length; i++) {
const [a, b] = [haieT[i - 1], haieT[i]], L = Math.hypot(b[0] - a[0], b[1] - a[1]), tx = (b[0] - a[0]) / L, tz = (b[1] - a[1]) / L;
let nx = -tz, nz = tx; if ((T.c.x - a[0]) * nx + (T.c.y - a[1]) * nz < 0) { nx = -nx; nz = -nz; }
for (let s = 0.5; s < L; s += 0.7) for (const off of [2.3]) {
const x = a[0] + tx * s + nx * off, z = a[1] + tz * s + nz * off;
if (Math.hypot(x - T.c.x, z - T.c.y) < 16 && Math.hypot(x - T.c.x, z - T.c.y) > 6) ptsPotager.push([x, z]);
}
}
}
const imPotager = ptsPotager.length ? potager(ptsPotager) : null;
if (imPotager) G.v2035.add(imPotager);
// ---------------------------------------------------------------- personnages, étals, voitures garées
const geoP = new THREE.CapsuleGeometry(0.28, 0.95, 3, 8);
const PAL_P = PAL.gens.map(C);
const imP = new THREE.InstancedMesh(geoP, new THREE.MeshLambertMaterial({ color: '#ffffff' }), D.persos.length); imP.castShadow = true;
D.persos.forEach(([x, z], i) => { imP.setMatrixAt(i, new THREE.Matrix4().makeTranslation(x, 0.76, z)); imP.setColorAt(i, PAL_P[i % PAL_P.length]); });
imP.instanceColor.needsUpdate = true; G.v2035.add(imP);
const matEtal = PAL.etals.map((c) => new THREE.MeshLambertMaterial({ color: c, emissive: '#000000' }));
const matPoteau = new THREE.MeshLambertMaterial({ color: '#7b6a55' }), matTable = new THREE.MeshLambertMaterial({ color: '#c49a64' });
function axes(pts) {
const p = pts.map(([x, z]) => new THREE.Vector2(x, z)), c = p.reduce((a, b) => a.add(b), new THREE.Vector2()).multiplyScalar(0.25);
const e1 = p[1].clone().sub(p[0]), e2 = p[2].clone().sub(p[1]); const [u, L, W] = e1.length() >= e2.length() ? [e1.clone().normalize(), e1.length(), e2.length()] : [e2.clone().normalize(), e2.length(), e1.length()];
return { c, rot: Math.atan2(-u.y, u.x), L, W };
}
D.etals.forEach((e, i) => {
const a = axes(e.p), g = new THREE.Group(); g.position.set(a.c.x, 0, a.c.y); g.rotation.y = a.rot;
const L = Math.min(a.L, 4.5), W = Math.min(a.W, 3);
const toile = new THREE.Mesh(new THREE.BoxGeometry(L + 0.4, 0.16, W + 0.4), matEtal[i % matEtal.length]); toile.position.y = 2.45; toile.castShadow = true; g.add(toile);
for (const [sx, sz] of [[1, 1], [1, -1], [-1, 1], [-1, -1]]) { const po = new THREE.Mesh(new THREE.CylinderGeometry(0.06, 0.06, 2.45, 5), matPoteau); po.position.set(sx * L / 2, 1.22, sz * W / 2); g.add(po); }
const tb = new THREE.Mesh(new THREE.BoxGeometry(L * 0.9, 0.85, W * 0.45), matTable); tb.position.set(0, 0.42, W * 0.18); tb.castShadow = true; g.add(tb);
G.v2035.add(g); cliquables.push({ o: g, fiche: 'quartier-marche' });
const v = voiture(PAL.autos[i % PAL.autos.length]); v.position.set(a.c.x, 0, a.c.y); v.rotation.y = a.rot + Math.PI / 2; G.vAuj.add(v);
});
// ---------------------------------------------------------------- eau, sentiers : surfaces qui s'allument
const aire = (pts) => Math.abs(pts.reduce((s, p, i) => { const q = pts[(i + 1) % pts.length]; return s + p[0] * q[1] - q[0] * p[1]; }, 0)) / 2;
function surface(pts, couleur, y) {
const sh = new THREE.Shape(pts.map(([x, z]) => new THREE.Vector2(x, -z)));
const g = new THREE.ShapeGeometry(sh).rotateX(-Math.PI / 2);
const m = new THREE.Mesh(g, new THREE.MeshBasicMaterial({ color: couleur, transparent: true, opacity: 0, depthWrite: false }));
m.position.y = y; m.renderOrder = 2; G.v2035.add(m); return m;
}
const surv = { noues: [], bassins: [], sentiers: [], liaisons: [], chemins: [], flux: {}, bulles: [], ondes: [] };
D.noues.forEach((pts, i) => { const m = surface(pts, PAL.eau, 0.08); m.userData.i = i; surv.noues.push(m); cliquables.push({ o: m, fiche: 'eau-noue' }); });
D.plantes.forEach((pts) => { if (pts.length > 3 && aire(pts) > 60) { const m = surface(pts, '#3e9aa3', 0.08); surv.bassins.push(m); cliquables.push({ o: m, fiche: 'eau-mare' }); } });
D.sentiers.forEach((pts) => surv.sentiers.push(surface(pts, PAL.sentier, 0.07)));
// rubans en pointillés qui coulent (liaisons, chemins, liens du hors-champ)
const vs = 'varying vec2 vUv; void main(){ vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }';
const fs = 'uniform float uT; uniform float uA; uniform float uV; uniform float uP; uniform vec3 uC; varying vec2 vUv; void main(){ float d = fract(vUv.x / uP - uT * uV); float t = smoothstep(0.0, 0.08, d) * (1.0 - smoothstep(0.45, 0.55, d)); float e = 1.0 - smoothstep(0.3, 0.5, abs(vUv.y - 0.5)); gl_FragColor = vec4(uC, uA * (0.22 + 0.78 * t) * e);\n#include <colorspace_fragment>\n}';
function ruban(pts, largeur, couleur, vitesse, pas, y, groupe, dessus = true) {
const pos = [], uv = [], idx = []; let L = 0;
for (let i = 0; i < pts.length; i++) {
const [x, z] = pts[i], a = pts[Math.max(0, i - 1)], b = pts[Math.min(pts.length - 1, i + 1)];
let dx = b[0] - a[0], dz = b[1] - a[1]; const n = Math.hypot(dx, dz) || 1; dx /= n; dz /= n;
const rx = -dz * largeur / 2, rz = dx * largeur / 2;
if (i > 0) L += Math.hypot(x - pts[i - 1][0], z - pts[i - 1][1]);
pos.push(x - rx, 0, z - rz, x + rx, 0, z + rz); uv.push(L, 0, L, 1);
if (i > 0) { const k = 2 * i; idx.push(k - 2, k - 1, k, k - 1, k + 1, k); }
}
const g = new THREE.BufferGeometry(); g.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3)); g.setAttribute('uv', new THREE.Float32BufferAttribute(uv, 2)); g.setIndex(idx);
// depthTest désactivé : un ruban allumé est un tracé de diagramme, il passe au-dessus du feuillage
const m = new THREE.ShaderMaterial({ vertexShader: vs, fragmentShader: fs, transparent: true, depthWrite: false, depthTest: !dessus, side: THREE.DoubleSide,
uniforms: { uT: { value: 0 }, uA: { value: 0 }, uV: { value: vitesse }, uP: { value: pas }, uC: { value: new THREE.Color(couleur) } } });
const mesh = new THREE.Mesh(g, m); mesh.position.y = y; mesh.renderOrder = 3; (groupe || G.v2035).add(mesh); return mesh;
}
const sommetsNoues = D.noues.flat();
const dNoue = ([x, z]) => Math.min(...sommetsNoues.map(([a, b]) => (a - x) ** 2 + (b - z) ** 2));
D.liaisons.forEach((l, i) => { const pts = dNoue(l[0]) < dNoue(l[l.length - 1]) ? l.slice().reverse() : l; const r = ruban(pts, 1.0, PAL.eauVive, 0.9, 2.2, 0.14); r.userData.i = i; surv.liaisons.push(r); });
D.chemins.forEach((l) => { let L = 0; for (let i = 1; i < l.length; i++) L += Math.hypot(l[i][0] - l[i - 1][0], l[i][1] - l[i - 1][1]); if (L > 12) surv.chemins.push(ruban(l, 1.1, '#c79528', 0.35, 3.0, 0.13)); });
// ---------------------------------------------------------------- gouttes : pluie, gouttière, pompe, fumée
const disque = (() => { const c = document.createElement('canvas'); c.width = c.height = 64; const x = c.getContext('2d'); const g = x.createRadialGradient(32, 32, 2, 32, 32, 30); g.addColorStop(0, 'rgba(255,255,255,1)'); g.addColorStop(1, 'rgba(255,255,255,0)'); x.fillStyle = g; x.fillRect(0, 0, 64, 64); const t = new THREE.CanvasTexture(c); t.colorSpace = THREE.SRGBColorSpace; return t; })();
const sprite = (couleur, op = 0, dessus = false) => new THREE.Sprite(new THREE.SpriteMaterial({ map: disque, color: couleur, transparent: true, opacity: op, depthWrite: false, depthTest: !dessus }));
// un « filet » : des gouttes qui parcourent un chemin 3D
function filet(chemin, n, taille, couleur, vitesse) {
const pts = chemin.map((p) => new THREE.Vector3(...p)), c = [0];
for (let i = 1; i < pts.length; i++) c.push(c[i - 1] + pts[i].distanceTo(pts[i - 1]));
const L = c[c.length - 1], sp = [];
for (let i = 0; i < n; i++) { const s = sprite(couleur, 0, true); s.scale.setScalar(taille); s.renderOrder = 6; G.v2035.add(s); sp.push(s); }
return { on: false, maj(t) {
sp.forEach((s, i) => {
s.material.opacity = this.on ? 0.9 : 0; if (!this.on) return;
const d = ((t * vitesse + (i / n) * L) % L); let k = 1; while (k < c.length - 1 && c[k] < d) k++;
const a = pts[k - 1], b = pts[k], u = (d - c[k - 1]) / ((c[k] - c[k - 1]) || 1); s.position.lerpVectors(a, b, u);
});
} };
}
const local = (x, y, z) => T.g.localToWorld(new THREE.Vector3(x, y, z)).toArray();
const filets = {};
// pluie sur le toit de la maison témoin
const pluie = [];
for (let i = 0; i < 46; i++) { const s = sprite('#5d9fc7'); s.scale.set(0.22, 0.9, 1); G.v2035.add(s); pluie.push({ s, x: (rnd(i * 3) - 0.5) * (T.Lu + 2), z: (rnd(i * 7) - 0.5) * (T.Lv + 2), ph: rnd(i * 11) }); }
if (T.gout) {
const xe = (T.Lu + T.ite) / 2;
filets.gouttiere = filet([local(-xe, T.eave - 0.1, T.ze), local(T.xd, T.eave - 0.1, T.ze), local(T.xd, 1.45, T.ze), local(T.xd, 1.0, T.ze), local(T.cu.position.x, 1.85, T.cu.position.z)], 14, 0.5, '#3f8fc7', 2.2);
const xp = T.pompe.position.x, mur = T.cu.position.x > 0 ? (T.Lu + T.ite) / 2 : -(T.Lu + T.ite) / 2;
filets.pompe = filet([local(T.cu.position.x, 0.3, T.cu.position.z), local(xp, 0.3, T.cu.position.z), local(mur, 0.4, T.cu.position.z * 0.6), local(mur * 0.6, 1.2, T.cu.position.z * 0.3)], 10, 0.42, '#3f8fc7', 1.4);
}
// les eaux grises : de la maison à la noue, le long de la liaison du plan
{
const l = D.liaisons[D.temoin.liaison];
if (l) {
const d0 = Math.hypot(l[0][0] - T.c.x, l[0][1] - T.c.y), d1 = Math.hypot(l[l.length - 1][0] - T.c.x, l[l.length - 1][1] - T.c.y);
const pts = (d0 <= d1 ? l : l.slice().reverse()).map(([x, z]) => [x, 0.45, z]);
filets['eaux-grises'] = filet([[T.c.x, 0.6, T.c.y], ...pts], 12, 0.55, '#8a9a8c', 1.6);
}
}
const fumee = [];
transformees.forEach((m, k) => { for (let i = 0; i < 6; i++) { const s = sprite('#b9b2a6', 0.5); s.userData = { m, ph: (i + rnd(k * 13 + i)) / 6 }; G.v2035.add(s); fumee.push(s); } });
const MARE = (() => { const p = D.mare; const c = p.reduce((a, q) => [a[0] + q[0] / p.length, a[1] + q[1] / p.length], [0, 0]); return { x: D.canards.c[0] ?? c[0], z: D.canards.c[1] ?? c[1] }; })();
for (let i = 0; i < 3; i++) {
const o = new THREE.Mesh(new THREE.RingGeometry(0.85, 1.15, 48).rotateX(-Math.PI / 2), new THREE.MeshBasicMaterial({ color: PAL.eauVive, transparent: true, opacity: 0, depthWrite: false }));
o.position.set(MARE.x, 0.16, MARE.z); o.userData = { ph: i / 3 }; G.v2035.add(o); surv.ondes.push(o);
}
// ---------------------------------------------------------------- étiquettes (essences de la haie, plantes de la noue, strates)
function etiquette(texte, couleur = '#62560f', taille = 1) {
const c = document.createElement('canvas'); c.width = 512; c.height = 112; const x = c.getContext('2d');
x.font = '600 52px "Barlow Condensed", Barlow, sans-serif'; const w = Math.min(500, x.measureText(texte).width + 44);
x.fillStyle = 'rgba(255,253,246,0.95)'; x.beginPath(); if (x.roundRect) x.roundRect((512 - w) / 2, 8, w, 92, 46); else x.rect((512 - w) / 2, 8, w, 92); x.fill();
x.strokeStyle = couleur; x.lineWidth = 5; x.stroke();
x.fillStyle = couleur; x.textAlign = 'center'; x.textBaseline = 'middle'; x.fillText(texte, 256, 56);
const t = new THREE.CanvasTexture(c); t.colorSpace = THREE.SRGBColorSpace;
const s = new THREE.Sprite(new THREE.SpriteMaterial({ map: t, transparent: true, depthTest: false, opacity: 0 })); s.scale.set(5.6 * taille, 1.23 * taille, 1); s.renderOrder = 12;
return s;
}
const etiquettes = { 'haie-essences': [], 'noue-plantes': [], strates: [], paturage: [] };
const pres = (it) => Math.hypot(it.x - T.c.x, it.z - T.c.y);
itemsHaies.filter((it) => haiesT.has(it.haie) && it.strate === "arbre").sort((a, b) => pres(a) - pres(b)).slice(0, 8)
.forEach((it) => { const s = etiquette(it.nom); s.position.set(it.x, it.haut + 1.1, it.z); G.v2035.add(s); etiquettes['haie-essences'].push(s); });
const noueT = D.noues[D.temoin.noue];
if (noueT) {
const dans = (x, z) => { let c = false; for (let i = 0, j = noueT.length - 1; i < noueT.length; j = i++) { const [xi, zi] = noueT[i], [xj, zj] = noueT[j]; if ((zi > z) !== (zj > z) && x < ((xj - xi) * (z - zi)) / (zj - zi) + xi) c = !c; } return c; };
// une étiquette par plante présente, posée sur la plante la plus éloignée des étiquettes déjà posées (pas de chevauchement)
const dansNoue = D.aqua.filter((p) => dans(p[0], p[1])).sort((a, b) => Math.hypot(a[0] - T.c.x, a[1] - T.c.y) - Math.hypot(b[0] - T.c.x, b[1] - T.c.y)).slice(0, 40), poses = [];
for (const type of [...new Set(dansNoue.map((p) => p[2]))]) {
const cand = dansNoue.filter((p) => p[2] === type);
const p = cand.reduce((m, q) => { const d = (r) => Math.min(99, ...poses.map((o) => Math.hypot(o[0] - r[0], o[1] - r[1]))); return d(q) > d(m) ? q : m; }, cand[0]);
poses.push(p);
const s = etiquette(NOMS_AQUA[type], '#1d5886', 0.8); s.position.set(p[0], 2.0 + (poses.length % 3) * 1.15, p[1]); G.v2035.add(s); etiquettes["noue-plantes"].push(s);
}
}
{
const a = itemsHaies.filter((it) => haiesT.has(it.haie) && it.strate === "arbre").sort((x, y) => pres(x) - pres(y))[0];
const b = itemsHaies.filter((it) => haiesT.has(it.haie) && it.strate === "arbuste").sort((x, y) => pres(x) - pres(y))[0];
const p = ptsPotager.length ? ptsPotager.reduce((m, q) => (Math.hypot(q[0] - T.c.x, q[1] - T.c.y) < Math.hypot(m[0] - T.c.x, m[1] - T.c.y) ? q : m)) : null;
if (a) { const s = etiquette('arbres · 5 ans'); s.position.set(a.x, a.haut + 1.2, a.z); etiquettes.strates.push(s); }
if (b) { const s = etiquette('arbustes · 2 à 3 ans'); s.position.set(b.x, b.haut + 2.4, b.z); etiquettes.strates.push(s); }
if (p) { const s = etiquette('légumes · 1 an'); s.position.set(p[0], 2.2, p[1]); etiquettes.strates.push(s); }
etiquettes.strates.forEach((s) => G.v2035.add(s));
}
// ---------------------------------------------------------------- animaux
const vie = vieAnimale(D, G.v2035);
anim.push(...vie.anim);
if (D.troupeaux.length) {
const [x, z] = D.troupeaux[0]; const s = etiquette('moutons et chèvres'); s.position.set(x, 3.4, z); G.v2035.add(s); etiquettes.paturage.push(s);
}
// le chantier-école : des voisins autour de la maison témoin
const chantier = new THREE.Group(); chantier.visible = false; G.v2035.add(chantier);
for (let i = 0; i < 6; i++) {
const a = (i / 6) * Math.PI * 2 + 0.4, r = Math.max(T.Lu, T.Lv) / 2 + 2.2;
const p = new THREE.Mesh(geoP, new THREE.MeshLambertMaterial({ color: PAL.gens[(i + 2) % PAL.gens.length] })); p.position.set(T.c.x + Math.cos(a) * r, 0.76, T.c.y + Math.sin(a) * r); p.castShadow = true;
const casque = new THREE.Mesh(new THREE.SphereGeometry(0.2, 8, 5, 0, Math.PI * 2, 0, Math.PI / 2), new THREE.MeshLambertMaterial({ color: '#e0b23a' })); casque.position.set(p.position.x, 1.55, p.position.z);
chantier.add(p, casque);
}
const botte = new THREE.Mesh(new THREE.BoxGeometry(1.1, 0.45, 0.5), new THREE.MeshLambertMaterial({ color: PAL.paille }));
for (let i = 0; i < 8; i++) { const b = botte.clone(); b.position.set(T.c.x + (T.Lu / 2 + 3.2) * (i < 4 ? 1 : -1) * 0.9, 0.23 + Math.floor((i % 4) / 2) * 0.46, T.c.y + ((i % 2) - 0.5) * 1.2); b.castShadow = true; chantier.add(b); }
// ---------------------------------------------------------------- véhicules, vélos, attelage, marcheurs
function trajet(pts) { const c = [0]; for (let i = 1; i < pts.length; i++) c.push(c[i - 1] + Math.hypot(pts[i][0] - pts[i - 1][0], pts[i][1] - pts[i - 1][1])); return { pts, c, L: c[c.length - 1] }; }
function surTrajet(t, s) {
s = Math.max(0, Math.min(t.L, s)); let i = 1; while (i < t.c.length - 1 && t.c[i] < s) i++;
const a = t.pts[i - 1], b = t.pts[i], k = (s - t.c[i - 1]) / ((t.c[i] - t.c[i - 1]) || 1);
return { x: a[0] + (b[0] - a[0]) * k, z: a[1] + (b[1] - a[1]) * k, dx: b[0] - a[0], dz: b[1] - a[1] };
}
const mobiles = [];
const tr = {}; for (const k in D.routes) tr[k] = trajet(D.routes[k]);
const cheminsLongs = D.chemins.map(trajet).filter((t) => t.L > 40).sort((a, b) => b.L - a.L);
const ajoute = (obj, t, vitesse, decal, depart, groupe, role) => { groupe.add(obj); const m = { obj, t, v: vitesse, d: decal, s: depart * t.L, sens: 1, role }; mobiles.push(m); return m; };
ajoute(velo(PAL.gens[0]), tr.principale, 4.2, 2.8, 0.1, G.v2035, 'velo'); ajoute(velo(PAL.gens[3]), tr.principale, 3.8, -2.6, 0.65, G.v2035, 'velo');
ajoute(velo(PAL.gens[1]), tr.verticale, 4.0, 2.2, 0.4, G.v2035, 'velo'); ajoute(velo(PAL.gens[4]), tr.anneau, 3.5, 2.0, 0.2, G.v2035, 'velo');
ajoute(veloCargo(PAL.gens[2]), tr.principale, 3.2, -2.4, 0.3, G.v2035, 'cargo'); ajoute(veloCargo(PAL.gens[5]), tr.verticale, 3.0, -2.0, 0.75, G.v2035, 'cargo');
ajoute(attelage(), tr.principale, 1.6, 2.2, 0.55, G.v2035, 'attelage');
ajoute(voiture('#7fa3a6', true), tr.principale, 7, 1.8, 0.85, G.v2035, 'partagee');
if (cheminsLongs[0]) ajoute(velo(PAL.gens[6]), cheminsLongs[0], 3.2, 0, 0.3, G.v2035, 'velo');
const marcheurs = cheminsLongs.slice(0, 5).map((t, i) => { const p = new THREE.Mesh(geoP, new THREE.MeshLambertMaterial({ color: PAL.gens[(i + 3) % PAL.gens.length] })); p.castShadow = true; const g = new THREE.Group(); p.position.y = 0.76; g.add(p); return ajoute(g, t, 1.3, 0, rnd(i + 3), G.v2035, 'marcheur'); });
marcheurs.slice(0, 2).forEach((m) => { const c = chien(); G.v2035.add(c); mobiles.push({ obj: c, suit: m, d: 0.9 }); });
[['principale', 0.05], ['principale', 0.35], ['principale', 0.7], ['verticale', 0.2], ['verticale', 0.6], ['liaison_anneau', 0.5], ['anneau', 0.3], ['droite', 0.5]]
.forEach(([k, d], i) => ajoute(voiture(PAL.autos[(i + 1) % PAL.autos.length]), tr[k], 9, 1.8, d, G.vAuj, 'auto'));
// ---------------------------------------------------------------- bulles de la planche 2030 (dessins de Jules, p. 15)
const VERT = new Set(['pomme', 'abeille', 'fleur', 'arbre', 'oiseaux']);
const matTige = { v: new THREE.MeshLambertMaterial({ color: '#9db878' }), q: new THREE.MeshLambertMaterial({ color: '#d9a866' }) };
const geoTige = new THREE.CylinderGeometry(0.1, 0.1, 1, 6).translate(0, 0.5, 0);
D.bulles.forEach((b, i) => {
const [x, z] = b.w, vert = VERT.has(b.id);
const tige = new THREE.Mesh(geoTige, vert ? matTige.v : matTige.q); tige.position.set(x, 0, z); tige.scale.y = 8.4; G.v2035.add(tige);
const t = chargeur.load(BULLES[b.id], () => (besoin = true)); t.colorSpace = THREE.SRGBColorSpace;
const s = new THREE.Sprite(new THREE.SpriteMaterial({ map: t, transparent: true, depthTest: false }));
const petit = b.id.startsWith('apaise'); s.scale.set(petit ? 3.4 : 6.4, petit ? 3.4 : 6.4, 1); s.position.set(x, petit ? 7.4 : 10.4, z); s.renderOrder = 10;
s.userData = { id: b.id, base: s.position.y, ph: rnd(i) * 6.28, taille: s.scale.x }; G.v2035.add(s); surv.bulles.push(s);
});
// ---------------------------------------------------------------- hors-champ : les grandes infrastructures et leurs liens
// Regroupées en trois systèmes, comme la planche 2 du concours Habiter Demain (« analyse territoriale », 08/10) :
// hydrique (puisement et potabilisation, station d'épuration), agraire (agriculture industrielle, supermarché),
// énergétique (gravière et cimenterie, centrale). Chacun prend la couleur de son fil : eau, vivant, énergie.
const matI = (c) => new THREE.MeshLambertMaterial({ color: c });
function panneau(texte, couleur, plein = false) {
const c = document.createElement('canvas'); c.width = 640; c.height = 120; const x = c.getContext('2d');
x.fillStyle = plein ? couleur : 'rgba(255,253,246,0.94)'; x.beginPath(); if (x.roundRect) x.roundRect(4, 4, 632, 112, 22); else x.rect(4, 4, 632, 112); x.fill(); x.strokeStyle = couleur; x.lineWidth = 5; x.stroke();
x.fillStyle = plein ? '#fffdf6' : '#102a3a'; x.font = `600 ${plein ? 56 : 50}px "Barlow Condensed", Barlow, sans-serif`; x.textAlign = 'center'; x.textBaseline = 'middle'; x.fillText(texte, 320, 62);
const t = new THREE.CanvasTexture(c); t.colorSpace = THREE.SRGBColorSpace;
const s = new THREE.Sprite(new THREE.SpriteMaterial({ map: t, transparent: true, depthTest: false })); s.scale.set(64, 12, 1); s.renderOrder = 9; return s;
}
const SYS = {
eau: { n: 'Système hydrique', c: '#3a74a8' },
vivant: { n: 'Système agraire', c: '#8f7b22' },
energie: { n: 'Système énergétique', c: '#c96d0c' },
};
const INFRA = [
{ k: 'potab', n: 'Puisement, potabilisation', x: 30, z: -370, sys: 'eau' },
{ k: 'step', n: "Station d'épuration", x: 230, z: -300, sys: 'eau' },
{ k: 'agri', n: 'Agriculture industrielle', x: 410, z: -40, sys: 'vivant' },
{ k: 'super', n: 'Supermarché', x: 340, z: 200, sys: 'vivant' },
{ k: 'ciment', n: 'Gravière, cimenterie', x: -410, z: 80, sys: 'energie' },
{ k: 'centrale', n: 'Centrale, gaz importé', x: -330, z: -200, sys: 'energie' },
{ k: 'emploi', n: "Bassin d'emploi", c: '#6b6458', x: -250, z: 330, sys: 'travail' },
];
const HC = new THREE.Group(); HC.position.y = -2.3; scene.add(HC);
const halos = {}, centresSys = {};
for (const [k, S] of Object.entries(SYS)) {
const L = INFRA.filter((f) => f.sys === k), cx = L.reduce((a, f) => a + f.x, 0) / L.length, cz = L.reduce((a, f) => a + f.z, 0) / L.length;
const ray = Math.max(...L.map((f) => Math.hypot(f.x - cx, f.z - cz))) + 85;
const h = new THREE.Mesh(new THREE.CircleGeometry(ray, 64).rotateX(-Math.PI / 2), new THREE.MeshBasicMaterial({ color: S.c, transparent: true, opacity: 0.1, depthWrite: false }));
h.position.set(cx, 0.2, cz); HC.add(h); halos[k] = h; centresSys[k] = { x: cx, z: cz, r: ray };
const e = panneau(S.n, S.c, true); e.position.set(cx, 105, cz); e.scale.set(136, 25.5, 1); HC.add(e);
}
for (const f of INFRA) {
f.c = f.c || SYS[f.sys].c;
const g = new THREE.Group(); g.position.set(f.x, 0, f.z);
const add = (geo, m, x, y, z) => { const o = new THREE.Mesh(geo, m); o.position.set(x, y, z); o.castShadow = true; g.add(o); return o; };
if (f.k === 'centrale') { add(new THREE.CylinderGeometry(8, 12, 34, 24), matI('#dcd6ca'), -12, 17, 0); add(new THREE.CylinderGeometry(8, 12, 34, 24), matI('#dcd6ca'), 14, 17, 8); add(new THREE.BoxGeometry(16, 14, 16), matI('#c9c3b7'), 2, 7, -22); }
if (f.k === 'potab') { add(new THREE.CylinderGeometry(2.5, 2.5, 26, 12), matI('#d3cec4'), 0, 13, 0); add(new THREE.CylinderGeometry(9, 6, 8, 20), matI('#c4d6df'), 0, 30, 0); }
if (f.k === 'agri') { for (let i = 0; i < 6; i++) add(new THREE.BoxGeometry(120, 0.3, 16), matI(['#dcd49a', '#cdd59f', '#e2d29a'][i % 3]), 0, 0.15, -48 + i * 17); for (let i = 0; i < 3; i++) add(new THREE.CylinderGeometry(4, 4, 22, 14), matI('#cdc7bb'), -70, 11, -12 + i * 10); }
if (f.k === 'step') { for (let i = 0; i < 3; i++) { add(new THREE.CylinderGeometry(12, 12, 2.4, 32), matI('#c9c5bd'), -30 + i * 28, 1.2, 0); add(new THREE.CylinderGeometry(11, 11, 0.2, 32), matI('#a7c8d6'), -30 + i * 28, 2.45, 0); } }
if (f.k === 'super') { add(new THREE.BoxGeometry(72, 9, 44), matI('#e2dbcd'), 0, 4.5, 0); add(new THREE.BoxGeometry(90, 0.2, 42), matI('#d3cfc6'), 0, 0.1, 48); }
if (f.k === 'ciment') { add(new THREE.ConeGeometry(14, 11, 18), matI('#cfbd9d'), -14, 5.5, 8); add(new THREE.ConeGeometry(10, 8, 18), matI('#c7b48f'), 14, 4, 16); add(new THREE.CylinderGeometry(4, 4, 34, 14), matI('#bdb6aa'), 6, 17, -14); }
if (f.k === 'emploi') { add(new THREE.BoxGeometry(16, 44, 16), matI('#cfd3d8'), -12, 22, 0); add(new THREE.BoxGeometry(14, 30, 14), matI('#c3c8ce'), 12, 15, 10); add(new THREE.BoxGeometry(12, 22, 12), matI('#d8dbe0'), 6, 11, -16); }
const e = panneau(f.n, f.c); e.position.set(0, 62, 0); e.scale.set(104, 19.5, 1); g.add(e); // lisible dans la vue d'ensemble (900 m de large)
HC.add(g);
const pts = []; const cx = f.x * 0.45 + (f.z > 0 ? 30 : -30), cz = f.z * 0.45;
for (let i = 0; i <= 40; i++) { const t = i / 40, a = (1 - t) * (1 - t), b = 2 * (1 - t) * t, c = t * t; pts.push([a * f.x + b * cx + c * MARE.x, a * f.z + b * cz + c * MARE.z]); }
surv.flux[f.k] = { r: ruban(pts, 4, f.c, 0.35, 14, 0.2, HC, false), sys: f.sys };
}
// ---------------------------------------------------------------- cadrages nommés
const P = (x, y) => ({ x: (x - D.W / 2) / D.s, z: (y - D.H / 2) / D.s });
function boite(pts, marge = 1.3, min = 30) {
let x0 = Infinity, x1 = -Infinity, z0 = Infinity, z1 = -Infinity;
for (const [x, z] of pts) { x0 = Math.min(x0, x); x1 = Math.max(x1, x); z0 = Math.min(z0, z); z1 = Math.max(z1, z); }
return { x: (x0 + x1) / 2, z: (z0 + z1) / 2, dx: x1 - x0, dz: z1 - z0, marge, min };
}
const ptsMaisonT = [[T.c.x - T.Lu / 2 - 3, T.c.y - T.Lv / 2 - 3], [T.c.x + T.Lu / 2 + 3, T.c.y + T.Lv / 2 + 3], ...(D.liaisons[D.temoin.liaison] || [])];
const bassinsPts = D.plantes.filter((p) => p.length > 3 && aire(p) > 60).flat();
const prochesHaie = [...haiesT].flatMap((k) => echantillon(D.haies[k])).filter((p) => pres2(p) < 20);
// la caméra des cadrages de la maison témoin regarde depuis le côté de sa noue : la chaîne de l'eau se lit au premier plan
const cN = noueT ? noueT.reduce((a, p) => [a[0] + p[0] / noueT.length, a[1] + p[1] / noueT.length], [0, 0]) : [T.c.x, T.c.y + 1];
const azT = Math.atan2(cN[0] - T.c.x, cN[1] - T.c.y) * 0.75; // trois quarts du chemin vers la noue : la façade sud reste lisible
const T_ = (c) => ({ ...c, az: azT });
const CAD = {
quartier: { x: 0, z: 2, w: 250 }, // le socle fait 268,9 m de large : les bords de la forêt et de l'ouest sont à peine rognés
maison: T_(boite(ptsMaisonT, 1.25, 34)),
jardin: T_(boite([...ptsMaisonT, ...prochesHaie, ...(noueT || [])], 1.15, 46)),
haie: T_(boite(prochesHaie.length ? [...prochesHaie, [T.c.x, T.c.y]] : ptsMaisonT, 1.15, 36)),
noue: T_(boite(noueT && noueT.some((p) => pres2(p) < 22) ? [...noueT.filter((p) => pres2(p) < 22), [T.c.x, T.c.y]] : ptsMaisonT, 1.3, 30)),
mare: boite([...D.mare, ...bassinsPts], 1.12, 60),
bassins: boite(bassinsPts.length ? bassinsPts : D.mare, 1.3, 40),
place: { ...P(505, 300), w: 56 },
grand: grand ? { x: grand.position.x, z: grand.position.z, w: 62 } : { ...P(330, 540), w: 62 },
foret: { ...P(1470, 330), w: 120 },
routes: { ...P(720, 520), w: 130 },
sentiers: { ...P(1330, 800), w: 110 },
horscamp: { x: 0, z: -10, w: 900 },
...Object.fromEntries(Object.entries({ eau: 'hc-hydrique', vivant: 'hc-agraire', energie: 'hc-energetique' }).map(([k, nom]) => {
const c = centresSys[k]; return [nom, { x: c.x * 0.62, z: c.z * 0.62, w: Math.max(420, c.r * 2 + 260) }]; // le système et le bord du quartier
})),
transformees: boite(transformees.map((m) => [m.c.x, m.c.y]), 1.2, 120),
paturage: (() => {
// le troupeau le plus proche de la maison témoin, et ses poules
const groupes = {}; for (const t of D.troupeaux) (groupes[t[4] ?? 0] ??= []).push([t[0], t[1]]);
const G_ = Object.values(groupes); if (!G_.length) return { x: 0, z: 0, w: 120 };
const c = (L) => L.reduce((a, p) => [a[0] + p[0] / L.length, a[1] + p[1] / L.length], [0, 0]);
const g = G_.reduce((m, L) => (Math.hypot(c(L)[0] - T.c.x, c(L)[1] - T.c.y) < Math.hypot(c(m)[0] - T.c.x, c(m)[1] - T.c.y) ? L : m));
return boite(g, 1.8, 34);
})(),
};
CAD.toit = CAD.maison;
// ---------------------------------------------------------------- caméra : axonométrie orthographique, pilotable au doigt
const vue = { x: 0, z: 0, az: 0, el: 0.62, w: 210 };
let vol = null, cadrageCourant = null, derive = 0;
const taille = () => ({ W: canvas.clientWidth, H: canvas.clientHeight });
function place() {
const { W, H } = taille(), a = W / H, vh = vue.w / a;
cam.left = -vue.w / 2; cam.right = vue.w / 2; cam.top = vh / 2; cam.bottom = -vh / 2; cam.updateProjectionMatrix();
const d = 1500; cam.position.set(vue.x + Math.sin(vue.az) * Math.cos(vue.el) * d, Math.sin(vue.el) * d, vue.z + Math.cos(vue.az) * Math.cos(vue.el) * d);
cam.lookAt(vue.x, 0, vue.z);
}
let panneau_ = () => ({ bas: 0, gauche: 0 });
function largeur(c) {
if (c.w) return c.w;
const { W, H } = taille(), a = W / H;
return Math.max(c.min, Math.max(c.dx, c.dz * Math.sin(vue.el) * a) * c.marge);
}
function cadrer(nom, duree = 1600, force = false) {
const c = CAD[nom] || CAD.quartier;
if (!force && nom === cadrageCourant) return;
cadrageCourant = nom;
const { W, H } = taille(); let w = largeur(c);
if (W < H && w < 400) w *= 0.8; // en portrait, la largeur de l'écran est la dimension qui manque
// le panneau des fiches cache une partie de l'écran : le cadrage se fait dans la zone libre, et la cible s'y décale
const p = panneau_();
if (p.gauche > 0) w *= W / Math.max(240, W - p.gauche);
const wpp = w / W;
const offX = (p.gauche / 2) * wpp, offY = ((p.bas - 64) / 2) * wpp / Math.sin(vue.el);
const rx = Math.cos(vue.az), rz = -Math.sin(vue.az), fx = -Math.sin(vue.az), fz = -Math.cos(vue.az);
// azimut du cadrage (0 = la planche dans son sens) ; on tourne par le plus court chemin
let az = c.az ?? 0; while (az - vue.az > Math.PI) az -= 2 * Math.PI; while (az - vue.az < -Math.PI) az += 2 * Math.PI;
const ra = Math.cos(az), rza = -Math.sin(az), fa = -Math.sin(az), fza = -Math.cos(az);
const apres = { x: c.x - ra * offX - fa * offY, z: c.z - rza * offX - fza * offY, az, el: vue.el, w };
if (reduit || duree === 0) { Object.assign(vue, apres); besoin = true; return; }
vol = { avant: { ...vue }, apres, t0: performance.now(), d: duree };
}
const bornes = () => { vue.w = Math.max(14, Math.min(1000, vue.w)); vue.x = Math.max(-450, Math.min(450, vue.x)); vue.z = Math.max(-450, Math.min(450, vue.z)); };
function solSous(px, py) {
const { W, H } = taille(), wpp = vue.w / W, sx = px - W / 2, sy = py - H / 2;
const rx = Math.cos(vue.az), rz = -Math.sin(vue.az), fx = -Math.sin(vue.az), fz = -Math.cos(vue.az), k = wpp / Math.sin(vue.el);
return { x: vue.x + rx * sx * wpp - fx * sy * k, z: vue.z + rz * sx * wpp - fz * sy * k };
}
function zoomVers(px, py, f) { const p = solSous(px, py); vue.w *= f; vue.x = p.x + (vue.x - p.x) * f; vue.z = p.z + (vue.z - p.z) * f; bornes(); besoin = true; cadrageCourant = null; }
const ptrs = new Map(); let geste = null;
canvas.addEventListener('pointerdown', (e) => { canvas.setPointerCapture(e.pointerId); ptrs.set(e.pointerId, { x: e.clientX, y: e.clientY }); geste = { t: performance.now(), bouge: 0 }; vol = null; canvas.classList.add('tire'); onToucher('geste'); });
canvas.addEventListener('pointermove', (e) => {
if (!ptrs.has(e.pointerId)) return;
const avant = [...ptrs.values()].map((p) => ({ ...p })); ptrs.set(e.pointerId, { x: e.clientX, y: e.clientY }); const apres = [...ptrs.values()];
const { W } = taille(), wpp = vue.w / W;
const cA = avant.reduce((a, p) => ({ x: a.x + p.x / avant.length, y: a.y + p.y / avant.length }), { x: 0, y: 0 });
const cB = apres.reduce((a, p) => ({ x: a.x + p.x / apres.length, y: a.y + p.y / apres.length }), { x: 0, y: 0 });
const dx = cB.x - cA.x, dy = cB.y - cA.y; if (geste) geste.bouge += Math.abs(dx) + Math.abs(dy);
const rx = Math.cos(vue.az), rz = -Math.sin(vue.az), fx = -Math.sin(vue.az), fz = -Math.cos(vue.az), k = wpp / Math.sin(vue.el);
vue.x -= rx * dx * wpp - fx * dy * k; vue.z -= rz * dx * wpp - fz * dy * k; cadrageCourant = null;
if (apres.length === 2 && avant.length === 2) {
const dA = Math.hypot(avant[0].x - avant[1].x, avant[0].y - avant[1].y), dB = Math.hypot(apres[0].x - apres[1].x, apres[0].y - apres[1].y);
if (dA > 0 && dB > 0) zoomVers(cB.x, cB.y, dA / dB);
const aA = Math.atan2(avant[1].y - avant[0].y, avant[1].x - avant[0].x), aB = Math.atan2(apres[1].y - apres[0].y, apres[1].x - apres[0].x);
let da = aB - aA; if (da > Math.PI) da -= 2 * Math.PI; if (da < -Math.PI) da += 2 * Math.PI; vue.az -= da;
}
bornes(); besoin = true;
});
const fin = (e) => {
ptrs.delete(e.pointerId); if (!ptrs.size) canvas.classList.remove('tire');
if (geste && !ptrs.size && geste.bouge < 8 && performance.now() - geste.t < 450) toucher(e.clientX, e.clientY);
if (!ptrs.size) geste = null;
};
canvas.addEventListener('pointerup', fin); canvas.addEventListener('pointercancel', fin);
canvas.addEventListener('wheel', (e) => { e.preventDefault(); vol = null; zoomVers(e.clientX, e.clientY, e.deltaY > 0 ? 1.12 : 1 / 1.12); onToucher('geste'); }, { passive: false });
const ray = new THREE.Raycaster();
const LIENS_BULLES = { marche: 'quartier-marche', pomme: 'vivant-haie', abeille: 'vivant-foret', miel: 'vivant-foret', fleur: 'vivant-foret', arbre: 'vivant-strates', biere: 'quartier-communs', couverts: 'quartier-communs', outils: 'energie-ensemble', oiseaux: 'eau-mare', promeneurs: 'vivant-sentier', enfant: 'quartier-mobilite', confiture: 'vivant-nourrir', bouteille: 'vivant-nourrir', apaise1: 'quartier-mobilite', apaise2: 'quartier-mobilite' };
function toucher(px, py) {
if (!G.v2035.visible) return;
const { W, H } = taille(); ray.setFromCamera(new THREE.Vector2((px / W) * 2 - 1, -(py / H) * 2 + 1), cam);
const hb = ray.intersectObjects(surv.bulles, false)[0];
if (hb) { onToucher('bulle', hb.object.userData.id, LIENS_BULLES[hb.object.userData.id] || null); return; }
const hits = ray.intersectObjects(cliquables.map((c) => c.o), true);
if (hits.length) { const o = hits[0].object; const c = cliquables.find((c) => { let x = o; while (x) { if (x === c.o) return true; x = x.parent; } return false; }); if (c) onToucher('fiche', c.fiche); }
}
// ---------------------------------------------------------------- état : époque, effet
const etat = { epoque: '2035', fx: null, auj: 0, cibleAuj: 0, fond: false };
function epoque(e) { etat.epoque = e; etat.cibleAuj = e === 'auj' ? 1 : 0; G.v2035.visible = e === '2035'; G.vAuj.visible = e === 'auj'; besoin = true; }
function effet(nom) { etat.fx = nom || null; chantier.visible = nom === 'chantier'; besoin = true; }
function tourner() { vol = { avant: { ...vue }, apres: { ...vue, az: vue.az + Math.PI / 2 }, t0: performance.now(), d: reduit ? 0 : 900 }; cadrageCourant = null; }
// ---------------------------------------------------------------- boucle
const horloge = new THREE.Clock(); let t = 0;
const ease = (k) => (k < 0.5 ? 4 * k * k * k : 1 - Math.pow(-2 * k + 2, 3) / 2);
function redim() { const { W, H } = taille(); renderer.setSize(W, H, false); besoin = true; }
addEventListener('resize', redim); redim();
const pulseEm = (m, on, r, g, b, puls) => m.emissive.setRGB(on ? r * puls : 0, on ? g * puls : 0, on ? b * puls : 0);
function boucle(now) {
requestAnimationFrame(boucle);
if (document.hidden) return;
const dt = Math.min(0.05, horloge.getDelta()); t += dt;
if (vol) { const k = Math.min(1, (now - vol.t0) / (vol.d || 1)), e = ease(k); for (const key of ['x', 'z', 'az', 'el', 'w']) vue[key] = vol.avant[key] + (vol.apres[key] - vol.avant[key]) * e; if (k >= 1) vol = null; }
if (etat.fond && !vol && !reduit) vue.az += dt * 0.025 * derive;
etat.auj += (etat.cibleAuj - etat.auj) * Math.min(1, dt * 4); matAuj.opacity = etat.auj;
const puls = 0.5 + 0.5 * Math.sin(t * 2.6), fx = etat.fx;
const eauT = (i) => i === D.temoin.noue;
surv.noues.forEach((m) => (m.material.opacity = fx === 'noue-plantes' ? (eauT(m.userData.i) ? 0.3 + 0.3 * puls : 0.12) : fx === 'mare' || fx === 'phyto' ? 0.22 : fx === 'eaux-grises' && eauT(m.userData.i) ? 0.3 : 0));
surv.bassins.forEach((m) => (m.material.opacity = fx === 'mare' || fx === 'phyto' ? 0.3 + 0.3 * puls : 0));
surv.sentiers.forEach((m) => (m.material.opacity = fx === 'sentiers' ? 0.3 + 0.35 * puls : 0));
surv.liaisons.forEach((m) => { m.material.uniforms.uT.value = t; m.material.uniforms.uA.value = fx === 'eaux-grises' ? (m.userData.i === D.temoin.liaison ? 0.95 : 0.12) : fx === 'mare' ? 0.35 : 0; });
surv.chemins.forEach((m) => { m.material.uniforms.uT.value = t; m.material.uniforms.uA.value = fx === 'sentiers' || fx === 'mobilite' ? 0.9 : fx === 'foret' ? 0.5 : 0; });
const sysFx = { 'sys-hydrique': 'eau', 'sys-agraire': 'vivant', 'sys-energetique': 'energie' }[fx] || null;
for (const k in surv.flux) {
const f = surv.flux[k], u = f.r.material.uniforms; u.uT.value = t;
const tendu = etat.epoque === 'auj' || f.sys === 'travail', vise = fx === 'flux' || (fx === 'travail' && f.sys === 'travail') || f.sys === sysFx;
u.uA.value = tendu ? (vise ? 0.95 : 0.7) : (vise ? (sysFx ? 0.8 : 0.22) : 0); u.uV.value = tendu ? 0.45 : sysFx && vise ? 0.3 : 0.05;
}
for (const k in halos) halos[k].material.opacity = sysFx === k ? 0.16 + 0.12 * puls : fx === 'flux' ? 0.14 : 0.08;
const vivant = fx === 'haie-essences' || fx === 'strates' || fx === 'nourrir';
pulseEm(matsHaie.couronne, vivant, 0.1, 0.2, 0.02, puls);
pulseEm(matsForet.couronne, fx === 'foret', 0.08, 0.18, 0.04, puls);
pulseEm(mat.paille, fx === 'transformees', 0.26, 0.16, 0, puls);
pulseEm(matT.paille, fx === 'paille' || fx === 'chantier' || fx === 'transformees', 0.26, 0.16, 0, puls);
pulseEm(matT.verre, fx === 'serre', 0.3, 0.25, 0.05, puls);
pulseEm(matT.cuve, fx === 'cuve' || fx === 'pompe', 0.05, 0.22, 0.4, puls);
pulseEm(matT.chem, fx === 'poele', 0.35, 0.12, 0, puls);
pulseEm(mat.pompe, fx === 'pompe', 0.05, 0.25, 0.45, puls);
pulseEm(mat.grandToit, fx === 'communs', 0.35, 0.12, 0, puls);
matEtal.forEach((m) => pulseEm(m, fx === 'marche', 0.25, 0.14, 0, puls));
if (imPotager) pulseEm(imPotager.material, fx === 'strates' || fx === 'nourrir', 0.15, 0.12, 0, puls);
eauV.meshes.forEach((m) => pulseEm(m.material, fx === 'noue-plantes' || fx === 'phyto', 0.06, 0.12, 0.08, puls));
const zoom = Math.max(0.55, Math.min(1.6, vue.w / 60));
for (const [k, L] of Object.entries(etiquettes)) L.forEach((s) => { s.material.opacity += ((fx === k ? 1 : 0) - s.material.opacity) * Math.min(1, dt * 5); s.visible = s.material.opacity > 0.02; s.scale.set(5.6 * zoom * (k === 'noue-plantes' ? 0.8 : 1), 1.23 * zoom * (k === 'noue-plantes' ? 0.8 : 1), 1); });
pluie.forEach((p) => {
const on = fx === 'pluie-toit'; p.s.material.opacity = on ? 0.75 : 0; if (!on) return;
const k = (t * 0.7 + p.ph) % 1; p.s.position.copy(T.g.localToWorld(new THREE.Vector3(p.x, T.eave + T.rh + 0.4 + (1 - k) * 12, p.z)));
});
if (filets.gouttiere) { filets.gouttiere.on = fx === 'gouttiere'; filets.gouttiere.maj(t); }
if (filets.pompe) { filets.pompe.on = fx === 'pompe'; filets.pompe.maj(t); }
if (filets['eaux-grises']) { filets['eaux-grises'].on = fx === 'eaux-grises'; filets['eaux-grises'].maj(t); }
fumee.forEach((s) => {
const k = (t * 0.12 + s.userData.ph) % 1, m = s.userData.m; s.position.set(m.chemW.x - k * 5, m.chemW.y + k * 9, m.chemW.z - k * 2.5);
const fort = fx === 'poele' && m.temoin, sc = (fort ? 1.6 : 1.2) + k * 4.5; s.scale.set(sc, sc, 1); s.material.opacity = (fort ? 0.8 : 0.45) * (1 - k) * Math.min(1, k * 6);
});
surv.ondes.forEach((o) => { const k = (t * 0.35 + o.userData.ph) % 1, on = fx === 'mare' || fx === 'phyto'; o.scale.setScalar(1 + k * 11); o.material.opacity = on ? 0.6 * (1 - k) : 0; });
const lieesA = { marche: ['marche', 'musique'], 'haie-essences': ['pomme'], nourrir: ['pomme', 'confiture', 'bouteille', 'marche'], foret: ['abeille', 'miel', 'fleur', 'arbre'], communs: ['biere', 'couverts', 'outils'], mare: ['oiseaux', 'musique'], sentiers: ['promeneurs'], mobilite: ['enfant', 'promeneurs', 'apaise1', 'apaise2', 'ballons'], chantier: ['outils'] }[fx] || [];
const zb = Math.max(0.32, Math.min(1, vue.w / 150)); // en gros plan, les bulles se font petites : elles ne masquent plus la maison
surv.bulles.forEach((s) => { const u = s.userData, on = lieesA.includes(u.id), k = (on ? 1.35 : 1) * zb; s.position.y = u.base + Math.sin(t * 1.4 + u.ph) * 0.35; s.scale.set(u.taille * k, u.taille * k, 1); s.material.opacity = (fx === 'haie-essences' || fx === 'noue-plantes' || fx === 'strates') && !on ? 0.25 : lieesA.length && !on ? 0.55 : 1; });
anim.forEach((f) => f(t));
vie.troupeau.children.forEach((o, i) => { o.position.y = fx === 'paturage' ? Math.abs(Math.sin(t * 3 + i)) * 0.25 : 0; });
mobiles.forEach((m) => {
if (m.suit) { const s = m.suit; m.obj.visible = s.obj.parent.visible; const p = surTrajet(s.t, s.s - s.sens * m.d); m.obj.position.set(p.x + 0.5, 0, p.z + 0.4); m.obj.rotation.y = s.obj.rotation.y; return; }
if (!m.obj.parent.visible) return; m.s += m.v * dt * m.sens; if (m.s > m.t.L) { m.s = m.t.L; m.sens = -1; } if (m.s < 0) { m.s = 0; m.sens = 1; }
const p = surTrajet(m.t, m.s); let dx = p.dx * m.sens, dz = p.dz * m.sens; const n = Math.hypot(dx, dz) || 1; dx /= n; dz /= n;
m.obj.position.set(p.x - dz * m.d, 0, p.z + dx * m.d); m.obj.rotation.y = Math.atan2(-dz, dx);
});
const vMob = fx === 'mobilite' || fx === 'travail';
mobiles.forEach((m) => { if (m.role && m.obj.children) { const k = vMob && (m.role === 'cargo' || m.role === 'attelage' || (fx === 'travail' && m.role === 'partagee')) ? 1.25 : 1; m.obj.scale.setScalar(k); } });
place(); renderer.render(scene, cam); besoin = false;
}
epoque('2035'); // le groupe « aujourd'hui » (voitures garées, thuyas) est caché dès le départ
place(); requestAnimationFrame(boucle);
return {
cadrer, effet, epoque, tourner,
poserPanneau(f) { panneau_ = f; },
fond(on, d = 1) { etat.fond = on; derive = d; },
cadrages: Object.keys(CAD),
apercu() { renderer.render(scene, cam); return renderer.domElement.toDataURL('image/jpeg', 0.86); },
};
}