All files / player-browser/src/skins/pixi-kit particle-layer.ts

99.03% Statements 103/104
96% Branches 24/25
100% Functions 16/16
100% Lines 96/96

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              48x     48x     48x 48x     48x   48x                                                         48x                       48x                                                                                                                       9x               9x                                         9x 9x 9x 9x             9x 9x 9x 9x 9x 9x 9x               9x 9x 9x 9x                     9x 9x 9x   9x 6x   9x 6x 6x 6x           8x         9x 9x         1008x 1008x 1008x 1008x 1008x 1008x                           1x 1x 1x 2x 2x 2x 2x                             2x 2x 2x 514x 514x 514x 514x 514x         1011x 8x 8x 8x   1011x 1011x 1011x 1011x 1011x       3x 3x 3x 3x               8x 8x 8x 8x 8x 5x   8x 8x         5x 5x   5x 5x 5x                 1021x 1021x     48x               13x 13x 8x 8x   13x 13x 9x 9x   13x    
import { Buffer, BufferUsage, Geometry, GlProgram, GpuProgram, Mesh, Shader, Texture, UniformGroup } from 'pixi.js';
import type { Container, Graphics } from 'pixi.js';
 
/**
 * Floats a point takes in a {@link PointLayer}'s instance buffer: `x, y, half size`, then the packed colour's bits (read
 * as `unorm8x4`, the way Pixi's particles read theirs).
 */
export const POINT_STRIDE = 4;
 
/** Points a layer has room for before its first growth. */
const INITIAL_POINTS = 256;
 
/** One ±1 quad every layer draws once per point, with the per-point data stepping by instance. */
const CORNERS = new Float32Array([-1, -1, 1, -1, 1, 1, -1, 1]);
const QUAD_INDICES = new Uint16Array([0, 1, 2, 0, 2, 3]);
 
/** Narrowest a point is drawn, in device pixels; a smaller one grows to it and fades by the area it gained. */
const MIN_POINT_PIXELS = '1.0';
 
const POINT_VERTEX_GLSL = /* glsl */ `#version 300 es
const float MIN_POINT_PIXELS = ${MIN_POINT_PIXELS};
in vec2 aPosition;
in vec3 aPoint;
in vec4 aColor;
uniform mat3 uProjectionMatrix;
uniform mat3 uWorldTransformMatrix;
uniform vec4 uWorldColorAlpha;
uniform vec2 uResolution;
uniform mat3 uTransformMatrix;
uniform vec4 uColor;
uniform vec4 uFrame;
out vec2 vUV;
out vec4 vColor;
 
void main() {
  mat3 matrix = uProjectionMatrix * uWorldTransformMatrix * uTransformMatrix;
  // A point under a device pixel across grows to one pixel and fades by the area it gained, so it gives off the same
  // light without dropping out between pixel centres (the player's skin texture is not multisampled).
  float side = aPoint.z * length(matrix[0].xy) * uResolution.x;
  float grown = max(side, MIN_POINT_PIXELS);
  float coverage = side * side / (grown * grown);
  vec2 world = aPoint.xy + aPosition * aPoint.z * (grown / max(side, 1e-6));
  gl_Position = vec4((matrix * vec3(world, 1.0)).xy, 0.0, 1.0);
  vUV = mix(uFrame.xy, uFrame.zw, aPosition * 0.5 + 0.5);
  vColor = vec4(aColor.rgb * aColor.a, aColor.a) * uColor * uWorldColorAlpha * coverage;
}
`;
 
const POINT_FRAGMENT_GLSL = /* glsl */ `#version 300 es
precision mediump float;
in vec2 vUV;
in vec4 vColor;
uniform sampler2D uTexture;
out vec4 finalColor;
 
void main() {
  finalColor = texture(uTexture, vUV) * vColor;
}
`;
 
const POINT_WGSL = /* wgsl */ `
const MIN_POINT_PIXELS: f32 = ${MIN_POINT_PIXELS};
struct GlobalUniforms {
  uProjectionMatrix: mat3x3<f32>,
  uWorldTransformMatrix: mat3x3<f32>,
  uWorldColorAlpha: vec4<f32>,
  uResolution: vec2<f32>,
}
 
struct LocalUniforms {
  uTransformMatrix: mat3x3<f32>,
  uColor: vec4<f32>,
  uRound: f32,
}
 
struct PointUniforms {
  uFrame: vec4<f32>,
}
 
@group(0) @binding(0) var<uniform> globalUniforms: GlobalUniforms;
@group(1) @binding(0) var<uniform> localUniforms: LocalUniforms;
@group(2) @binding(0) var uTexture: texture_2d<f32>;
@group(2) @binding(1) var uSampler: sampler;
@group(2) @binding(2) var<uniform> pointUniforms: PointUniforms;
 
struct PointOut {
  @builtin(position) position: vec4<f32>,
  @location(0) uv: vec2<f32>,
  @location(1) color: vec4<f32>,
}
 
@vertex
fn mainVertex(
  @location(0) aPosition: vec2<f32>,
  @location(1) aPoint: vec3<f32>,
  @location(2) aColor: vec4<f32>,
) -> PointOut {
  let matrix = globalUniforms.uProjectionMatrix * globalUniforms.uWorldTransformMatrix * localUniforms.uTransformMatrix;
  let side = aPoint.z * length(matrix[0].xy) * globalUniforms.uResolution.x;
  let grown = max(side, MIN_POINT_PIXELS);
  let coverage = side * side / (grown * grown);
  let world = aPoint.xy + aPosition * aPoint.z * (grown / max(side, 1e-6));
  var out: PointOut;
  out.position = vec4<f32>((matrix * vec3<f32>(world, 1.0)).xy, 0.0, 1.0);
  out.uv = mix(pointUniforms.uFrame.xy, pointUniforms.uFrame.zw, aPosition * 0.5 + 0.5);
  out.color = vec4<f32>(aColor.rgb * aColor.a, aColor.a) * localUniforms.uColor * globalUniforms.uWorldColorAlpha * coverage;
  return out;
}
 
@fragment
fn mainFragment(@location(0) uv: vec2<f32>, @location(1) color: vec4<f32>) -> @location(0) vec4<f32> {
  return textureSample(uTexture, uSampler, uv) * color;
}
`;
 
let programs: { gl: GlProgram; gpu: GpuProgram } | undefined;
let corners: Buffer | undefined;
let quadIndices: Buffer | undefined;
 
function pointPrograms(): { gl: GlProgram; gpu: GpuProgram } {
  programs ??= {
    gl: GlProgram.from({ vertex: POINT_VERTEX_GLSL, fragment: POINT_FRAGMENT_GLSL, name: 'point-layer' }),
    gpu: GpuProgram.from({
      name: 'point-layer',
      vertex: { source: POINT_WGSL, entryPoint: 'mainVertex' },
      fragment: { source: POINT_WGSL, entryPoint: 'mainFragment' },
    }),
  };
  return programs;
}
 
/**
 * Points drawn as instanced quads instead of `Graphics` geometry.
 *
 * The built-in skins scatter thousands of tiny marks every frame (Synesthesia's floor lattice, dust, and powder — some
 * sixteen thousand a frame). As `Graphics` shapes each one is re-triangulated and re-uploaded per frame; even as Pixi
 * particles each one is four vertices rebuilt by a per-particle upload loop. Here each mark is four floats written
 * straight into the layer's instance buffer — `x, y, half size, colour` — and the buffer goes to the GPU as it is, 16
 * bytes a point, with no per-point object or upload loop. One draw per layer instances a quad over them.
 *
 * A layer rides along with an anchor `Graphics` (a pooled graphics the skin acquires in the same order every frame):
 * it sits right after the anchor in the anchor's parent and copies the anchor's blend mode, so its points paint exactly
 * where the anchor's rects would have. Each frame the points written since the last render are shown and the rest are
 * dropped, so a layer whose anchor stops receiving points simply goes empty.
 */
export class PointLayer {
  private readonly mesh: Mesh<Geometry, Shader>;
  private readonly geometry: Geometry;
  private readonly buffer: Buffer;
  private data = new Float32Array(INITIAL_POINTS * POINT_STRIDE);
  private words = new Uint32Array(this.data.buffer);
  private cursor = 0;
  private touched = false;
 
  private readonly anchor: Graphics;
  /** Position among the anchor's layers; layer `slot` sits `slot + 1` children after the anchor. */
  private readonly slot: number;
 
  public constructor(anchor: Graphics, texture: Texture = Texture.WHITE, slot = 0) {
    this.anchor = anchor;
    this.slot = slot;
    corners ??= new Buffer({ data: CORNERS, usage: BufferUsage.VERTEX });
    quadIndices ??= new Buffer({ data: QUAD_INDICES, usage: BufferUsage.INDEX });
    this.buffer = new Buffer({ data: this.data, usage: BufferUsage.VERTEX | BufferUsage.COPY_DST });
    const stride = POINT_STRIDE * 4;
    this.geometry = new Geometry({
      attributes: {
        aPosition: { buffer: corners, format: 'float32x2' },
        aPoint: { buffer: this.buffer, format: 'float32x3', stride, offset: 0, instance: true },
        aColor: { buffer: this.buffer, format: 'unorm8x4', stride, offset: 12, instance: true },
      },
      indexBuffer: quadIndices,
    });
    this.geometry.instanceCount = 0;
    const { uvs } = texture;
    const { gl, gpu } = pointPrograms();
    const shader = new Shader({
      glProgram: gl,
      gpuProgram: gpu,
      resources: {
        uTexture: texture.source,
        uSampler: texture.source.style,
        pointUniforms: new UniformGroup({
          uFrame: { value: new Float32Array([uvs.x0, uvs.y0, uvs.x2, uvs.y2]), type: 'vec4<f32>' },
        }),
      },
    });
    this.mesh = new Mesh({ geometry: this.geometry, shader, texture });
    this.mesh.label = 'point-layer';
    this.mesh.onRender = () => this.commit();
    // The mesh is a sibling, so trimming or destroying the pooled anchor does not destroy it as a child.
    anchor.once('destroyed', () => {
      if (!this.mesh.destroyed) this.mesh.destroy();
    });
    this.mesh.once('destroyed', () => {
      this.geometry.destroy();
      this.buffer.destroy();
      shader.destroy();
    });
  }
 
  /** Points written since the last render. */
  public get count(): number {
    return this.touched ? this.cursor : 0;
  }
 
  /** The point `index` written since the last render, as `[x, y, half size, packed colour]`. */
  public pointAt(index: number): [number, number, number, number] {
    const at = index * POINT_STRIDE;
    return [this.data[at]!, this.data[at + 1]!, this.data[at + 2]!, this.words[at + 3]!];
  }
 
  /** Writes one square point (or one copy of the layer's texture) `size` px wide, centred on `(cx, cy)`. */
  public point(cx: number, cy: number, size: number, color: number, alpha: number): void {
    const at = this.reserve(1);
    const data = this.data;
    data[at] = cx;
    data[at + 1] = cy;
    data[at + 2] = size * 0.5;
    this.words[at + 3] = packParticleColor(color, alpha);
  }
 
  /** Append a cluster with one colour and size, expanding its XY offset pairs directly into the upload buffer. */
  public cluster(
    cx: number,
    cy: number,
    offsets: Float32Array,
    count: number,
    spread: number,
    size: number,
    color: number,
    alpha: number,
  ): void {
    let at = this.reserve(count);
    const packed = packParticleColor(color, alpha);
    for (let i = 0; i < count; i++, at += POINT_STRIDE) {
      this.data[at] = cx + offsets[i * 2]! * spread;
      this.data[at + 1] = cy + offsets[i * 2 + 1]! * spread;
      this.data[at + 2] = size * 0.5;
      this.words[at + 3] = packed;
    }
  }
 
  /** Append evenly spaced grains from one end of a segment to the other (a single grain sits in the middle). */
  public trail(
    x: number,
    y: number,
    dx: number,
    dy: number,
    count: number,
    size: number,
    color: number,
    alpha: number,
  ): void {
    let at = this.reserve(count);
    const packed = packParticleColor(color, alpha);
    for (let i = 0; i < count; i++, at += POINT_STRIDE) {
      const t = count === 1 ? 0.5 : i / (count - 1);
      this.data[at] = x + dx * t;
      this.data[at + 1] = y + dy * t;
      this.data[at + 2] = size * 0.5;
      this.words[at + 3] = packed;
    }
  }
 
  private reserve(count: number): number {
    if (!this.touched) {
      this.attach();
      this.touched = true;
      this.cursor = 0;
    }
    const at = this.cursor * POINT_STRIDE;
    this.cursor += count;
    const required = this.cursor * POINT_STRIDE;
    if (required > this.data.length) this.grow(required);
    return at;
  }
 
  private grow(required: number): void {
    const data = new Float32Array(Math.max(required, this.data.length * 2));
    data.set(this.data);
    this.data = data;
    this.words = new Uint32Array(data.buffer);
  }
 
  /**
   * Keeps the layer right after its anchor (after the anchor's earlier layers), so the anchor's later siblings (HUD
   * panels, text) stay on top.
   */
  private attach(): void {
    const parent: Container | null = this.anchor.parent;
    Iif (!parent) return;
    if (this.mesh.parent !== parent) parent.addChild(this.mesh);
    const target = parent.getChildIndex(this.anchor) + 1 + this.slot;
    if (parent.getChildIndex(this.mesh) !== target) {
      parent.setChildIndex(this.mesh, Math.min(target, parent.children.length - 1));
    }
    this.mesh.blendMode = this.anchor.blendMode;
    this.mesh.visible = this.anchor.visible;
  }
 
  /** Runs just before the mesh renders: uploads this frame's points and drops the rest. */
  private commit(): void {
    const count = this.count;
    this.geometry.instanceCount = count;
    // Only the points shown go up; a grown array replaces the GPU buffer.
    if (count > 0) this.buffer.setDataWithSize(this.data, count * POINT_STRIDE, true);
    this.touched = false;
    this.cursor = 0;
  }
}
 
/**
 * A particle's packed `color` for an `0xRRGGBB` tint and an alpha, as Pixi's `Particle` computes it from `tint` and
 * `alpha` (BGR in the low 24 bits, the alpha byte above, added as Pixi adds them).
 */
export function packParticleColor(rgb: number, alpha: number): number {
  const bgr = ((rgb & 0xff) << 16) | (rgb & 0xff00) | ((rgb >> 16) & 0xff);
  return bgr + (((Math.min(Math.max(alpha, 0), 1) * 255) | 0) << 24);
}
 
const LAYERS = new WeakMap<Graphics, Map<Texture, PointLayer>>();
 
/**
 * The point layer riding along with `anchor` that draws `texture` (Pixi's 1x1 white by default), created on first use.
 * A layer draws a single texture, so each texture gets its own layer; an anchor's layers stack in the order they were
 * first asked for.
 */
export function pointLayerFor(anchor: Graphics, texture: Texture = Texture.WHITE): PointLayer {
  let layers = LAYERS.get(anchor);
  if (!layers) {
    layers = new Map();
    LAYERS.set(anchor, layers);
  }
  let layer = layers.get(texture);
  if (!layer) {
    layer = new PointLayer(anchor, texture, layers.size);
    layers.set(texture, layer);
  }
  return layer;
}