File size: 9,809 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import * as THREE from 'three';

const SIZE = 512;

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

function rnd(a, b) { return a + Math.random() * (b - a); }

function finish(c, repeatX = 1, repeatY = 1, srgb = true) {
  const t = new THREE.CanvasTexture(c);
  t.wrapS = t.wrapT = THREE.RepeatWrapping;
  t.repeat.set(repeatX, repeatY);
  t.anisotropy = 8;
  if (srgb) t.colorSpace = THREE.SRGBColorSpace;
  return t;
}

// Convert a canvas to a grayscale height/roughness texture canvas.
function grayscale(src, invert = false) {
  const c = canvas(src.width);
  const ctx = c.getContext('2d');
  ctx.drawImage(src, 0, 0);
  const img = ctx.getImageData(0, 0, c.width, c.height);
  const d = img.data;
  for (let i = 0; i < d.length; i += 4) {
    let v = (d[i] * 0.299 + d[i + 1] * 0.587 + d[i + 2] * 0.114) | 0;
    if (invert) v = 255 - v;
    d[i] = d[i + 1] = d[i + 2] = v;
    d[i + 3] = 255;
  }
  ctx.putImageData(img, 0, 0);
  return c;
}

// ---------- brick ----------
export function brickMaps(repeatX = 8, repeatY = 8, palette = 'warm') {
  const c = canvas(1024);
  const ctx = c.getContext('2d');
  const W = c.width, H = c.height;
  ctx.fillStyle = palette === 'wall' ? '#8d8578' : '#a89a88';
  ctx.fillRect(0, 0, W, H);

  const bw = 128, bh = 52, gap = 7;
  const palettes = {
    warm: ['#8f4936', '#9b523b', '#7f3f30', '#a85c42', '#6f3a2e', '#94503f', '#815042'],
    wall: ['#8a5a45', '#96614b', '#7a4d3c', '#9d6952', '#6e4638', '#8a5f4e'],
  };
  const colors = palettes[palette] || palettes.warm;
  const rows = Math.ceil(H / (bh + gap)) + 1;
  for (let r = 0; r < rows; r++) {
    const off = (r % 2) * (bw + gap) * 0.5;
    for (let x = -bw; x < W + bw; x += bw + gap) {
      const px = x + off, py = r * (bh + gap);
      const base = colors[(Math.random() * colors.length) | 0];
      ctx.fillStyle = base;
      ctx.fillRect(px, py, bw, bh);
      // brick surface shading
      const g = ctx.createLinearGradient(px, py, px, py + bh);
      g.addColorStop(0, 'rgba(255,235,210,0.10)');
      g.addColorStop(0.45, 'rgba(0,0,0,0)');
      g.addColorStop(1, 'rgba(0,0,0,0.28)');
      ctx.fillStyle = g;
      ctx.fillRect(px, py, bw, bh);
      // subtle color patchiness
      for (let k = 0; k < 20; k++) {
        ctx.fillStyle = Math.random() > 0.5 ? 'rgba(255,220,180,0.045)' : 'rgba(30,10,5,0.06)';
        const s = rnd(4, 24);
        ctx.fillRect(px + rnd(0, bw - s), py + rnd(0, bh - s), s, s * 0.5);
      }
      // chipped edge
      if (Math.random() < 0.22) {
        ctx.fillStyle = 'rgba(70,55,45,0.5)';
        ctx.beginPath();
        const s = rnd(4, 12);
        ctx.moveTo(px + rnd(0, bw), py + (Math.random() > 0.5 ? 0 : bh));
        ctx.lineTo(px + rnd(0, bw), py + (Math.random() > 0.5 ? s : bh - s));
        ctx.lineTo(px + rnd(0, bw), py + (Math.random() > 0.5 ? 0 : bh));
        ctx.fill();
      }
      // mortar edge shadow
      ctx.strokeStyle = 'rgba(35,28,22,0.35)';
      ctx.lineWidth = 2;
      ctx.strokeRect(px + 1, py + 1, bw - 2, bh - 2);
    }
  }
  // grime speckle
  const img = ctx.getImageData(0, 0, W, H);
  const d = img.data;
  for (let i = 0; i < d.length; i += 4) {
    const n = (Math.random() - 0.5) * 18;
    d[i] += n; d[i + 1] += n; d[i + 2] += n;
  }
  ctx.putImageData(img, 0, 0);

  const map = finish(c, repeatX, repeatY);
  const bump = finish(grayscale(c), repeatX, repeatY, false);
  const roughC = grayscale(c, true);
  // mortar is rougher than brick: use inverted luminance with contrast
  const rough = finish(roughC, repeatX, repeatY, false);
  return { map, bumpMap: bump, roughnessMap: rough };
}

// ---------- painted red metal ----------
export function redPaintMaps(repeatX = 1, repeatY = 1) {
  const c = canvas();
  const ctx = c.getContext('2d');
  const W = c.width, H = c.height;
  const g = ctx.createLinearGradient(0, 0, 0, H);
  g.addColorStop(0, '#a21b18');
  g.addColorStop(0.55, '#8a1414');
  g.addColorStop(1, '#6d1011');
  ctx.fillStyle = g;
  ctx.fillRect(0, 0, W, H);

  // mottled paint / oxidation
  for (let i = 0; i < 1400; i++) {
    const x = Math.random() * W, y = Math.random() * H;
    const r = rnd(4, 60);
    const grd = ctx.createRadialGradient(x, y, 0, x, y, r);
    const dark = Math.random() < 0.6;
    grd.addColorStop(0, dark ? 'rgba(60,8,8,0.10)' : 'rgba(210,80,55,0.08)');
    grd.addColorStop(1, 'rgba(0,0,0,0)');
    ctx.fillStyle = grd;
    ctx.fillRect(x - r, y - r, r * 2, r * 2);
  }
  // fine scratches
  for (let i = 0; i < 220; i++) {
    ctx.strokeStyle = Math.random() < 0.5 ? 'rgba(240,190,160,0.06)' : 'rgba(40,5,5,0.10)';
    ctx.lineWidth = rnd(0.5, 1.6);
    ctx.beginPath();
    const x = Math.random() * W, y = Math.random() * H;
    ctx.moveTo(x, y);
    ctx.lineTo(x + rnd(-60, 60), y + rnd(-10, 10));
    ctx.stroke();
  }
  // a few rust chips low down
  for (let i = 0; i < 26; i++) {
    const x = Math.random() * W, y = H * (0.55 + Math.random() * 0.44);
    ctx.fillStyle = 'rgba(85,45,25,0.35)';
    ctx.beginPath();
    ctx.ellipse(x, y, rnd(1, 5), rnd(1, 3), Math.random() * 3, 0, Math.PI * 2);
    ctx.fill();
  }
  return {
    map: finish(c, repeatX, repeatY),
    bumpMap: finish(grayscale(c), repeatX, repeatY, false),
    roughnessMap: finish(grayscale(c, true), repeatX, repeatY, false),
  };
}

// ---------- wood ----------
export function woodMaps(repeatX = 1, repeatY = 1, tone = 'oak') {
  const c = canvas();
  const ctx = c.getContext('2d');
  const W = c.width, H = c.height;
  const cols = tone === 'oak'
    ? ['#7a5128', '#8a5d2e', '#6c4622', '#946736']
    : ['#4b3320', '#5a3d26', '#3f2c1c'];
  ctx.fillStyle = cols[0];
  ctx.fillRect(0, 0, W, H);

  // planks
  const planks = 4;
  const ph = H / planks;
  for (let p = 0; p < planks; p++) {
    ctx.fillStyle = cols[p % cols.length];
    ctx.fillRect(0, p * ph, W, ph - 2);
    const g = ctx.createLinearGradient(0, p * ph, 0, (p + 1) * ph);
    g.addColorStop(0, 'rgba(255,220,160,0.08)');
    g.addColorStop(1, 'rgba(0,0,0,0.20)');
    ctx.fillStyle = g;
    ctx.fillRect(0, p * ph, W, ph - 2);
    // grain
    for (let i = 0; i < 70; i++) {
      const y = p * ph + rnd(2, ph - 4);
      ctx.strokeStyle = Math.random() < 0.55 ? 'rgba(40,22,8,0.22)' : 'rgba(230,190,130,0.14)';
      ctx.lineWidth = rnd(0.5, 2.2);
      ctx.beginPath();
      ctx.moveTo(0, y);
      for (let x = 0; x <= W; x += 16) {
        ctx.lineTo(x, y + Math.sin(x * 0.021 + i) * rnd(1, 5));
      }
      ctx.stroke();
    }
    // knot
    if (Math.random() < 0.7) {
      const kx = rnd(40, W - 40), ky = p * ph + ph * 0.5;
      for (let r = 16; r > 1; r -= 2.5) {
        ctx.strokeStyle = 'rgba(40,22,8,0.25)';
        ctx.lineWidth = 1.4;
        ctx.beginPath();
        ctx.ellipse(kx, ky, r, r * 0.55, 0.3, 0, Math.PI * 2);
        ctx.stroke();
      }
    }
    // plank seam
    ctx.fillStyle = 'rgba(15,8,3,0.65)';
    ctx.fillRect(0, (p + 1) * ph - 3, W, 3);
  }
  return {
    map: finish(c, repeatX, repeatY),
    bumpMap: finish(grayscale(c), repeatX, repeatY, false),
    roughnessMap: finish(grayscale(c, true), repeatX, repeatY, false),
  };
}

// ---------- brass ----------
export function brassMaps(repeatX = 1, repeatY = 1) {
  const c = canvas(256);
  const ctx = c.getContext('2d');
  const W = c.width, H = c.height;
  const g = ctx.createLinearGradient(0, 0, W, H);
  g.addColorStop(0, '#c9a24a');
  g.addColorStop(0.35, '#e8c878');
  g.addColorStop(0.6, '#a88436');
  g.addColorStop(1, '#d8b962');
  ctx.fillStyle = g;
  ctx.fillRect(0, 0, W, H);
  for (let i = 0; i < 260; i++) {
    ctx.fillStyle = Math.random() < 0.5 ? 'rgba(80,60,20,0.10)' : 'rgba(255,240,190,0.10)';
    ctx.fillRect(Math.random() * W, Math.random() * H, rnd(2, 40), rnd(1, 12));
  }
  return {
    map: finish(c, repeatX, repeatY),
    bumpMap: finish(grayscale(c), repeatX, repeatY, false),
  };
}

// ---------- rubber / hose ----------
export function rubberMaps(repeatX = 1, repeatY = 1) {
  const c = canvas(256);
  const ctx = c.getContext('2d');
  const W = c.width, H = c.height;
  ctx.fillStyle = '#17161a';
  ctx.fillRect(0, 0, W, H);
  for (let i = 0; i < 7000; i++) {
    const v = (Math.random() * 40) | 0;
    ctx.fillStyle = `rgba(${v + 20},${v + 18},${v + 22},0.5)`;
    ctx.fillRect(Math.random() * W, Math.random() * H, 1, 1);
  }
  return {
    map: finish(c, repeatX, repeatY),
    bumpMap: finish(grayscale(c), repeatX, repeatY, false),
  };
}

// ---------- canvas / fabric ----------
export function canvasMaps(repeatX = 1, repeatY = 1) {
  const c = canvas(256);
  const ctx = c.getContext('2d');
  const W = c.width, H = c.height;
  ctx.fillStyle = '#6d6455';
  ctx.fillRect(0, 0, W, H);
  for (let y = 0; y < H; y += 4) {
    ctx.fillStyle = 'rgba(0,0,0,0.10)';
    ctx.fillRect(0, y, W, 1);
  }
  for (let x = 0; x < W; x += 4) {
    ctx.fillStyle = 'rgba(255,245,220,0.06)';
    ctx.fillRect(x, 0, 1, H);
  }
  return {
    map: finish(c, repeatX, repeatY),
    bumpMap: finish(grayscale(c), repeatX, repeatY, false),
  };
}

// ---------- asphalt / courtyard border ----------
export function stoneMaps(repeatX = 2, repeatY = 2) {
  const c = canvas(512);
  const ctx = c.getContext('2d');
  const W = c.width, H = c.height;
  ctx.fillStyle = '#4a4642';
  ctx.fillRect(0, 0, W, H);
  for (let i = 0; i < 90; i++) {
    const x = Math.random() * W, y = Math.random() * H, r = rnd(20, 70);
    ctx.fillStyle = `rgba(${rnd(90, 150) | 0},${rnd(85, 140) | 0},${rnd(80, 130) | 0},0.18)`;
    ctx.beginPath();
    ctx.ellipse(x, y, r, r * 0.7, Math.random() * 3, 0, Math.PI * 2);
    ctx.fill();
  }
  return {
    map: finish(c, repeatX, repeatY),
    bumpMap: finish(grayscale(c), repeatX, repeatY, false),
    roughnessMap: finish(grayscale(c, true), repeatX, repeatY, false),
  };
}