simulationjsv2 0.2.1 → 0.2.2

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.
@@ -137,9 +137,13 @@ export declare class Spline2d extends SimulationElement {
137
137
  private curves;
138
138
  private thickness;
139
139
  private detail;
140
+ private interpolateStart;
140
141
  private interpolateLimit;
141
142
  private distance;
142
143
  constructor(pos: Vertex, points: SplinePoint2d[], thickness?: number, detail?: number);
144
+ setInterpolateStart(start: number, t?: number, f?: LerpFunc): Promise<void>;
143
145
  setInterpolateLimit(limit: number, t?: number, f?: LerpFunc): Promise<void>;
146
+ interpolateSlope(t: number): readonly [Vector2, Vector2];
147
+ interpolate(t: number): Vector2;
144
148
  getBuffer(camera: Camera, force: boolean): number[];
145
149
  }
package/dist/graphics.js CHANGED
@@ -742,6 +742,7 @@ export class BezierCurve2d {
742
742
  this.points = points;
743
743
  }
744
744
  interpolateSlope(t) {
745
+ t = Math.max(0, Math.min(1, t));
745
746
  let vectors = this.points;
746
747
  let slopeVector = vector2(1);
747
748
  while (vectors.length > 2) {
@@ -828,15 +829,15 @@ export class SplinePoint2d {
828
829
  }
829
830
  getColors(prevColor) {
830
831
  const colors = [null, null];
831
- if (this.start && this.start.getColor()) {
832
+ if (prevColor) {
833
+ colors[0] = prevColor;
834
+ }
835
+ else if (this.start && this.start.getColor()) {
832
836
  colors[0] = this.start.getColor();
833
837
  }
834
838
  if (this.end.getColor()) {
835
839
  colors[1] = this.end.getColor();
836
840
  }
837
- if (prevColor) {
838
- colors.unshift(prevColor);
839
- }
840
841
  if (colors.at(-1) === null) {
841
842
  colors.pop();
842
843
  }
@@ -862,6 +863,7 @@ export class Spline2d extends SimulationElement {
862
863
  curves;
863
864
  thickness;
864
865
  detail;
866
+ interpolateStart;
865
867
  interpolateLimit;
866
868
  distance;
867
869
  constructor(pos, points, thickness = devicePixelRatio, detail = 40) {
@@ -869,6 +871,7 @@ export class Spline2d extends SimulationElement {
869
871
  this.curves = [];
870
872
  this.thickness = thickness * devicePixelRatio;
871
873
  this.detail = detail;
874
+ this.interpolateStart = 0;
872
875
  this.interpolateLimit = 1;
873
876
  this.distance = 0;
874
877
  for (let i = 0; i < points.length; i++) {
@@ -888,6 +891,16 @@ export class Spline2d extends SimulationElement {
888
891
  this.curves.push(curve);
889
892
  }
890
893
  }
894
+ setInterpolateStart(start, t = 0, f) {
895
+ const diff = start - this.interpolateStart;
896
+ return transitionValues((p) => {
897
+ this.interpolateStart += diff * p;
898
+ this.vertexCache.updated();
899
+ }, () => {
900
+ this.interpolateStart = start;
901
+ this.vertexCache.updated();
902
+ }, t, f);
903
+ }
891
904
  setInterpolateLimit(limit, t = 0, f) {
892
905
  const diff = limit - this.interpolateLimit;
893
906
  return transitionValues((p) => {
@@ -898,12 +911,26 @@ export class Spline2d extends SimulationElement {
898
911
  this.vertexCache.updated();
899
912
  }, t, f);
900
913
  }
914
+ interpolateSlope(t) {
915
+ const curveInterval = 1 / this.curves.length;
916
+ let index = Math.floor(t / curveInterval);
917
+ if (index === this.curves.length)
918
+ index--;
919
+ const diff = (t - curveInterval * index) * 2;
920
+ console.log(index);
921
+ return this.curves[index].interpolateSlope(diff);
922
+ }
923
+ interpolate(t) {
924
+ const [vec] = this.interpolateSlope(t);
925
+ return vec;
926
+ }
901
927
  getBuffer(camera, force) {
902
928
  if (this.vertexCache.shouldUpdate() || force) {
903
929
  const screenSize = camera.getScreenSize();
904
930
  let verticesTop = [];
905
931
  const verticesBottom = [];
906
932
  let currentDistance = 0;
933
+ let interpolationStarted = false;
907
934
  outer: for (let i = 0; i < this.curves.length; i++) {
908
935
  const detail = this.curves[i].getDetail() || this.detail;
909
936
  const step = 1 / detail;
@@ -920,6 +947,14 @@ export class Spline2d extends SimulationElement {
920
947
  atLimit = true;
921
948
  currentInterpolation = (this.interpolateLimit - distanceRatio) / sectionRatio;
922
949
  }
950
+ if (distanceRatio + currentInterpolation / this.curves.length < this.interpolateStart) {
951
+ continue;
952
+ }
953
+ if (!interpolationStarted) {
954
+ interpolationStarted = true;
955
+ currentInterpolation = (this.interpolateStart - distanceRatio) / sectionRatio;
956
+ j--;
957
+ }
923
958
  const [point, slope] = this.curves[i].interpolateSlope(currentInterpolation);
924
959
  const pos = this.getPos();
925
960
  point[0] += pos[0];
@@ -927,7 +962,9 @@ export class Spline2d extends SimulationElement {
927
962
  const normal = vector2(-slope[1], slope[0]);
928
963
  vec2.normalize(normal, normal);
929
964
  vec2.scale(normal, this.thickness / 2, normal);
930
- const colors = this.curves[i].getColors().map((c) => (c ? c : this.getColor()));
965
+ const colorsBefore = this.curves[i].getColors();
966
+ console.log(colorsBefore);
967
+ const colors = colorsBefore.map((c) => (c ? c : this.getColor()));
931
968
  const vertexColor = interpolateColors(colors, currentInterpolation);
932
969
  const vertTop = vertex(point[0] + normal[0], point[1] + normal[1], 0, vertexColor);
933
970
  verticesTop.push(vertTop);
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from './simulation.js';
2
2
  export * from './graphics.js';
3
3
  export * from './types.js';
4
- export { Vertex, Color, cloneBuf, vector4, vector3, vector2, vector3FromVector2, colorFromVector4, randomInt, randomColor, vertex, color, colorf, transitionValues, lerp, smoothStep, linearStep, exponentialStep, splinePoint2d, continuousSplinePoint2d } from './utils.js';
4
+ export { Vertex, Color, cloneBuf, vector4, vector3, vector2, vector3FromVector2, colorFromVector4, randomInt, randomColor, vertex, color, colorf, transitionValues, lerp, smoothStep, linearStep, splinePoint2d, continuousSplinePoint2d, easeInOutQuad, easeInOutExpo, easeInOutQuart } from './utils.js';
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from './simulation.js';
2
2
  export * from './graphics.js';
3
3
  export * from './types.js';
4
- export { Vertex, Color, cloneBuf, vector4, vector3, vector2, vector3FromVector2, colorFromVector4, randomInt, randomColor, vertex, color, colorf, transitionValues, lerp, smoothStep, linearStep, exponentialStep, splinePoint2d, continuousSplinePoint2d } from './utils.js';
4
+ export { Vertex, Color, cloneBuf, vector4, vector3, vector2, vector3FromVector2, colorFromVector4, randomInt, randomColor, vertex, color, colorf, transitionValues, lerp, smoothStep, linearStep, splinePoint2d, continuousSplinePoint2d, easeInOutQuad, easeInOutExpo, easeInOutQuart } from './utils.js';
package/dist/utils.d.ts CHANGED
@@ -74,7 +74,9 @@ export declare function transitionValues(callback1: (deltaT: number, t: number)
74
74
  export declare function lerp(a: number, b: number, t: number): number;
75
75
  export declare function smoothStep(t: number): number;
76
76
  export declare function linearStep(t: number): number;
77
- export declare function exponentialStep(t: number): number;
77
+ export declare function easeInOutExpo(t: number): number;
78
+ export declare function easeInOutQuart(t: number): number;
79
+ export declare function easeInOutQuad(t: number): number;
78
80
  export declare function vertexBuffer3d(x: number, y: number, z: number, color: Color, uv?: Vector2): number[];
79
81
  export declare function vertexBuffer2d(x: number, y: number, color: Color, uv?: Vector2): number[];
80
82
  export declare function vec3ToPixelRatio(vec: Vector3): void;
package/dist/utils.js CHANGED
@@ -248,7 +248,7 @@ export function smoothStep(t) {
248
248
  export function linearStep(t) {
249
249
  return t;
250
250
  }
251
- export function exponentialStep(t) {
251
+ export function easeInOutExpo(t) {
252
252
  return t === 0
253
253
  ? 0
254
254
  : t === 1
@@ -257,6 +257,12 @@ export function exponentialStep(t) {
257
257
  ? Math.pow(2, 20 * t - 10) / 2
258
258
  : (2 - Math.pow(2, -20 * t + 10)) / 2;
259
259
  }
260
+ export function easeInOutQuart(t) {
261
+ return t < 0.5 ? 8 * t * t * t * t : 1 - Math.pow(-2 * t + 2, 4) / 2;
262
+ }
263
+ export function easeInOutQuad(t) {
264
+ return t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2;
265
+ }
260
266
  export function vertexBuffer3d(x, y, z, color, uv = vector2()) {
261
267
  return [x, y, z, 1, ...color.toBuffer(), ...uv, 1];
262
268
  }
@@ -318,21 +324,25 @@ export function continuousSplinePoint2d(end, control, detail) {
318
324
  return new SplinePoint2d(null, end, null, control, rawControls, detail);
319
325
  }
320
326
  export function interpolateColors(colors, t) {
327
+ t = Math.min(1, Math.max(0, t));
321
328
  if (colors.length === 0)
322
329
  return color();
323
330
  if (colors.length === 1)
324
331
  return colors[0];
325
332
  const colorInterval = 1 / colors.length;
326
333
  let index = Math.floor(t / colorInterval);
327
- if (index === colors.length)
328
- index--;
334
+ if (index === -1)
335
+ console.log(t);
336
+ if (index >= colors.length)
337
+ index = colors.length - 1;
329
338
  const from = index === colors.length - 1 ? colors[index - 1] : colors[index];
330
339
  const to = index === colors.length - 1 ? colors[index] : colors[index + 1];
331
340
  const diff = to.diff(from);
332
- diff.r *= t / (colorInterval * colors.length);
333
- diff.g *= t / (colorInterval * colors.length);
334
- diff.b *= t / (colorInterval * colors.length);
335
- diff.a *= t / (colorInterval * colors.length);
341
+ const scale = t / (colorInterval * colors.length);
342
+ diff.r *= scale;
343
+ diff.g *= scale;
344
+ diff.b *= scale;
345
+ diff.a *= scale;
336
346
  const res = from.clone();
337
347
  res.r += diff.r;
338
348
  res.g += diff.g;
package/package.json CHANGED
@@ -5,13 +5,18 @@
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Jackson Otto",
7
7
  "description": "A simple graphics library using WebGPU",
8
- "version": "0.2.1",
8
+ "version": "0.2.2",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": "./dist/index.js",
12
12
  "types": "./dist/index.d.ts"
13
13
  }
14
14
  },
15
+ "scripts": {
16
+ "dev": "npx nodemon --watch src -e ts --exec 'npx tsc || exit 1'",
17
+ "test": "vite --port 3000",
18
+ "build": "npx tsc"
19
+ },
15
20
  "devDependencies": {
16
21
  "nodemon": "^3.0.1",
17
22
  "typescript": "^5.1.6",
@@ -20,10 +25,5 @@
20
25
  "dependencies": {
21
26
  "@webgpu/types": "^0.1.34",
22
27
  "wgpu-matrix": "^2.5.1"
23
- },
24
- "scripts": {
25
- "dev": "npx nodemon --watch src -e ts --exec 'npx tsc || exit 1'",
26
- "test": "vite --port 3000",
27
- "build": "npx tsc"
28
28
  }
29
- }
29
+ }