/** * Terreno procedural: capa superior mullida (borde festoneado o losetas) sobre un cuerpo con vetas, * extruido con biselado redondo y profundidad visible hacia atrás. Todo va a un GeoBatch. */ import * as THREE from 'three'; import { BODY_REPEAT, bodyTexture, lavaVeins } from './textures'; import { type GeoSink, P, extrude, kitMat, paint, placed, rng, smooth } from './util'; export interface TerrainStyle { capTop: number; capSide: number; capDark: number; capThick: number; /** Profundidad de los festones del borde (0 = recto). */ scallop: number; /** Ancho de loseta (capa superior troceada: acera, baldosas, tablones). */ tile?: number; body: string; capGloss: number; bodyGloss: number; /** Material físico por defecto (pasos, fricción). */ material: string; tufts?: boolean; crystals?: boolean; icicles?: boolean; pebble?: number; lava?: boolean; } export const STYLES: Record = { grass: { capTop: 0x78d64a, capSide: 0x52b838, capDark: 0x2f8a2c, capThick: 0.55, scallop: 0.3, body: 'dirt', capGloss: 0.3, bodyGloss: 0.2, material: 'grass', tufts: true, pebble: 0xd9a066 }, stone: { capTop: 0xd5dbe8, capSide: 0xb4bccd, capDark: 0x868ea5, capThick: 0.42, scallop: 0, tile: 1.6, body: 'stone', capGloss: 0.3, bodyGloss: 0.25, material: 'stone', pebble: 0xa7afc4 }, sand: { capTop: 0xffe4a3, capSide: 0xf6cf82, capDark: 0xd9a95c, capThick: 0.45, scallop: 0.16, body: 'sand', capGloss: 0.2, bodyGloss: 0.15, material: 'sand', pebble: 0xfff0d0 }, city: { capTop: 0xe4e0d8, capSide: 0xc8c3b9, capDark: 0x99938a, capThick: 0.36, scallop: 0, tile: 1.25, body: 'city', capGloss: 0.25, bodyGloss: 0.2, material: 'stone' }, cave: { capTop: 0xb78cf0, capSide: 0x9264d4, capDark: 0x5e3c99, capThick: 0.42, scallop: 0.22, body: 'cave', capGloss: 0.35, bodyGloss: 0.25, material: 'stone', crystals: true, pebble: 0x7a62a6 }, wood: { capTop: 0xe0a35e, capSide: 0xc98a4b, capDark: 0x93592c, capThick: 0.3, scallop: 0, tile: 0.55, body: 'wood', capGloss: 0.3, bodyGloss: 0.25, material: 'wood' }, ice: { capTop: 0xeef6ff, capSide: 0xd6ebfb, capDark: 0x9ccbec, capThick: 0.5, scallop: 0.3, body: 'ice', capGloss: 0.55, bodyGloss: 0.75, material: 'ice', icicles: true }, snow: { capTop: 0xf0f5ff, capSide: 0xdce8f8, capDark: 0xa9c2e2, capThick: 0.55, scallop: 0.34, body: 'dirt', capGloss: 0.35, bodyGloss: 0.2, material: 'ice', icicles: true, pebble: 0xd9a066 }, castle: { capTop: 0xd2cbe6, capSide: 0xafa7cb, capDark: 0x7f77a1, capThick: 0.4, scallop: 0, tile: 1.4, body: 'castle', capGloss: 0.3, bodyGloss: 0.25, material: 'stone' }, lava: { capTop: 0x5a3434, capSide: 0x452628, capDark: 0x261214, capThick: 0.36, scallop: 0.14, body: 'lava', capGloss: 0.3, bodyGloss: 0.25, material: 'stone', lava: true, pebble: 0x5a363b }, cloud: { capTop: 0xf2f4ff, capSide: 0xdde4fa, capDark: 0xaebbe8, capThick: 0.6, scallop: 0.45, body: 'cloud', capGloss: 0.2, bodyGloss: 0.15, material: 'cloud' }, metal: { capTop: 0xc9d3e6, capSide: 0xa9b5cc, capDark: 0x76829c, capThick: 0.3, scallop: 0, tile: 2, body: 'metal', capGloss: 0.7, bodyGloss: 0.6, material: 'metal' }, }; export function styleOf(name?: string): TerrainStyle { return STYLES[name ?? 'grass'] ?? STYLES.grass; } // ───────────── Materiales ───────────── export function capMat(name: string): THREE.Material { const st = styleOf(name); return kitMat(`cap:${name}`, { color: 0xffffff, vertexColors: true, gloss: st.capGloss, rim: 0.35 }); } export function bodyMat(name: string): THREE.Material { const st = styleOf(name); const tex = bodyTexture(st.body); tex.repeat.set(BODY_REPEAT, BODY_REPEAT); return kitMat(`body:${name}`, { color: 0xffffff, vertexColors: true, map: tex, gloss: st.bodyGloss, rim: 0.25, ...(st.lava ? { emissive: 0xff6a1a, emissiveIntensity: 1.0, emissiveMap: lavaVeins() } : {}), }); } export const detailMat = (): THREE.Material => kitMat('detail', { color: 0xffffff, vertexColors: true, gloss: 0.35 }); export const glossyDetailMat = (): THREE.Material => kitMat('detailGloss', { color: 0xffffff, vertexColors: true, gloss: 0.85 }); export const crystalMat = (i: number): THREE.Material => i % 2 ? kitMat('crystalPink', { color: 0xff9ae6, emissive: 0xff5ad0, emissiveIntensity: 0.7, gloss: 1 }) : kitMat('crystalCyan', { color: 0x9ff2ff, emissive: 0x3fd0ff, emissiveIntensity: 0.7, gloss: 1 }); // ───────────── Formas ───────────── const V = (x: number, y: number) => new THREE.Vector2(x, y); function arc(pts: THREE.Vector2[], cx: number, cy: number, r: number, a0: number, a1: number, n = 5): void { for (let i = 0; i <= n; i++) { const a = a0 + ((a1 - a0) * i) / n; pts.push(V(cx + Math.cos(a) * r, cy + Math.sin(a) * r)); } } /** Rectángulo de esquinas redondeadas (contorno ya encogido `b`). */ export function roundRect(x0: number, y0: number, x1: number, y1: number, rt: number, rb: number, b: number): THREE.Shape { const L = x0 + b, R = x1 - b, B = y0 + b, T = y1 - b; const w = R - L, h = T - B; rt = Math.max(0, Math.min(rt, w / 2 - 0.001, h / 2 - 0.001)); rb = Math.max(0, Math.min(rb, w / 2 - 0.001, h / 2 - 0.001)); const pts: THREE.Vector2[] = []; if (rb > 0) arc(pts, L + rb, B + rb, rb, Math.PI, Math.PI * 1.5); else pts.push(V(L, B)); if (rb > 0) arc(pts, R - rb, B + rb, rb, Math.PI * 1.5, Math.PI * 2); else pts.push(V(R, B)); if (rt > 0) arc(pts, R - rt, T - rt, rt, 0, Math.PI / 2); else pts.push(V(R, T)); if (rt > 0) arc(pts, L + rt, T - rt, rt, Math.PI / 2, Math.PI); else pts.push(V(L, T)); return new THREE.Shape(pts); } /** Capa superior con festones colgando por debajo. */ function capShape(L0: number, R0: number, top: number, thick: number, scallop: number, seed: number, b: number): THREE.Shape { const r = rng(seed); const L = L0 + b, R = R0 - b; const yT = top - b; const yB = top - thick + b; const rr = Math.min(thick * 0.42, 0.24); const pts: THREE.Vector2[] = []; if (scallop > 0) { let x = L; while (x < R - 0.05) { const w = Math.min(0.5 + r() * 0.6, R - x); const d = (0.35 + r() * 0.65) * scallop * Math.min(1, w / 0.5); const n = Math.max(3, Math.ceil(w / 0.08)); for (let i = 0; i < n; i++) { const t = i / n; pts.push(V(x + w * t, yB - d * Math.pow(Math.sin(Math.PI * t), 0.75))); } x += w; } } else { pts.push(V(L, yB)); } pts.push(V(R, yB)); arc(pts, R - rr, yT - rr, rr, 0, Math.PI / 2, 4); arc(pts, L + rr, yT - rr, rr, Math.PI / 2, Math.PI, 4); return new THREE.Shape(pts); } function capPainter(st: TerrainStyle, top: number, zBack: number, k = 1) { const top_ = new THREE.Color(st.capTop); const side = new THREE.Color(st.capSide); const dark = new THREE.Color(st.capDark); const tmp = new THREE.Color(); return (p: THREE.Vector3, n: THREE.Vector3, out: THREE.Color) => { const t = smooth(top - st.capThick - st.scallop, top - 0.04, p.y); out.copy(dark).lerp(side, t); tmp.copy(top_); // variación suave a lo largo del suelo const v = 0.94 + 0.12 * Math.sin(p.x * 0.73 + p.z * 0.41) * Math.sin(p.x * 0.31 + 1.7); tmp.multiplyScalar(v); out.lerp(tmp, smooth(0.3, 0.85, n.y)); if (p.z < zBack + 1.5) out.multiplyScalar(0.72 + 0.28 * smooth(zBack, zBack + 1.5, p.z)); out.multiplyScalar(k); }; } function bodyPainter(top: number) { return (p: THREE.Vector3, n: THREE.Vector3, out: THREE.Color) => { let k = 0.58 + 0.42 * smooth(top - 7, top - 0.5, p.y); if (Math.abs(n.x) > 0.6) k *= 0.82; out.setRGB(k, k * 0.98, k * 0.97); }; } /** * El bisel trasero mira hacia atrás (-z): visto desde la cámara queda rasante y la luz de borde * (fresnel) lo convertía en una franja casi blanca. Se reorientan esas normales hacia arriba/delante. */ function tameBackEdge(g: THREE.BufferGeometry): THREE.BufferGeometry { const n = g.attributes.normal as THREE.BufferAttribute; const v = new THREE.Vector3(); for (let i = 0; i < n.count; i++) { v.fromBufferAttribute(n, i); if (v.z >= -0.02) continue; v.z = -v.z * 0.15; v.y = Math.max(v.y, 0.2); v.normalize(); n.setXYZ(i, v.x, v.y, v.z); } n.needsUpdate = true; return g; } export interface SlabOpts { style: string; zBack: number; zFront: number; seed: number; /** 'ground' (fondo recto, va hasta abajo) | 'block' (redondeado entero) | 'island' (fondo cónico) */ kind: 'ground' | 'block' | 'island'; /** Sin decoración automática (matas, piedrecitas, cristales). */ plain?: boolean; } /** Construye un tramo de terreno con superficie en `top` entre L y R hasta `bottom`. */ export function buildSlab(sink: GeoSink, L: number, R: number, top: number, bottom: number, o: SlabOpts): void { const st = styleOf(o.style); const r = rng(o.seed); const w = R - L; const capFront = o.zFront; const bodyFront = o.zFront - 0.16; const bodyBack = o.zBack + 0.1; const over = 0.12; const capThick = Math.min(st.capThick, Math.max(0.2, (top - bottom) * 0.7)); const bodyTop = top - capThick * 0.5; // Cuerpo if (top - bottom > capThick * 0.6) { let shape: THREE.Shape; const bb = 0.22; if (o.kind === 'island') { const cx = (L + R) / 2 + (r() - 0.5) * w * 0.12; const right: THREE.Vector2[] = []; const left: THREE.Vector2[] = []; const n = 12; for (let i = 0; i <= n; i++) { const t = i / n; const y = bodyTop - bb - (bodyTop - bottom - bb * 2) * t; const f = 1 - Math.pow(t, 1.5); const bump = (1 - t) * t * 0.5; right.push(V(cx + (R - bb - cx) * f + 0.1 + bump * Math.sin(i * 1.7 + o.seed), y)); left.push(V(cx - (cx - L - bb) * f - 0.1 - bump * Math.sin(i * 2.3 + o.seed * 0.5), y)); } shape = new THREE.Shape([...right, ...left.reverse()]); } else { const rb = o.kind === 'block' ? 0.3 : 0; shape = roundRect(L, bottom, R, bodyTop, 0.3, rb, bb); } const g = extrude(shape, bodyBack, bodyFront, bb, 6, 3); paint(g, bodyPainter(top)); sink.add(bodyMat(o.style), tameBackEdge(g)); } // Capa superior const cm = capMat(o.style); if (st.tile) { const n = Math.max(1, Math.round((w + over * 2) / st.tile)); const tw = (w + over * 2) / n; for (let i = 0; i < n; i++) { const a = L - over + i * tw + 0.03; const b = a + tw - 0.06; const g = extrude(roundRect(a, top - capThick, b, top, 0.1, 0.06, 0.06), o.zBack, capFront, 0.06, 3, 2); paint(g, capPainter(st, top, o.zBack, 0.93 + r() * 0.12)); sink.add(cm, tameBackEdge(g)); } } else { const g = extrude(capShape(L - over, R + over, top, capThick, st.scallop, o.seed, 0.12), o.zBack, capFront, 0.12, 6, 3); paint(g, capPainter(st, top, o.zBack)); sink.add(cm, tameBackEdge(g)); } if (o.plain) return; addDetails(sink, st, L, R, top, bottom, capThick, bodyFront, capFront, r, o.kind); } const cTmp = new THREE.Color(); function addDetails(sink: GeoSink, st: TerrainStyle, L: number, R: number, top: number, bottom: number, capThick: number, bodyFront: number, capFront: number, r: () => number, kind: SlabOpts['kind']): void { const w = R - L; const dm = detailMat(); // matas de hierba asomando por el borde if (st.tufts) { let x = L + 0.4 + r() * 0.8; while (x < R - 0.3) { const n = 3 + Math.floor(r() * 3); const z = capFront - 0.35 - r() * 1.4; for (let i = 0; i < n; i++) { const h = 0.22 + r() * 0.22; const g = placed(P.coneLo(), x + (r() - 0.5) * 0.3, top + h / 2 - 0.05, z + (r() - 0.5) * 0.3, 0.055, h, 0.055, 0, 0, (r() - 0.5) * 0.7); cTmp.setHex(i % 2 ? 0x8ee858 : 0x5cc23e); const c = cTmp.clone(); paint(g, (p, _n, out) => out.copy(c).multiplyScalar(0.75 + 0.35 * smooth(top, top + 0.35, p.y))); sink.add(dm, g); } if (r() < 0.25) { const fc = [0xffffff, 0xffc928, 0xff7ab8][Math.floor(r() * 3)]; const g = placed(P.sphereLo(), x + 0.1, top + 0.22, z + 0.1, 0.08, 0.08, 0.08); paint(g, (_p, _n, out) => out.setHex(fc)); sink.add(dm, g); } x += 0.9 + r() * 1.8; } } // piedrecitas incrustadas en la cara frontal if (st.pebble !== undefined && top - bottom > 1.2) { const span = Math.min(top - bottom - capThick - 0.5, 6); const n = Math.floor(w * (kind === 'island' ? 0.4 : 0.55)); const base = new THREE.Color(st.pebble); for (let i = 0; i < n; i++) { const yy = top - capThick - 0.35 - Math.pow(r(), 1.4) * span; let xx = L + 0.4 + r() * (w - 0.8); if (kind === 'island') { const t = (top - yy) / Math.max(0.5, top - bottom); const half = (w / 2) * (1 - Math.pow(t, 1.6)) - 0.4; if (half < 0.2) continue; xx = (L + R) / 2 + (r() - 0.5) * 2 * half; } const s = 0.1 + r() * 0.14; const g = placed(P.sphereLo(), xx, yy, bodyFront - 0.02, s * (1.1 + r() * 0.5), s * 0.8, s * 0.6, 0, 0, (r() - 0.5)); const k = 0.85 + r() * 0.3; paint(g, (p, _n, out) => out.copy(base).multiplyScalar(k * (0.75 + 0.35 * smooth(yy - s, yy + s, p.y)))); sink.add(dm, g); } } // cristales de cueva if (st.crystals) { let x = L + 0.6 + r() * 2; while (x < R - 0.4) { const onTop = r() < 0.5; const ci = Math.floor(r() * 2); const n = 2 + Math.floor(r() * 3); for (let i = 0; i < n; i++) { const h = 0.25 + r() * 0.35; const tilt = (i - n / 2) * 0.35 + (r() - 0.5) * 0.3; const g = onTop ? placed(P.octa(), x + (i - n / 2) * 0.1, top + h * 0.6, -1.2 - r() * 2.5, 0.09, h, 0.09, 0, r(), tilt) : placed(P.octa(), x + (i - n / 2) * 0.12, top - capThick - 0.5 - r() * 1.5, bodyFront, 0.08, h * 0.8, 0.08, 0.4, r(), tilt + Math.PI); sink.add(crystalMat(ci), g); } x += 2 + r() * 4; } } // carámbanos colgando del borde if (st.icicles && kind !== 'block') { let x = L + 0.3 + r(); const c = new THREE.Color(0xcdeeff); while (x < R - 0.2) { const h = 0.2 + r() * 0.45; const g = placed(P.cone(), x, top - capThick - h / 2 + 0.05, capFront - 0.25, 0.07, h, 0.07, Math.PI, 0, 0); paint(g, (_p, _n, out) => out.copy(c)); sink.add(glossyDetailMat(), g); x += 0.5 + r() * 1.6; } } } // ───────────── Plataformas ───────────── /** Geometría de plataforma en coordenadas locales (centro x=0, superficie y=0). */ export function buildPlatform(sink: GeoSink, w: number, thick: number, style: string, seed: number): void { const r = rng(seed); const L = -w / 2; const R = w / 2; switch (style) { case 'wood': { const n = Math.max(2, Math.round(w / 0.5)); const pw = w / n; const mat = bodyMat('wood'); for (let i = 0; i < n; i++) { const a = L + i * pw + 0.025; const dy = (r() - 0.5) * 0.03; const g = extrude(roundRect(a, -thick * 0.75 + dy, a + pw - 0.05, dy, 0.06, 0.06, 0.05), -0.95, 0.95, 0.05, 3, 2); const k = 0.9 + r() * 0.2; paint(g, (p, n_, out) => { const s = (0.75 + 0.3 * smooth(-thick, 0, p.y)) * k * (n_.y > 0.5 ? 1.08 : 1); out.setRGB(s, s, s); }); sink.add(mat, g); } for (const z of [-0.6, 0.6]) { const g = extrude(roundRect(L + 0.15, -thick - 0.18, R - 0.15, -thick * 0.7, 0.08, 0.08, 0.05), z - 0.17, z + 0.17, 0.05, 3, 2); paint(g, (_p, _n, out) => out.setRGB(0.55, 0.5, 0.48)); sink.add(mat, g); } break; } case 'cloud': { const dm = kitMat('cloudPuff', { color: 0xffffff, vertexColors: true, gloss: 0.2, rim: 0.8 }); const n = Math.max(2, Math.ceil(w / 0.65)); const puff = (x: number, y: number, z: number, rad: number, sy: number) => { const g = placed(P.sphere(), x, y, z, rad, rad * sy, rad * 0.9); paint(g, (p, _n, out) => { const t = smooth(-0.7, 0.05, p.y); out.setRGB(0.78 + 0.22 * t, 0.84 + 0.16 * t, 0.98 + 0.02 * t); }); sink.add(dm, g); }; for (let i = 0; i < n; i++) { const x = L + 0.35 + ((w - 0.7) * i) / (n - 1); const rad = 0.42 + r() * 0.18; puff(x, -rad * 0.55, (r() - 0.5) * 0.3, rad, 0.6); puff(x + 0.2, -0.3, -0.55, rad * 0.9, 0.55); if (i % 2 === 0) puff(x + (r() - 0.5) * 0.3, -0.48, 0.45, rad * 0.75, 0.55); } break; } case 'metal': { const g = extrude(roundRect(L, -thick, R, 0, 0.12, 0.12, 0.08), -0.9, 0.9, 0.08, 4, 3); paint(g, (p, n_, out) => { const s = (0.8 + 0.25 * smooth(-thick, 0, p.y)) * (n_.y > 0.5 ? 1.1 : 1); out.setRGB(s, s, s * 1.03); }); sink.add(kitMat('metalPlat', { color: 0xa9b4cb, vertexColors: true, gloss: 0.8, rim: 0.6 }), g); // franja de aviso const stripes = Math.max(2, Math.round(w / 0.35)); for (let i = 0; i < stripes; i++) { const a = L + 0.1 + ((w - 0.2) * i) / stripes; const s = placed(P.rbox(), a + (w - 0.2) / stripes / 2, -thick * 0.5, 0.91, (w - 0.2) / stripes - 0.02, thick * 0.45, 0.04); const c = i % 2 ? 0x2b2d3a : 0xffc928; paint(s, (_p, _n, out) => out.setHex(c)); sink.add(detailMat(), s); } for (const x of [L + 0.25, R - 0.25]) { const rv = placed(P.sphereLo(), x, -thick * 0.5, 0.92, 0.07, 0.07, 0.05); paint(rv, (_p, _n, out) => out.setHex(0xe8eefc)); sink.add(glossyDetailMat(), rv); } break; } default: { // isla flotante con el estilo de terreno pedido ('stone' → piedra flotante) const st = STYLES[style] ? style : 'grass'; const depthBody = Math.max(thick + 0.6, Math.min(2.4, w * 0.4)); buildSlab(sink, L, R, 0, -depthBody, { style: st, zBack: -1.1, zFront: 1.1, seed, kind: 'island' }); } } }