import { BufferAttribute, BufferGeometry, Color, DoubleSide, LinearSRGBColorSpace, Mesh, MeshBasicMaterial, OrthographicCamera, Scene as GlRoot, WebGLRenderer } from 'three' import { Batch, RING_CORNERS, rgbOf, ring } from '~/diorama/figures' import { buildGl, type GlScene } from '~/diorama/gl' import type { Scene, View } from '~/diorama/scene' /** * Draws a village scene with WebGL into `canvas`: everything that stands still in one mesh * built when the scene changes, people, animals, smoke and crows in a second mesh written * every frame, and an orthographic camera on `view` (cut to the canvas like SVG's * xMidYMid slice), so panning and zooming only move the camera. */ export function useDioramaGl(opts: { canvas: Ref scene: () => Scene view: () => View size: () => { w: number; h: number } /** Animate; without it, people who stay put stand still and the rest is not drawn. */ motion: () => boolean /** Buildings whose ground ring shows: the open one solid, the keyboard's focus dashed. */ marks: () => { id: number; dashed: boolean }[] background: string }) { let renderer: WebGLRenderer | null = null const root = new GlRoot() const camera = new OrthographicCamera() const material = new MeshBasicMaterial({ vertexColors: true, side: DoubleSide }) let still: Mesh | null = null let moving: Mesh | null = null let batch: Batch | null = null let gl: GlScene | null = null let frame = 0 let dirty = true function build() { gl = buildGl(opts.scene()) if (still) { root.remove(still) still.geometry.dispose() } const geometry = new BufferGeometry() geometry.setAttribute('position', new BufferAttribute(gl.positions, 3)) geometry.setAttribute('color', new BufferAttribute(gl.colors, 3, true)) geometry.setIndex(new BufferAttribute(gl.indices, 1)) still = new Mesh(geometry, material) still.frustumCulled = false still.renderOrder = 0 root.add(still) const capacity = gl.actors.reduce((n, a) => n + a.actor.corners, 0) + RING_CORNERS * 2 if (!batch || batch.capacity < capacity) { batch = new Batch(capacity) if (moving) { root.remove(moving) moving.geometry.dispose() } const g = new BufferGeometry() g.setAttribute('position', new BufferAttribute(batch.positions, 3)) g.setAttribute('color', new BufferAttribute(batch.colors, 3)) moving = new Mesh(g, material) moving.frustumCulled = false moving.renderOrder = 1 root.add(moving) } // Look down at the nearest step from just above it. camera.position.z = gl.top + 1 camera.near = 0.5 camera.far = gl.top + 2 aim() } function aim() { const { w, h } = opts.size() const v = opts.view() const k = Math.max(w / v.w, h / v.h) const fw = w / k const fh = h / k const left = v.x + (v.w - fw) / 2 const top = v.y + (v.h - fh) / 2 camera.left = left camera.right = left + fw camera.top = -top camera.bottom = -(top + fh) camera.updateProjectionMatrix() dirty = true } function draw(now: number) { if (!renderer || !gl || !batch || !moving) return const motion = opts.motion() batch.count = 0 const t = now / 1000 for (const mark of opts.marks()) { const at = gl.rings.get(mark.id) if (at) ring(batch, at.ring, mark.dashed, at.z) } for (const { actor, z } of gl.actors) if (motion || !actor.moving) actor.draw(batch, t, motion, z) const g = moving.geometry ;(g.attributes.position as BufferAttribute).needsUpdate = true ;(g.attributes.color as BufferAttribute).needsUpdate = true g.setDrawRange(0, batch.count) renderer.render(root, camera) dirty = false } function loop(now: number) { frame = requestAnimationFrame(loop) if (opts.motion() || dirty) draw(now) } function resize() { const { w, h } = opts.size() renderer?.setSize(w, h, false) aim() } onMounted(() => { const canvas = opts.canvas.value if (!canvas) return renderer = new WebGLRenderer({ canvas, antialias: true }) // Colours go out exactly as the palette writes them, like the SVG did. renderer.outputColorSpace = LinearSRGBColorSpace const [r, g, b] = rgbOf(opts.background) renderer.setClearColor(new Color().setRGB(r, g, b, LinearSRGBColorSpace), 1) renderer.setPixelRatio(Math.min(2, window.devicePixelRatio || 1)) resize() build() frame = requestAnimationFrame(loop) }) onBeforeUnmount(() => { cancelAnimationFrame(frame) still?.geometry.dispose() moving?.geometry.dispose() material.dispose() renderer?.dispose() renderer?.forceContextLoss() renderer = null }) watch(() => opts.scene(), () => renderer && build()) watch(() => opts.size(), () => renderer && resize()) watch(() => opts.view(), () => aim()) watch(() => [JSON.stringify(opts.marks()), opts.motion()], () => (dirty = true)) }