Spaces:
Running
Running
Download dengtaishan-src/src/video/render.ts from Ethscriptions/mx: direct link, hf CLI and curl.
- Browser
- Download file 3.82 kB
-
https://huggingface.co/spaces/Ethscriptions/mx/resolve/main/dengtaishan-src/src/video/render.ts
- Command line
-
hf download hf://spaces/Ethscriptions/mx/dengtaishan-src/src/video/render.ts
-
curl -L -o render.ts https://huggingface.co/spaces/Ethscriptions/mx/resolve/main/dengtaishan-src/src/video/render.ts
3.82 kB
| // 主渲染器:根据时间 t 合成整帧(场景 + 转场 + 字幕 + HUD + 篇章卡 + 胶片颗粒) | |
| import { Assets } from './assets' | |
| import { RC, chapterCard, drawSubtitle, hud } from './draw' | |
| import { TL, lineAt, sceneIndexAt, DURATION, Scene } from './timeline' | |
| import { Canvas, Ctx, ctx2d, makeCanvas, fbm1, easeInOut, inv, smooth } from './util' | |
| import { SCENES } from './scenes' | |
| export interface SceneDef { | |
| tone: 'dark' | 'light' | |
| draw: (rc: RC, sc: Scene, lt: number) => void | |
| } | |
| const TR = 1.25 // 转场时长 | |
| export class Renderer { | |
| W: number | |
| H: number | |
| A: Assets | |
| private buf: Canvas | |
| private bctx: Ctx | |
| constructor(W: number, H: number) { | |
| this.W = W | |
| this.H = H | |
| this.A = new Assets(W, H) | |
| this.buf = makeCanvas(W, H) | |
| this.bctx = ctx2d(this.buf) | |
| } | |
| dispose() { | |
| this.A.dispose() | |
| } | |
| private rc(g: Ctx, t: number): RC { | |
| return { g, W: this.W, H: this.H, u: Math.min(this.W, this.H) / 1080, P: this.H > this.W, A: this.A, t } | |
| } | |
| private drawScene(g: Ctx, idx: number, t: number) { | |
| const sc = TL.scenes[idx] | |
| const def = SCENES[sc.id] | |
| const rc = this.rc(g, t) | |
| const lt = t - sc.start | |
| g.save() | |
| def.draw(rc, sc, lt) | |
| g.restore() | |
| const shot = sc.id.startsWith('shot') ? Number(sc.id.slice(4)) - 1 : -1 | |
| hud(rc, sc, lt, def.tone, shot) | |
| } | |
| render(g: Ctx, t: number) { | |
| const { W, H } = this | |
| t = Math.max(0, Math.min(DURATION - 1e-3, t)) | |
| const idx = sceneIndexAt(t) | |
| const sc = TL.scenes[idx] | |
| const lt = t - sc.start | |
| g.setTransform(1, 0, 0, 1, 0, 0) | |
| g.globalAlpha = 1 | |
| g.globalCompositeOperation = 'source-over' | |
| if (idx > 0 && lt < TR) { | |
| // 墨晕转场:旧场景在下,新场景经由不规则墨团遮罩显现 | |
| this.drawScene(g, idx - 1, t) | |
| const b = this.bctx | |
| b.setTransform(1, 0, 0, 1, 0, 0) | |
| b.globalCompositeOperation = 'source-over' | |
| b.globalAlpha = 1 | |
| b.clearRect(0, 0, W, H) | |
| this.drawScene(b, idx, t) | |
| const p = easeInOut(lt / TR) | |
| b.globalCompositeOperation = 'destination-in' | |
| b.fillStyle = '#000' | |
| const R = Math.hypot(W, H) * 0.62 | |
| const blobs = [ | |
| [0.5, 0.5, 1], | |
| [0.28, 0.35, 0.75], | |
| [0.74, 0.66, 0.8], | |
| ] | |
| b.beginPath() | |
| for (const [bx, by, k] of blobs) { | |
| const r = R * p * k * (p < 0.98 ? 1 : 2) | |
| const cx = bx * W | |
| const cy = by * H | |
| for (let i = 0; i <= 72; i++) { | |
| const a = (i / 72) * Math.PI * 2 | |
| const n = 1 + 0.16 * fbm1(a * 2.2 + bx * 10 + idx * 3, idx) + 0.05 * fbm1(a * 9 + idx, idx + 4) | |
| const x = cx + Math.cos(a) * r * n | |
| const y = cy + Math.sin(a) * r * n | |
| if (i === 0) b.moveTo(x, y) | |
| else b.lineTo(x, y) | |
| } | |
| b.closePath() | |
| } | |
| b.fill() | |
| b.globalCompositeOperation = 'source-over' | |
| g.drawImage(this.buf as CanvasImageSource, 0, 0) | |
| } else { | |
| this.drawScene(g, idx, t) | |
| } | |
| const rc = this.rc(g, t) | |
| const def = SCENES[sc.id] | |
| drawSubtitle(rc, lineAt(t), def.tone) | |
| if (sc.intro > 2) chapterCard(rc, sc, lt, def.tone) | |
| // 胶片颗粒 | |
| const fr = Math.floor(t * 24) | |
| g.save() | |
| g.globalAlpha = def.tone === 'dark' ? 0.055 : 0.07 | |
| g.globalCompositeOperation = 'overlay' | |
| const tile = this.A.grain(fr) | |
| const pat = g.createPattern(tile as CanvasImageSource, 'repeat') | |
| if (pat) { | |
| const ox = (fr * 37) % 256 | |
| const oy = (fr * 91) % 256 | |
| g.translate(-ox, -oy) | |
| g.fillStyle = pat | |
| g.fillRect(0, 0, W + 256, H + 256) | |
| } | |
| g.restore() | |
| // 片头片尾黑场 | |
| const fade = Math.min(smooth(inv(0, 1.2, t)), 1 - smooth(inv(DURATION - 1.8, DURATION, t))) | |
| if (fade < 1) { | |
| g.fillStyle = `rgba(0,0,0,${1 - fade})` | |
| g.fillRect(0, 0, W, H) | |
| } | |
| } | |
| } | |