File size: 3,815 Bytes
6fff8e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 主渲染器:根据时间 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)
    }
  }
}