Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | 47x 24x 24x 24x 24x 2x 2x 2x 2x 2x 2x 24x 24x 2x 2x 2x 2x 2x | import { Container, Graphics, WebGLRenderer, type Renderer, type Texture } from 'pixi.js';
import { runGPUPasses } from './gpu-pass.ts';
import { PIXI_HOST_RENDER, pixiHostTarget } from './host-render.ts';
import {
defineBeMusicSkin,
findWhiteLaneWidth,
type BeMusicDecideFrame,
type BeMusicGameplayFrame,
type BeMusicResultFrame,
type BeMusicSelectFrame,
type BeMusicSkin,
type BeMusicSurface,
} from '@be-music/player-skin-sdk';
import { drawCautionMine } from './mine.ts';
import { ChildPool, LaggedDisposer } from './pools.ts';
import { setTextResolution } from './resolution.ts';
import { collectHitAreas, type PixiHitArea } from './skin-text.ts';
import type { PixiDecideSkin, PixiSelectFrame, PixiSelectRenderer, PixiSkinDefinition } from './types.ts';
/** LR2's opacity for a dropped long note (`0x80` of `0xff`). */
const DROPPED_LONG_NOTE_ALPHA = 0x80 / 0xff;
/**
* Turns a {@link PixiSkinDefinition} into a Be-Music Skin. Each frame replays the player's plain frame data into the
* skin's Pixi renderer functions and renders the result. A player that renders with Pixi draws it with its own renderer
* into a texture it shows (see {@link PIXI_HOST_RENDER}); anywhere else every surface gets its own Pixi `WebGLRenderer`
* on the surface's WebGL 2 context and the frame is rendered into the canvas.
*/
export function definePixiSkin(definition: PixiSkinDefinition): BeMusicSkin<'webgl2'> {
const { gameplay, select, result: _result, decide, ...meta } = definition;
const surfaces = new WeakMap<HTMLCanvasElement, PixiSurface>();
const surfaceFor = (surface: BeMusicSurface<'webgl2'>): PixiSurface | undefined => surfaces.get(surface.canvas);
const skin = defineBeMusicSkin<BeMusicSkin<'webgl2'>>({
...meta,
context: 'webgl2',
// Pixi needs a stencil buffer for masks; the canvas is composited over the BGA, so it keeps (premultiplied) alpha.
// The player uploads the canvas in the same frame it is drawn, so the drawing buffer needn't be preserved (keeping
// it would cost a full-surface copy every frame).
contextAttributes: {
alpha: true,
premultipliedAlpha: true,
antialias: true,
stencil: true,
preserveDrawingBuffer: false,
},
async setup(surface) {
const host = pixiHostTarget(surface);
Eif (host) {
surfaces.set(surface.canvas, new PixiSurface(host.renderer, host.texture));
return;
}
const renderer = new WebGLRenderer();
await renderer.init({
canvas: surface.canvas,
context: surface.context,
width: surface.width,
height: surface.height,
resolution: surface.pixelRatio,
backgroundAlpha: 0,
useBackBuffer: true,
antialias: true,
autoDensity: false,
});
surfaces.set(surface.canvas, new PixiSurface(renderer, undefined));
},
teardown(surface) {
surfaceFor(surface)?.destroy();
surfaces.delete(surface.canvas);
},
gameplay: {
bombDurationMs: gameplay.bombDurationMs,
...(gameplay.guideLabel ? { guideLabel: gameplay.guideLabel } : {}),
...(gameplay.laneCover ? { laneCover: gameplay.laneCover } : {}),
// Pixi skins always draw mines: their own `renderMine`, or the player's caution-striped bar.
drawsMines: true,
draw(surface, frame) {
const target = surfaceFor(surface);
if (!target) return;
target.screen ??= new GameplayScreen(definition);
if (!(target.screen instanceof GameplayScreen)) return;
const screen = target.screen;
target.render(surface, () => screen.build(frame));
},
},
select: {
layout: select.layout,
searchTextSize: select.searchTextSize,
portraitSearchTextSize: select.portraitSearchTextSize,
portraitLayout: select.portraitLayout,
outroMs: select.outroMs,
draw(surface, frame) {
const target = surfaceFor(surface);
if (!target) return;
target.screen ??= new SelectScreen(definition.select.createRenderer());
if (!(target.screen instanceof SelectScreen)) return;
const screen = target.screen;
target.render(surface, () => screen.build(frame));
},
},
result: {
draw(surface, frame) {
const target = surfaceFor(surface);
if (!target) return;
target.screen ??= new ResultScreen(definition);
if (!(target.screen instanceof ResultScreen)) return;
const screen = target.screen;
target.render(surface, () => screen.build(frame));
},
},
...(decide !== undefined
? {
decide: {
durationMs: decide.durationMs,
draw(surface: BeMusicSurface<'webgl2'>, frame: BeMusicDecideFrame) {
const target = surfaceFor(surface);
if (!target) return;
target.screen ??= new DecideScreen(decide);
if (!(target.screen instanceof DecideScreen)) return;
const screen = target.screen;
target.render(surface, () => screen.build(frame));
},
},
}
: {}),
});
Object.defineProperty(skin, PIXI_HOST_RENDER, { value: true });
return skin;
}
/**
* One surface's renderer and the screen it shows: the surface's own `WebGLRenderer`, or — with a `target` — the
* player's renderer, drawing into the player's texture.
*/
class PixiSurface {
screen: GameplayScreen | SelectScreen | ResultScreen | DecideScreen | undefined;
private readonly renderer: Renderer;
private readonly target: Texture | undefined;
constructor(renderer: Renderer, target: Texture | undefined) {
this.renderer = renderer;
this.target = target;
}
/** Matches the renderer to the surface's size and density, builds the frame, and renders it into the canvas. */
render(surface: BeMusicSurface, build: () => Container): void {
const renderer = this.renderer;
setTextResolution(surface.pixelRatio);
const root = build();
// The canvas never sees the pointer (the player handles input), so skip Pixi's hit-testing tree entirely.
root.eventMode = 'none';
root.interactiveChildren = false;
// GPU work the frame asked for while it was built (simulation steps into render textures) runs first.
runGPUPasses(renderer);
if (this.target) {
// The player rounds its own sprites to device pixels; the skin's vector shapes were drawn unrounded on their own
// renderer, and keep that.
const roundPixels = renderer._roundPixels;
renderer._roundPixels = 0;
// Backdrop filters capture this offscreen texture, without enabling a back buffer on the host's transparent canvas.
const backBuffer = renderer instanceof WebGLRenderer ? renderer.backBuffer : undefined;
const previousBackBuffer = backBuffer?.useBackBuffer;
if (backBuffer) backBuffer.useBackBuffer = true;
try {
renderer.render({ container: root, target: this.target, clear: true, clearColor: [0, 0, 0, 0] });
} finally {
renderer._roundPixels = roundPixels;
if (backBuffer) backBuffer.useBackBuffer = previousBackBuffer!;
}
return;
}
if (
renderer.resolution !== surface.pixelRatio ||
renderer.screen.width !== surface.width ||
renderer.screen.height !== surface.height
) {
renderer.resize(surface.width, surface.height, surface.pixelRatio);
}
renderer.render({ container: root, clear: true });
}
destroy(): void {
this.screen?.destroy();
this.screen = undefined;
// The player owns its renderer.
Iif (!this.target) this.renderer.destroy();
}
}
/** Gameplay: chrome behind, then lanes, notes, hit effects, and the chrome overlay — the player's old layer order. */
class GameplayScreen {
private readonly definition: PixiSkinDefinition;
private readonly root = new Container();
private readonly layer = new Container();
private readonly lanes = new Graphics();
/** Every note and long note this frame, drawn into one Graphics (they only add geometry, in draw order). */
private readonly notes = new Graphics();
/**
* Long notes missed at the head or let go early (`state: 'dropped'`), drawn under the rest at LR2's half alpha
* (`0x80` of `0xff`) until they go.
*/
private readonly droppedLongNotes = new Graphics({ alpha: DROPPED_LONG_NOTE_ALPHA });
private readonly bombs = new Container();
private readonly overlayLayer = new Container();
private readonly layerPool = new ChildPool(this.layer);
private readonly overlayLayerPool = new ChildPool(this.overlayLayer);
private readonly bombPool = new ChildPool(this.bombs);
constructor(definition: PixiSkinDefinition) {
this.definition = definition;
this.root.addChild(this.layer, this.lanes, this.droppedLongNotes, this.notes, this.overlayLayer, this.bombs);
// Each rebuilt-every-frame layer is its own render group: a pooled child shown or hidden, or a graphics whose
// batches change, then re-collects only that layer's instructions instead of the whole screen's.
this.layer.isRenderGroup = true;
this.overlayLayer.isRenderGroup = true;
this.bombs.isRenderGroup = true;
}
build(frame: BeMusicGameplayFrame): Container {
const skin = this.definition.gameplay;
const { runtime, nowMs, effects, audio } = frame;
const combo = runtime.combo;
// Chrome renderers may shake or scale their layers; every frame starts from rest.
this.layer.position.set(0, 0);
this.layer.scale.set(1);
this.overlayLayer.position.set(0, 0);
this.overlayLayer.scale.set(1);
this.layerPool.begin();
this.overlayLayerPool.begin();
try {
skin.renderChrome({
layer: this.layer,
overlayLayer: this.overlayLayer,
layerPool: this.layerPool,
overlayLayerPool: this.overlayLayerPool,
runtime,
layout: frame.layout,
settings: frame.settings,
});
} finally {
this.layerPool.end();
this.overlayLayerPool.end();
}
this.lanes.clear();
skin.renderLanes({
graphics: this.lanes,
lanes: frame.lanes,
beatPhase: frame.beatPhase,
nowMs,
combo,
effects,
audio,
});
const notes = this.notes;
notes.clear();
const whiteLaneWidth = findWhiteLaneWidth(frame.lanes);
this.droppedLongNotes.clear();
for (const note of frame.longNotes) {
skin.renderLongNote({
graphics: note.state === 'dropped' ? this.droppedLongNotes : notes,
kind: note.kind,
x: note.x,
w: note.w,
top: note.top,
bottom: note.bottom,
nowMs,
whiteLaneWidth,
mode: note.mode,
backSpinScratch: note.backSpinScratch,
state: note.state,
releasedElapsedMs: note.releasedElapsedMs,
});
}
for (const note of frame.notes) {
skin.renderNote({ graphics: notes, kind: note.kind, x: note.x, w: note.w, y: note.y, nowMs, whiteLaneWidth });
}
// Mines after the notes, so a mine sharing a beat with a note paints on top.
for (const mine of frame.mines) {
if (skin.renderMine)
skin.renderMine({ graphics: notes, kind: mine.kind, x: mine.x, w: mine.w, y: mine.y, nowMs, whiteLaneWidth });
else drawCautionMine(notes, mine.x, mine.w, mine.y);
}
this.bombPool.begin();
try {
skin.renderBombs({
layer: this.bombs,
pool: this.bombPool,
bombs: frame.bombs,
nowMs,
combo,
effects,
audio,
settings: frame.settings,
lanes: frame.lanes,
});
} finally {
this.bombPool.end();
}
return this.root;
}
destroy(): void {
for (const pool of [this.layerPool, this.overlayLayerPool, this.bombPool]) pool.destroy();
this.root.destroy({ children: true });
}
}
/**
* Select: the renderer's persistent back / front layers around a layer rebuilt whenever the select state changes, the
* list is pulled (`frame.listScroll`), or while the renderer asks for it. Hit areas declared during the rebuild are handed to the player on every frame.
*/
class SelectScreen {
private readonly renderer: PixiSelectRenderer;
private readonly root = new Container();
private readonly layer = new Container();
/** Retires the previous rebuild a frame late, so unchanged labels keep their text textures. */
private readonly disposer = new LaggedDisposer();
private revision = -1;
/** `frame.listScroll` the layer was last built with: a drag moves it every frame without moving the revision. */
private listScroll = 0;
private needsFrame = false;
private hits: PixiHitArea[] = [];
constructor(renderer: PixiSelectRenderer) {
this.renderer = renderer;
this.root.addChild(renderer.backLayer, this.layer, renderer.frontLayer);
}
build(frame: BeMusicSelectFrame): Container {
if (
frame.revision !== this.revision ||
frame.listScroll !== this.listScroll ||
this.needsFrame ||
frame.launchAt !== undefined
) {
this.revision = frame.revision;
this.listScroll = frame.listScroll;
this.disposer.cycle(this.layer);
// The Pixi renderer declares its click targets through `addHitArea`; they are replayed into `frame.hit` below.
const pixiFrame: PixiSelectFrame = { ...frame, layer: this.layer };
this.hits = collectHitAreas(() => {
this.needsFrame = this.renderer.render(pixiFrame);
});
}
for (const area of this.hits) frame.hit(area.x, area.y, area.w, area.h, area.action, area.cursor);
this.renderer.tick(frame.nowMs, frame.focusedChart, frame.launchAt, frame.audio);
return this.root;
}
destroy(): void {
this.disposer.flush();
this.renderer.dispose();
this.root.destroy({ children: true });
}
}
/** Result: one layer rebuilt every frame, its nodes retired a frame late so unchanged labels keep their textures. */
class ResultScreen {
private readonly definition: PixiSkinDefinition;
private readonly root = new Container();
private readonly backdrop = new Container();
private readonly layer = new Container();
private readonly disposer = new LaggedDisposer();
constructor(definition: PixiSkinDefinition) {
this.definition = definition;
this.root.addChild(this.backdrop, this.layer);
}
build(frame: BeMusicResultFrame): Container {
this.disposer.cycle(this.layer);
this.definition.result.render({ ...frame, layer: this.layer, backdrop: this.backdrop });
return this.root;
}
destroy(): void {
this.disposer.flush();
this.root.destroy({ children: true });
}
}
/** Decide: like the result, one layer rebuilt every frame over a persistent backdrop. */
class DecideScreen {
private readonly skin: PixiDecideSkin;
private readonly root = new Container();
private readonly backdrop = new Container();
private readonly layer = new Container();
private readonly disposer = new LaggedDisposer();
constructor(skin: PixiDecideSkin) {
this.skin = skin;
this.root.addChild(this.backdrop, this.layer);
}
build(frame: BeMusicDecideFrame): Container {
this.disposer.cycle(this.layer);
this.skin.render({ ...frame, layer: this.layer, backdrop: this.backdrop });
return this.root;
}
destroy(): void {
this.disposer.flush();
this.root.destroy({ children: true });
}
}
|