-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparticles.ts
More file actions
231 lines (215 loc) · 6.42 KB
/
particles.ts
File metadata and controls
231 lines (215 loc) · 6.42 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
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
/**
* Particles (Render Module)
*
* Single fullscreen pass that instanced-draws particle quads into the scene.
* Fragment shader renders a soft-disc using the particle color and alpha falloff.
*/
import {
Module,
type WebGPUDescriptor,
ModuleRole,
RenderPassKind,
CPUDescriptor,
CanvasComposition,
DataType,
} from "../../module";
export enum ParticlesColorType {
Default = 0,
Custom = 1,
Hue = 2,
}
type ParticlesInputs = {
colorType: number; // ParticleColorType enum value
customColorR: number; // 0..1
customColorG: number; // 0..1
customColorB: number; // 0..1
hue: number; // 0..1
};
export class Particles extends Module<"particles", ParticlesInputs> {
readonly name = "particles" as const;
readonly role = ModuleRole.Render;
readonly inputs = {
colorType: DataType.NUMBER,
customColorR: DataType.NUMBER,
customColorG: DataType.NUMBER,
customColorB: DataType.NUMBER,
hue: DataType.NUMBER,
} as const;
constructor(opts?: {
enabled?: boolean;
colorType?: ParticlesColorType;
customColor?: { r: number; g: number; b: number; a: number };
hue?: number;
}) {
super();
this.write({
colorType: opts?.colorType ?? ParticlesColorType.Default,
customColorR: opts?.customColor?.r ?? 1,
customColorG: opts?.customColor?.g ?? 1,
customColorB: opts?.customColor?.b ?? 1,
hue: Math.min(1, Math.max(0, opts?.hue ?? 0)),
});
if (opts?.enabled !== undefined) this.setEnabled(!!opts.enabled);
}
setColorType(type: ParticlesColorType): void {
this.write({ colorType: type });
}
setCustomColor(color: { r: number; g: number; b: number; a: number }): void {
this.write({
customColorR: color.r,
customColorG: color.g,
customColorB: color.b,
});
}
setHue(hue: number): void {
const clamped = Math.min(1, Math.max(0, hue));
this.write({ hue: clamped });
}
webgpu(): WebGPUDescriptor<ParticlesInputs> {
return {
// Single fullscreen pass that draws particles into the scene texture
passes: [
{
kind: RenderPassKind.Fullscreen,
fragment: ({ getUniform }) => `{
let center = vec2<f32>(0.5, 0.5);
let dist = distance(uv, center);
// Fetch uniforms
let colorType = ${getUniform("colorType")};
let custom = vec3<f32>(
${getUniform("customColorR")},
${getUniform("customColorG")},
${getUniform("customColorB")}
);
let hue = ${getUniform("hue")};
var baseColor = color;
if (colorType == 1.0) {
baseColor = vec4<f32>(custom, 1.0); // Custom RGB with alpha 1
} else if (colorType == 2.0) {
// Inline hue->RGB conversion at full saturation/value
let h = fract(hue) * 6.0;
let i = floor(h);
let f = h - i;
let q = 1.0 - f;
var rgb = vec3<f32>(1.0, 0.0, 0.0);
if (i < 1.0) {
rgb = vec3<f32>(1.0, f, 0.0);
} else if (i < 2.0) {
rgb = vec3<f32>(q, 1.0, 0.0);
} else if (i < 3.0) {
rgb = vec3<f32>(0.0, 1.0, f);
} else if (i < 4.0) {
rgb = vec3<f32>(0.0, q, 1.0);
} else if (i < 5.0) {
rgb = vec3<f32>(f, 0.0, 1.0);
} else {
rgb = vec3<f32>(1.0, 0.0, q);
}
baseColor = vec4<f32>(rgb, 1.0);
}
var finalColor = baseColor.rgb;
var finalAlpha = baseColor.a;
// Pinned particles render as hollow circles (donut shape)
if (mass < 0.0) {
// Create hollow ring effect
let innerRadius = 0.30;
let outerRadius = 0.45;
let edgeSmooth = 0.05;
// Calculate ring alpha based on distance
var ringAlpha = 1.0 - smoothstep(outerRadius - edgeSmooth, outerRadius + edgeSmooth, dist);
ringAlpha = ringAlpha * smoothstep(innerRadius - edgeSmooth, innerRadius + edgeSmooth, dist);
finalAlpha = finalAlpha * ringAlpha;
} else {
// Normal solid particle
finalAlpha = finalAlpha * (1.0 - smoothstep(0.45, 0.5, dist));
}
return vec4<f32>(finalColor, finalAlpha);
}`,
bindings: [
"colorType",
"customColorR",
"customColorG",
"customColorB",
"hue",
] as const,
readsScene: false,
writesScene: true,
},
],
};
}
cpu(): CPUDescriptor<ParticlesInputs> {
return {
composition: CanvasComposition.RequiresClear,
render: ({
particle,
screenX,
screenY,
screenSize,
utils,
context,
input,
}) => {
// Determine render color based on input colorType
const type = typeof input.colorType === "number" ? input.colorType : 0;
let rgba = particle.color;
if (type === 1) {
const r = (input as any).customColorR as number;
const g = (input as any).customColorG as number;
const b = (input as any).customColorB as number;
rgba = { r: r ?? 1, g: g ?? 1, b: b ?? 1, a: 1 };
} else if (type === 2) {
const h = Math.min(
1,
Math.max(0, typeof input.hue === "number" ? input.hue : 0)
);
// Convert hue to RGB (HSV with S=1, V=1)
const h6 = h * 6;
const i = Math.floor(h6);
const f = h6 - i;
const q = 1 - f;
switch (i % 6) {
case 0:
rgba = { r: 1, g: f, b: 0, a: 1 };
break;
case 1:
rgba = { r: q, g: 1, b: 0, a: 1 };
break;
case 2:
rgba = { r: 0, g: 1, b: f, a: 1 };
break;
case 3:
rgba = { r: 0, g: q, b: 1, a: 1 };
break;
case 4:
rgba = { r: f, g: 0, b: 1, a: 1 };
break;
default:
rgba = { r: 1, g: 0, b: q, a: 1 };
}
}
if (particle.mass < 0) {
// Draw hollow circle for pinned particles
const color = rgba;
const lineWidth = Math.max(2, screenSize * 0.15); // Ring width scales with particle size
context.strokeStyle = `rgba(${color.r * 255}, ${color.g * 255}, ${
color.b * 255
}, ${color.a})`;
context.lineWidth = lineWidth;
context.beginPath();
context.arc(
screenX,
screenY,
screenSize - lineWidth / 2,
0,
Math.PI * 2
);
context.stroke();
} else {
// Draw solid circle for normal particles
utils.drawCircle(screenX, screenY, screenSize, rgba);
}
},
};
}
}