// Le végétal de la maquette, généré depuis content/essences.md (esprit Grasshopper : des paramètres, un générateur, // des instances). Les positions viennent du plan (haies = limites de parcelles, forêt et arbres existants = blocs Rhino) ; // l'essence, la taille, la forme et la couleur viennent du tableau. Changer le tableau change toute la scène. import * as THREE from 'three'; import { mergeGeometries, mergeVertices } from 'three/addons/utils/BufferGeometryUtils.js'; export const rnd = (i) => { const x = Math.sin(i * 12.9898 + 78.233) * 43758.5453; return x - Math.floor(x); }; const norm = (s) => s.normalize('NFD').replace(/[̀-ͯ]/g, '').toLowerCase().trim(); // ---------------------------------------------------------------- géométries // couronne « lavis » : sphère légèrement bosselée, lissée (pas de facettes : la planche est peinte, pas taillée) function couronne(seed, bosse = 0.13, detail = 2) { let g = new THREE.IcosahedronGeometry(1, detail); g.deleteAttribute('normal'); g.deleteAttribute('uv'); g = mergeVertices(g); const p = g.attributes.position, v = new THREE.Vector3(); for (let i = 0; i < p.count; i++) { v.fromBufferAttribute(p, i); const n = 1 + bosse * Math.sin(v.x * 3.1 + seed) * Math.sin(v.y * 2.6 + seed * 2.1) * Math.sin(v.z * 3.4 + seed * 2.9); p.setXYZ(i, v.x * n, v.y * n, v.z * n); } g.computeVertexNormals(); return g; } function buisson() { const a = couronne(3, 0.16, 1).translate(-0.32, -0.05, 0.1).scale(0.72, 0.8, 0.72); const b = couronne(5, 0.16, 1).translate(0.3, 0.02, -0.12).scale(0.7, 0.85, 0.7); const c = couronne(7, 0.16, 1).translate(0, 0.22, 0.05).scale(0.62, 0.7, 0.62); return mergeVertices(mergeGeometries([a, b, c].map((x) => { x.deleteAttribute('uv'); return x; }))); } // tronc : cylindre unitaire posé sur le sol const geoTronc = new THREE.CylinderGeometry(0.55, 1, 1, 6).translate(0, 0.5, 0); const geoFruit = new THREE.IcosahedronGeometry(1, 0); // part de tronc et proportions de la couronne, par forme (lu sur la description des dessins du recueil) const FORMES = { 'étalée': { tronc: 0.34, sx: 1, sy: 0.92, geo: () => couronne(1) }, 'boule': { tronc: 0.3, sx: 1, sy: 1, geo: () => couronne(2) }, 'boule basse': { tronc: 0.1, sx: 1, sy: 1, geo: () => couronne(4) }, 'arrondie': { tronc: 0.46, sx: 1, sy: 1, geo: () => couronne(6) }, 'ovoïde': { tronc: 0.32, sx: 0.84, sy: 1, geo: () => couronne(8, 0.18) }, 'buisson': { tronc: 0, sx: 1, sy: 1, geo: buisson }, }; const GEO = {}; const geoForme = (f) => (GEO[f] ??= FORMES[f].geo()); // ---------------------------------------------------------------- couleurs export function teinte(ton, tons, i) { let hex = ton; if (ton === 'melange') hex = tons[['clair', 'moyen', 'fonce'][Math.floor(rnd(i * 3.7) * 3)]]; else if (tons[ton]) hex = tons[ton]; const c = new THREE.Color(hex); // un lavis n'est jamais uniforme : ±6 % de valeur, ±3 % de teinte const hsl = {}; c.getHSL(hsl); c.setHSL(hsl.h + (rnd(i * 1.3) - 0.5) * 0.03, hsl.s * (0.92 + rnd(i * 2.1) * 0.12), Math.min(0.92, hsl.l * (0.94 + rnd(i * 5.9) * 0.12))); return c; } // ---------------------------------------------------------------- un lot d'arbres → meshes instanciés // items : { x, z, H, W, forme, couleur: THREE.Color, fruit: '#hex'|null, rot } export function lot(items, mat, avecFruits) { const groupe = new THREE.Group(); const M = new THREE.Matrix4(), Q = new THREE.Quaternion(), S = new THREE.Vector3(), V = new THREE.Vector3(), Y = new THREE.Vector3(0, 1, 0); const parForme = {}; for (const it of items) (parForme[it.forme] ??= []).push(it); const troncs = items.filter((it) => FORMES[it.forme].tronc > 0); const imT = troncs.length ? new THREE.InstancedMesh(geoTronc, mat.tronc, troncs.length) : null; if (imT) { imT.castShadow = true; groupe.add(imT); } let iT = 0; const fruits = []; for (const [forme, L] of Object.entries(parForme)) { const F = FORMES[forme]; const im = new THREE.InstancedMesh(geoForme(forme), mat.couronne, L.length); im.castShadow = true; im.receiveShadow = true; L.forEach((it, i) => { const ht = it.H * F.tronc, rx = (it.W / 2) * F.sx, ry = ((it.H - ht) / 2) * F.sy; const cy = ht + ry; Q.setFromAxisAngle(Y, it.rot); im.setMatrixAt(i, M.compose(V.set(it.x, cy, it.z), Q, S.set(rx, ry, rx))); im.setColorAt(i, it.couleur); it.haut = cy + ry; it.centre = cy; it.rx = rx; it.ry = ry; if (imT && F.tronc > 0) { const rt = Math.max(0.07, it.W * 0.035); imT.setMatrixAt(iT++, M.compose(V.set(it.x, 0, it.z), Q.identity(), S.set(rt, ht + ry * 0.4, rt))); } if (avecFruits && it.fruit) { const n = forme === 'buisson' ? 3 : 6, r = forme === 'buisson' ? 0.07 : Math.max(0.11, it.W * 0.035); for (let k = 0; k < n; k++) { const a = rnd(it.x * 7 + it.z * 3 + k * 11) * Math.PI * 2, e = -0.15 + rnd(it.z * 5 + k * 13) * 0.9; const c = Math.sqrt(1 - e * e); fruits.push({ x: it.x + Math.cos(a) * c * rx * 0.96, y: cy + e * ry * 0.96, z: it.z + Math.sin(a) * c * rx * 0.96, r, c: it.fruit }); } } }); im.instanceColor.needsUpdate = true; groupe.add(im); } if (fruits.length) { const imF = new THREE.InstancedMesh(geoFruit, mat.fruit, fruits.length); const col = new THREE.Color(); fruits.forEach((f, i) => { imF.setMatrixAt(i, M.compose(V.set(f.x, f.y, f.z), Q.identity(), S.setScalar(f.r))); imF.setColorAt(i, col.set(f.c)); }); imF.instanceColor.needsUpdate = true; groupe.add(imF); } return groupe; } // ---------------------------------------------------------------- poser les essences le long des haies function polyligne(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] }; } export function surPolyligne(t, 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); const dx = b[0] - a[0], dz = b[1] - a[1], n = Math.hypot(dx, dz) || 1; return { x: a[0] + dx * k, z: a[1] + dz * k, tx: dx / n, tz: dz / n }; } export function haiesEssences(haies, E) { const R = E.reglages, arbres = new Map(E.arbres.map((e) => [norm(e.nom), e])), arbustes = new Map(E.arbustes.map((e) => [norm(e.nom), e])); const seqA = E.sequenceArbres.map((n) => arbres.get(norm(n))), seqB = E.sequenceArbustes.map((n) => arbustes.get(norm(n))); const items = []; haies.forEach((h, k) => { const t = polyligne(h); const dA = Math.floor(rnd(k * 7 + 1) * seqA.length), dB = Math.floor(rnd(k * 13 + 2) * seqB.length); const sA = []; for (let s = R.pasArbres * (0.3 + rnd(k) * 0.4), i = 0; s < t.L - 0.4; s += R.pasArbres, i++) { sA.push(s); const p = surPolyligne(t, s), e = seqA[(dA + i) % seqA.length], j = 0.9 + rnd(k * 31 + i) * 0.2, lat = (rnd(k * 17 + i) - 0.5) * 0.5; items.push({ x: p.x - p.tz * lat, z: p.z + p.tx * lat, H: e.hauteur * R.echelleHauteur * j, W: e.couronne * R.echelleCouronne * j, forme: e.forme, couleur: teinte(e.ton, R.tons, k * 1000 + i), fruit: R.fruits ? e.fruit : null, rot: rnd(k * 5 + i) * 6.28, nom: e.nom, haie: k, s, strate: 'arbre' }); } for (let s = R.pasArbustes / 2, i = 0; s < t.L; s += R.pasArbustes, i++) { if (sA.some((a) => Math.abs(a - s) < 1.0)) continue; const p = surPolyligne(t, s), e = seqB[(dB + i) % seqB.length], j = 0.85 + rnd(k * 41 + i) * 0.3, lat = (rnd(k * 19 + i) - 0.5) * 0.7; items.push({ x: p.x - p.tz * lat, z: p.z + p.tx * lat, H: e.hauteur * j, W: e.couronne * j, forme: e.forme, couleur: teinte(e.ton, R.tons, k * 1000 + 500 + i), fruit: R.fruits ? e.fruit : null, rot: rnd(k * 9 + i) * 6.28, nom: e.nom, haie: k, s, strate: 'arbuste' }); } }); return items; } // fruitiers isolés au milieu des jardins : une essence du tableau, au hasard mais toujours la même export function vergers(points, E) { const R = E.reglages; return points.map(([x, z], i) => { const e = E.arbres[Math.floor(rnd(x * 3.1 + z * 1.7) * E.arbres.length)], j = 0.9 + rnd(i * 3) * 0.2; return { x, z, H: e.hauteur * R.echelleHauteur * j, W: e.couronne * R.echelleCouronne * j, forme: e.forme, couleur: teinte(e.ton, R.tons, 70000 + i), fruit: R.fruits ? e.fruit : null, rot: rnd(i * 7) * 6.28, nom: e.nom }; }); } // arbres du plan (forêt, existants, buissons) : taille du tableau × taille relative du bloc Rhino export function arbresPlan(arbres, E, genre, ref, mediane) { const A = E.autres[genre], R = E.reglages, ech = genre === 'buisson existant' ? 1 : R.echelleHauteur, echW = genre === 'buisson existant' ? 1 : R.echelleCouronne; return arbres.map((a, i) => { const k = Math.max(0.7, Math.min(1.35, a[2] / mediane)) * (0.92 + rnd(i * 5 + ref) * 0.16); return { x: a[0], z: a[1], H: A.hauteur * ech * k, W: A.couronne * echW * k, forme: A.forme, couleur: teinte(A.ton, R.tons, ref * 10000 + i), fruit: null, rot: rnd(i + ref) * 6.28 }; }); } // ---------------------------------------------------------------- aujourd'hui : des haies de thuyas taillées export function thuyas(haies, mat) { const segs = []; for (const h of haies) for (let i = 1; i < h.length; i++) segs.push([h[i - 1], h[i]]); const im = new THREE.InstancedMesh(new THREE.BoxGeometry(1, 1, 1).translate(0, 0.5, 0), mat, segs.length); const M = new THREE.Matrix4(), Q = new THREE.Quaternion(), S = new THREE.Vector3(), V = new THREE.Vector3(), Y = new THREE.Vector3(0, 1, 0); segs.forEach(([a, b], i) => { const L = Math.hypot(b[0] - a[0], b[1] - a[1]); Q.setFromAxisAngle(Y, Math.atan2(-(b[1] - a[1]), b[0] - a[0])); im.setMatrixAt(i, M.compose(V.set((a[0] + b[0]) / 2, 0, (a[1] + b[1]) / 2), Q, S.set(L + 0.3, 1.9, 0.85))); }); im.castShadow = true; im.receiveShadow = true; return im; } // ---------------------------------------------------------------- les plantes d'eau (recueil p. 19 et p. 21) // 0 jonc · 1 lysimaque · 2 salicaire · 3 papyrus · 4 pétasite · 5 nénuphar · 6 iris jaune · 7 roseau export const NOMS_AQUA = ['jonc', 'lysimaque', 'salicaire', 'papyrus', 'pétasite', 'nénuphar', 'iris jaune', 'roseau']; function tiges(n, h, r, evase, seed) { const L = []; for (let i = 0; i < n; i++) { const a = (i / n) * Math.PI * 2 + rnd(seed + i), e = evase * (0.5 + rnd(seed * 3 + i) * 0.5), hh = h * (0.75 + rnd(seed * 7 + i) * 0.35); const g = new THREE.ConeGeometry(r, hh, 4).translate(0, hh / 2, 0); g.rotateZ(Math.cos(a) * e); g.rotateX(Math.sin(a) * e); g.deleteAttribute('uv'); L.push(g); } return mergeGeometries(L); } function touffe(r, h) { const g = couronne(9, 0.2, 1).scale(r, h, r).translate(0, h * 0.8, 0); g.deleteAttribute('uv'); return g; } const AQUA = [ { feuille: () => tiges(9, 1.1, 0.035, 0.35, 1), cf: '#8ea366' }, { feuille: () => touffe(0.42, 0.42), cf: '#87a35d', fleur: () => tiges(5, 0.35, 0.06, 0.25, 2).translate(0, 0.62, 0), cfl: '#e2c24a' }, { feuille: () => touffe(0.38, 0.45), cf: '#86a060', fleur: () => tiges(4, 0.55, 0.07, 0.2, 3).translate(0, 0.7, 0), cfl: '#b0608a' }, { feuille: () => tiges(7, 1.5, 0.025, 0.18, 4), cf: '#94ad62', fleur: () => { const L = []; for (let i = 0; i < 4; i++) { const g = new THREE.SphereGeometry(0.28, 8, 4).scale(1, 0.35, 1).translate((rnd(i) - 0.5) * 0.4, 1.55 + rnd(i * 3) * 0.2, (rnd(i * 5) - 0.5) * 0.4); g.deleteAttribute('uv'); L.push(g); } return mergeGeometries(L); }, cfl: '#a9bd6c' }, { feuille: () => touffe(0.62, 0.3), cf: '#6f8f55' }, { feuille: () => { const g = new THREE.CylinderGeometry(0.42, 0.42, 0.04, 14).translate(0, 0.06, 0); g.deleteAttribute('uv'); return g; }, cf: '#7fa77a', fleur: () => { const g = new THREE.SphereGeometry(0.11, 8, 6).translate(0.12, 0.13, 0.05); g.deleteAttribute('uv'); return g; }, cfl: '#efe7c8' }, { feuille: () => tiges(6, 0.95, 0.05, 0.22, 5), cf: '#8aa665', fleur: () => tiges(3, 0.2, 0.07, 0.6, 6).translate(0, 0.85, 0), cfl: '#e6c84f' }, { feuille: () => tiges(8, 2.1, 0.03, 0.15, 7), cf: '#a9a670', fleur: () => { const g = new THREE.SphereGeometry(0.16, 6, 4).scale(1, 2.2, 1).translate(0, 2.15, 0); g.deleteAttribute('uv'); return g; }, cfl: '#b39b6b' }, ]; export function plantesEau(aqua) { const groupe = new THREE.Group(), parType = {}; for (const p of aqua) (parType[p[2]] ??= []).push(p); const M = new THREE.Matrix4(), Q = new THREE.Quaternion(), S = new THREE.Vector3(), V = new THREE.Vector3(), Y = new THREE.Vector3(0, 1, 0); const meshes = []; for (const [t, L] of Object.entries(parType)) { const def = AQUA[t]; const parts = [[def.feuille(), def.cf], def.fleur ? [def.fleur(), def.cfl] : null].filter(Boolean); for (const [geo, c] of parts) { const im = new THREE.InstancedMesh(geo, new THREE.MeshLambertMaterial({ color: c, emissive: '#000000' }), L.length); L.forEach((p, i) => { Q.setFromAxisAngle(Y, rnd(i * 3 + +t) * 6.28); im.setMatrixAt(i, M.compose(V.set(p[0], 0, p[1]), Q, S.setScalar(p[3]))); }); im.castShadow = +t !== 5; groupe.add(im); meshes.push(im); } } return { groupe, meshes }; } // ---------------------------------------------------------------- le potager au pied de la haie témoin (strate herbacée) export function potager(points) { const C = ['#8aa35f', '#b8714f', '#9db36a', '#cf9c52', '#7d965a', '#c9b05a']; const im = new THREE.InstancedMesh(new THREE.IcosahedronGeometry(1, 1), new THREE.MeshLambertMaterial({ color: '#ffffff', emissive: '#000000' }), points.length); const M = new THREE.Matrix4(), col = new THREE.Color(); points.forEach(([x, z], i) => { const r = 0.13 + rnd(i * 3) * 0.08; im.setMatrixAt(i, M.makeScale(r * 1.4, r, r * 1.4).setPosition(x, r * 0.8, z)); im.setColorAt(i, col.set(C[i % C.length])); }); im.instanceColor.needsUpdate = true; im.castShadow = true; return im; }