Skip to content

Commit 6a7d7db

Browse files
committed
Add custom color for particles
1 parent 11390af commit 6a7d7db

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/src/shaders/render-frag.glsl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#version 300 es
22
precision mediump float;
33

4-
out vec4 c;
4+
uniform vec4 c; /** Particle Color */
5+
6+
out vec4 oC;
57

68
void main() {
7-
c = vec4(1.0);
9+
oC = c;
810
}

lib/src/simulator.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const U_RANDOM_RG = "rg";
1212
const U_FORCE_FIELD = "g"; /** gravity */
1313
const U_ORIGIN = "o";
1414
const U_ANGLE_RANGE = "aR";
15+
const U_PARTICLE_COLOR = "c";
1516

1617
// inputs
1718
const IN_POSITION = "p";
@@ -30,6 +31,8 @@ let mouseX = 0,
3031
type Vector2D = [number, number];
3132

3233
export interface ParticlesOptions {
34+
/** particle Color @defaultValue [1, 0, 0, 1] -> red */
35+
rgba?: [number, number, number, number];
3336
/** @defaultValue 100_000 */
3437
maxParticles?: number;
3538
/** @defaultValue 0.5 */
@@ -50,6 +53,7 @@ export interface ParticlesOptions {
5053
}
5154

5255
const defaultOptions: ParticlesOptions = {
56+
rgba: [1, 0, 0, 1],
5357
maxParticles: 100_000,
5458
generationRate: 0.5,
5559
forceField: [0, -0.1],
@@ -247,6 +251,8 @@ const simulate = (gl: WebGL2RenderingContext, options: ParticlesOptions) => {
247251
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, null);
248252
gl.bindVertexArray(vertexArrayObjects[readIndex + 2]);
249253
gl.useProgram(renderProgram);
254+
// skipcq: JS-0339 -- set in default options
255+
gl.uniform4f(gl.getUniformLocation(renderProgram, U_PARTICLE_COLOR), ...options.rgba!);
250256
gl.drawArrays(gl.POINTS, 0, bornParticles);
251257
[readIndex, writeIndex] = [writeIndex, readIndex];
252258

0 commit comments

Comments
 (0)