simulationjsv2 0.4.8 → 0.4.9

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.
@@ -1,10 +1,10 @@
1
- import { CircleGeometryParams, CubeGeometryParams, Line2dGeometryParams, Line3dGeometryParams, Mat4, PolygonGeometryParams, Spline2dGeometryParams, SquareGeometryParams, Vector2, Vector3, VertexColorMap } from './types.js';
1
+ import { CircleGeometryParams, CubeGeometryParams, EmptyParams, Line2dGeometryParams, Line3dGeometryParams, Mat4, PolygonGeometryParams, Spline2dGeometryParams, SquareGeometryParams, Vector2, Vector3, VertexColorMap } from './types.js';
2
2
  import { Color, Vertex } from './utils.js';
3
3
  import { CubicBezierCurve2d, SplinePoint2d } from './graphics.js';
4
- export declare abstract class Geometry {
4
+ export declare abstract class Geometry<T extends EmptyParams> {
5
5
  protected abstract wireframeOrder: number[];
6
6
  protected abstract triangleOrder: number[];
7
- protected abstract params: Record<string, any>;
7
+ protected abstract params: T;
8
8
  protected vertices: Vector3[];
9
9
  protected matrix: Mat4;
10
10
  protected geometryType: 'list' | 'strip';
@@ -18,7 +18,7 @@ export declare abstract class Geometry {
18
18
  getWireframeBuffer(color: Color): number[];
19
19
  getTriangleBuffer(color: Color): number[];
20
20
  }
21
- export declare class PlaneGeometry extends Geometry {
21
+ export declare class PlaneGeometry extends Geometry<EmptyParams> {
22
22
  protected params: {};
23
23
  protected wireframeOrder: number[];
24
24
  protected triangleOrder: number[];
@@ -28,7 +28,7 @@ export declare class PlaneGeometry extends Geometry {
28
28
  updateVertices(vertices: Vertex[]): void;
29
29
  getTriangleBuffer(color: Color): number[];
30
30
  }
31
- export declare class CubeGeometry extends Geometry {
31
+ export declare class CubeGeometry extends Geometry<CubeGeometryParams> {
32
32
  protected params: CubeGeometryParams;
33
33
  protected wireframeOrder: number[];
34
34
  protected triangleOrder: number[];
@@ -39,7 +39,7 @@ export declare class CubeGeometry extends Geometry {
39
39
  recompute(): void;
40
40
  updateSize(width: number, height: number, depth: number): void;
41
41
  }
42
- export declare class SquareGeometry extends Geometry {
42
+ export declare class SquareGeometry extends Geometry<SquareGeometryParams> {
43
43
  protected wireframeOrder: number[];
44
44
  protected triangleOrder: number[];
45
45
  protected params: SquareGeometryParams;
@@ -50,14 +50,14 @@ export declare class SquareGeometry extends Geometry {
50
50
  recompute(): void;
51
51
  getTriangleBuffer(color: Color): number[];
52
52
  }
53
- export declare class BlankGeometry extends Geometry {
53
+ export declare class BlankGeometry extends Geometry<EmptyParams> {
54
54
  protected wireframeOrder: never[];
55
55
  protected triangleOrder: never[];
56
56
  protected params: {};
57
57
  constructor();
58
58
  recompute(): void;
59
59
  }
60
- export declare class CircleGeometry extends Geometry {
60
+ export declare class CircleGeometry extends Geometry<CircleGeometryParams> {
61
61
  protected wireframeOrder: number[];
62
62
  protected triangleOrder: number[];
63
63
  protected params: CircleGeometryParams;
@@ -67,7 +67,7 @@ export declare class CircleGeometry extends Geometry {
67
67
  private updateTriangleOrder;
68
68
  recompute(): void;
69
69
  }
70
- export declare class Spline2dGeometry extends Geometry {
70
+ export declare class Spline2dGeometry extends Geometry<Spline2dGeometryParams> {
71
71
  protected wireframeOrder: number[];
72
72
  protected triangleOrder: number[];
73
73
  protected params: Spline2dGeometryParams;
@@ -86,21 +86,21 @@ export declare class Spline2dGeometry extends Geometry {
86
86
  getWireframeBuffer(color: Color): number[];
87
87
  getTriangleBuffer(_: Color): number[];
88
88
  }
89
- export declare class Line2dGeometry extends Geometry {
89
+ export declare class Line2dGeometry extends Geometry<Line2dGeometryParams> {
90
90
  protected wireframeOrder: number[];
91
91
  protected triangleOrder: number[];
92
92
  protected params: Line2dGeometryParams;
93
93
  constructor(pos: Vector2, to: Vector2, thickness: number);
94
94
  recompute(): void;
95
95
  }
96
- export declare class Line3dGeometry extends Geometry {
96
+ export declare class Line3dGeometry extends Geometry<Line3dGeometryParams> {
97
97
  protected wireframeOrder: number[];
98
98
  protected triangleOrder: number[];
99
99
  protected params: Line3dGeometryParams;
100
100
  constructor(pos: Vector3, to: Vector3, thickness: number);
101
101
  recompute(): void;
102
102
  }
103
- export declare class PolygonGeometry extends Geometry {
103
+ export declare class PolygonGeometry extends Geometry<PolygonGeometryParams> {
104
104
  protected wireframeOrder: number[];
105
105
  protected triangleOrder: number[];
106
106
  protected params: PolygonGeometryParams;
package/dist/geometry.js CHANGED
@@ -358,6 +358,7 @@ export class Spline2dGeometry extends Geometry {
358
358
  })
359
359
  .flat();
360
360
  }
361
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
361
362
  getTriangleBuffer(_) {
362
363
  return this.triangleOrder
363
364
  .map((vertexIndex) => {
@@ -1,12 +1,12 @@
1
1
  /// <reference types="@webgpu/types" />
2
2
  import { Camera } from './simulation.js';
3
- import type { Vector2, Vector3, LerpFunc, VertexColorMap, ElementRotation, Mat4 } from './types.js';
3
+ import type { Vector2, Vector3, LerpFunc, VertexColorMap, ElementRotation, Mat4, AnySimulationElement } from './types.js';
4
4
  import { Vertex, Color } from './utils.js';
5
5
  import { BlankGeometry, CircleGeometry, CubeGeometry, Geometry, Line2dGeometry, Line3dGeometry, PlaneGeometry, PolygonGeometry, Spline2dGeometry, SquareGeometry } from './geometry.js';
6
6
  import { VertexCache } from './internalUtils.js';
7
7
  export declare abstract class SimulationElement<T extends Vector2 | Vector3 = Vector3> {
8
8
  protected abstract pos: T;
9
- protected abstract geometry: Geometry;
9
+ protected abstract geometry: Geometry<any>;
10
10
  protected color: Color;
11
11
  protected wireframe: boolean;
12
12
  protected vertexCache: VertexCache;
@@ -177,7 +177,7 @@ export declare class Spline2d extends SimulationElement2d {
177
177
  interpolate(t: number): Vector2;
178
178
  protected updateMatrix(camera: Camera): void;
179
179
  }
180
- export declare class Instance<T extends SimulationElement2d | SimulationElement3d> extends SimulationElement3d {
180
+ export declare class Instance<T extends AnySimulationElement> extends SimulationElement3d {
181
181
  protected geometry: BlankGeometry;
182
182
  private obj;
183
183
  private instanceMatrix;
package/dist/graphics.js CHANGED
@@ -650,9 +650,9 @@ export class BezierCurve2d {
650
650
  interpolateSlope(t) {
651
651
  t = Math.max(0, Math.min(1, t));
652
652
  let vectors = this.points;
653
- let slopeVector = vector2(1);
653
+ const slopeVector = vector2(1);
654
654
  while (vectors.length > 2) {
655
- let newVectors = [];
655
+ const newVectors = [];
656
656
  for (let i = 1; i < vectors.length - 1; i++) {
657
657
  const from = vector2();
658
658
  const to = vector2();
@@ -670,7 +670,7 @@ export class BezierCurve2d {
670
670
  vectors = newVectors;
671
671
  }
672
672
  vec2.sub(vectors[1], vectors[0], slopeVector);
673
- let resVector = vector2();
673
+ const resVector = vector2();
674
674
  vec2.scale(slopeVector, t, resVector);
675
675
  vec2.add(resVector, vectors[0], resVector);
676
676
  return [resVector, slopeVector];
@@ -987,6 +987,7 @@ export class Instance extends SimulationElement3d {
987
987
  getGeometryType() {
988
988
  return this.obj.getGeometryType();
989
989
  }
990
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
990
991
  updateMatrix(_) { }
991
992
  getBuffer(camera) {
992
993
  if (this.needsRemap)
@@ -1,7 +1,6 @@
1
1
  /// <reference types="@webgpu/types" />
2
- import { Mat4, Vector2, Vector3 } from './types.js';
2
+ import { AnySimulationElement, Mat4, Vector2, Vector3 } from './types.js';
3
3
  import { Color } from './utils.js';
4
- import { SimulationElement } from './graphics.js';
5
4
  export declare class VertexCache {
6
5
  private vertices;
7
6
  private hasUpdated;
@@ -17,15 +16,15 @@ export declare const getTransformationMatrix: (pos: Vector3, rotation: Vector3,
17
16
  export declare const getOrthoMatrix: (screenSize: [number, number]) => Float32Array;
18
17
  export declare const buildDepthTexture: (device: GPUDevice, width: number, height: number) => GPUTexture;
19
18
  export declare const buildMultisampleTexture: (device: GPUDevice, ctx: GPUCanvasContext, width: number, height: number) => GPUTexture;
20
- export declare const addObject: (scene: SimSceneObjInfo[], el: SimulationElement<any>, device: GPUDevice | null, id?: string) => void;
21
- export declare const removeObject: (scene: SimSceneObjInfo[], el: SimulationElement<any>) => void;
19
+ export declare const addObject: (scene: SimSceneObjInfo[], el: AnySimulationElement, device: GPUDevice | null, id?: string) => void;
20
+ export declare const removeObject: (scene: SimSceneObjInfo[], el: AnySimulationElement) => void;
22
21
  export declare const removeObjectId: (scene: SimSceneObjInfo[], id: string) => void;
23
22
  export declare class SimSceneObjInfo {
24
23
  private obj;
25
24
  private id;
26
25
  private lifetime;
27
26
  private currentLife;
28
- constructor(obj: SimulationElement<any>, id?: string);
27
+ constructor(obj: AnySimulationElement, id?: string);
29
28
  /**
30
29
  * @param lifetime - ms
31
30
  */
@@ -36,7 +35,7 @@ export declare class SimSceneObjInfo {
36
35
  * @param amount - ms
37
36
  */
38
37
  traverseLife(amount: number): void;
39
- getObj(): SimulationElement<Vector3 | Vector2>;
38
+ getObj(): AnySimulationElement;
40
39
  getId(): string | null;
41
40
  }
42
41
  declare class Logger {
@@ -197,7 +197,7 @@ export function vector2ToPixelRatio(vec) {
197
197
  vec[1] *= devicePixelRatio;
198
198
  }
199
199
  export function matrixFromRotation(rotation) {
200
- let rotMatrix = mat4.identity();
200
+ const rotMatrix = mat4.identity();
201
201
  mat4.rotateZ(rotMatrix, rotation[2], rotMatrix);
202
202
  mat4.rotateY(rotMatrix, rotation[1], rotMatrix);
203
203
  mat4.rotateX(rotMatrix, rotation[0], rotMatrix);
@@ -1,6 +1,6 @@
1
1
  /// <reference types="@webgpu/types" />
2
- import { SimulationElement, SimulationElement3d } from './graphics.js';
3
- import type { Vector2, Vector3, LerpFunc } from './types.js';
2
+ import { SimulationElement3d } from './graphics.js';
3
+ import type { Vector2, Vector3, LerpFunc, AnySimulationElement } from './types.js';
4
4
  import { Color } from './utils.js';
5
5
  import { BlankGeometry } from './geometry.js';
6
6
  import { SimSceneObjInfo } from './internalUtils.js';
@@ -17,20 +17,20 @@ export declare class Simulation {
17
17
  private pipelines;
18
18
  private renderInfo;
19
19
  constructor(idOrCanvasRef: string | HTMLCanvasElement, camera?: Camera | null, showFrameRate?: boolean);
20
- add(el: SimulationElement<any>, id?: string): void;
21
- remove(el: SimulationElement<any>): void;
20
+ add(el: AnySimulationElement, id?: string): void;
21
+ remove(el: AnySimulationElement): void;
22
22
  removeId(id: string): void;
23
23
  /**
24
24
  * @param lifetime - ms
25
25
  */
26
- setLifetime(el: SimulationElement<any>, lifetime: number): void;
26
+ setLifetime(el: AnySimulationElement, lifetime: number): void;
27
27
  setCanvasSize(width: number, height: number): void;
28
28
  start(): void;
29
29
  private propagateDevice;
30
30
  stop(): void;
31
31
  setBackground(color: Color): void;
32
32
  getScene(): SimSceneObjInfo[];
33
- getSceneObjects(): SimulationElement<Vector3 | Vector2>[];
33
+ getSceneObjects(): AnySimulationElement[];
34
34
  private render;
35
35
  private renderScene;
36
36
  fitElement(): void;
@@ -47,16 +47,16 @@ export declare class SceneCollection extends SimulationElement3d {
47
47
  setDevice(device: GPUDevice): void;
48
48
  private propagateDevice;
49
49
  getVertexCount(): number;
50
- getSceneObjects(): SimulationElement<Vector3 | Vector2>[];
51
- setSceneObjects(newScene: SimulationElement<any>[]): void;
50
+ getSceneObjects(): AnySimulationElement[];
51
+ setSceneObjects(newScene: AnySimulationElement[]): void;
52
52
  setScene(newScene: SimSceneObjInfo[]): void;
53
- add(el: SimulationElement<any>, id?: string): void;
54
- remove(el: SimulationElement<any>): void;
53
+ add(el: AnySimulationElement, id?: string): void;
54
+ remove(el: AnySimulationElement): void;
55
55
  removeId(id: string): void;
56
56
  /**
57
57
  * @param lifetime - ms
58
58
  */
59
- setLifetime(el: SimulationElement<any>, lifetime: number): void;
59
+ setLifetime(el: AnySimulationElement, lifetime: number): void;
60
60
  empty(): void;
61
61
  getSceneBuffer(camera: Camera): number[];
62
62
  getWireframe(camera: Camera): number[];
@@ -299,6 +299,7 @@ export class Simulation {
299
299
  ]
300
300
  });
301
301
  const colorAttachment = {
302
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
302
303
  // @ts-ignore
303
304
  view: undefined, // Assigned later
304
305
  clearValue: this.bgColor.toObject(),
@@ -361,8 +362,10 @@ export class Simulation {
361
362
  depthTexture = buildDepthTexture(device, screenSize[0], screenSize[1]);
362
363
  renderPassDescriptor.depthStencilAttachment.view = depthTexture.createView();
363
364
  }
365
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
364
366
  // @ts-ignore
365
367
  renderPassDescriptor.colorAttachments[0].view = multisampleTexture.createView();
368
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
366
369
  // @ts-ignore
367
370
  renderPassDescriptor.colorAttachments[0].resolveTarget = ctx.getCurrentTexture().createView();
368
371
  if (this.camera.hasUpdated()) {
@@ -395,7 +398,7 @@ export class Simulation {
395
398
  if (this.pipelines === null)
396
399
  return 0;
397
400
  let currentOffset = startOffset;
398
- let toRemove = [];
401
+ const toRemove = [];
399
402
  for (let i = 0; i < scene.length; i++) {
400
403
  const lifetime = scene[i].getLifetime();
401
404
  if (lifetime !== null) {
@@ -501,6 +504,7 @@ export class SceneCollection extends SimulationElement3d {
501
504
  this.scene = [];
502
505
  this.geometry = new BlankGeometry();
503
506
  }
507
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
504
508
  setWireframe(_) { }
505
509
  getName() {
506
510
  return this.name;
package/dist/types.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  /// <reference types="@webgpu/types" />
2
- import { CubicBezierCurve2d, SplinePoint2d } from './graphics.js';
2
+ import { CubicBezierCurve2d, SimulationElement2d, SimulationElement3d, SplinePoint2d } from './graphics.js';
3
3
  import { Color, Vertex } from './utils.js';
4
- export type Shift<T extends any[]> = ((...args: T) => any) extends (arg1: any, ...rest: infer R) => any ? R : never;
5
4
  export type FloatArray = Float32Array | Float64Array;
6
5
  export type Vector4 = FloatArray & [number, number, number, number];
7
6
  export type Vector3 = FloatArray & [number, number, number];
@@ -27,6 +26,8 @@ export type Mat4 = FloatArray & [
27
26
  export type LerpFunc = (n: number) => number;
28
27
  export type VertexColorMap = Record<number, Color>;
29
28
  export type ElementRotation<T extends Vector2 | Vector3> = T extends Vector2 ? number : T;
29
+ export type AnySimulationElement = SimulationElement2d | SimulationElement3d;
30
+ export type EmptyParams = object;
30
31
  export type CubeGeometryParams = {
31
32
  width: number;
32
33
  height: number;
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { SimulationElement, SplinePoint2d } from './graphics.js';
2
- import { FloatArray, Mat4, Shift, Vector2, Vector3, Vector4 } from './types.js';
1
+ import { SplinePoint2d } from './graphics.js';
2
+ import { AnySimulationElement, FloatArray, Mat4, Vector2, Vector3, Vector4 } from './types.js';
3
3
  import { SimSceneObjInfo } from './internalUtils.js';
4
4
  export declare class Color {
5
5
  r: number;
@@ -44,7 +44,7 @@ export declare class Vertex {
44
44
  * @returns {Promise<void>}
45
45
  */
46
46
  export declare function transitionValues(callback1: (deltaT: number, t: number) => void, callback2: () => void, transitionLength: number, func?: (n: number) => number): Promise<void>;
47
- export declare function frameLoop<T extends (...args: any[]) => any>(cb: T): (...params: Shift<Parameters<T>>) => void;
47
+ export declare function frameLoop<T extends (dt: number, ...args: any[]) => any>(cb: T): (...params: Parameters<T>) => void;
48
48
  export declare function lerp(a: number, b: number, t: number): number;
49
49
  export declare function smoothStep(t: number): number;
50
50
  export declare function linearStep(t: number): number;
@@ -78,6 +78,6 @@ export declare function continuousSplinePoint2d(end: Vertex, control: Vector2, d
78
78
  export declare function waitFor(t: number): Promise<unknown>;
79
79
  export declare function distance2d(vector1: Vector2, vector2: Vector2): number;
80
80
  export declare function distance3d(vector1: Vector3, vector2: Vector3): number;
81
- export declare function toSceneObjInfo(el: SimulationElement<any>, id?: string): SimSceneObjInfo;
82
- export declare function toSceneObjInfoMany(el: SimulationElement<any>[], id?: (string | undefined)[]): SimSceneObjInfo[];
81
+ export declare function toSceneObjInfo(el: AnySimulationElement, id?: string): SimSceneObjInfo;
82
+ export declare function toSceneObjInfoMany(el: AnySimulationElement[], id?: (string | undefined)[]): SimSceneObjInfo[];
83
83
  export declare function interpolateColors(colors: Color[], t: number): Color;
package/dist/utils.js CHANGED
@@ -121,6 +121,7 @@ export function transitionValues(callback1, callback2, transitionLength, func) {
121
121
  }
122
122
  });
123
123
  }
124
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
124
125
  export function frameLoop(cb) {
125
126
  let prevFrame = 0;
126
127
  let prevTime = 0;
@@ -0,0 +1,6 @@
1
+ // @ts-check
2
+
3
+ import eslint from '@eslint/js';
4
+ import tseslint from 'typescript-eslint';
5
+
6
+ export default tseslint.config(eslint.configs.recommended, ...tseslint.configs.recommended);
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.4.8",
8
+ "version": "0.4.9",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": "./dist/index.js",