Spaces:
Paused
Paused
File size: 16,630 Bytes
2b0432b | 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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | // Every texture in the film is painted procedurally at load time.
import * as THREE from 'three'
import { fbm2, mulberry32, valueNoise2 } from './rng'
function canvas(w: number, h: number) {
const c = document.createElement('canvas')
c.width = w
c.height = h
const g = c.getContext('2d', { willReadFrequently: true })!
return { c, g }
}
function tex(c: HTMLCanvasElement, srgb = true, repeat = true) {
const t = new THREE.CanvasTexture(c)
if (srgb) t.colorSpace = THREE.SRGBColorSpace
if (repeat) t.wrapS = t.wrapT = THREE.RepeatWrapping
t.anisotropy = 8
t.needsUpdate = true
return t
}
/** Tileable value noise sampled on a torus so textures repeat seamlessly. */
function tnoise(x: number, y: number, period: number, seed: number) {
const ax = (x / period) * Math.PI * 2, ay = (y / period) * Math.PI * 2
const R = period / (Math.PI * 2)
return valueNoise2(Math.cos(ax) * R + Math.sin(ay) * R * 0.37 + 50, Math.sin(ax) * R + Math.cos(ay) * R + 91, seed)
}
function tfbm(x: number, y: number, period: number, seed: number, oct = 4) {
let s = 0, a = 0.5, n = 0, f = 1
for (let i = 0; i < oct; i++) {
s += a * tnoise(x * f, y * f, period * f, seed + i * 13)
n += a
a *= 0.5
f *= 2
}
return s / n
}
function heightToNormal(hf: Float32Array, w: number, h: number, strength: number) {
const { c, g } = canvas(w, h)
const img = g.createImageData(w, h)
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {
const xl = hf[y * w + ((x - 1 + w) % w)], xr = hf[y * w + ((x + 1) % w)]
const yu = hf[((y - 1 + h) % h) * w + x], yd = hf[((y + 1) % h) * w + x]
let nx = (xl - xr) * strength, ny = (yd - yu) * strength, nz = 1
const l = Math.hypot(nx, ny, nz)
nx /= l; ny /= l; nz /= l
const i = (y * w + x) * 4
img.data[i] = (nx * 0.5 + 0.5) * 255
img.data[i + 1] = (ny * 0.5 + 0.5) * 255
img.data[i + 2] = (nz * 0.5 + 0.5) * 255
img.data[i + 3] = 255
}
g.putImageData(img, 0, 0)
return tex(c, false)
}
export interface Textures {
wall: THREE.Texture; wallN: THREE.Texture
roof: THREE.Texture; roofN: THREE.Texture
slab: THREE.Texture; slabN: THREE.Texture
stone: THREE.Texture; stoneN: THREE.Texture
wood: THREE.Texture
cloth: THREE.Texture[]
leaf: THREE.Texture; bambooLeaf: THREE.Texture
puff: THREE.Texture
lantern: THREE.Texture
basket: THREE.Texture
ground: THREE.Texture
bark: THREE.Texture; barkN: THREE.Texture
glow: THREE.Texture
lattice: THREE.Texture
}
function wallTexture() {
// u wraps every 6 m of wall, v spans ground (0) -> eave (1)
const W = 512, H = 512
const { c, g } = canvas(W, H)
const img = g.createImageData(W, H)
const hf = new Float32Array(W * H)
const r = mulberry32(7)
const streakX: number[] = []
for (let i = 0; i < 70; i++) streakX.push(r() * W)
for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) {
const v = 1 - y / H // 1 at top
const n = tfbm(x, y, W, 3, 5)
const fine = tnoise(x * 4, y * 4, W * 4, 9)
// rain streaks running down from under the eaves
let streak = 0
for (let k = 0; k < streakX.length; k++) {
const dx = Math.abs(((x - streakX[k] + W * 1.5) % W) - W / 2)
const len = 0.25 + (k % 7) * 0.09
if (dx < 6 + (k % 3) * 5 && v > 1 - len) streak += (1 - dx / (6 + (k % 3) * 5)) * Math.pow((v - (1 - len)) / len, 1.5) * 0.18
}
const topGrime = Math.pow(Math.max(0, v - 0.86) / 0.14, 2) * 0.25
const damp = Math.pow(Math.max(0, 0.22 - v) / 0.22, 1.6) * (0.35 + 0.4 * n)
const patch = Math.max(0, n - 0.62) * 1.6
let l = 0.92 - streak - topGrime * 0.8 - patch * 0.25 + (fine - 0.5) * 0.04
l = Math.max(0.25, l)
const i = (y * W + x) * 4
// warm white lime plaster; damp areas greenish grey
img.data[i] = Math.min(255, (l * 236 - damp * 70))
img.data[i + 1] = Math.min(255, (l * 232 - damp * 56))
img.data[i + 2] = Math.min(255, (l * 222 - damp * 60))
img.data[i + 3] = 255
hf[y * W + x] = n * 0.6 + fine * 0.5 - patch * 0.6
}
g.putImageData(img, 0, 0)
return { map: tex(c), normal: heightToNormal(hf, W, H, 1.2) }
}
function roofTexture() {
// one texture repeat = 1.2 m across (6 tile channels) x 1.2 m down slope
const W = 256, H = 256
const { c, g } = canvas(W, H)
const img = g.createImageData(W, H)
const hf = new Float32Array(W * H)
const cols = 6, courses = 8
for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) {
const u = (x / W) * cols
const ci = Math.floor(u), fu = u - ci
// alternating concave pan (阴瓦) and convex cover (阳瓦)
const cover = ci % 2 === 0
const prof = Math.sin(fu * Math.PI)
const vv = (y / H) * courses + (cover ? 0.5 : 0)
const fv = vv - Math.floor(vv)
const overlap = Math.pow(fv, 3) * 0.6 // each tile edge lips over the next
const hgt = (cover ? prof * 1.0 : -prof * 0.5 + 0.2) + overlap * 0.4
const n = tfbm(x, y, W, 21, 4)
const ti = Math.floor(vv) * 7 + ci * 13
const tileVar = valueNoise2(ti, ti * 0.37, 5)
let l = 0.2 + tileVar * 0.07 + n * 0.08 + (cover ? prof * 0.06 : -prof * 0.03) - (1 - fv) * 0.015
// occasional moss / lichen
const moss = Math.max(0, n - 0.68) * 2.5
l = Math.max(0.06, l)
const i = (y * W + x) * 4
img.data[i] = (l * 0.92 + moss * 0.03) * 255
img.data[i + 1] = (l * 0.96 + moss * 0.06) * 255
img.data[i + 2] = (l * 1.05) * 255
img.data[i + 3] = 255
hf[y * W + x] = hgt + n * 0.15
}
g.putImageData(img, 0, 0)
return { map: tex(c), normal: heightToNormal(hf, W, H, 3.2) }
}
function slabTexture() {
// blue-stone street slabs, one repeat = 4 m
const W = 512, H = 512
const { c, g } = canvas(W, H)
const img = g.createImageData(W, H)
const hf = new Float32Array(W * H)
const rows = 6
for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) {
const vv = (y / H) * rows
const ri = Math.floor(vv)
const lenScale = 2 + (ri % 3)
const off = valueNoise2(ri, 3, 2) * 3
const uu = (x / W) * lenScale + off
const ci = Math.floor(uu)
const fu = uu - ci, fv = vv - ri
const edge = Math.min(fu, 1 - fu, fv * 0.7, (1 - fv) * 0.7)
const gap = edge < 0.02 ? 1 : 0
const id = ri * 17 + ((ci % lenScale) + lenScale) % lenScale * 5
const sv = valueNoise2(id, id * 0.3, 11)
const n = tfbm(x, y, W, 33, 5)
const worn = Math.pow(Math.min(1, edge / 0.08), 0.5)
let l = 0.34 + sv * 0.12 + (n - 0.5) * 0.12
if (gap) l *= 0.45
const i = (y * W + x) * 4
img.data[i] = l * 0.93 * 255
img.data[i + 1] = l * 0.98 * 255
img.data[i + 2] = l * 1.04 * 255
img.data[i + 3] = 255
hf[y * W + x] = (gap ? -0.6 : 0) + worn * 0.4 + n * 0.25
}
g.putImageData(img, 0, 0)
return { map: tex(c), normal: heightToNormal(hf, W, H, 2.4) }
}
function stoneTexture() {
// dressed granite blocks (bridge, embankment), repeat = 3 m
const W = 512, H = 512
const { c, g } = canvas(W, H)
const img = g.createImageData(W, H)
const hf = new Float32Array(W * H)
const rows = 8
for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) {
const vv = (y / H) * rows
const ri = Math.floor(vv)
const uu = (x / W) * 4 + (ri % 2) * 0.5 + valueNoise2(ri, 1, 4) * 0.3
const ci = Math.floor(uu)
const fu = uu - ci, fv = vv - ri
const edge = Math.min(fu * 2, (1 - fu) * 2, fv, 1 - fv)
const id = ri * 31 + (((ci % 4) + 4) % 4) * 7
const sv = valueNoise2(id, id * 0.71, 8)
const n = tfbm(x, y, W, 44, 5)
const speck = tnoise(x * 8, y * 8, W * 8, 2)
const moss = Math.max(0, tfbm(x, y, W, 71, 3) - 0.58) * 2 * (1 - y / H * 0.3)
let l = 0.42 + sv * 0.1 + (n - 0.5) * 0.14 + (speck - 0.5) * 0.06
if (edge < 0.04) l *= 0.5
const i = (y * W + x) * 4
img.data[i] = (l * 0.96 - moss * 0.12) * 255
img.data[i + 1] = (l * 0.95 - moss * 0.02) * 255
img.data[i + 2] = (l * 0.9 - moss * 0.1) * 255
img.data[i + 3] = 255
hf[y * W + x] = (edge < 0.04 ? -0.5 : Math.min(1, edge * 6) * 0.3) + n * 0.3 + speck * 0.05
}
g.putImageData(img, 0, 0)
return { map: tex(c), normal: heightToNormal(hf, W, H, 2.0) }
}
function woodTexture() {
const W = 256, H = 256
const { c, g } = canvas(W, H)
const img = g.createImageData(W, H)
for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) {
const n = tfbm(x * 0.25, y * 3, W, 5, 4)
const grain = Math.sin((x / W) * 60 + n * 14) * 0.5 + 0.5
const l = 0.2 + grain * 0.06 + n * 0.08
const i = (y * W + x) * 4
img.data[i] = l * 1.25 * 255
img.data[i + 1] = l * 0.86 * 255
img.data[i + 2] = l * 0.6 * 255
img.data[i + 3] = 255
}
g.putImageData(img, 0, 0)
return tex(c)
}
function barkTexture() {
const W = 256, H = 512
const { c, g } = canvas(W, H)
const img = g.createImageData(W, H)
const hf = new Float32Array(W * H)
for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) {
const n = tfbm(x * 2, y * 0.35, W * 2, 15, 5)
const ridge = Math.abs(Math.sin((x / W) * Math.PI * 14 + n * 9))
const l = 0.13 + ridge * 0.1 + n * 0.08
const moss = Math.max(0, tfbm(x, y, W, 88, 3) - 0.55) * 1.5
const i = (y * W + x) * 4
img.data[i] = (l * 1.1 - moss * 0.05) * 255
img.data[i + 1] = (l * 0.95 + moss * 0.08) * 255
img.data[i + 2] = (l * 0.8) * 255
img.data[i + 3] = 255
hf[y * W + x] = ridge * 0.8 + n * 0.4
}
g.putImageData(img, 0, 0)
return { map: tex(c), normal: heightToNormal(hf, W, H, 3) }
}
function clothTexture(variant: number) {
// 蓝印花布: indigo cotton with resist-printed white motifs
const W = 256, H = 512
const { c, g } = canvas(W, H)
const r = mulberry32(100 + variant)
g.fillStyle = variant % 2 ? '#1c2f63' : '#213a72'
g.fillRect(0, 0, W, H)
// dyed fabric unevenness
const img = g.getImageData(0, 0, W, H)
for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) {
const n = tfbm(x, y, W, 60 + variant, 3)
const i = (y * W + x) * 4
const k = 0.85 + n * 0.3
img.data[i] *= k; img.data[i + 1] *= k; img.data[i + 2] *= k
}
g.putImageData(img, 0, 0)
g.fillStyle = '#e8ecf2'
g.strokeStyle = '#e8ecf2'
// border bands of dots
for (let y = 10; y < H; y += 12) {
for (const x of [10, W - 10]) { g.beginPath(); g.arc(x, y, 2.6, 0, Math.PI * 2); g.fill() }
}
g.lineWidth = 2
g.strokeRect(20, 6, W - 40, H - 12)
// floral medallions
const flower = (cx: number, cy: number, s: number, petals: number) => {
for (let p = 0; p < petals; p++) {
const a = (p / petals) * Math.PI * 2
g.beginPath()
g.ellipse(cx + Math.cos(a) * s * 0.55, cy + Math.sin(a) * s * 0.55, s * 0.42, s * 0.2, a, 0, Math.PI * 2)
g.fill()
}
g.fillStyle = variant % 2 ? '#1c2f63' : '#213a72'
g.beginPath(); g.arc(cx, cy, s * 0.2, 0, Math.PI * 2); g.fill()
g.fillStyle = '#e8ecf2'
g.beginPath(); g.arc(cx, cy, s * 0.09, 0, Math.PI * 2); g.fill()
}
const step = variant % 2 ? 64 : 80
for (let y = 40; y < H - 20; y += step) for (let x = 56; x < W - 30; x += step * 0.9) {
const off = (Math.floor(y / step) % 2) * step * 0.45
flower(x + off * (x + off < W - 40 ? 1 : 0), y, 20 + r() * 6, variant % 2 ? 6 : 8)
// small dots around
for (let k = 0; k < 6; k++) {
const a = r() * Math.PI * 2
g.beginPath(); g.arc(x + Math.cos(a) * 34, y + Math.sin(a) * 34, 2, 0, Math.PI * 2); g.fill()
}
}
return tex(c, true, false)
}
function leafTexture(bamboo: boolean) {
// alpha-tested cluster of leaves, white-ish albedo modulated by instance colour
const W = 512, H = 512
const { c, g } = canvas(W, H)
g.clearRect(0, 0, W, H)
const r = mulberry32(bamboo ? 5 : 3)
const N = bamboo ? 140 : 420
for (let i = 0; i < N; i++) {
// denser toward the centre, rounded cluster silhouette
const a = r() * Math.PI * 2
const rad = Math.pow(r(), 0.6) * (W * 0.46)
const x = W / 2 + Math.cos(a) * rad, y = H / 2 + Math.sin(a) * rad * (bamboo ? 0.8 : 0.9)
const len = bamboo ? 34 + r() * 26 : 11 + r() * 8
const wid = bamboo ? 5 + r() * 2 : 6 + r() * 3
const ang = bamboo ? a + (r() - 0.5) * 0.9 + Math.PI * 0.5 * (r() < 0.5 ? 1 : 0) : r() * Math.PI * 2
const shade = 0.55 + r() * 0.45
const hue = r()
g.save()
g.translate(x, y)
g.rotate(ang)
g.fillStyle = `rgb(${Math.round((150 + hue * 40) * shade)},${Math.round((200 + hue * 30) * shade)},${Math.round((110 + hue * 20) * shade)})`
g.beginPath()
g.ellipse(0, 0, len, wid, 0, 0, Math.PI * 2)
g.fill()
g.strokeStyle = `rgba(40,60,20,0.35)`
g.lineWidth = 1
g.beginPath(); g.moveTo(-len, 0); g.lineTo(len, 0); g.stroke()
g.restore()
}
const t = tex(c, true, false)
t.generateMipmaps = true
return t
}
function puffTexture() {
const W = 128
const { c, g } = canvas(W, W)
const img = g.createImageData(W, W)
for (let y = 0; y < W; y++) for (let x = 0; x < W; x++) {
const dx = (x - W / 2) / (W / 2), dy = (y - W / 2) / (W / 2)
const d = Math.hypot(dx, dy)
const n = fbm2(x * 0.06, y * 0.06, 4, 12)
const a = Math.max(0, 1 - d * (1.1 - n * 0.5)) ** 1.6
const i = (y * W + x) * 4
img.data[i] = img.data[i + 1] = img.data[i + 2] = 255
img.data[i + 3] = a * 255
}
g.putImageData(img, 0, 0)
return tex(c, false, false)
}
function glowTexture() {
const W = 128
const { c, g } = canvas(W, W)
const grd = g.createRadialGradient(W / 2, W / 2, 0, W / 2, W / 2, W / 2)
grd.addColorStop(0, 'rgba(255,255,255,1)')
grd.addColorStop(0.15, 'rgba(255,255,255,0.55)')
grd.addColorStop(0.45, 'rgba(255,255,255,0.12)')
grd.addColorStop(1, 'rgba(255,255,255,0)')
g.fillStyle = grd
g.fillRect(0, 0, W, W)
return tex(c, false, false)
}
function lanternTexture() {
const W = 256, H = 256
const { c, g } = canvas(W, H)
const grd = g.createLinearGradient(0, 0, 0, H)
grd.addColorStop(0, '#5a0d08')
grd.addColorStop(0.12, '#c4231a')
grd.addColorStop(0.5, '#e8382a')
grd.addColorStop(0.88, '#c4231a')
grd.addColorStop(1, '#5a0d08')
g.fillStyle = grd
g.fillRect(0, 0, W, H)
g.strokeStyle = 'rgba(70,10,5,0.55)'
g.lineWidth = 2
for (let x = 0; x < W; x += 16) { g.beginPath(); g.moveTo(x, 0); g.lineTo(x, H); g.stroke() }
g.fillStyle = '#d9a63a'
g.fillRect(0, 0, W, 10)
g.fillRect(0, H - 10, W, 10)
// 福 character (painted, gold-ish black)
g.fillStyle = 'rgba(30,8,4,0.85)'
g.font = 'bold 92px serif'
g.textAlign = 'center'
g.textBaseline = 'middle'
g.fillText('福', W * 0.25, H / 2)
g.fillText('福', W * 0.75, H / 2)
return tex(c, true, true)
}
function basketTexture() {
const W = 128, H = 128
const { c, g } = canvas(W, H)
g.fillStyle = '#8a6a3a'
g.fillRect(0, 0, W, H)
for (let y = 0; y < H; y += 8) for (let x = 0; x < W; x += 8) {
const odd = ((x + y) / 8) % 2
g.fillStyle = odd ? '#b08a4e' : '#7a5a2e'
g.fillRect(x + 1, y + 1, 6, 6)
}
return tex(c)
}
function groundTexture() {
const W = 256
const { c, g } = canvas(W, W)
const img = g.createImageData(W, W)
for (let y = 0; y < W; y++) for (let x = 0; x < W; x++) {
const n = tfbm(x, y, W, 90, 5)
const f = tnoise(x * 6, y * 6, W * 6, 3)
const l = 0.75 + (n - 0.5) * 0.5 + (f - 0.5) * 0.25
const i = (y * W + x) * 4
img.data[i] = img.data[i + 1] = img.data[i + 2] = Math.max(0, Math.min(255, l * 255))
img.data[i + 3] = 255
}
g.putImageData(img, 0, 0)
return tex(c, false)
}
function latticeTexture() {
// wooden window lattice (冰裂 / 方格) for the window panes; alpha = paper
const W = 128, H = 128
const { c, g } = canvas(W, H)
g.fillStyle = '#ffffff'
g.fillRect(0, 0, W, H)
g.strokeStyle = '#000000'
g.lineWidth = 7
g.strokeRect(0, 0, W, H)
g.lineWidth = 4
for (let i = 1; i < 4; i++) {
g.beginPath(); g.moveTo((i * W) / 4, 0); g.lineTo((i * W) / 4, H); g.stroke()
g.beginPath(); g.moveTo(0, (i * H) / 4); g.lineTo(W, (i * H) / 4); g.stroke()
}
return tex(c, false, true)
}
let cache: Textures | null = null
export function getTextures(): Textures {
if (cache) return cache
const wall = wallTexture(), roof = roofTexture(), slab = slabTexture(), stone = stoneTexture(), bark = barkTexture()
cache = {
wall: wall.map, wallN: wall.normal,
roof: roof.map, roofN: roof.normal,
slab: slab.map, slabN: slab.normal,
stone: stone.map, stoneN: stone.normal,
wood: woodTexture(),
cloth: [0, 1, 2].map(clothTexture),
leaf: leafTexture(false), bambooLeaf: leafTexture(true),
puff: puffTexture(), glow: glowTexture(),
lantern: lanternTexture(), basket: basketTexture(), ground: groundTexture(),
bark: bark.map, barkN: bark.normal,
lattice: latticeTexture(),
}
return cache
}
|