xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / kit.js 132 lines4.4 kB Raw
1import * as THREE from '../vendor/three.module.js';
2
3const MAX_PX = 4096;
4
5export function createLayer(renderer) {
6  const scene = new THREE.Scene();
7  const camera = new THREE.OrthographicCamera(0, 1, 1, 0, -10, 10);
8  const widgets = [];
9  const geometry = new THREE.PlaneGeometry(1, 1);
10  const layer = { scene, camera, widgets, scale: 1, lw: 1, lh: 1, w: 1, h: 1, hover: null };
11
12  layer.add = (name, o) => {
13    const mesh = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ transparent: true, depthTest: false, depthWrite: false, toneMapped: false }));
14    mesh.visible = false;
15    mesh.renderOrder = o.z ?? 0;
16    scene.add(mesh);
17    const wd = { name, essential: true, modal: false, lowres: false, ...o, mesh, rect: null, hits: [], dirty: true, px: '' };
18    wd.hit = (x, y, w, h, extra) => wd.hits.push({ x, y, w, h, ...extra });
19    wd.invalidate = () => (wd.dirty = true);
20    wd.hoverKey = () => (layer.hover?.wd === wd ? layer.hover.key : null);
21    wd.hovered = (key) => layer.hover?.wd === wd && layer.hover.key === key;
22    widgets.push(wd);
23    return wd;
24  };
25
26  layer.resize = (w, h, scale) => {
27    layer.w = w;
28    layer.h = h;
29    layer.scale = scale;
30    layer.lw = w / scale;
31    layer.lh = h / scale;
32    camera.right = w;
33    camera.top = h;
34    camera.updateProjectionMatrix();
35    for (const wd of widgets) wd.px = '';
36  };
37
38  layer.place = (wd, rect) => {
39    if (!rect) {
40      wd.rect = null;
41      wd.mesh.visible = false;
42      return;
43    }
44    const prev = wd.rect;
45    if (!prev || prev.w !== rect.w || prev.h !== rect.h) wd.dirty = true;
46    wd.rect = rect;
47    wd.mesh.visible = true;
48    const s = layer.scale;
49    wd.mesh.scale.set(rect.w * s, rect.h * s, 1);
50    wd.mesh.position.set((rect.x + rect.w / 2) * s, layer.h - (rect.y + rect.h / 2) * s, 0);
51  };
52
53  function paint(wd) {
54    const k = wd.lowres ? 0.05 : Math.min(layer.scale * renderer.getPixelRatio(), MAX_PX / Math.max(wd.rect.w, wd.rect.h));
55    const pw = Math.max(1, Math.ceil(wd.rect.w * k));
56    const ph = Math.max(1, Math.ceil(wd.rect.h * k));
57    const sig = `${pw}x${ph}`;
58    if (sig !== wd.px) {
59      wd.px = sig;
60      wd.mesh.material.map?.dispose();
61      const t = new THREE.CanvasTexture(new OffscreenCanvas(pw, ph));
62      t.colorSpace = THREE.SRGBColorSpace;
63      t.generateMipmaps = false;
64      t.minFilter = THREE.LinearFilter;
65      wd.mesh.material.map = t;
66      wd.mesh.material.needsUpdate = true;
67    }
68    const t = wd.mesh.material.map;
69    const ctx = t.image.getContext('2d');
70    ctx.setTransform(1, 0, 0, 1, 0, 0);
71    ctx.clearRect(0, 0, pw, ph);
72    ctx.setTransform(pw / wd.rect.w, 0, 0, ph / wd.rect.h, 0, 0);
73    wd.hits = [];
74    wd.draw(ctx, wd.rect.w, wd.rect.h, wd);
75    t.needsUpdate = true;
76    wd.dirty = false;
77  }
78
79  layer.update = () => {
80    for (const wd of widgets) if (wd.rect && (wd.dirty || wd.px === '')) paint(wd);
81  };
82
83  layer.render = () => {
84    renderer.clearDepth();
85    renderer.render(scene, camera);
86  };
87
88  layer.pick = (cx, cy) => {
89    const x = cx / layer.scale;
90    const y = cy / layer.scale;
91    let best = null;
92    for (const wd of widgets) {
93      const r = wd.rect;
94      if (!r || !(x >= r.x && y >= r.y && x < r.x + r.w && y < r.y + r.h)) continue;
95      if (wd.passive) continue;
96      if (!best || (wd.z ?? 0) >= (best.z ?? 0)) best = wd;
97    }
98    if (!best) {
99      const modal = widgets.find((q) => q.rect && q.modal);
100      return modal ? { widget: modal, hit: null, x: 0, y: 0 } : null;
101    }
102    const lx = x - best.rect.x;
103    const ly = y - best.rect.y;
104    let hit = null;
105    for (const h of best.hits) if (lx >= h.x && ly >= h.y && lx < h.x + h.w && ly < h.y + h.h) hit = h;
106    return { widget: best, hit, x: lx, y: ly };
107  };
108
109  layer.setHover = (p) => {
110    const key = p?.hit?.key ?? null;
111    const wd = key == null ? null : p.widget;
112    if (layer.hover?.wd === wd && layer.hover?.key === key) return;
113    layer.hover?.wd?.invalidate();
114    layer.hover = wd ? { wd, key } : null;
115    wd?.invalidate();
116  };
117
118  layer.rects = () => {
119    layer.update();
120    const s = layer.scale;
121    const out = [];
122    for (const wd of widgets) {
123      if (!wd.rect) continue;
124      const r = wd.rect;
125      out.push({ name: wd.name, x: r.x * s, y: r.y * s, w: r.w * s, h: r.h * s, essential: wd.essential });
126      for (const h of wd.hits) if (h.name) out.push({ name: h.name, x: (r.x + h.x) * s, y: (r.y + h.y) * s, w: h.w * s, h: h.h * s, essential: h.essential ?? true });
127    }
128    return out;
129  };
130
131  return layer;
132}