-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparticle-store.ts
More file actions
161 lines (142 loc) · 4.87 KB
/
particle-store.ts
File metadata and controls
161 lines (142 loc) · 4.87 KB
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
/**
* ParticleStore
*
* CPU-side SoA-like packed Float32Array for particle attributes with helpers
* to set/add/read particles and to synchronize active slices to the GPU
* storage buffer owned by GPUResources.
*
* Layout (floatsPerParticle=12):
* [pos.x, pos.y, vel.x, vel.y, ax, ay, size, mass, color.r, color.g, color.b, color.a]
*/
import { GPUResources } from "./gpu-resources";
import { IParticle } from "../../interfaces";
/**
* CPU-side particle storage and synchronization to GPU storage buffer.
*/
export class ParticleStore {
private readonly maxParticles: number;
private readonly floatsPerParticle: number;
private readonly data: Float32Array;
private count: number = 0;
constructor(maxParticles: number, floatsPerParticle = 12) {
this.maxParticles = Math.max(0, Math.floor(maxParticles));
this.floatsPerParticle = Math.max(1, Math.floor(floatsPerParticle));
this.data = new Float32Array(this.maxParticles * this.floatsPerParticle);
}
setParticles(list: IParticle[]): void {
const n = Math.min(list.length, this.maxParticles);
for (let i = 0; i < n; i++) this.writeAtIndex(i, list[i]);
this.count = n;
}
addParticle(p: IParticle): number {
if (this.count >= this.maxParticles) return -1;
const index = this.count;
this.writeAtIndex(index, p);
this.count++;
return index;
}
setParticle(index: number, p: IParticle): void {
if (index < 0 || index >= this.count) return;
this.writeAtIndex(index, p);
}
setParticleMass(index: number, mass: number): void {
if (index < 0 || index >= this.count) return;
const base = index * this.floatsPerParticle;
this.data[base + 7] = mass;
}
clear(): void {
this.count = 0;
}
getCount(): number {
return this.count;
}
getParticles(): IParticle[] {
const particles: IParticle[] = [];
for (let i = 0; i < this.count; i++) {
particles.push(this.getParticle(i));
}
return particles;
}
getParticle(index: number): IParticle {
const base = index * this.floatsPerParticle;
return {
position: { x: this.data[base + 0], y: this.data[base + 1] },
velocity: { x: this.data[base + 2], y: this.data[base + 3] },
size: this.data[base + 6],
mass: this.data[base + 7],
color: {
r: this.data[base + 8],
g: this.data[base + 9],
b: this.data[base + 10],
a: this.data[base + 11],
},
};
}
getFloatsPerParticle(): number {
return this.floatsPerParticle;
}
/**
* Write a full particle record at index into GPU storage.
*/
syncParticleToGPU(resources: GPUResources, index: number): void {
if (index < 0 || index >= this.count) return;
const base = index * this.floatsPerParticle;
const slice = this.data.subarray(base, base + this.floatsPerParticle);
resources.writeParticleSlice(base, slice);
}
/**
* Write a single mass value at index into GPU storage.
*/
syncParticleMassToGPU(
resources: GPUResources,
index: number
): void {
if (index < 0 || index >= this.count) return;
const offset = index * this.floatsPerParticle + 7;
resources.writeParticleSlice(offset, new Float32Array([this.data[offset]]));
}
/**
* Writes the currently active particle slice to the GPU particle buffer.
* Assumes the GPU storage buffer has already been created with matching capacity.
*/
syncToGPU(resources: GPUResources): void {
if (this.count === 0) return;
const slice = this.data.subarray(0, this.count * this.floatsPerParticle);
resources.writeParticleBuffer(slice);
}
/**
* Reads particle data back from GPU to CPU, updating the local data array.
* This ensures getParticles() returns current GPU simulation state.
*/
async syncFromGPU(resources: GPUResources): Promise<void> {
if (this.count === 0) return;
const sizeFloats = this.count * this.floatsPerParticle;
try {
const gpuData = await resources.readParticleBuffer(sizeFloats);
// Update our CPU-side data with fresh GPU data
this.data.set(gpuData.subarray(0, sizeFloats));
} catch (error) {
throw error;
}
}
/**
* Internal: encode one particle into the CPU buffer at a given index.
*/
private writeAtIndex(index: number, particle: IParticle): void {
const base = index * this.floatsPerParticle;
// Layout: pos2, vel2, accel2, size, mass, color4
this.data[base + 0] = particle.position.x;
this.data[base + 1] = particle.position.y;
this.data[base + 2] = particle.velocity.x;
this.data[base + 3] = particle.velocity.y;
this.data[base + 4] = 0; // ax
this.data[base + 5] = 0; // ay
this.data[base + 6] = particle.size ?? 5;
this.data[base + 7] = particle.mass ?? 1;
const c = particle.color ?? { r: 1, g: 1, b: 1, a: 1 };
this.data[base + 8] = c.r;
this.data[base + 9] = c.g;
this.data[base + 10] = c.b;
this.data[base + 11] = c.a;
}
}