mx / dengtaishan-src /src /video /assets.ts
Ethscriptions's picture
登泰山记:渲染引擎、九个篇章场景、程序化配乐音效、MP4 导出、发布文案与封面(初版)
6fff8e3 verified
Raw History Blame Contribute Delete
11.4 kB
// 程序化素材:宣纸、胶片颗粒、水墨山峦、云团、印章 —— 按输出尺寸生成并缓存
import { Canvas, Ctx, makeCanvas, ctx2d, fbm1, fbm2, mulberry32, noise1, rgba } from './util'
import { font } from './fonts'
export interface RidgeOpts {
seed: number
base: number // 山脊基线(占高度比例,0=顶 1=底)
amp: number // 起伏幅度(占高度比例)
freq: number // 横向频率
color: string
alpha: number
depth: number // 墨色向下渐隐的高度(比例)
wf?: number // 宽度倍数(便于横向平移)
snow?: number // 积雪强度 0~1
tex?: number // 皴擦密度
ridged?: number // 尖峰程度
peaks?: { x: number; h: number; w: number }[] // 额外主峰
mist?: number // 底部雾化
}
export class Assets {
W: number
H: number
u: number
private cache = new Map<string, Canvas>()
constructor(W: number, H: number) {
this.W = W
this.H = H
this.u = Math.min(W, H) / 1080
}
get(key: string, gen: () => Canvas) {
let c = this.cache.get(key)
if (!c) {
c = gen()
this.cache.set(key, c)
}
return c
}
dispose() {
this.cache.clear()
}
/** 宣纸 / 墨底纹理 */
paper(kind: 'light' | 'dark' | 'stone') {
return this.get('paper-' + kind, () => {
const s = 0.5
const w = Math.ceil(this.W * s)
const h = Math.ceil(this.H * s)
const c = makeCanvas(w, h)
const g = ctx2d(c)
const img = g.createImageData(w, h)
const d = img.data
const rnd = mulberry32(kind === 'light' ? 7 : kind === 'dark' ? 11 : 23)
const base = kind === 'light' ? [236, 229, 213] : kind === 'dark' ? [17, 18, 21] : [38, 38, 40]
const sc = 3.2 / Math.max(w, h)
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
const n = fbm2(x * sc * 2, y * sc * 2, 3, 3)
const n2 = kind === 'stone' ? fbm2(x * sc * 14, y * sc * 14, 9, 3) : 0.5
const r = rnd() - 0.5
const i = (y * w + x) * 4
let k: number
if (kind === 'light') k = (n - 0.5) * 22 + r * 9
else if (kind === 'dark') k = (n - 0.5) * 14 + r * 6
else k = (n - 0.5) * 30 + (n2 - 0.5) * 34 + r * 14
d[i] = base[0] + k
d[i + 1] = base[1] + k * (kind === 'light' ? 0.97 : 1)
d[i + 2] = base[2] + k * (kind === 'light' ? 0.9 : 1.04)
d[i + 3] = 255
}
}
g.putImageData(img, 0, 0)
// 纸纤维
const fib = kind === 'light' ? 'rgba(150,120,80,' : 'rgba(255,255,255,'
const n = Math.round((w * h) / 900)
g.lineCap = 'round'
for (let i = 0; i < n; i++) {
const x = rnd() * w
const y = rnd() * h
const L = 4 + rnd() * 22
const a = rnd() * Math.PI * 2
g.strokeStyle = fib + (kind === 'light' ? 0.05 + rnd() * 0.08 : 0.015 + rnd() * 0.03) + ')'
g.lineWidth = 0.4 + rnd() * 0.7
g.beginPath()
g.moveTo(x, y)
g.quadraticCurveTo(x + Math.cos(a + 0.6) * L * 0.5, y + Math.sin(a + 0.6) * L * 0.5, x + Math.cos(a) * L, y + Math.sin(a) * L)
g.stroke()
}
return c
})
}
/** 胶片颗粒贴图 */
grain(i: number) {
return this.get('grain' + (i % 4), () => {
const n = 256
const c = makeCanvas(n, n)
const g = ctx2d(c)
const img = g.createImageData(n, n)
const rnd = mulberry32(100 + (i % 4))
for (let p = 0; p < n * n; p++) {
const v = rnd() * 255
img.data[p * 4] = v
img.data[p * 4 + 1] = v
img.data[p * 4 + 2] = v
img.data[p * 4 + 3] = 255
}
g.putImageData(img, 0, 0)
return c
})
}
/** 水墨山峦层:渐隐墨色 + 皴擦 + 积雪 */
ridge(key: string, o: RidgeOpts) {
return this.get('ridge-' + key, () => {
const s = 0.5
const wf = o.wf ?? 1
const w = Math.ceil(this.W * wf * s)
const h = Math.ceil(this.H * s)
const c = makeCanvas(w, h)
const g = ctx2d(c)
const rnd = mulberry32(o.seed * 991 + 3)
const ys: number[] = []
const step = 2
const ridged = o.ridged ?? 0.6
// 以竖屏/横屏统一的“视觉宽度”计算频率,保证形态一致
const span = (this.W * wf) / Math.max(this.W, this.H)
for (let x = 0; x <= w + step; x += step) {
const fx = (x / w) * span * o.freq
const f = fbm1(fx, o.seed, 6)
const r = 1 - Math.abs(noise1(fx * 0.9 + 3.7, o.seed + 5))
let v = f * (1 - ridged) + (r * r - 0.4) * ridged
if (o.peaks)
for (const p of o.peaks) {
const dx = (x / w - p.x) / p.w
v += p.h * Math.exp(-dx * dx * 2.2) * (1 + 0.15 * fbm1(fx * 3, o.seed + 9))
}
ys.push(h * (o.base - o.amp * v))
}
const minY = Math.min(...ys)
const path = () => {
g.beginPath()
g.moveTo(0, h + 2)
ys.forEach((y, i) => g.lineTo(i * step, y))
g.lineTo(w, h + 2)
g.closePath()
}
// 墨色主体
const gr = g.createLinearGradient(0, minY, 0, Math.min(h, minY + o.depth * h))
gr.addColorStop(0, rgba(o.color, o.alpha))
gr.addColorStop(0.55, rgba(o.color, o.alpha * 0.55))
gr.addColorStop(1, rgba(o.color, o.mist === undefined ? 0 : o.alpha * o.mist))
path()
g.fillStyle = gr
g.fill()
g.save()
path()
g.clip()
// 山脊附近加深
for (let i = 0; i < ys.length; i += 1) {
const x = i * step
const y = ys[i]
const lg = g.createLinearGradient(0, y, 0, y + h * 0.12)
lg.addColorStop(0, rgba(o.color, o.alpha * 0.55))
lg.addColorStop(1, rgba(o.color, 0))
g.fillStyle = lg
g.fillRect(x, y, step + 0.5, h * 0.12)
}
// 积雪
if (o.snow) {
for (let i = 0; i < ys.length; i++) {
const x = i * step
const y = ys[i]
const slope = (ys[Math.min(ys.length - 1, i + 1)] - ys[Math.max(0, i - 1)]) / (2 * step)
const dep = h * 0.09 * o.snow * (0.4 + 0.6 * (0.5 + 0.5 * fbm1(x * 0.02, o.seed + 31))) * (1 - Math.min(1, Math.abs(slope) * 0.8))
const lg = g.createLinearGradient(0, y, 0, y + dep)
lg.addColorStop(0, `rgba(240,242,246,${0.92 * o.snow})`)
lg.addColorStop(0.6, `rgba(225,230,238,${0.5 * o.snow})`)
lg.addColorStop(1, 'rgba(220,226,235,0)')
g.fillStyle = lg
g.fillRect(x, y - 0.5, step + 0.5, dep)
}
// 雪中沟壑的白色流线
g.lineCap = 'round'
for (let i = 0; i < w * 0.5 * o.snow; i++) {
const xi = Math.floor(rnd() * ys.length)
const x = xi * step
const y = ys[xi] + rnd() * h * 0.16
const len = h * (0.02 + rnd() * 0.07)
const dir = rnd() < 0.5 ? -1 : 1
g.strokeStyle = `rgba(235,238,244,${0.15 + rnd() * 0.35})`
g.lineWidth = 0.6 + rnd() * 1.6
g.beginPath()
g.moveTo(x, y)
g.quadraticCurveTo(x + dir * len * 0.2, y + len * 0.5, x + dir * len * 0.45, y + len)
g.stroke()
}
}
// 皴擦
const tex = o.tex ?? 1
g.lineCap = 'round'
for (let i = 0; i < w * 0.9 * tex; i++) {
const xi = Math.floor(rnd() * ys.length)
const x = xi * step
const y = ys[xi] + Math.pow(rnd(), 1.6) * h * o.depth * 0.7
const len = h * (0.01 + rnd() * 0.05)
const dir = rnd() < 0.5 ? -1 : 1
g.strokeStyle = rgba(o.color, (0.05 + rnd() * 0.16) * Math.min(1, o.alpha * 1.4))
g.lineWidth = 0.5 + rnd() * 1.8
g.beginPath()
g.moveTo(x, y)
g.lineTo(x + dir * len * (0.3 + rnd() * 0.5), y + len)
g.stroke()
}
g.restore()
// 山脊线
g.beginPath()
ys.forEach((y, i) => (i ? g.lineTo(i * step, y) : g.moveTo(0, y)))
g.strokeStyle = rgba(o.color, Math.min(1, o.alpha * 0.9))
g.lineWidth = 1.1
g.stroke()
return c
})
}
/** 云团精灵 */
cloud(i: number, tint = '#ffffff') {
return this.get('cloud' + i + tint, () => {
const w = 640
const h = 260
const c = makeCanvas(w, h)
const g = ctx2d(c)
const rnd = mulberry32(500 + i * 17)
for (let k = 0; k < 70; k++) {
const t = rnd()
const x = w * (0.12 + 0.76 * t)
const y = h * (0.62 - 0.32 * Math.sin(Math.PI * t) * rnd())
const r = h * (0.1 + 0.24 * rnd()) * (0.6 + Math.sin(Math.PI * t) * 0.7)
const rg = g.createRadialGradient(x, y, 0, x, y, r)
rg.addColorStop(0, rgba(tint, 0.2 + rnd() * 0.12))
rg.addColorStop(0.6, rgba(tint, 0.08))
rg.addColorStop(1, rgba(tint, 0))
g.fillStyle = rg
g.fillRect(x - r, y - r, r * 2, r * 2)
}
return c
})
}
/** 朱红印章 */
seal(text: string, white = true, size = 256) {
return this.get('seal-' + text + white + size, () => {
const c = makeCanvas(size, size)
const g = ctx2d(c)
const rnd = mulberry32(text.charCodeAt(0) * 13 + text.length)
const m = size * 0.06
const jitter = () => (rnd() - 0.5) * size * 0.018
g.beginPath()
g.moveTo(m + jitter(), m + jitter())
for (let i = 1; i <= 8; i++) g.lineTo(m + ((size - 2 * m) * i) / 8 + jitter(), m + jitter())
for (let i = 1; i <= 8; i++) g.lineTo(size - m + jitter(), m + ((size - 2 * m) * i) / 8 + jitter())
for (let i = 1; i <= 8; i++) g.lineTo(size - m - ((size - 2 * m) * i) / 8 + jitter(), size - m + jitter())
for (let i = 1; i <= 8; i++) g.lineTo(m + jitter(), size - m - ((size - 2 * m) * i) / 8 + jitter())
g.closePath()
if (white) {
g.fillStyle = '#b3261e'
g.fill()
g.globalCompositeOperation = 'destination-out'
g.fillStyle = '#000'
} else {
g.strokeStyle = '#b3261e'
g.lineWidth = size * 0.05
g.stroke()
g.fillStyle = '#b3261e'
}
const chars = [...text]
g.textAlign = 'center'
g.textBaseline = 'middle'
if (chars.length === 1) {
g.font = font(size * 0.62, 'kai')
g.fillText(chars[0], size / 2, size * 0.53)
} else if (chars.length === 2) {
g.font = font(size * 0.4, 'kai')
g.fillText(chars[0], size / 2, size * 0.3)
g.fillText(chars[1], size / 2, size * 0.72)
} else {
// 四字:右上起竖读
g.font = font(size * 0.36, 'kai')
const pos = [
[0.71, 0.3], [0.71, 0.72], [0.29, 0.3], [0.29, 0.72],
]
chars.slice(0, 4).forEach((ch, i) => g.fillText(ch, size * pos[i][0], size * pos[i][1]))
}
// 斑驳
g.globalCompositeOperation = 'destination-out'
for (let i = 0; i < 140; i++) {
g.globalAlpha = 0.15 + rnd() * 0.5
g.beginPath()
g.arc(rnd() * size, rnd() * size, rnd() * size * 0.012 + 0.5, 0, Math.PI * 2)
g.fill()
}
g.globalAlpha = 1
g.globalCompositeOperation = 'source-over'
return c
})
}
}
/** 软边光斑 */
export function glow(g: Ctx, x: number, y: number, r: number, color: string, a: number) {
if (a <= 0 || r <= 0) return
const rg = g.createRadialGradient(x, y, 0, x, y, r)
rg.addColorStop(0, rgba(color, a))
rg.addColorStop(0.4, rgba(color, a * 0.45))
rg.addColorStop(1, rgba(color, 0))
g.fillStyle = rg
g.fillRect(x - r, y - r, r * 2, r * 2)
}