import * as THREE from 'three'; /* ------------------------------------------------------------------ * * Small procedural noise helpers * ------------------------------------------------------------------ */ function hash2(x, y) { const n = Math.sin(x * 127.1 + y * 311.7) * 43758.5453123; return n - Math.floor(n); } function vnoise(x, y) { const xi = Math.floor(x), yi = Math.floor(y); const xf = x - xi, yf = y - yi; const u = xf * xf * (3 - 2 * xf), v = yf * yf * (3 - 2 * yf); const a = hash2(xi, yi), b = hash2(xi + 1, yi); const c = hash2(xi, yi + 1), d = hash2(xi + 1, yi + 1); return a * (1 - u) * (1 - v) + b * u * (1 - v) + c * (1 - u) * v + d * u * v; } function fbm(x, y, oct = 4) { let s = 0, a = 0.5, f = 1; for (let i = 0; i < oct; i++) { s += a * vnoise(x * f, y * f); f *= 2; a *= 0.5; } return s; } function rng(seed) { let s = seed >>> 0; return () => { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; }; } function cv(w, h) { const c = document.createElement('canvas'); c.width = w; c.height = h; return c; } function tex(canvas, srgb = true) { const t = new THREE.CanvasTexture(canvas); t.wrapS = t.wrapT = THREE.RepeatWrapping; t.colorSpace = srgb ? THREE.SRGBColorSpace : THREE.NoColorSpace; t.anisotropy = 8; return t; } function shade(hex, f) { const c = new THREE.Color(hex); c.multiplyScalar(f); return '#' + c.getHexString(); } /* overlay fine fbm noise onto a 2d context */ function noiseOverlay(ctx, w, h, scale, alpha, seed = 0) { const img = ctx.getImageData(0, 0, w, h); const d = img.data; for (let y = 0; y < h; y++) { for (let x = 0; x < w; x++) { const i = (y * w + x) * 4; const n = (fbm(x / w * scale + seed * 7.3, y / h * scale + seed * 3.1, 4) - 0.5) * 2; d[i] += n * alpha; d[i + 1] += n * alpha; d[i + 2] += n * alpha; } } ctx.putImageData(img, 0, 0); } function grainStroke(ctx, x0, y0, x1, y1, color, width, alpha) { ctx.save(); ctx.globalAlpha = alpha; ctx.strokeStyle = color; ctx.lineWidth = width; ctx.beginPath(); ctx.moveTo(x0, y0); ctx.bezierCurveTo(x0 + (x1 - x0) * 0.33, y0 + 3, x0 + (x1 - x0) * 0.66, y0 - 3, x1, y1); ctx.stroke(); ctx.restore(); } /* ------------------------------------------------------------------ * * BRICK — warm hand-formed brick, lime mortar, soot at the base * ------------------------------------------------------------------ */ function brickSet() { const S = 1024, rows = 18, bh = S / rows, bw = bh * 2.35; const c = cv(S, S), x = c.getContext('2d'); const b = cv(S, S), bx = b.getContext('2d'); const r = cv(S, S), rx = r.getContext('2d'); x.fillStyle = '#8a7f70'; x.fillRect(0, 0, S, S); bx.fillStyle = '#3a3a3a'; bx.fillRect(0, 0, S, S); rx.fillStyle = '#e6e6e6'; rx.fillRect(0, 0, S, S); const rnd = rng(41); for (let row = 0; row < rows; row++) { const off = (row % 2) * bw * 0.5; for (let col = -1; col < Math.ceil(S / bw) + 1; col++) { const X = col * bw + off + 3, Y = row * bh + 3; const W = bw - 6, H = bh - 6; const t = rnd(); const base = new THREE.Color().setHSL(0.032 + t * 0.022, 0.52 + rnd() * 0.16, 0.22 + rnd() * 0.10); if (rnd() < 0.12) base.setHSL(0.06, 0.16, 0.26 + rnd() * 0.10); x.fillStyle = '#' + base.getHexString(); x.beginPath(); x.roundRect(X, Y, W, H, 3); x.fill(); // inner shading & lichen const g = x.createLinearGradient(X, Y, X, Y + H); g.addColorStop(0, 'rgba(255,240,220,0.16)'); g.addColorStop(1, 'rgba(30,15,10,0.18)'); x.fillStyle = g; x.fill(); if (rnd() < 0.22) { x.fillStyle = `rgba(${70 + rnd() * 40},${75 + rnd() * 30},${45 + rnd() * 25},${0.10 + rnd() * 0.16})`; x.beginPath(); x.ellipse(X + rnd() * W, Y + rnd() * H, W * 0.3, H * 0.4, rnd() * 3, 0, 7); x.fill(); } const gv = Math.round(150 + rnd() * 80); bx.fillStyle = `rgb(${gv},${gv},${gv})`; bx.beginPath(); bx.roundRect(X + 2, Y + 2, W - 4, H - 4, 4); bx.fill(); const rv = Math.round(180 + rnd() * 55); rx.fillStyle = `rgb(${rv},${rv},${rv})`; rx.beginPath(); rx.roundRect(X, Y, W, H, 3); rx.fill(); } } // mortar crumble + weathering streaks on the fabric maps const rnd2 = rng(9); for (let i = 0; i < 900; i++) { const X = rnd2() * S, Y = rnd2() * S, w = 2 + rnd2() * 22, h = 1 + rnd2() * 3; x.fillStyle = `rgba(${190 + rnd2() * 40},${180 + rnd2() * 30},${160 + rnd2() * 30},${0.05 + rnd2() * 0.1})`; x.fillRect(X, Y, w, h); bx.fillStyle = `rgba(90,90,90,${0.1 + rnd2() * 0.2})`; bx.fillRect(X, Y, w, h); } noiseOverlay(x, S, S, 140, 18, 3); noiseOverlay(bx, S, S, 160, 26, 5); const map = tex(c), bumpMap = tex(b, false), roughnessMap = tex(r, false); map.repeat.set(2.1, 1.5); bumpMap.repeat.set(2.1, 1.5); roughnessMap.repeat.set(2.1, 1.5); return { map, bumpMap, roughnessMap }; } /* ------------------------------------------------------------------ * * WOOD — weathered horizontal cladding boards * ------------------------------------------------------------------ */ function woodSet(seed = 3, dark = false) { const S = 1024, planks = 9, ph = S / planks; const c = cv(S, S), x = c.getContext('2d'); const b = cv(S, S), bx = b.getContext('2d'); const rnd = rng(seed); x.fillStyle = dark ? '#241810' : '#40301f'; x.fillRect(0, 0, S, S); bx.fillStyle = '#4a4a4a'; bx.fillRect(0, 0, S, S); const hue = dark ? 0.075 : 0.085; for (let p = 0; p < planks; p++) { const Y = p * ph; const t = rnd(); const base = new THREE.Color().setHSL(hue + t * 0.02, 0.34 + rnd() * 0.16, (dark ? 0.10 : 0.18) + rnd() * 0.10); x.fillStyle = '#' + base.getHexString(); x.fillRect(0, Y + 1, S, ph - 2); const grd = x.createLinearGradient(0, Y, 0, Y + ph); grd.addColorStop(0, 'rgba(255,230,200,0.13)'); grd.addColorStop(0.5, 'rgba(255,255,255,0.02)'); grd.addColorStop(1, 'rgba(15,8,4,0.28)'); x.fillStyle = grd; x.fillRect(0, Y + 1, S, ph - 2); // grain for (let i = 0; i < 46; i++) { const yy = Y + 4 + rnd() * (ph - 8); const col = rnd() < 0.5 ? '#2a1c10' : '#c9a878'; grainStroke(x, -20, yy, S + 20, yy + (rnd() - 0.5) * 5, col, 0.9 + rnd() * 1.6, 0.10 + rnd() * 0.16); } // knots if (rnd() < 0.65) { const kx = rnd() * S, ky = Y + ph * 0.5; for (let k = 5; k > 0; k--) { x.strokeStyle = `rgba(40,24,12,${0.10 + k * 0.04})`; x.lineWidth = 1.4; x.beginPath(); x.ellipse(kx, ky, k * 2.2, k * 1.3, 0.3, 0, 7); x.stroke(); } } // nails for (const nx of [S * 0.12, S * 0.62]) { x.fillStyle = '#241a12'; x.beginPath(); x.arc(nx + rnd() * 40, Y + ph * 0.5, 3.2, 0, 7); x.fill(); x.fillStyle = '#8d7a63'; x.beginPath(); x.arc(nx + rnd() * 40 - 1, Y + ph * 0.5 - 1, 1.4, 0, 7); x.fill(); } // shadow seam / highlight x.fillStyle = 'rgba(10,5,2,0.5)'; x.fillRect(0, Y, S, 2.5); x.fillStyle = 'rgba(255,235,205,0.10)'; x.fillRect(0, Y + 3, S, 2); // bump: board domed, seams recessed const bg = bx.createLinearGradient(0, Y, 0, Y + ph); bg.addColorStop(0, '#3c3c3c'); bg.addColorStop(0.12, '#c8c8c8'); bg.addColorStop(0.85, '#bdbdbd'); bg.addColorStop(1, '#303030'); bx.fillStyle = bg; bx.fillRect(0, Y, S, ph); for (let i = 0; i < 24; i++) { bx.strokeStyle = `rgba(120,120,120,${0.15 + rnd() * 0.25})`; bx.lineWidth = 1 + rnd() * 2; const yy = Y + 6 + rnd() * (ph - 12); bx.beginPath(); bx.moveTo(0, yy); bx.bezierCurveTo(S * .3, yy + 3, S * .7, yy - 3, S, yy); bx.stroke(); } } // lichen / paint wear for (let i = 0; i < 50; i++) { x.fillStyle = `rgba(190,196,170,${0.03 + rnd() * 0.07})`; x.beginPath(); x.ellipse(rnd() * S, rnd() * S, 4 + rnd() * 26, 2 + rnd() * 9, rnd() * 3, 0, 7); x.fill(); } noiseOverlay(x, S, S, 120, 16, seed); noiseOverlay(bx, S, S, 100, 20, seed + 2); const map = tex(c), bumpMap = tex(b, false); map.repeat.set(2, 2.5); bumpMap.repeat.set(2, 2.5); return { map, bumpMap }; } /* ------------------------------------------------------------------ * * THATCH / REED ROOF * ------------------------------------------------------------------ */ function thatchSet() { const S = 1024; const c = cv(S, S), x = c.getContext('2d'); const b = cv(S, S), bx = b.getContext('2d'); const rnd = rng(77); x.fillStyle = '#3c3020'; x.fillRect(0, 0, S, S); bx.fillStyle = '#707070'; bx.fillRect(0, 0, S, S); // layered reed courses const course = 128; for (let y0 = 0; y0 < S + course; y0 += course) { const yTop = y0 - 34; // individual reeds, denser at bottom edge of each course for (let i = 0; i < 2400; i++) { const X = rnd() * S; const Y = yTop + rnd() * (course + 40); const L = 22 + rnd() * 48; const tone = 0.15 + rnd() * 0.24; const col = new THREE.Color().setHSL(0.075 + rnd() * 0.02, 0.22 + rnd() * 0.2, tone); x.strokeStyle = '#' + col.getHexString(); x.globalAlpha = 0.55 + rnd() * 0.45; x.lineWidth = 1 + rnd() * 1.6; x.beginPath(); x.moveTo(X, Y); x.lineTo(X + (rnd() - 0.5) * 4, Y + L); x.stroke(); } x.globalAlpha = 1; // shadow under course lip, sun-bleached lower edge const g = x.createLinearGradient(0, yTop, 0, yTop + 30); g.addColorStop(0, 'rgba(3,2,1,0.62)'); g.addColorStop(1, 'rgba(3,2,1,0)'); x.fillStyle = g; x.fillRect(0, yTop, S, 30); x.fillStyle = 'rgba(205,180,130,0.10)'; x.fillRect(0, yTop + 28, S, 8); bx.fillStyle = 'rgba(25,25,25,0.7)'; bx.fillRect(0, yTop, S, 26); } // moss patches for (let i = 0; i < 120; i++) { x.fillStyle = `rgba(${70 + rnd() * 40},${80 + rnd() * 35},${45 + rnd() * 20},${0.05 + rnd() * 0.13})`; x.beginPath(); x.ellipse(rnd() * S, rnd() * S, 3 + rnd() * 16, 2 + rnd() * 8, rnd() * 3, 0, 7); x.fill(); } noiseOverlay(x, S, S, 220, 20, 8); noiseOverlay(bx, S, S, 220, 22, 11); const map = tex(c), bumpMap = tex(b, false); map.repeat.set(3, 3); bumpMap.repeat.set(3, 3); return { map, bumpMap }; } /* ------------------------------------------------------------------ * * SAIL CANVAS — flax cloth, seams, stitches, water stains * ------------------------------------------------------------------ */ function canvasSet() { const S = 1024; const c = cv(S, S), x = c.getContext('2d'); const b = cv(S, S), bx = b.getContext('2d'); const rnd = rng(123); x.fillStyle = '#d9c8ac'; x.fillRect(0, 0, S, S); bx.fillStyle = '#9a9a9a'; bx.fillRect(0, 0, S, S); // wide cloth panels with seams const panels = 4; for (let p = 0; p < panels; p++) { const X = p * S / panels; const tone = 0.80 + rnd() * 0.12; x.fillStyle = shade('#d9c8ac', tone); x.fillRect(X, 0, S / panels, S); const g = x.createLinearGradient(0, 0, S, 0); g.addColorStop(0, 'rgba(120,95,65,0.08)'); g.addColorStop(0.5, 'rgba(255,255,255,0.05)'); g.addColorStop(1, 'rgba(120,95,65,0.10)'); x.fillStyle = g; x.fillRect(X, 0, S / panels, S); x.fillStyle = 'rgba(95,75,50,0.45)'; x.fillRect(X, 0, 2, S); x.fillStyle = 'rgba(255,245,225,0.35)'; x.fillRect(X + 2, 0, 1.5, S); // stitches for (let y = 0; y < S; y += 13) { x.fillStyle = 'rgba(80,60,40,0.55)'; x.beginPath(); x.arc(X + 6, y + rnd() * 4, 1.4, 0, 7); x.fill(); } } // weave for (let i = 0; i < S; i += 3) { x.fillStyle = 'rgba(90,70,45,0.07)'; x.fillRect(0, i, S, 1); x.fillStyle = 'rgba(255,248,230,0.05)'; x.fillRect(i, 0, 1, S); bx.fillStyle = 'rgba(210,210,210,0.5)'; bx.fillRect(0, i, S, 1); bx.fillStyle = 'rgba(120,120,120,0.5)'; bx.fillRect(i, 0, 1, S); } // stains / weathering for (let i = 0; i < 130; i++) { const X = rnd() * S, Y = rnd() * S; const rad = 8 + rnd() * 90; const g = x.createRadialGradient(X, Y, 0, X, Y, rad); const dark = rnd() < 0.6; g.addColorStop(0, dark ? 'rgba(110,85,55,0.10)' : 'rgba(245,235,210,0.13)'); g.addColorStop(1, 'rgba(0,0,0,0)'); x.fillStyle = g; x.beginPath(); x.arc(X, Y, rad, 0, 7); x.fill(); } // frayed edge x.fillStyle = 'rgba(90,70,45,0.35)'; x.fillRect(0, 0, S, 10); x.fillRect(0, S - 10, S, 10); noiseOverlay(x, S, S, 260, 12, 21); const map = tex(c), bumpMap = tex(b, false); return { map, bumpMap }; } /* ------------------------------------------------------------------ * * GROUND — grass & packed earth * ------------------------------------------------------------------ */ function grassSet() { const S = 1024; const c = cv(S, S), x = c.getContext('2d'); const rnd = rng(55); const img = x.createImageData(S, S); const d = img.data; for (let y = 0; y < S; y++) { for (let X = 0; X < S; X++) { const i = (y * S + X) * 4; const n = fbm(X / S * 7, y / S * 7, 4); const n2 = fbm(X / S * 30 + 13, y / S * 30 + 7, 3); const col = new THREE.Color().setHSL(0.22 + n * 0.035, 0.34 + n2 * 0.2, 0.16 + n * 0.10 + n2 * 0.05); d[i] = col.r * 255; d[i + 1] = col.g * 255; d[i + 2] = col.b * 255; d[i + 3] = 255; } } x.putImageData(img, 0, 0); // blades for (let i = 0; i < 9000; i++) { const X = rnd() * S, Y = rnd() * S, L = 2 + rnd() * 7; const col = new THREE.Color().setHSL(0.20 + rnd() * 0.06, 0.4, 0.15 + rnd() * 0.22); x.strokeStyle = '#' + col.getHexString(); x.globalAlpha = 0.4 + rnd() * 0.5; x.lineWidth = 0.8 + rnd(); x.beginPath(); x.moveTo(X, Y); x.lineTo(X + (rnd() - .5) * 3, Y - L); x.stroke(); } x.globalAlpha = 1; for (let i = 0; i < 45; i++) { x.fillStyle = `rgba(${60 + rnd() * 50},${55 + rnd() * 40},${30 + rnd() * 25},${0.04 + rnd() * 0.10})`; x.beginPath(); x.ellipse(rnd() * S, rnd() * S, 10 + rnd() * 70, 6 + rnd() * 40, rnd() * 3, 0, 7); x.fill(); } const map = tex(c); map.repeat.set(14, 14); return { map }; } function dirtSet() { const S = 512; const c = cv(S, S), x = c.getContext('2d'); const rnd = rng(88); const img = x.createImageData(S, S), d = img.data; for (let y = 0; y < S; y++) for (let X = 0; X < S; X++) { const i = (y * S + X) * 4; const n = fbm(X / S * 10, y / S * 10, 4), n2 = fbm(X / S * 40 + 5, y / S * 40, 2); const col = new THREE.Color().setHSL(0.07 + n * 0.02, 0.28 + n2 * 0.15, 0.24 + n * 0.16 + n2 * 0.06); d[i] = col.r * 255; d[i + 1] = col.g * 255; d[i + 2] = col.b * 255; d[i + 3] = 255; } x.putImageData(img, 0, 0); for (let i = 0; i < 2600; i++) { const X = rnd() * S, Y = rnd() * S, R = 0.6 + rnd() * 2.4; x.fillStyle = `rgba(${90 + rnd() * 80},${75 + rnd() * 60},${55 + rnd() * 40},${0.3 + rnd() * 0.4})`; x.beginPath(); x.arc(X, Y, R, 0, 7); x.fill(); } // wheel ruts for (let k = 0; k < 2; k++) { x.fillStyle = 'rgba(60,45,30,0.18)'; x.fillRect(0, S * (0.32 + k * 0.28), S, S * 0.045); x.fillStyle = 'rgba(230,205,170,0.10)'; x.fillRect(0, S * (0.36 + k * 0.28), S, S * 0.012); } noiseOverlay(x, S, S, 90, 18, 17); const map = tex(c); map.repeat.set(3, 6); return { map }; } /* ------------------------------------------------------------------ * * MISC — cloud sprite, metal / rope detail * ------------------------------------------------------------------ */ function cloudSet() { const S = 512, c = cv(S, S), x = c.getContext('2d'); const rnd = rng(202); x.clearRect(0, 0, S, S); for (let i = 0; i < 34; i++) { const X = S * 0.18 + rnd() * S * 0.64; const Y = S * 0.36 + rnd() * S * 0.30; const R = S * (0.08 + rnd() * 0.15); const g = x.createRadialGradient(X, Y, 0, X, Y, R); g.addColorStop(0, 'rgba(255,253,248,0.55)'); g.addColorStop(0.55, 'rgba(250,250,252,0.28)'); g.addColorStop(1, 'rgba(255,255,255,0)'); x.fillStyle = g; x.beginPath(); x.arc(X, Y, R, 0, 7); x.fill(); } const t = tex(c); t.wrapS = t.wrapT = THREE.ClampToEdgeWrapping; return t; } /* ------------------------------------------------------------------ * * GRASS CLUMP CARD — alpha-tested blades for near-field meadow * ------------------------------------------------------------------ */ function grassClumpSet() { const S = 256, c = cv(S, S), x = c.getContext('2d'); const rnd = rng(611); x.clearRect(0, 0, S, S); for (let i = 0; i < 34; i++) { const bx = S * (0.12 + rnd() * 0.76); const baseY = S * 0.97; const tipY = S * (0.08 + rnd() * 0.5); const bend = (rnd() - 0.5) * S * 0.22; const w = 3 + rnd() * 5; const g = x.createLinearGradient(0, baseY, 0, tipY); const hue = 88 + rnd() * 26; g.addColorStop(0, `hsl(${hue - 10}, 45%, ${14 + rnd() * 8}%)`); g.addColorStop(0.6, `hsl(${hue}, 48%, ${24 + rnd() * 12}%)`); g.addColorStop(1, `hsl(${hue + 8}, 55%, ${34 + rnd() * 16}%)`); x.fillStyle = g; x.beginPath(); x.moveTo(bx - w, baseY); x.quadraticCurveTo(bx - w * 0.4 + bend * 0.5, (baseY + tipY) / 2, bx + bend, tipY); x.quadraticCurveTo(bx + w * 0.4 + bend * 0.5, (baseY + tipY) / 2, bx + w, baseY); x.closePath(); x.fill(); } // a few seed heads for (let i = 0; i < 7; i++) { const bx = S * (0.2 + rnd() * 0.6), by = S * (0.18 + rnd() * 0.28); x.strokeStyle = `hsl(55,40%,${30 + rnd() * 15}%)`; x.lineWidth = 1.5; x.beginPath(); x.moveTo(bx, by + 26); x.lineTo(bx + 6, by); x.stroke(); x.fillStyle = `hsl(50,45%,${36 + rnd() * 18}%)`; x.beginPath(); x.ellipse(bx + 6, by, 2.5, 7, 0.2, 0, 7); x.fill(); } const t = tex(c); t.wrapS = t.wrapT = THREE.ClampToEdgeWrapping; return t; } export { brickSet, woodSet, thatchSet, canvasSet, grassSet, grassClumpSet, dirtSet, cloudSet, fbm, rng, hash2, tex, cv, shade };