@cazala/party 0.1.0-next.43.f9fb257 → 0.1.0-next.46.59ce407

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -399,6 +399,33 @@ class GPUResources {
399
399
  stagingBuffer.destroy();
400
400
  }
401
401
  }
402
+ /**
403
+ * Read an arbitrary GPUBuffer (storage/uniform) into an ArrayBuffer.
404
+ * Caller is responsible for interpreting the bytes.
405
+ */
406
+ async readBuffer(buffer, sizeBytes) {
407
+ const bytes = Math.max(0, Math.floor(sizeBytes));
408
+ if (bytes === 0)
409
+ return new ArrayBuffer(0);
410
+ const stagingBuffer = this.getDevice().createBuffer({
411
+ size: bytes,
412
+ usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
413
+ });
414
+ try {
415
+ const encoder = this.getDevice().createCommandEncoder();
416
+ encoder.copyBufferToBuffer(buffer, 0, stagingBuffer, 0, bytes);
417
+ this.getDevice().queue.submit([encoder.finish()]);
418
+ await this.getDevice().queue.onSubmittedWorkDone();
419
+ await stagingBuffer.mapAsync(GPUMapMode.READ);
420
+ const mapped = stagingBuffer.getMappedRange();
421
+ const out = mapped.slice(0);
422
+ stagingBuffer.unmap();
423
+ return out;
424
+ }
425
+ finally {
426
+ stagingBuffer.destroy();
427
+ }
428
+ }
402
429
  createModuleUniformBuffers(layouts) {
403
430
  // Destroy old
404
431
  this.moduleUniformBuffers.forEach(({ buffer }) => buffer.destroy());
@@ -1160,9 +1187,22 @@ class ParticleStore {
1160
1187
  }
1161
1188
  addParticle(p) {
1162
1189
  if (this.count >= this.maxParticles)
1163
- return;
1164
- this.writeAtIndex(this.count, p);
1190
+ return -1;
1191
+ const index = this.count;
1192
+ this.writeAtIndex(index, p);
1165
1193
  this.count++;
1194
+ return index;
1195
+ }
1196
+ setParticle(index, p) {
1197
+ if (index < 0 || index >= this.count)
1198
+ return;
1199
+ this.writeAtIndex(index, p);
1200
+ }
1201
+ setParticleMass(index, mass) {
1202
+ if (index < 0 || index >= this.count)
1203
+ return;
1204
+ const base = index * this.floatsPerParticle;
1205
+ this.data[base + 7] = mass;
1166
1206
  }
1167
1207
  clear() {
1168
1208
  this.count = 0;
@@ -1192,6 +1232,28 @@ class ParticleStore {
1192
1232
  },
1193
1233
  };
1194
1234
  }
1235
+ getFloatsPerParticle() {
1236
+ return this.floatsPerParticle;
1237
+ }
1238
+ /**
1239
+ * Write a full particle record at index into GPU storage.
1240
+ */
1241
+ syncParticleToGPU(resources, index) {
1242
+ if (index < 0 || index >= this.count)
1243
+ return;
1244
+ const base = index * this.floatsPerParticle;
1245
+ const slice = this.data.subarray(base, base + this.floatsPerParticle);
1246
+ resources.writeParticleSlice(base, slice);
1247
+ }
1248
+ /**
1249
+ * Write a single mass value at index into GPU storage.
1250
+ */
1251
+ syncParticleMassToGPU(resources, index) {
1252
+ if (index < 0 || index >= this.count)
1253
+ return;
1254
+ const offset = index * this.floatsPerParticle + 7;
1255
+ resources.writeParticleSlice(offset, new Float32Array([this.data[offset]]));
1256
+ }
1195
1257
  /**
1196
1258
  * Writes the currently active particle slice to the GPU particle buffer.
1197
1259
  * Assumes the GPU storage buffer has already been created with matching capacity.
@@ -3437,6 +3499,219 @@ class RenderPipeline {
3437
3499
  }
3438
3500
  }
3439
3501
 
3502
+ /**
3503
+ * LocalQuery
3504
+ *
3505
+ * A small WebGPU compute pipeline used to query a bounded set of particles
3506
+ * in a region without doing a full GPU→CPU readback of the entire particle buffer.
3507
+ *
3508
+ * This is used by tool-like features (brush/pin/remove) that need local occupancy.
3509
+ */
3510
+ class LocalQuery {
3511
+ constructor() {
3512
+ Object.defineProperty(this, "pipeline", {
3513
+ enumerable: true,
3514
+ configurable: true,
3515
+ writable: true,
3516
+ value: null
3517
+ });
3518
+ Object.defineProperty(this, "uniform", {
3519
+ enumerable: true,
3520
+ configurable: true,
3521
+ writable: true,
3522
+ value: null
3523
+ });
3524
+ Object.defineProperty(this, "count", {
3525
+ enumerable: true,
3526
+ configurable: true,
3527
+ writable: true,
3528
+ value: null
3529
+ });
3530
+ Object.defineProperty(this, "out", {
3531
+ enumerable: true,
3532
+ configurable: true,
3533
+ writable: true,
3534
+ value: null
3535
+ });
3536
+ Object.defineProperty(this, "outIdx", {
3537
+ enumerable: true,
3538
+ configurable: true,
3539
+ writable: true,
3540
+ value: null
3541
+ });
3542
+ Object.defineProperty(this, "capacity", {
3543
+ enumerable: true,
3544
+ configurable: true,
3545
+ writable: true,
3546
+ value: 0
3547
+ });
3548
+ Object.defineProperty(this, "device", {
3549
+ enumerable: true,
3550
+ configurable: true,
3551
+ writable: true,
3552
+ value: null
3553
+ });
3554
+ }
3555
+ dispose() {
3556
+ this.pipeline = null;
3557
+ this.uniform?.destroy();
3558
+ this.count?.destroy();
3559
+ this.out?.destroy();
3560
+ this.outIdx?.destroy();
3561
+ this.uniform = null;
3562
+ this.count = null;
3563
+ this.out = null;
3564
+ this.outIdx = null;
3565
+ this.capacity = 0;
3566
+ this.device = null;
3567
+ }
3568
+ ensure(resources, maxResults) {
3569
+ const device = resources.getDevice();
3570
+ // If device changed (runtime toggle / recreate), rebuild everything.
3571
+ if (this.device && this.device !== device) {
3572
+ this.dispose();
3573
+ }
3574
+ this.device = device;
3575
+ // (Re)allocate buffers if capacity changed
3576
+ if (this.capacity !== maxResults) {
3577
+ this.capacity = maxResults;
3578
+ this.uniform?.destroy();
3579
+ this.count?.destroy();
3580
+ this.out?.destroy();
3581
+ this.outIdx?.destroy();
3582
+ this.uniform = device.createBuffer({
3583
+ size: 8 * 4,
3584
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
3585
+ });
3586
+ this.count = device.createBuffer({
3587
+ size: 4,
3588
+ usage: GPUBufferUsage.STORAGE |
3589
+ GPUBufferUsage.COPY_DST |
3590
+ GPUBufferUsage.COPY_SRC,
3591
+ });
3592
+ this.out = device.createBuffer({
3593
+ size: maxResults * 16,
3594
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC,
3595
+ });
3596
+ this.outIdx = device.createBuffer({
3597
+ size: maxResults * 4,
3598
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC,
3599
+ });
3600
+ }
3601
+ if (this.pipeline)
3602
+ return;
3603
+ const code = `
3604
+ struct Particle {
3605
+ position: vec2<f32>,
3606
+ velocity: vec2<f32>,
3607
+ acceleration: vec2<f32>,
3608
+ size: f32,
3609
+ mass: f32,
3610
+ color: vec4<f32>,
3611
+ };
3612
+
3613
+ struct QueryUniforms {
3614
+ v0: vec4<f32>, // center.x, center.y, radius, maxResults (as f32)
3615
+ v1: vec4<f32>, // particleCount, 0,0,0
3616
+ };
3617
+
3618
+ @group(0) @binding(0) var<storage, read> particles: array<Particle>;
3619
+ @group(0) @binding(1) var<uniform> query: QueryUniforms;
3620
+ @group(0) @binding(2) var<storage, read_write> outCount: atomic<u32>;
3621
+ @group(0) @binding(3) var<storage, read_write> outData: array<vec4<f32>>;
3622
+ @group(0) @binding(4) var<storage, read_write> outIndex: array<u32>;
3623
+
3624
+ @compute @workgroup_size(256)
3625
+ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
3626
+ let i = gid.x;
3627
+ let count = u32(query.v1.x);
3628
+ if (i >= count) { return; }
3629
+
3630
+ let p = particles[i];
3631
+ if (p.mass == 0.0) { return; }
3632
+
3633
+ let cx = query.v0.x;
3634
+ let cy = query.v0.y;
3635
+ let radius = query.v0.z;
3636
+ let maxResults = u32(query.v0.w);
3637
+
3638
+ // Disc-intersection semantics: dist <= radius + p.size
3639
+ let dx = p.position.x - cx;
3640
+ let dy = p.position.y - cy;
3641
+ let rr = radius + p.size;
3642
+ if (dx * dx + dy * dy > rr * rr) { return; }
3643
+
3644
+ let outIdx = atomicAdd(&outCount, 1u);
3645
+ if (outIdx >= maxResults) { return; }
3646
+ outData[outIdx] = vec4<f32>(p.position.x, p.position.y, p.size, p.mass);
3647
+ outIndex[outIdx] = i;
3648
+ }
3649
+ `;
3650
+ this.pipeline = device.createComputePipeline({
3651
+ layout: "auto",
3652
+ compute: { module: device.createShaderModule({ code }), entryPoint: "main" },
3653
+ });
3654
+ }
3655
+ async getParticlesInRadius(resources, center, radius, particleCount, opts) {
3656
+ const maxResults = Math.max(1, Math.floor(opts?.maxResults ?? 20000));
3657
+ this.ensure(resources, maxResults);
3658
+ const device = resources.getDevice();
3659
+ const particleBuffer = resources.getParticleBuffer();
3660
+ if (!particleBuffer)
3661
+ return { particles: [], truncated: false };
3662
+ // Reset atomic counter
3663
+ device.queue.writeBuffer(this.count, 0, new Uint32Array([0]));
3664
+ // Uniform: v0 = [cx, cy, radius, maxResults], v1 = [particleCount, 0, 0, 0]
3665
+ const u = new Float32Array(8);
3666
+ u[0] = center.x;
3667
+ u[1] = center.y;
3668
+ u[2] = radius;
3669
+ u[3] = maxResults;
3670
+ u[4] = particleCount;
3671
+ device.queue.writeBuffer(this.uniform, 0, u);
3672
+ const bindGroup = device.createBindGroup({
3673
+ layout: this.pipeline.getBindGroupLayout(0),
3674
+ entries: [
3675
+ { binding: 0, resource: { buffer: particleBuffer } },
3676
+ { binding: 1, resource: { buffer: this.uniform } },
3677
+ { binding: 2, resource: { buffer: this.count } },
3678
+ { binding: 3, resource: { buffer: this.out } },
3679
+ { binding: 4, resource: { buffer: this.outIdx } },
3680
+ ],
3681
+ });
3682
+ const encoder = device.createCommandEncoder();
3683
+ const pass = encoder.beginComputePass();
3684
+ pass.setPipeline(this.pipeline);
3685
+ pass.setBindGroup(0, bindGroup);
3686
+ const wg = 256;
3687
+ const n = Math.max(0, Math.floor(particleCount));
3688
+ pass.dispatchWorkgroups(Math.ceil(n / wg));
3689
+ pass.end();
3690
+ device.queue.submit([encoder.finish()]);
3691
+ const countBuf = await resources.readBuffer(this.count, 4);
3692
+ const found = new Uint32Array(countBuf)[0] ?? 0;
3693
+ const truncated = found > maxResults;
3694
+ const readCount = Math.min(found, maxResults);
3695
+ if (readCount === 0)
3696
+ return { particles: [], truncated };
3697
+ const idxBuf = await resources.readBuffer(this.outIdx, readCount * 4);
3698
+ const indices = new Uint32Array(idxBuf);
3699
+ const outBuf = await resources.readBuffer(this.out, readCount * 16);
3700
+ const outFloats = new Float32Array(outBuf);
3701
+ const particles = [];
3702
+ for (let i = 0; i < readCount; i++) {
3703
+ const base = i * 4;
3704
+ particles.push({
3705
+ index: indices[i] ?? 0,
3706
+ position: { x: outFloats[base + 0], y: outFloats[base + 1] },
3707
+ size: outFloats[base + 2],
3708
+ mass: outFloats[base + 3],
3709
+ });
3710
+ }
3711
+ return { particles, truncated };
3712
+ }
3713
+ }
3714
+
3440
3715
  class WebGPUEngine extends AbstractEngine {
3441
3716
  constructor(options) {
3442
3717
  super({
@@ -3515,6 +3790,12 @@ class WebGPUEngine extends AbstractEngine {
3515
3790
  writable: true,
3516
3791
  value: false
3517
3792
  });
3793
+ Object.defineProperty(this, "localQuery", {
3794
+ enumerable: true,
3795
+ configurable: true,
3796
+ writable: true,
3797
+ value: void 0
3798
+ });
3518
3799
  Object.defineProperty(this, "animate", {
3519
3800
  enumerable: true,
3520
3801
  configurable: true,
@@ -3584,6 +3865,7 @@ class WebGPUEngine extends AbstractEngine {
3584
3865
  this.sim = new SimulationPipeline();
3585
3866
  this.render = new RenderPipeline();
3586
3867
  this.grid = new SpacialGrid(this.cellSize);
3868
+ this.localQuery = new LocalQuery();
3587
3869
  }
3588
3870
  async initialize() {
3589
3871
  await this.resources.initialize();
@@ -3640,6 +3922,7 @@ class WebGPUEngine extends AbstractEngine {
3640
3922
  cancelAnimationFrame(this.animationId);
3641
3923
  this.animationId = null;
3642
3924
  }
3925
+ this.localQuery.dispose();
3643
3926
  await this.resources.dispose();
3644
3927
  }
3645
3928
  // Override setSize to also update WebGPU-specific resources
@@ -3663,12 +3946,24 @@ class WebGPUEngine extends AbstractEngine {
3663
3946
  this.updateMaxSize(particle.size);
3664
3947
  }
3665
3948
  }
3666
- async addParticle(p) {
3667
- await this.particles.syncFromGPU(this.resources);
3668
- this.particles.addParticle(p);
3669
- this.particles.syncToGPU(this.resources);
3949
+ addParticle(p) {
3950
+ const index = this.particles.addParticle(p);
3951
+ if (index < 0)
3952
+ return -1;
3953
+ // Push only the new particle record to GPU (no full-scene readback).
3954
+ this.particles.syncParticleToGPU(this.resources, index);
3670
3955
  // Update maxSize tracking
3671
3956
  this.updateMaxSize(p.size);
3957
+ return index;
3958
+ }
3959
+ setParticle(index, p) {
3960
+ this.particles.setParticle(index, p);
3961
+ this.particles.syncParticleToGPU(this.resources, index);
3962
+ this.updateMaxSize(p.size);
3963
+ }
3964
+ setParticleMass(index, mass) {
3965
+ this.particles.setParticleMass(index, mass);
3966
+ this.particles.syncParticleMassToGPU(this.resources, index);
3672
3967
  }
3673
3968
  /**
3674
3969
  * Forces GPU-to-CPU synchronization and returns current particle data.
@@ -3682,6 +3977,9 @@ class WebGPUEngine extends AbstractEngine {
3682
3977
  await this.particles.syncFromGPU(this.resources);
3683
3978
  return this.particles.getParticle(index);
3684
3979
  }
3980
+ async getParticlesInRadius(center, radius, opts) {
3981
+ return await this.localQuery.getParticlesInRadius(this.resources, center, radius, this.getCount(), opts);
3982
+ }
3685
3983
  getCount() {
3686
3984
  const actualCount = this.particles.getCount();
3687
3985
  if (this.maxParticles === null) {
@@ -4298,6 +4596,12 @@ class CPUEngine extends AbstractEngine {
4298
4596
  writable: true,
4299
4597
  value: null
4300
4598
  });
4599
+ Object.defineProperty(this, "particleIdToIndex", {
4600
+ enumerable: true,
4601
+ configurable: true,
4602
+ writable: true,
4603
+ value: new Map()
4604
+ });
4301
4605
  Object.defineProperty(this, "animate", {
4302
4606
  enumerable: true,
4303
4607
  configurable: true,
@@ -4372,6 +4676,7 @@ class CPUEngine extends AbstractEngine {
4372
4676
  this.fpsEstimate = 60;
4373
4677
  // Reset maxSize tracking
4374
4678
  this.resetMaxSize();
4679
+ this.particleIdToIndex.clear();
4375
4680
  }
4376
4681
  getCount() {
4377
4682
  const actualCount = this.particles.length;
@@ -4395,11 +4700,33 @@ class CPUEngine extends AbstractEngine {
4395
4700
  for (const p of particle) {
4396
4701
  this.updateMaxSize(p.size);
4397
4702
  }
4703
+ this.particleIdToIndex.clear();
4398
4704
  }
4399
4705
  addParticle(particle) {
4706
+ const index = this.particles.length;
4400
4707
  this.particles.push(new Particle(particle));
4401
4708
  // Update maxSize tracking
4402
4709
  this.updateMaxSize(particle.size);
4710
+ const created = this.particles[index];
4711
+ if (created)
4712
+ this.particleIdToIndex.set(created.id, index);
4713
+ return index;
4714
+ }
4715
+ setParticle(index, p) {
4716
+ if (index < 0)
4717
+ return;
4718
+ if (index >= this.particles.length)
4719
+ return;
4720
+ this.particles[index] = new Particle(p);
4721
+ // Best-effort maxSize tracking (monotonic)
4722
+ this.updateMaxSize(p.size);
4723
+ }
4724
+ setParticleMass(index, mass) {
4725
+ if (index < 0)
4726
+ return;
4727
+ if (index >= this.particles.length)
4728
+ return;
4729
+ this.particles[index].mass = mass;
4403
4730
  }
4404
4731
  getParticles() {
4405
4732
  return Promise.resolve(this.particles.map((p) => p.toJSON()));
@@ -4407,10 +4734,47 @@ class CPUEngine extends AbstractEngine {
4407
4734
  getParticle(index) {
4408
4735
  return Promise.resolve(this.particles[index]);
4409
4736
  }
4737
+ async getParticlesInRadius(center, radius, opts) {
4738
+ const maxResults = Math.max(1, Math.floor(opts?.maxResults ?? 20000));
4739
+ // Expand search radius to ensure we can find large particles whose discs
4740
+ // intersect the query circle: dist <= radius + p.size.
4741
+ const searchRadius = Math.max(0, radius) + this.getMaxSize();
4742
+ // Use the existing spatial grid (built during the last simulation tick).
4743
+ // Snapshot semantics: this is "as of last grid build" which is good enough
4744
+ // for tool usage and avoids global scans.
4745
+ const neighbors = this.grid.getParticles(new Vector(center.x, center.y), searchRadius,
4746
+ // Ask for up to maxResults+1 so we can mark truncated more reliably.
4747
+ maxResults + 1);
4748
+ const out = [];
4749
+ const r = Math.max(0, radius);
4750
+ for (const p of neighbors) {
4751
+ if (p.mass === 0)
4752
+ continue;
4753
+ const index = this.particleIdToIndex.get(p.id);
4754
+ if (index === undefined)
4755
+ continue;
4756
+ const dx = p.position.x - center.x;
4757
+ const dy = p.position.y - center.y;
4758
+ const rr = r + p.size;
4759
+ if (dx * dx + dy * dy <= rr * rr) {
4760
+ out.push({
4761
+ index,
4762
+ position: { x: p.position.x, y: p.position.y },
4763
+ size: p.size,
4764
+ mass: p.mass,
4765
+ });
4766
+ if (out.length >= maxResults + 1)
4767
+ break;
4768
+ }
4769
+ }
4770
+ const truncated = out.length > maxResults;
4771
+ return { particles: truncated ? out.slice(0, maxResults) : out, truncated };
4772
+ }
4410
4773
  destroy() {
4411
4774
  this.pause();
4412
4775
  this.particles = [];
4413
4776
  this.grid.clear();
4777
+ this.particleIdToIndex.clear();
4414
4778
  return Promise.resolve();
4415
4779
  }
4416
4780
  // Handle configuration changes
@@ -4457,8 +4821,10 @@ class CPUEngine extends AbstractEngine {
4457
4821
  // Update spatial grid with current particle positions and camera
4458
4822
  this.grid.setCamera(this.view.getCamera().x, this.view.getCamera().y, this.view.getZoom());
4459
4823
  this.grid.clear();
4824
+ this.particleIdToIndex.clear();
4460
4825
  for (let i = 0; i < effectiveCount; i++) {
4461
4826
  this.grid.insert(this.particles[i]);
4827
+ this.particleIdToIndex.set(this.particles[i].id, i);
4462
4828
  }
4463
4829
  // Global state for modules that need it
4464
4830
  const globalState = {};
@@ -4948,7 +5314,13 @@ class Engine {
4948
5314
  this.engine.setParticles(p);
4949
5315
  }
4950
5316
  addParticle(p) {
4951
- this.engine.addParticle(p);
5317
+ return this.engine.addParticle(p);
5318
+ }
5319
+ setParticle(index, p) {
5320
+ this.engine.setParticle(index, p);
5321
+ }
5322
+ setParticleMass(index, mass) {
5323
+ this.engine.setParticleMass(index, mass);
4952
5324
  }
4953
5325
  getParticles() {
4954
5326
  return this.engine.getParticles();
@@ -4956,6 +5328,9 @@ class Engine {
4956
5328
  getParticle(index) {
4957
5329
  return this.engine.getParticle(index);
4958
5330
  }
5331
+ getParticlesInRadius(center, radius, opts) {
5332
+ return this.engine.getParticlesInRadius(center, radius, opts);
5333
+ }
4959
5334
  // Helpers for pinning/unpinning
4960
5335
  async pinParticles(indexes) {
4961
5336
  const particles = await this.getParticles();
@@ -5720,15 +6095,11 @@ class Boundary extends Module {
5720
6095
  webgpu() {
5721
6096
  return {
5722
6097
  apply: ({ particleVar, getUniform }) => `{
5723
- // Bounce using global grid extents
5724
6098
  let halfSize = ${particleVar}.size;
5725
6099
  let minX = GRID_MINX();
5726
6100
  let maxX = GRID_MAXX();
5727
6101
  let minY = GRID_MINY();
5728
6102
  let maxY = GRID_MAXY();
5729
- let bounce = ${getUniform("restitution")};
5730
- let friction = ${getUniform("friction")};
5731
- let mode = ${getUniform("mode")};
5732
6103
  let repelDist = ${getUniform("repelDistance")};
5733
6104
  let repelStrength = ${getUniform("repelStrength")};
5734
6105
 
@@ -5773,34 +6144,54 @@ class Boundary extends Module {
5773
6144
  }
5774
6145
  ${particleVar}.acceleration += vec2<f32>(fx, fy);
5775
6146
  }
6147
+ }`,
6148
+ correct: ({ particleVar, prevPosVar, postPosVar, getUniform }) => `{
6149
+ // Resolve boundaries post-integration to reduce dt-dependent jitter.
6150
+ // We use prev/post integration positions (SIM_STATE) to detect crossings.
6151
+ let halfSize = ${particleVar}.size;
6152
+ let minX = GRID_MINX();
6153
+ let maxX = GRID_MAXX();
6154
+ let minY = GRID_MINY();
6155
+ let maxY = GRID_MAXY();
6156
+ let bounce = ${getUniform("restitution")};
6157
+ let friction = ${getUniform("friction")};
6158
+ let mode = ${getUniform("mode")};
6159
+
6160
+ let prevPos = ${prevPosVar};
6161
+ let postPos = ${postPosVar};
6162
+ var vel = ${particleVar}.velocity;
5776
6163
 
5777
6164
  if (mode == 0.0) {
5778
- // bounce
5779
- // X axis
5780
- if (${particleVar}.position.x - halfSize < minX) {
6165
+ // bounce (CCD-ish using prev/post)
6166
+ // Left wall
6167
+ if (postPos.x - halfSize < minX) {
5781
6168
  ${particleVar}.position.x = minX + halfSize;
5782
- ${particleVar}.velocity.x = -${particleVar}.velocity.x * bounce;
5783
- ${particleVar}.velocity.y *= max(0.0, 1.0 - friction);
5784
- } else if (${particleVar}.position.x + halfSize > maxX) {
6169
+ if (vel.x < 0.0) { vel.x = -vel.x * bounce; }
6170
+ vel.y = vel.y * max(0.0, 1.0 - friction);
6171
+ }
6172
+ // Right wall
6173
+ else if (postPos.x + halfSize > maxX) {
5785
6174
  ${particleVar}.position.x = maxX - halfSize;
5786
- ${particleVar}.velocity.x = -${particleVar}.velocity.x * bounce;
5787
- ${particleVar}.velocity.y *= max(0.0, 1.0 - friction);
6175
+ if (vel.x > 0.0) { vel.x = -vel.x * bounce; }
6176
+ vel.y = vel.y * max(0.0, 1.0 - friction);
5788
6177
  }
5789
6178
 
5790
- // Y axis
5791
- if (${particleVar}.position.y - halfSize < minY) {
6179
+ // Top
6180
+ if (postPos.y - halfSize < minY) {
5792
6181
  ${particleVar}.position.y = minY + halfSize;
5793
- ${particleVar}.velocity.y = -${particleVar}.velocity.y * bounce;
5794
- ${particleVar}.velocity.x *= max(0.0, 1.0 - friction);
5795
- } else if (${particleVar}.position.y + halfSize > maxY) {
6182
+ if (vel.y < 0.0) { vel.y = -vel.y * bounce; }
6183
+ vel.x = vel.x * max(0.0, 1.0 - friction);
6184
+ }
6185
+ // Bottom (floor)
6186
+ else if (postPos.y + halfSize > maxY) {
5796
6187
  ${particleVar}.position.y = maxY - halfSize;
5797
- ${particleVar}.velocity.y = -${particleVar}.velocity.y * bounce;
5798
- ${particleVar}.velocity.x *= max(0.0, 1.0 - friction);
6188
+ if (vel.y > 0.0) { vel.y = -vel.y * bounce; }
6189
+ vel.x = vel.x * max(0.0, 1.0 - friction);
5799
6190
  }
6191
+ ${particleVar}.velocity = vel;
5800
6192
  } else if (mode == 1.0) {
5801
- // warp
5802
- // Only warp once the particle is fully outside the bounds
5803
- let eps = 1.0; // spawn just outside the opposite edge so it slides in
6193
+ // warp (post-integration)
6194
+ let eps = 1.0;
5804
6195
  if (${particleVar}.position.x + halfSize < minX) {
5805
6196
  ${particleVar}.position.x = maxX + halfSize + eps;
5806
6197
  } else if (${particleVar}.position.x - halfSize > maxX) {
@@ -5812,24 +6203,15 @@ class Boundary extends Module {
5812
6203
  ${particleVar}.position.y = minY - halfSize - eps;
5813
6204
  }
5814
6205
  } else if (mode == 2.0) {
5815
- // kill
5816
- // Only kill once the particle is fully outside the bounds
5817
- if (${particleVar}.position.x - halfSize < minX) {
5818
- ${particleVar}.position.x = minX - halfSize * 4;
5819
- ${particleVar}.mass = 0.0;
5820
- } else if (${particleVar}.position.x + halfSize > maxX) {
5821
- ${particleVar}.position.x = maxX + halfSize * 4;
6206
+ // kill (post-integration)
6207
+ if (${particleVar}.position.x + halfSize < minX ||
6208
+ ${particleVar}.position.x - halfSize > maxX ||
6209
+ ${particleVar}.position.y + halfSize < minY ||
6210
+ ${particleVar}.position.y - halfSize > maxY) {
5822
6211
  ${particleVar}.mass = 0.0;
5823
6212
  }
5824
- if (${particleVar}.position.y - halfSize < minY) {
5825
- ${particleVar}.position.y = minY - halfSize * 4;
5826
- ${particleVar}.mass = 0.0;
5827
- } else if (${particleVar}.position.y + halfSize > maxY) {
5828
- ${particleVar}.position.y = maxY + halfSize * 4;
5829
- ${particleVar}.mass = 0.0;
5830
- }
5831
6213
  } else if (mode == 3.0) {
5832
- // none: no boundary constraints; repel force above still applies
6214
+ // none
5833
6215
  }
5834
6216
  }`,
5835
6217
  };
@@ -5848,9 +6230,6 @@ class Boundary extends Module {
5848
6230
  const minY = camera.y - halfH;
5849
6231
  const maxY = camera.y + halfH;
5850
6232
  const halfSize = particle.size;
5851
- const bounce = input.restitution;
5852
- const friction = input.friction;
5853
- const mode = input.mode;
5854
6233
  const repelDist = input.repelDistance;
5855
6234
  const repelStrength = input.repelStrength;
5856
6235
  // Repel force applied for all modes
@@ -5901,35 +6280,52 @@ class Boundary extends Module {
5901
6280
  }
5902
6281
  particle.acceleration.add(new Vector(fx, fy));
5903
6282
  }
6283
+ },
6284
+ correct: ({ particle, input, view }) => {
6285
+ // Resolve boundaries post-integration to reduce dt-dependent jitter.
6286
+ const camera = view.getCamera();
6287
+ const zoom = Math.max(view.getZoom(), 0.0001);
6288
+ const size = view.getSize();
6289
+ const halfW = size.width / (2 * zoom);
6290
+ const halfH = size.height / (2 * zoom);
6291
+ const minX = camera.x - halfW;
6292
+ const maxX = camera.x + halfW;
6293
+ const minY = camera.y - halfH;
6294
+ const maxY = camera.y + halfH;
6295
+ const halfSize = particle.size;
6296
+ const bounce = input.restitution;
6297
+ const friction = input.friction;
6298
+ const mode = input.mode;
5904
6299
  if (mode === 0) {
5905
6300
  // bounce
5906
- // X axis
5907
6301
  if (particle.position.x - halfSize < minX) {
5908
6302
  particle.position.x = minX + halfSize;
5909
- particle.velocity.x = -particle.velocity.x * bounce;
6303
+ if (particle.velocity.x < 0)
6304
+ particle.velocity.x = -particle.velocity.x * bounce;
5910
6305
  particle.velocity.y *= Math.max(0, 1 - friction);
5911
6306
  }
5912
6307
  else if (particle.position.x + halfSize > maxX) {
5913
6308
  particle.position.x = maxX - halfSize;
5914
- particle.velocity.x = -particle.velocity.x * bounce;
6309
+ if (particle.velocity.x > 0)
6310
+ particle.velocity.x = -particle.velocity.x * bounce;
5915
6311
  particle.velocity.y *= Math.max(0, 1 - friction);
5916
6312
  }
5917
- // Y axis
5918
6313
  if (particle.position.y - halfSize < minY) {
5919
6314
  particle.position.y = minY + halfSize;
5920
- particle.velocity.y = -particle.velocity.y * bounce;
6315
+ if (particle.velocity.y < 0)
6316
+ particle.velocity.y = -particle.velocity.y * bounce;
5921
6317
  particle.velocity.x *= Math.max(0, 1 - friction);
5922
6318
  }
5923
6319
  else if (particle.position.y + halfSize > maxY) {
5924
6320
  particle.position.y = maxY - halfSize;
5925
- particle.velocity.y = -particle.velocity.y * bounce;
6321
+ if (particle.velocity.y > 0)
6322
+ particle.velocity.y = -particle.velocity.y * bounce;
5926
6323
  particle.velocity.x *= Math.max(0, 1 - friction);
5927
6324
  }
5928
6325
  }
5929
6326
  else if (mode === 1) {
5930
6327
  // warp
5931
- // Only warp once the particle is fully outside the bounds
5932
- const eps = 1; // spawn just outside the opposite edge so it slides in
6328
+ const eps = 1;
5933
6329
  if (particle.position.x + halfSize < minX) {
5934
6330
  particle.position.x = maxX + halfSize + eps;
5935
6331
  }
@@ -5945,7 +6341,6 @@ class Boundary extends Module {
5945
6341
  }
5946
6342
  else if (mode === 2) {
5947
6343
  // kill
5948
- // Only kill once the particle is fully outside the bounds
5949
6344
  if (particle.position.x + halfSize < minX ||
5950
6345
  particle.position.x - halfSize > maxX ||
5951
6346
  particle.position.y + halfSize < minY ||
@@ -5953,7 +6348,6 @@ class Boundary extends Module {
5953
6348
  particle.mass = 0;
5954
6349
  }
5955
6350
  }
5956
- else ;
5957
6351
  },
5958
6352
  };
5959
6353
  }