simulationjsv2 0.2.0 → 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
@@ -7,7 +7,11 @@ export class SimulationElement {
7
7
  vertexCache;
8
8
  constructor(pos, color = new Color()) {
9
9
  this.pos = pos;
10
- vec3ToPixelRatio(vector3(...this.pos));
10
+ const temp = vector3(...this.pos);
11
+ vec3ToPixelRatio(temp);
12
+ for (let i = 0; i < this.pos.length; i++) {
13
+ this.pos[i] = temp[i];
14
+ }
11
15
  this.color = color;
12
16
  this.vertexCache = new VertexCache();
13
17
  this.camera = null;
@@ -738,6 +742,7 @@ export class BezierCurve2d {
738
742
  this.points = points;
739
743
  }
740
744
  interpolateSlope(t) {
745
+ t = Math.max(0, Math.min(1, t));
741
746
  let vectors = this.points;
742
747
  let slopeVector = vector2(1);
743
748
  while (vectors.length > 2) {
@@ -824,15 +829,15 @@ export class SplinePoint2d {
824
829
  }
825
830
  getColors(prevColor) {
826
831
  const colors = [null, null];
827
- if (this.start && this.start.getColor()) {
832
+ if (prevColor) {
833
+ colors[0] = prevColor;
834
+ }
835
+ else if (this.start && this.start.getColor()) {
828
836
  colors[0] = this.start.getColor();
829
837
  }
830
838
  if (this.end.getColor()) {
831
839
  colors[1] = this.end.getColor();
832
840
  }
833
- if (prevColor) {
834
- colors.unshift(prevColor);
835
- }
836
841
  if (colors.at(-1) === null) {
837
842
  colors.pop();
838
843
  }
@@ -858,6 +863,7 @@ export class Spline2d extends SimulationElement {
858
863
  curves;
859
864
  thickness;
860
865
  detail;
866
+ interpolateStart;
861
867
  interpolateLimit;
862
868
  distance;
863
869
  constructor(pos, points, thickness = devicePixelRatio, detail = 40) {
@@ -865,6 +871,7 @@ export class Spline2d extends SimulationElement {
865
871
  this.curves = [];
866
872
  this.thickness = thickness * devicePixelRatio;
867
873
  this.detail = detail;
874
+ this.interpolateStart = 0;
868
875
  this.interpolateLimit = 1;
869
876
  this.distance = 0;
870
877
  for (let i = 0; i < points.length; i++) {
@@ -884,6 +891,16 @@ export class Spline2d extends SimulationElement {
884
891
  this.curves.push(curve);
885
892
  }
886
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
+ }
887
904
  setInterpolateLimit(limit, t = 0, f) {
888
905
  const diff = limit - this.interpolateLimit;
889
906
  return transitionValues((p) => {
@@ -894,12 +911,26 @@ export class Spline2d extends SimulationElement {
894
911
  this.vertexCache.updated();
895
912
  }, t, f);
896
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
+ }
897
927
  getBuffer(camera, force) {
898
928
  if (this.vertexCache.shouldUpdate() || force) {
899
929
  const screenSize = camera.getScreenSize();
900
930
  let verticesTop = [];
901
931
  const verticesBottom = [];
902
932
  let currentDistance = 0;
933
+ let interpolationStarted = false;
903
934
  outer: for (let i = 0; i < this.curves.length; i++) {
904
935
  const detail = this.curves[i].getDetail() || this.detail;
905
936
  const step = 1 / detail;
@@ -916,6 +947,14 @@ export class Spline2d extends SimulationElement {
916
947
  atLimit = true;
917
948
  currentInterpolation = (this.interpolateLimit - distanceRatio) / sectionRatio;
918
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
+ }
919
958
  const [point, slope] = this.curves[i].interpolateSlope(currentInterpolation);
920
959
  const pos = this.getPos();
921
960
  point[0] += pos[0];
@@ -923,7 +962,9 @@ export class Spline2d extends SimulationElement {
923
962
  const normal = vector2(-slope[1], slope[0]);
924
963
  vec2.normalize(normal, normal);
925
964
  vec2.scale(normal, this.thickness / 2, normal);
926
- 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()));
927
968
  const vertexColor = interpolateColors(colors, currentInterpolation);
928
969
  const vertTop = vertex(point[0] + normal[0], point[1] + normal[1], 0, vertexColor);
929
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';
@@ -25,7 +25,7 @@ export declare class SceneCollection extends SimulationElement {
25
25
  private scene;
26
26
  constructor(name: string);
27
27
  getName(): string;
28
- add(el: SimulationElement): void;
28
+ add(el: SimulationElement<any>): void;
29
29
  empty(): void;
30
30
  getBuffer(camera: Camera, force: boolean): number[];
31
31
  }
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,7 +5,7 @@
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.0",
8
+ "version": "0.2.2",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": "./dist/index.js",