Skip to content

Commit 48da729

Browse files
committed
Support Angle Range
1 parent 0ce4011 commit 48da729

File tree

3 files changed

+49
-79
lines changed

3 files changed

+49
-79
lines changed

.tkb

Lines changed: 19 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,11 @@
11
{
22
"scope": "Workspace",
33
"tasks": {
4-
"G-1H47JpSI69kav1qZ-kp": {
5-
"id": "G-1H47JpSI69kav1qZ-kp",
6-
"description": "Star [this repository](https://github.com/react18-tools/turborepo-template/) for easy access and to show your support",
7-
"columnId": "column-todo"
8-
},
9-
"gbzyhx0FwFfzRoE-iqVeu": {
10-
"id": "gbzyhx0FwFfzRoE-iqVeu",
11-
"description": "🌟 **Update `sripts/rebrand.config.json` if required.**",
12-
"columnId": "column-todo"
13-
},
14-
"Y5__aEZl3Pbxp7r90HKpV": {
15-
"id": "Y5__aEZl3Pbxp7r90HKpV",
16-
"description": "Install `pnpm` using `npm i -g pnpm`",
17-
"columnId": "column-todo"
18-
},
19-
"jcpymbi71e702p7n6fwvZ": {
20-
"id": "jcpymbi71e702p7n6fwvZ",
21-
"description": "Close this Kanban Board tab and install dependencies using `pnpm`\n - Run `pnpm i` to install dependencies; the `rebrand` script will automatically rebrand the repo, adjust workflows, and create a commit",
22-
"columnId": "column-todo"
23-
},
24-
"jdZxwLymOo7w2eZeNpvsN": {
25-
"id": "jdZxwLymOo7w2eZeNpvsN",
26-
"description": "Run `yarn plop`, and follow prompts to generate server or client components for your library",
27-
"columnId": "column-todo"
28-
},
294
"RX4J5v4y5IOe_ucf8pMRT": {
305
"id": "RX4J5v4y5IOe_ucf8pMRT",
316
"description": "🌟 Enable [private vulnerability reporting](https://github.com/react18-tools/webgl-generative-particles/security) (For public repo - do this after updating visibility to public)",
327
"columnId": "column-todo"
338
},
34-
"EdPbrbJLllUHfZmCS80f7": {
35-
"id": "EdPbrbJLllUHfZmCS80f7",
36-
"description": "Set up `CodeCov`\n - Visit Codecov and set up your repo\n - Create [repository secret]((https://github.com/react18-tools/webgl-generative-particles/settings/secrets/actions)) for `CODECOV_TOKEN`",
37-
"columnId": "column-todo"
38-
},
399
"MLLUsAhCKaKxvEXFY0HSq": {
4010
"id": "MLLUsAhCKaKxvEXFY0HSq",
4111
"description": "Set up `CodeClimate`\n - Visit CodeClimate and set up your repo\n - Create [repository secret] for `CC_TEST_REPORTER_ID`\n - Add `*.test.*` to ignore patterns on the website\n - Update Code Climate badge",
@@ -96,40 +66,23 @@
9666
"description": "You can also fork the [`terborepo-template`](https://github.com/react18-tools/turborepo-template/fork) and add your package to `scripts/featured.json`\n - If approved, your package will be automatically added to FEATURED.md and also published on the home page of this repo.",
9767
"columnId": "column-todo"
9868
},
99-
"_JiH9RZLmUF5nxD7lBC5B": {
100-
"id": "_JiH9RZLmUF5nxD7lBC5B",
101-
"description": "Create a new GitHub repository using this template.\n - Click the `Use this template` button at the top right -> `Create a new repository`\n - Click `Create repository` and wait for the setup workflow to finish rebranding your repo.",
102-
"columnId": "column-done"
103-
},
104-
"t5d22lVYbF7GuQHzv6DRO": {
105-
"id": "t5d22lVYbF7GuQHzv6DRO",
106-
"description": "Install and set up Node.js and your IDE (VSCode recommended)",
107-
"columnId": "column-done"
108-
},
109-
"wkDDELNeOfYSVmWI1eWu0": {
110-
"id": "wkDDELNeOfYSVmWI1eWu0",
111-
"description": "Install the recommended VSCode extensions:\n - [Trello Kanban](https://marketplace.visualstudio.com/items?itemName=mayank1513.trello-kanban-task-board)",
112-
"columnId": "column-done"
69+
"9-0sxOV9Cw-jVjngwbWKm": {
70+
"id": "9-0sxOV9Cw-jVjngwbWKm",
71+
"description": "Add colors",
72+
"columnId": "column-KpcfQ3OwiDZ1c11EOpcsT"
11373
},
114-
"J5lp9I5d12B4tpGKawaqF": {
115-
"id": "J5lp9I5d12B4tpGKawaqF",
116-
"description": "Run `node scripts/rebrand.js` to rebrand this repo.",
117-
"columnId": "column-todo"
74+
"GJ4z5bji9Cpt9LjmhSwkW": {
75+
"id": "GJ4z5bji9Cpt9LjmhSwkW",
76+
"description": "fix angles",
77+
"columnId": "column-KpcfQ3OwiDZ1c11EOpcsT"
11878
}
11979
},
12080
"columns": [
12181
{
12282
"id": "column-todo",
12383
"title": "To do",
12484
"tasksIds": [
125-
"G-1H47JpSI69kav1qZ-kp",
126-
"gbzyhx0FwFfzRoE-iqVeu",
127-
"Y5__aEZl3Pbxp7r90HKpV",
128-
"jcpymbi71e702p7n6fwvZ",
129-
"J5lp9I5d12B4tpGKawaqF",
130-
"jdZxwLymOo7w2eZeNpvsN",
13185
"RX4J5v4y5IOe_ucf8pMRT",
132-
"EdPbrbJLllUHfZmCS80f7",
13386
"MLLUsAhCKaKxvEXFY0HSq",
13487
"gMYfaAh2RABMP8uZRQgNx",
13588
"_BfuX7quWBANpDK1bI7YM",
@@ -144,19 +97,23 @@
14497
"IjuAtNquJ4-Ut874rHKIW"
14598
]
14699
},
100+
{
101+
"id": "column-KpcfQ3OwiDZ1c11EOpcsT",
102+
"title": "Backlog",
103+
"tasksIds": [
104+
"9-0sxOV9Cw-jVjngwbWKm",
105+
"GJ4z5bji9Cpt9LjmhSwkW"
106+
]
107+
},
147108
{
148109
"id": "column-doing",
149110
"title": "Doing",
150111
"tasksIds": []
151112
},
152113
{
153-
"id": "column-done",
114+
"id": "column-WTA-ZOqIlXE23ewJglzO0",
154115
"title": "Done",
155-
"tasksIds": [
156-
"_JiH9RZLmUF5nxD7lBC5B",
157-
"t5d22lVYbF7GuQHzv6DRO",
158-
"wkDDELNeOfYSVmWI1eWu0"
159-
]
116+
"tasksIds": []
160117
}
161118
]
162-
}
119+
}

lib/src/shaders/update-vert.glsl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,31 @@ precision mediump float;
33

44
uniform float dt; /** time delta */
55
uniform sampler2D rg; /** random rg */
6-
uniform vec2 g; /** gravity */
6+
uniform vec2 g; /** gravity - forceField */
77
uniform vec2 o; /** origin*/
8+
uniform vec2 aR; /** Angle Range */
89

910
in vec2 p; /** position */
10-
in float a; /** Age */
11+
in float l; /** Life */
1112
in vec2 v; /** Velocity */
1213

1314
out vec2 oP;
14-
out float oA;
15+
out float oL;
1516
out vec2 oV;
1617

1718
void main() {
18-
if(a <= 0.0) {
19+
if(l <= 0.0) {
1920
ivec2 ij = ivec2(gl_VertexID % 512, gl_VertexID / 512);
2021
vec2 rd = texelFetch(rg, ij, 0).rg;
21-
float th = rd.r * 6.2832;
22+
float th = aR.x + rd.r * (aR.y - aR.x);
2223
float x = cos(th);
2324
float y = sin(th);
2425
oP = o;
25-
oA = rd.r + rd.g;
26+
oL = rd.r + rd.g;
2627
oV = vec2(x, y) * rd.g;
2728
} else {
2829
oP = p + v * dt;
29-
oA = a - dt;
30+
oL = l - dt;
3031
oV = v + g * dt;
3132
}
3233
}

lib/src/simulator.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,53 @@ import updateFragmentShaderSource from "./shaders/update-frag.glsl?raw";
33
import renderVertexShaderSource from "./shaders/render-vert.glsl?raw";
44
import renderFragmentShaderSource from "./shaders/render-frag.glsl?raw";
55

6+
// constnats
7+
const PI = Math.PI;
68
/** shader names */
79
// Uniforms
810
const U_DT = "dt";
911
const U_RANDOM_RG = "rg";
1012
const U_FORCE_FIELD = "g"; /** gravity */
1113
const U_ORIGIN = "o";
14+
const U_ANGLE_RANGE = "aR";
1215

1316
// inputs
1417
const IN_POSITION = "p";
15-
const IN_AGE = "a";
18+
const IN_LIFE = "l";
1619
const IN_VELOCITY = "v";
1720

1821
// outputs
1922
const OUT_POSITION = "oP";
20-
const OUT_AGE = "oA";
23+
const OUT_LIFE = "oL";
2124
const OUT_VELOCITY = "oV";
2225

2326
/** module scoped global variables/constants */
2427
let mouseX = 0,
2528
mouseY = 0;
2629

30+
type Vector2D = [number, number];
31+
2732
export interface ParticlesOptions {
2833
maxParticles?: number;
2934
generationRate?: number;
3035
overlay?: boolean;
3136
mouseOff?: boolean;
37+
/** min and max Angles in radians: -Math.PI to Math.PI */
38+
angleRage?: [number, number];
3239
/** todo */
3340
/** min and max age of particles */
3441
ageRange?: [number, number];
3542
/** [minSpeed, maxSpeed] */
3643
speedRange?: [number, number];
37-
/** min and max Angles in radians: -Math.PI to Math.PI */
38-
angleRage?: [number, number];
39-
/** todo: WIP constant force [fx, fy, fz] or a force field texture */
40-
forceField?: [number, number, number] | number[][] | string;
44+
/** todo: WIP constant force [fx, fy] or a force field texture */
45+
forceField?: Vector2D; //| Vector[][] | string;
4146
}
4247

4348
const defaultOptions: ParticlesOptions = {
4449
maxParticles: 100_000,
4550
generationRate: 0.5,
51+
forceField: [0, -0.1],
52+
angleRage: [-PI, PI],
4653
};
4754

4855
const getInitialData = (maxParticles: number) => {
@@ -58,7 +65,9 @@ const randomRGData = (sizeX: number, sizeY: number): Uint8Array => {
5865
};
5966

6067
/** Particles simulator */
61-
const simulate = (gl: WebGL2RenderingContext, options = defaultOptions) => {
68+
const simulate = (gl: WebGL2RenderingContext, options: ParticlesOptions) => {
69+
/** Normalize options */
70+
options.angleRage?.map(a => a % PI).sort();
6271
/** Create shader */
6372
const createShader = (type: number, source: string): WebGLShader => {
6473
const shader = gl.createShader(type);
@@ -107,7 +116,7 @@ const simulate = (gl: WebGL2RenderingContext, options = defaultOptions) => {
107116

108117
const updateProgram = createProgram(updateVertexShaderSource, updateFragmentShaderSource, [
109118
OUT_POSITION,
110-
OUT_AGE,
119+
OUT_LIFE,
111120
OUT_VELOCITY,
112121
]);
113122

@@ -131,7 +140,7 @@ const simulate = (gl: WebGL2RenderingContext, options = defaultOptions) => {
131140
nVect: 2,
132141
},
133142
{
134-
location: gl.getAttribLocation(updateProgram, IN_AGE),
143+
location: gl.getAttribLocation(updateProgram, IN_LIFE),
135144
nVect: 1,
136145
},
137146
{
@@ -214,8 +223,11 @@ const simulate = (gl: WebGL2RenderingContext, options = defaultOptions) => {
214223
gl.useProgram(updateProgram);
215224

216225
setUniform(U_DT, dt / 1000);
217-
setUniform(U_FORCE_FIELD, 0, -0.1);
226+
// skipcq: JS-0339 -- forcefield is always set by the default options
227+
setUniform(U_FORCE_FIELD, ...options.forceField!);
218228
setUniform(U_ORIGIN, mouseX, mouseY);
229+
// skipcq: JS-0339 -- set in default options
230+
setUniform(U_ANGLE_RANGE, ...options.angleRage!);
219231
gl.activeTexture(gl.TEXTURE0);
220232
gl.bindTexture(gl.TEXTURE_2D, rgNoiseTexture);
221233
setUniform(U_RANDOM_RG, 0);

0 commit comments

Comments
 (0)