File size: 10,257 Bytes
59c7fd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import * as THREE from 'three';

/* ---------- tiny deterministic RNG ---------- */
function rng(seed) {
  let a = seed >>> 0;
  return function () {
    a |= 0; a = (a + 0x6D2B79F5) | 0;
    let t = Math.imul(a ^ (a >>> 15), 1 | a);
    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
}

function cv(size = 512) {
  const c = document.createElement('canvas');
  c.width = c.height = size;
  return c;
}

function tex(canvas, srgb = true, repeat = [1, 1], aniso = 8) {
  const t = new THREE.CanvasTexture(canvas);
  t.wrapS = t.wrapT = THREE.RepeatWrapping;
  t.repeat.set(repeat[0], repeat[1]);
  t.anisotropy = aniso;
  if (srgb) t.colorSpace = THREE.SRGBColorSpace;
  return t;
}

function rgba(hex, a) {
  const c = new THREE.Color(hex);
  return `rgba(${Math.round(c.r * 255)},${Math.round(c.g * 255)},${Math.round(c.b * 255)},${a})`;
}

/* ---------- wood ---------- */
export function makeWood({
  base = '#8a5a32', dark = '#4a2b15', light = '#c19460',
  seed = 3, size = 512, repeat = [1, 1], knots = 5, grainFreq = 0.012,
} = {}) {
  const rand = rng(seed);
  const alb = cv(size), bmp = cv(size), rgh = cv(size);
  const a = alb.getContext('2d'), b = bmp.getContext('2d'), r = rgh.getContext('2d');
  a.fillStyle = base; a.fillRect(0, 0, size, size);
  b.fillStyle = '#7f7f7f'; b.fillRect(0, 0, size, size);
  r.fillStyle = '#a8a8a8'; r.fillRect(0, 0, size, size);

  // wide tonal planks / colour variation
  for (let i = 0; i < 16; i++) {
    const y = rand() * size, h = 20 + rand() * 90;
    const g = a.createLinearGradient(0, y, 0, y + h);
    const col = rand() > 0.5 ? light : dark;
    g.addColorStop(0, rgba(col, 0));
    g.addColorStop(0.5, rgba(col, 0.05 + rand() * 0.09));
    g.addColorStop(1, rgba(col, 0));
    a.fillStyle = g; a.fillRect(0, y, size, h);
  }

  // long grain strokes
  const strokes = Math.round(size * 1.15);
  for (let i = 0; i < strokes; i++) {
    const y = rand() * size;
    const amp = 1 + rand() * 8;
    const freq = grainFreq * (0.5 + rand() * 1.5);
    const phase = rand() * Math.PI * 2;
    const drift = (rand() - 0.5) * 0.02;
    const col = rand() > 0.42 ? dark : light;
    const alpha = 0.025 + rand() * 0.11;
    const w = 0.4 + rand() * 1.7;
    a.beginPath(); b.beginPath();
    for (let x = 0; x <= size; x += 8) {
      const yy = y + Math.sin(x * freq + phase) * amp + x * drift;
      if (x === 0) { a.moveTo(x, yy); b.moveTo(x, yy); }
      else { a.lineTo(x, yy); b.lineTo(x, yy); }
    }
    a.strokeStyle = rgba(col, alpha); a.lineWidth = w; a.stroke();
    const gv = Math.round(105 + rand() * 60);
    b.strokeStyle = `rgba(${gv},${gv},${gv},${0.25 + rand() * 0.3})`;
    b.lineWidth = w; b.stroke();
    // roughness variation
    r.beginPath();
    for (let x = 0; x <= size; x += 16) {
      const yy = y + Math.sin(x * freq + phase) * amp + x * drift;
      if (x === 0) r.moveTo(x, yy); else r.lineTo(x, yy);
    }
    const rv = Math.round(150 + rand() * 70);
    r.strokeStyle = `rgba(${rv},${rv},${rv},0.18)`; r.lineWidth = w * 2; r.stroke();
  }

  // knots
  for (let k = 0; k < knots; k++) {
    const kx = rand() * size, ky = rand() * size;
    const kr = 8 + rand() * 30;
    for (let ring = 0; ring < 8; ring++) {
      const rr = kr * (1 - ring / 10);
      a.beginPath();
      a.ellipse(kx + ring * 1.5, ky, rr, rr * (0.3 + rand() * 0.15), 0.1, 0, Math.PI * 2);
      a.strokeStyle = rgba(dark, 0.12 + ring * 0.03);
      a.lineWidth = 1.1 + rand() * 2.2;
      a.stroke();
      b.beginPath();
      b.ellipse(kx + ring * 1.5, ky, rr, rr * (0.3 + rand() * 0.15), 0.1, 0, Math.PI * 2);
      b.strokeStyle = `rgba(70,70,70,${0.2 + ring * 0.03})`; b.lineWidth = 1.5; b.stroke();
    }
    a.beginPath(); a.ellipse(kx, ky, kr * 0.28, kr * 0.22, 0.1, 0, Math.PI * 2);
    a.fillStyle = rgba(dark, 0.4); a.fill();
  }

  // fine pores / speckle
  for (let i = 0; i < size * 2; i++) {
    const x = rand() * size, y = rand() * size;
    a.fillStyle = rgba(rand() > 0.5 ? dark : light, 0.03 + rand() * 0.08);
    a.fillRect(x, y, 1 + rand() * 5, 0.6 + rand() * 1.2);
  }

  // subtle weathering patches
  for (let i = 0; i < 12; i++) {
    const x = rand() * size, y = rand() * size, rad = 30 + rand() * 110;
    const g = a.createRadialGradient(x, y, 0, x, y, rad);
    g.addColorStop(0, 'rgba(40,32,24,0.10)');
    g.addColorStop(1, 'rgba(40,32,24,0)');
    a.fillStyle = g; a.fillRect(x - rad, y - rad, rad * 2, rad * 2);
  }

  return {
    map: tex(alb, true, repeat),
    bumpMap: tex(bmp, false, repeat),
    roughnessMap: tex(rgh, false, repeat),
  };
}

/* ---------- metal ---------- */
export function makeMetal({
  base = '#9aa0a4', seed = 7, size = 256, repeat = [1, 1], brushed = true,
} = {}) {
  const rand = rng(seed);
  const alb = cv(size), bmp = cv(size), rgh = cv(size);
  const a = alb.getContext('2d'), b = bmp.getContext('2d'), r = rgh.getContext('2d');
  a.fillStyle = base; a.fillRect(0, 0, size, size);
  b.fillStyle = '#808080'; b.fillRect(0, 0, size, size);
  r.fillStyle = '#707070'; r.fillRect(0, 0, size, size);
  for (let i = 0; i < size * 4; i++) {
    const x = rand() * size, y = rand() * size;
    const v = Math.round(60 + rand() * 170);
    a.fillStyle = `rgba(${v},${v},${v},${0.05 + rand() * 0.12})`;
    if (brushed) a.fillRect(x, y, 6 + rand() * 40, 0.7);
    else a.fillRect(x, y, 1 + rand() * 2, 1 + rand() * 2);
    b.fillStyle = `rgba(${v},${v},${v},0.15)`;
    b.fillRect(x, y, brushed ? 10 + rand() * 40 : 1.5, brushed ? 0.8 : 1.5);
  }
  // patina blotches
  for (let i = 0; i < 24; i++) {
    const x = rand() * size, y = rand() * size, rad = 4 + rand() * 25;
    const g = a.createRadialGradient(x, y, 0, x, y, rad);
    g.addColorStop(0, `rgba(70,60,48,${0.04 + rand() * 0.12})`);
    g.addColorStop(1, 'rgba(70,60,48,0)');
    a.fillStyle = g; a.fillRect(x - rad, y - rad, rad * 2, rad * 2);
  }
  return {
    map: tex(alb, true, repeat),
    bumpMap: tex(bmp, false, repeat),
    roughnessMap: tex(rgh, false, repeat),
  };
}

/* ---------- concrete / stone floor ---------- */
export function makeConcrete({ seed = 11, size = 512, repeat = [1, 1] } = {}) {
  const rand = rng(seed);
  const alb = cv(size), bmp = cv(size), rgh = cv(size);
  const a = alb.getContext('2d'), b = bmp.getContext('2d'), r = rgh.getContext('2d');
  a.fillStyle = '#77726a'; a.fillRect(0, 0, size, size);
  b.fillStyle = '#808080'; b.fillRect(0, 0, size, size);
  r.fillStyle = '#bdbdbd'; r.fillRect(0, 0, size, size);
  for (let i = 0; i < 1500; i++) {
    const x = rand() * size, y = rand() * size, rad = 1 + rand() * 18;
    const v = rand();
    const col = v > 0.5 ? '30,28,24' : '212,205,190';
    const g = a.createRadialGradient(x, y, 0, x, y, rad);
    g.addColorStop(0, `rgba(${col},${0.03 + rand() * 0.14})`);
    g.addColorStop(1, `rgba(${col},0)`);
    a.fillStyle = g; a.fillRect(x - rad, y - rad, rad * 2, rad * 2);
    const gv = Math.round(90 + rand() * 90);
    b.fillStyle = `rgba(${gv},${gv},${gv},0.12)`;
    b.fillRect(x, y, rad * 0.6, rad * 0.6);
  }
  for (let i = 0; i < 1300; i++) {
    const x = rand() * size, y = rand() * size;
    a.fillStyle = `rgba(${rand() > 0.5 ? '20,18,16' : '230,225,210'},${0.05 + rand() * 0.12})`;
    a.fillRect(x, y, 1 + rand() * 2, 1 + rand() * 2);
  }
  // cracks
  for (let c = 0; c < 5; c++) {
    let x = rand() * size, y = rand() * size;
    a.beginPath(); a.moveTo(x, y); b.beginPath(); b.moveTo(x, y);
    for (let s = 0; s < 26; s++) {
      x += (rand() - 0.5) * 34; y += (rand() - 0.5) * 34;
      a.lineTo(x, y); b.lineTo(x, y);
    }
    a.strokeStyle = 'rgba(28,25,22,0.35)'; a.lineWidth = 1 + rand() * 1.2; a.stroke();
    b.strokeStyle = 'rgba(40,40,40,0.5)'; b.lineWidth = 2; b.stroke();
  }
  return {
    map: tex(alb, true, repeat),
    bumpMap: tex(bmp, false, repeat),
    roughnessMap: tex(rgh, false, repeat),
  };
}

/* ---------- rough sawn end grain ---------- */
export function makeEndGrain({ base = '#9a6b3d', seed = 21, size = 512 } = {}) {
  const rand = rng(seed);
  const alb = cv(size), bmp = cv(size);
  const a = alb.getContext('2d'), b = bmp.getContext('2d');
  a.fillStyle = base; a.fillRect(0, 0, size, size);
  b.fillStyle = '#7a7a7a'; b.fillRect(0, 0, size, size);
  for (let i = 0; i < 700; i++) {
    const x = rand() * size, y = rand() * size;
    const rr = 2 + rand() * 22;
    a.beginPath(); a.ellipse(x, y, rr, rr * (0.4 + rand() * 0.6), rand() * 3, 0, Math.PI * 2);
    a.strokeStyle = rgba('#4d3017', 0.05 + rand() * 0.15);
    a.lineWidth = 0.7 + rand() * 1.6; a.stroke();
    b.beginPath(); b.ellipse(x, y, rr, rr * 0.6, 0, 0, Math.PI * 2);
    b.strokeStyle = 'rgba(90,90,90,0.2)'; b.lineWidth = 1.2; b.stroke();
  }
  return { map: tex(alb), bumpMap: tex(bmp, false) };
}

/* ---------- window sky gradient ---------- */
export function makeWindowGlow() {
  const size = 512;
  const c = cv(size); const g = c.getContext('2d');
  const grad = g.createLinearGradient(0, 0, 0, size);
  grad.addColorStop(0, '#cfe3ef');
  grad.addColorStop(0.45, '#f4e8c9');
  grad.addColorStop(0.72, '#f8d79c');
  grad.addColorStop(1, '#d8a86a');
  g.fillStyle = grad; g.fillRect(0, 0, size, size);
  // soft distant trees / haze
  const r = rng(4);
  for (let i = 0; i < 18; i++) {
    const x = r() * size, y = size * (0.55 + r() * 0.2), rad = 40 + r() * 110;
    const gg = g.createRadialGradient(x, y, 0, x, y, rad);
    gg.addColorStop(0, `rgba(95,96,62,${0.06 + r() * 0.1})`);
    gg.addColorStop(1, 'rgba(95,96,62,0)');
    g.fillStyle = gg; g.fillRect(x - rad, y - rad, rad * 2, rad * 2);
  }
  return tex(c, true, [1, 1]);
}

/* ---------- cloth / canvas ---------- */
export function makeCanvasCloth({ base = '#c8b58f', seed = 33, size = 256, repeat = [1, 1] } = {}) {
  const rand = rng(seed);
  const c = cv(size); const g = c.getContext('2d');
  g.fillStyle = base; g.fillRect(0, 0, size, size);
  for (let y = 0; y < size; y += 2) {
    g.fillStyle = `rgba(${rand() > 0.5 ? '255,250,235' : '70,55,35'},${0.04 + rand() * 0.05})`;
    g.fillRect(0, y, size, 1);
  }
  for (let x = 0; x < size; x += 2) {
    g.fillStyle = `rgba(${rand() > 0.5 ? '255,250,235' : '60,48,30'},${0.03 + rand() * 0.05})`;
    g.fillRect(x, 0, 1, size);
  }
  return { map: tex(c, true, repeat) };
}