simulationjsv2 0.8.3 → 0.9.0

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.
@@ -105,4 +105,5 @@ export declare class TraceLines2dGeometry extends Geometry<TraceLinesParams> {
105
105
  getVertexCount(): number;
106
106
  getOrder(_: boolean): readonly [Vector3[], number[]];
107
107
  addVertex(vert: Vector3): void;
108
+ clear(): void;
108
109
  }
package/dist/geometry.js CHANGED
@@ -110,10 +110,10 @@ export class SquareGeometry extends Geometry {
110
110
  }
111
111
  recompute() {
112
112
  this.vertices = [
113
- vector3(-this.params.width, this.params.height),
114
- vector3(this.params.width, this.params.height),
115
- vector3(this.params.width, -this.params.height),
116
- vector3(-this.params.width, -this.params.height)
113
+ vector3(-this.params.width / 2, this.params.height / 2),
114
+ vector3(this.params.width / 2, this.params.height / 2),
115
+ vector3(this.params.width / 2, -this.params.height / 2),
116
+ vector3(-this.params.width / 2, -this.params.height / 2)
117
117
  ];
118
118
  }
119
119
  }
@@ -310,7 +310,7 @@ export class Line2dGeometry extends Geometry {
310
310
  recompute() {
311
311
  const normal = vector2(-this.params.to[1], this.params.to[0]);
312
312
  vec2.normalize(normal, normal);
313
- vec2.scale(normal, this.params.thickness / 2, normal);
313
+ vec2.scale(normal, this.params.thickness, normal);
314
314
  this.vertices = [
315
315
  vector3(-normal[0], -normal[1]),
316
316
  vector3(normal[0], normal[1]),
@@ -386,4 +386,8 @@ export class TraceLines2dGeometry extends Geometry {
386
386
  this.wireframeOrder.push(this.wireframeOrder.length);
387
387
  }
388
388
  }
389
+ clear() {
390
+ this.vertices = [];
391
+ this.wireframeOrder = [];
392
+ }
389
393
  }
@@ -1,8 +1,8 @@
1
1
  /// <reference types="@webgpu/types" />
2
- import type { Vector2, Vector3, LerpFunc, Mat4, AnySimulationElement } from './types.js';
2
+ import type { Vector2, Vector3, LerpFunc, Mat4 } from './types.js';
3
3
  import { Vertex, Color } from './utils.js';
4
4
  import { BlankGeometry, CircleGeometry, CubeGeometry, Geometry, Line2dGeometry, Line3dGeometry, PlaneGeometry, PolygonGeometry, Spline2dGeometry, SquareGeometry, TraceLines2dGeometry as TraceLinesGeometry } from './geometry.js';
5
- import { SimSceneObjInfo, Float32ArrayCache } from './internalUtils.js';
5
+ import { Float32ArrayCache } from './internalUtils.js';
6
6
  import { Shader } from './shaders.js';
7
7
  import { Material } from './materials.js';
8
8
  export declare abstract class SimulationElement3d {
@@ -10,6 +10,7 @@ export declare abstract class SimulationElement3d {
10
10
  private uniformBuffer;
11
11
  private prevInfo;
12
12
  private pipeline;
13
+ protected id: string | null;
13
14
  protected shader: Shader;
14
15
  protected material: Material;
15
16
  protected cullMode: GPUCullMode;
@@ -25,14 +26,16 @@ export declare abstract class SimulationElement3d {
25
26
  isInstanced: boolean;
26
27
  is3d: boolean;
27
28
  isEmpty: boolean;
28
- /**
29
- * @param pos - Expected to be adjusted to devicePixelRatio before reaching constructor
30
- */
31
29
  constructor(pos: Vector3, rotation: Vector3, color?: Color);
30
+ getId(): string | null;
31
+ setId(id: string): void;
32
+ getColor(): Color;
32
33
  add(el: SimulationElement3d, id?: string): void;
33
34
  remove(el: SimulationElement3d): void;
35
+ removeId(id: string): void;
34
36
  getChildren(): SimulationElement3d[];
35
- getChildrenInfos(): SimSceneObjInfo[];
37
+ setChildren(elements: SimulationElement3d[]): void;
38
+ getChildrenInfos(): SimulationElement3d[];
36
39
  hasChildren(): boolean;
37
40
  setParent(parent: SimulationElement3d): void;
38
41
  getParent(): SimulationElement3d | null;
@@ -61,8 +64,8 @@ export declare abstract class SimulationElement3d {
61
64
  getRotation(): Vector3;
62
65
  getCenterOffset(): Vector3;
63
66
  fill(newColor: Color, t?: number, f?: LerpFunc): Promise<void>;
64
- move(amount: Vector3, t?: number, f?: LerpFunc, fromDevicePixelRatio?: boolean): Promise<void>;
65
- moveTo(pos: Vector3, t?: number, f?: LerpFunc, fromDevicePixelRatio?: boolean): Promise<void>;
67
+ move(amount: Vector3, t?: number, f?: LerpFunc): Promise<void>;
68
+ moveTo(pos: Vector3, t?: number, f?: LerpFunc): Promise<void>;
66
69
  rotateChildren(angle: Vector3): void;
67
70
  rotate(amount: Vector3, t?: number, f?: LerpFunc): Promise<void>;
68
71
  rotateTo(rot: Vector3, t?: number, f?: LerpFunc): Promise<void>;
@@ -205,7 +208,7 @@ export declare class Spline2d extends SimulationElement2d {
205
208
  interpolateSlope(t: number): Vector2[] | readonly [Vector2, Vector2];
206
209
  interpolate(t: number): Vector2;
207
210
  }
208
- export declare class Instance<T extends AnySimulationElement> extends SimulationElement3d {
211
+ export declare class Instance<T extends SimulationElement3d> extends SimulationElement3d {
209
212
  protected geometry: BlankGeometry;
210
213
  private obj;
211
214
  private instanceMatrix;
@@ -231,12 +234,13 @@ export declare class Instance<T extends AnySimulationElement> extends Simulation
231
234
  export declare class TraceLines2d extends SimulationElement2d {
232
235
  protected geometry: TraceLinesGeometry;
233
236
  constructor(color?: Color, maxLen?: number);
234
- addPoint(vert: Vector2 | Vector3, _color?: Color): void;
237
+ addPoint(vert: Vector2 | Vector3, color?: Color): void;
238
+ clear(): void;
235
239
  isWireframe(): boolean;
236
240
  }
237
241
  export declare class TraceLines3d extends SimulationElement3d {
238
242
  protected geometry: TraceLinesGeometry;
239
243
  constructor(color?: Color, maxLen?: number);
240
- addPoint(vert: Vector2 | Vector3, _color?: Color): void;
244
+ addPoint(vert: Vector2 | Vector3, color?: Color): void;
241
245
  isWireframe(): boolean;
242
246
  }
package/dist/graphics.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { vec3, mat4, vec2 } from 'wgpu-matrix';
2
2
  import { cloneBuf, vector2, vector3, Color, vector2FromVector3, matrix4, vector3FromVector2, distance2d, color, interpolateColors } from './utils.js';
3
3
  import { BlankGeometry, CircleGeometry, CubeGeometry, Line2dGeometry, Line3dGeometry, PlaneGeometry, PolygonGeometry, Spline2dGeometry, SquareGeometry, TraceLines2dGeometry as TraceLinesGeometry } from './geometry.js';
4
- import { SimSceneObjInfo, Float32ArrayCache, internalTransitionValues, posTo2dScreen, vector3ToPixelRatio } from './internalUtils.js';
4
+ import { Float32ArrayCache, internalTransitionValues, posTo2dScreen } from './internalUtils.js';
5
5
  import { mat4ByteLength, modelProjMatOffset } from './constants.js';
6
6
  import { MemoBuffer } from './buffers.js';
7
7
  import { globalInfo, logger, pipelineCache } from './globals.js';
@@ -12,6 +12,7 @@ export class SimulationElement3d {
12
12
  uniformBuffer;
13
13
  prevInfo;
14
14
  pipeline;
15
+ id;
15
16
  shader;
16
17
  material;
17
18
  cullMode;
@@ -26,9 +27,6 @@ export class SimulationElement3d {
26
27
  isInstanced = false;
27
28
  is3d = true;
28
29
  isEmpty = false;
29
- /**
30
- * @param pos - Expected to be adjusted to devicePixelRatio before reaching constructor
31
- */
32
30
  constructor(pos, rotation, color = new Color()) {
33
31
  this.pos = pos;
34
32
  this.centerOffset = vector3();
@@ -44,21 +42,44 @@ export class SimulationElement3d {
44
42
  this.shader = defaultShader;
45
43
  this.material = new BasicMaterial(color);
46
44
  this.cullMode = 'back';
45
+ this.id = null;
46
+ }
47
+ getId() {
48
+ return this.id;
49
+ }
50
+ setId(id) {
51
+ this.id = id;
52
+ }
53
+ getColor() {
54
+ return this.material.getColor();
47
55
  }
48
56
  add(el, id) {
49
57
  el.setParent(this);
50
- const info = new SimSceneObjInfo(el, id);
51
- this.children.push(info);
58
+ if (id)
59
+ el.setId(id);
60
+ this.children.push(el);
52
61
  }
53
62
  remove(el) {
54
63
  for (let i = 0; i < this.children.length; i++) {
55
- if (this.children[i].getObj() === el) {
64
+ if (this.children[i] === el) {
65
+ this.children.splice(i, 1);
66
+ break;
67
+ }
68
+ }
69
+ }
70
+ removeId(id) {
71
+ for (let i = 0; i < this.children.length; i++) {
72
+ if (this.children[i].getId() === id) {
56
73
  this.children.splice(i, 1);
74
+ break;
57
75
  }
58
76
  }
59
77
  }
60
78
  getChildren() {
61
- return this.children.map((child) => child.getObj());
79
+ return this.children;
80
+ }
81
+ setChildren(elements) {
82
+ this.children = elements;
62
83
  }
63
84
  getChildrenInfos() {
64
85
  return this.children;
@@ -126,7 +147,8 @@ export class SimulationElement3d {
126
147
  if (!this.parent)
127
148
  return;
128
149
  this.parent.mirrorParentTransforms3d(mat);
129
- mat4.translate(mat, this.parent.getRelativePos(), mat);
150
+ const pos = cloneBuf(this.parent.getRelativePos());
151
+ mat4.translate(mat, pos, mat);
130
152
  const parentRot = this.parent.getRotation();
131
153
  mat4.rotateZ(mat, parentRot[2], mat);
132
154
  mat4.rotateY(mat, parentRot[1], mat);
@@ -137,7 +159,8 @@ export class SimulationElement3d {
137
159
  if (this.parent) {
138
160
  this.mirrorParentTransforms3d(this.modelMatrix);
139
161
  }
140
- mat4.translate(this.modelMatrix, this.pos, this.modelMatrix);
162
+ const pos = cloneBuf(this.pos);
163
+ mat4.translate(this.modelMatrix, pos, this.modelMatrix);
141
164
  mat4.rotateZ(this.modelMatrix, this.rotation[2], this.modelMatrix);
142
165
  mat4.rotateY(this.modelMatrix, this.rotation[1], this.modelMatrix);
143
166
  mat4.rotateX(this.modelMatrix, this.rotation[0], this.modelMatrix);
@@ -152,7 +175,8 @@ export class SimulationElement3d {
152
175
  this.parent.mirrorParentTransforms2d(mat);
153
176
  const parentRot = this.parent.getRotation();
154
177
  mat4.rotateZ(mat, parentRot[2], mat);
155
- mat4.translate(mat, this.pos, mat);
178
+ const pos = cloneBuf(this.pos);
179
+ mat4.translate(mat, pos, mat);
156
180
  }
157
181
  updateModelMatrix2d() {
158
182
  mat4.identity(this.modelMatrix);
@@ -211,10 +235,8 @@ export class SimulationElement3d {
211
235
  this.vertexCache.updated();
212
236
  }, t, f);
213
237
  }
214
- move(amount, t = 0, f, fromDevicePixelRatio = false) {
238
+ move(amount, t = 0, f) {
215
239
  const tempAmount = cloneBuf(amount);
216
- if (!fromDevicePixelRatio)
217
- vector3ToPixelRatio(tempAmount);
218
240
  const finalPos = cloneBuf(this.pos);
219
241
  vec3.add(finalPos, tempAmount, finalPos);
220
242
  return internalTransitionValues((p) => {
@@ -225,10 +247,8 @@ export class SimulationElement3d {
225
247
  this.pos = finalPos;
226
248
  }, t, f);
227
249
  }
228
- moveTo(pos, t = 0, f, fromDevicePixelRatio = false) {
250
+ moveTo(pos, t = 0, f) {
229
251
  const tempPos = cloneBuf(pos);
230
- if (!fromDevicePixelRatio)
231
- vector3ToPixelRatio(tempPos);
232
252
  const diff = vector3();
233
253
  vec3.sub(tempPos, this.pos, diff);
234
254
  return internalTransitionValues((p) => {
@@ -241,7 +261,7 @@ export class SimulationElement3d {
241
261
  }
242
262
  rotateChildren(angle) {
243
263
  for (let i = 0; i < this.children.length; i++) {
244
- this.children[i].getObj().rotate(angle);
264
+ this.children[i].rotate(angle);
245
265
  }
246
266
  }
247
267
  rotate(amount, t = 0, f) {
@@ -275,14 +295,14 @@ export class SimulationElement3d {
275
295
  }
276
296
  let vertexCount = this.geometry.getIndexes(this.isWireframe()).length;
277
297
  for (let i = 0; i < this.children.length; i++) {
278
- vertexCount += this.children[i].getObj().getVertexCount();
298
+ vertexCount += this.children[i].getVertexCount();
279
299
  }
280
300
  return vertexCount;
281
301
  }
282
302
  getIndexCount() {
283
303
  let indexCount = this.geometry.getIndexes(this.isWireframe()).length;
284
304
  for (let i = 0; i < this.children.length; i++) {
285
- indexCount += this.children[i].getObj().getIndexCount();
305
+ indexCount += this.children[i].getIndexCount();
286
306
  }
287
307
  return indexCount;
288
308
  }
@@ -295,9 +315,8 @@ export class SimulationElement3d {
295
315
  const vertices = this.geometry.getVertices();
296
316
  const stride = this.shader.getBufferLength();
297
317
  const vertexBuffer = new Float32Array(vertices.length * stride);
298
- const shader = this.isWireframe() ? defaultShader : this.shader;
299
318
  for (let i = 0; i < vertices.length; i++) {
300
- shader.setVertexInfo(this, vertexBuffer, vertices[i], i, i * stride);
319
+ this.shader.setVertexInfo(this, vertexBuffer, vertices[i], i, i * stride);
301
320
  }
302
321
  this.vertexCache.setCache(vertexBuffer);
303
322
  return vertexBuffer;
@@ -325,7 +344,6 @@ export class SimulationElement2d extends SimulationElement3d {
325
344
  is3d = false;
326
345
  constructor(pos, rotation = vector3(), color) {
327
346
  super(vector3FromVector2(pos), rotation, color);
328
- vector3ToPixelRatio(this.pos);
329
347
  }
330
348
  rotate2d(amount, t = 0, f) {
331
349
  return super.rotate(vector3(0, 0, amount), t, f);
@@ -363,8 +381,8 @@ export class Square extends SimulationElement2d {
363
381
  */
364
382
  constructor(pos, width, height, color, rotation) {
365
383
  super(pos, vector3(0, 0, rotation), color);
366
- this.width = width / devicePixelRatio;
367
- this.height = height / devicePixelRatio;
384
+ this.width = width;
385
+ this.height = height;
368
386
  this.geometry = new SquareGeometry(this.width, this.height);
369
387
  }
370
388
  getWidth() {
@@ -419,7 +437,6 @@ export class Square extends SimulationElement2d {
419
437
  }, t, f);
420
438
  }
421
439
  setWidth(num, t = 0, f) {
422
- num *= devicePixelRatio;
423
440
  const diffWidth = num - this.width;
424
441
  return internalTransitionValues((p) => {
425
442
  this.width += diffWidth * p;
@@ -432,7 +449,6 @@ export class Square extends SimulationElement2d {
432
449
  }, t, f);
433
450
  }
434
451
  setHeight(num, t = 0, f) {
435
- num *= devicePixelRatio;
436
452
  const diffHeight = num - this.height;
437
453
  return internalTransitionValues((p) => {
438
454
  this.height += diffHeight * p;
@@ -451,12 +467,11 @@ export class Circle extends SimulationElement2d {
451
467
  detail;
452
468
  constructor(pos, radius, color, detail = 50) {
453
469
  super(pos, vector3(), color);
454
- this.radius = radius * devicePixelRatio;
470
+ this.radius = radius;
455
471
  this.detail = detail;
456
472
  this.geometry = new CircleGeometry(this.radius, this.detail);
457
473
  }
458
474
  setRadius(num, t = 0, f) {
459
- num *= devicePixelRatio;
460
475
  const diff = num - this.radius;
461
476
  return internalTransitionValues((p) => {
462
477
  this.radius += diff * p;
@@ -568,7 +583,6 @@ export class Line3d extends SimulationElement3d {
568
583
  super(pos.getPos(), vector3(), to.getColor() ?? undefined);
569
584
  this.thickness = thickness;
570
585
  this.to = to.getPos();
571
- vec3.scale(this.to, devicePixelRatio, this.to);
572
586
  vec3.sub(this.to, this.pos, this.to);
573
587
  this.geometry = new Line3dGeometry(this.pos, this.to, this.thickness);
574
588
  }
@@ -597,7 +611,7 @@ export class Line2d extends SimulationElement2d {
597
611
  thickness;
598
612
  constructor(from, to, thickness = 1) {
599
613
  super(vector2FromVector3(from.getPos()), vector3(), from.getColor() ?? undefined);
600
- this.thickness = thickness * devicePixelRatio;
614
+ this.thickness = thickness;
601
615
  this.to = to.getPos();
602
616
  vec2.sub(this.to, this.pos, this.to);
603
617
  this.geometry = new Line2dGeometry(this.pos, this.to, this.thickness);
@@ -607,8 +621,6 @@ export class Line2d extends SimulationElement2d {
607
621
  }
608
622
  setEnd(pos, t = 0, f) {
609
623
  const tempPos = cloneBuf(pos);
610
- vector3ToPixelRatio(tempPos);
611
- // vec2.sub(tempPos, this.getPos(), tempPos);
612
624
  const diff = vector3();
613
625
  vec2.sub(tempPos, this.to, diff);
614
626
  return internalTransitionValues((p) => {
@@ -636,7 +648,6 @@ export class Cube extends SimulationElement3d {
636
648
  this.geometry = new CubeGeometry(this.width, this.height, this.depth);
637
649
  }
638
650
  setWidth(width, t = 0, f) {
639
- width *= devicePixelRatio;
640
651
  const diff = width - this.width;
641
652
  return internalTransitionValues((p) => {
642
653
  this.width += diff * p;
@@ -649,7 +660,6 @@ export class Cube extends SimulationElement3d {
649
660
  }, t, f);
650
661
  }
651
662
  setHeight(height, t = 0, f) {
652
- height *= devicePixelRatio;
653
663
  const diff = height - this.width;
654
664
  return internalTransitionValues((p) => {
655
665
  this.height += diff * p;
@@ -662,7 +672,6 @@ export class Cube extends SimulationElement3d {
662
672
  }, t, f);
663
673
  }
664
674
  setDepth(depth, t = 0, f) {
665
- depth *= devicePixelRatio;
666
675
  const diff = depth - this.width;
667
676
  return internalTransitionValues((p) => {
668
677
  this.depth += diff * p;
@@ -848,10 +857,10 @@ export class Spline2d extends SimulationElement2d {
848
857
  interpolateStart;
849
858
  interpolateLimit;
850
859
  length;
851
- constructor(pos, points, thickness = devicePixelRatio, detail = 40) {
860
+ constructor(pos, points, thickness = 1, detail = 40) {
852
861
  const tempPos = vector2FromVector3(pos.getPos());
853
862
  super(tempPos, vector3(), pos.getColor() ?? undefined);
854
- this.thickness = thickness * devicePixelRatio;
863
+ this.thickness = thickness;
855
864
  this.detail = detail;
856
865
  this.interpolateStart = 0;
857
866
  this.interpolateLimit = 1;
@@ -950,7 +959,6 @@ export class Spline2d extends SimulationElement2d {
950
959
  this.vertexCache.updated();
951
960
  }
952
961
  setThickness(thickness, t = 0, f) {
953
- thickness *= devicePixelRatio;
954
962
  const diff = thickness - this.thickness;
955
963
  return internalTransitionValues((p) => {
956
964
  this.thickness += diff * p;
@@ -1044,7 +1052,6 @@ export class Instance extends SimulationElement3d {
1044
1052
  const device = globalInfo.getDevice();
1045
1053
  if (!device)
1046
1054
  return;
1047
- // this.allocBuffer(size);
1048
1055
  const gpuBuffer = this.matrixBuffer.getBuffer();
1049
1056
  const buf = new Float32Array(transformation);
1050
1057
  device.queue.writeBuffer(gpuBuffer, instance * mat4ByteLength, buf.buffer, buf.byteOffset, buf.byteLength);
@@ -1099,14 +1106,20 @@ export class TraceLines2d extends SimulationElement2d {
1099
1106
  constructor(color, maxLen) {
1100
1107
  super(vector2(), vector3(), color);
1101
1108
  this.geometry = new TraceLinesGeometry(maxLen);
1109
+ this.material = new VertexColorMaterial();
1110
+ if (color)
1111
+ this.material.setColor(color);
1112
+ this.shader = vertexColorShader;
1102
1113
  }
1103
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
1104
- addPoint(vert, _color) {
1114
+ addPoint(vert, color) {
1105
1115
  const newVert = vert.length < 3 ? vector3(vert[0] ?? 0, vert[1] ?? 0, 0) : vert;
1106
- // const vert = vertex(point[0], point[1], point?.[2] || 0, color);
1107
1116
  this.geometry.addVertex(newVert);
1117
+ this.material.addVertexColor(color ?? this.material.getColor());
1108
1118
  this.vertexCache.updated();
1109
1119
  }
1120
+ clear() {
1121
+ this.geometry.clear();
1122
+ }
1110
1123
  // always being wireframe means that triangleOrder
1111
1124
  // in in the geometry does not need to be a duplicate
1112
1125
  // of wireframeOrder
@@ -1120,11 +1133,10 @@ export class TraceLines3d extends SimulationElement3d {
1120
1133
  super(vector3(), vector3(), color);
1121
1134
  this.geometry = new TraceLinesGeometry(maxLen);
1122
1135
  }
1123
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
1124
- addPoint(vert, _color) {
1125
- // const vert = vertex(point[0], point[1], point?.[2] || 0, color);
1136
+ addPoint(vert, color) {
1126
1137
  const newVert = vert.length < 3 ? vector3(vert[0] ?? 0, vert[1] ?? 0, 0) : vert;
1127
1138
  this.geometry.addVertex(newVert);
1139
+ this.material.addVertexColor(color ?? this.material.getColor());
1128
1140
  this.vertexCache.updated();
1129
1141
  }
1130
1142
  // always being wireframe means that triangleOrder
@@ -1,7 +1,7 @@
1
1
  /// <reference types="@webgpu/types" />
2
- import { Mat4, Vector2, Vector3 } from './types.js';
3
- import { SimulationElement3d } from './graphics.js';
2
+ import { Mat4, Vector3 } from './types.js';
4
3
  import { Shader } from './shaders.js';
4
+ import { SimulationElement3d } from './graphics.js';
5
5
  export declare class Float32ArrayCache {
6
6
  private vertices;
7
7
  private hasUpdated;
@@ -26,33 +26,14 @@ export declare const updateWorldProjectionMatrix: (worldProjMat: Mat4, projMat:
26
26
  export declare const updateOrthoProjectionMatrix: (mat: Mat4, screenSize: [number, number]) => Float32Array;
27
27
  export declare const buildDepthTexture: (device: GPUDevice, width: number, height: number) => GPUTexture;
28
28
  export declare const buildMultisampleTexture: (device: GPUDevice, ctx: GPUCanvasContext, width: number, height: number) => GPUTexture;
29
- export declare const removeObjectId: (scene: SimSceneObjInfo[], id: string) => void;
30
- export declare class SimSceneObjInfo {
31
- private obj;
32
- private id;
33
- private lifetime;
34
- private currentLife;
35
- constructor(obj: SimulationElement3d, id?: string);
36
- /**
37
- * @param lifetime - ms
38
- */
39
- setLifetime(lifetime: number): void;
40
- getLifetime(): number | null;
41
- lifetimeComplete(): boolean;
42
- /**
43
- * @param amount - ms
44
- */
45
- traverseLife(amount: number): void;
46
- getObj(): SimulationElement3d;
47
- getId(): string | null;
48
- }
49
29
  export declare function lossyTriangulate<T>(vertices: T[]): (readonly [T, T, T])[];
50
30
  export declare function lossyTriangulateStrip<T>(vertices: T[]): T[];
51
31
  export declare function createIndexArray(length: number): number[];
52
- export declare function vector3ToPixelRatio(vec: Vector3): void;
53
- export declare function vector2ToPixelRatio(vec: Vector2): void;
54
32
  export declare function triangulateWireFrameOrder(len: number): number[];
55
- export declare function getVertexAndIndexSize(scene: SimSceneObjInfo[]): readonly [number, number];
33
+ export declare function getVertexAndIndexSize(scene: SimulationElement3d[]): readonly [number, number];
56
34
  export declare function internalTransitionValues(onFrame: (deltaT: number, t: number, total: number) => void, adjustment: () => void, transitionLength: number, func?: (n: number) => number): Promise<void>;
57
35
  export declare function posTo2dScreen(pos: Vector3): Vector3;
58
36
  export declare function createPipeline(device: GPUDevice, info: string, shader: Shader): GPURenderPipeline;
37
+ export declare function addToScene(scene: SimulationElement3d[], el: SimulationElement3d, id?: string): void;
38
+ export declare function removeSceneObj(scene: SimulationElement3d[], el: SimulationElement3d): void;
39
+ export declare function removeSceneId(scene: SimulationElement3d[], id: string): void;
@@ -2,6 +2,8 @@ import { mat4, vec3 } from 'wgpu-matrix';
2
2
  import { cloneBuf, transitionValues } from './utils.js';
3
3
  import { camera } from './simulation.js';
4
4
  import { settings } from './settings.js';
5
+ import { SimulationElement3d } from './graphics.js';
6
+ import { logger } from './globals.js';
5
7
  export class Float32ArrayCache {
6
8
  vertices;
7
9
  hasUpdated = true;
@@ -86,52 +88,6 @@ export const buildMultisampleTexture = (device, ctx, width, height) => {
86
88
  sampleCount: 4
87
89
  });
88
90
  };
89
- export const removeObjectId = (scene, id) => {
90
- for (let i = 0; i < scene.length; i++) {
91
- if (scene[i].getId() === id) {
92
- scene.splice(i, 1);
93
- break;
94
- }
95
- }
96
- };
97
- export class SimSceneObjInfo {
98
- obj;
99
- id;
100
- lifetime; // ms
101
- currentLife;
102
- constructor(obj, id) {
103
- this.obj = obj;
104
- this.id = id || null;
105
- this.lifetime = null;
106
- this.currentLife = 0;
107
- }
108
- /**
109
- * @param lifetime - ms
110
- */
111
- setLifetime(lifetime) {
112
- this.lifetime = lifetime;
113
- }
114
- getLifetime() {
115
- return this.lifetime;
116
- }
117
- lifetimeComplete() {
118
- if (this.lifetime === null)
119
- return false;
120
- return this.currentLife >= this.lifetime;
121
- }
122
- /**
123
- * @param amount - ms
124
- */
125
- traverseLife(amount) {
126
- this.currentLife += amount;
127
- }
128
- getObj() {
129
- return this.obj;
130
- }
131
- getId() {
132
- return this.id;
133
- }
134
- }
135
91
  // optomized for speed, depending on orientation of vertices as input, shape may not be preserved
136
92
  export function lossyTriangulate(vertices) {
137
93
  const res = [];
@@ -185,15 +141,6 @@ export function createIndexArray(length) {
185
141
  .fill(0)
186
142
  .map((_, index) => index);
187
143
  }
188
- export function vector3ToPixelRatio(vec) {
189
- vec[0] *= devicePixelRatio;
190
- vec[1] *= devicePixelRatio;
191
- vec[2] *= devicePixelRatio;
192
- }
193
- export function vector2ToPixelRatio(vec) {
194
- vec[0] *= devicePixelRatio;
195
- vec[1] *= devicePixelRatio;
196
- }
197
144
  export function triangulateWireFrameOrder(len) {
198
145
  const order = Array(len)
199
146
  .fill(0)
@@ -211,7 +158,7 @@ export function getVertexAndIndexSize(scene) {
211
158
  let vertexSize = 0;
212
159
  let indexSize = 0;
213
160
  for (let i = 0; i < scene.length; i++) {
214
- const obj = scene[i].getObj();
161
+ const obj = scene[i];
215
162
  vertexSize += obj.getVertexCount() * obj.getShader().getBufferLength();
216
163
  indexSize += obj.getIndexCount();
217
164
  }
@@ -276,3 +223,29 @@ export function createPipeline(device, info, shader) {
276
223
  }
277
224
  });
278
225
  }
226
+ export function addToScene(scene, el, id) {
227
+ if (el instanceof SimulationElement3d) {
228
+ if (id)
229
+ el.setId(id);
230
+ scene.unshift(el);
231
+ }
232
+ else {
233
+ throw logger.error('Cannot add invalid SimulationElement');
234
+ }
235
+ }
236
+ export function removeSceneObj(scene, el) {
237
+ for (let i = 0; i < scene.length; i++) {
238
+ if (scene[i] === el) {
239
+ scene.splice(i, 1);
240
+ break;
241
+ }
242
+ }
243
+ }
244
+ export function removeSceneId(scene, id) {
245
+ for (let i = 0; i < scene.length; i++) {
246
+ if (scene[i].getId() === id) {
247
+ scene.splice(i, 1);
248
+ break;
249
+ }
250
+ }
251
+ }
package/dist/shaders.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { MemoBuffer } from './buffers.js';
2
2
  import { mat4ByteLength } from './constants.js';
3
3
  import { globalInfo } from './globals.js';
4
- import { color, createBindGroup, writeUniformWorldMatrix } from './utils.js';
4
+ import { createBindGroup, writeUniformWorldMatrix } from './utils.js';
5
5
  export const uniformBufferSize = mat4ByteLength * 2 + 4 * 2 + 8; // 4x4 matrix * 2 + vec2<f32> + 8 bc 144 is cool
6
6
  const defaultInfos = [
7
7
  {
@@ -216,14 +216,14 @@ fn fragment_main(
216
216
  }
217
217
  ], defaultInfos, defaultBufferWriter, defaultBindGroupGenerator, (el, buffer, vertex, _, offset) => {
218
218
  const material = el.getMaterial();
219
- const color = material.getColor();
219
+ const vertexColor = material.getColor();
220
220
  buffer[offset] = vertex[0];
221
221
  buffer[offset + 1] = vertex[1];
222
222
  buffer[offset + 2] = vertex[2];
223
- buffer[offset + 3] = color.r / 255;
224
- buffer[offset + 4] = color.g / 255;
225
- buffer[offset + 5] = color.b / 255;
226
- buffer[offset + 6] = color.a;
223
+ buffer[offset + 3] = vertexColor.r / 255;
224
+ buffer[offset + 4] = vertexColor.g / 255;
225
+ buffer[offset + 5] = vertexColor.b / 255;
226
+ buffer[offset + 6] = vertexColor.a;
227
227
  // TODO possibly change uv for textures
228
228
  buffer[offset + 7] = 0;
229
229
  buffer[offset + 8] = 0;
@@ -315,7 +315,8 @@ fn fragment_main(
315
315
  ], defaultInfos, defaultBufferWriter, defaultBindGroupGenerator, (el, buffer, vertex, vertexIndex, offset) => {
316
316
  const material = el.getMaterial();
317
317
  const colors = material.getVertexColors();
318
- const vertexColor = colors[vertexIndex] ?? color();
318
+ const vertexColor = colors[vertexIndex] ?? el.getColor();
319
+ // const vertexColor = color(0, 255, 255);
319
320
  buffer[offset] = vertex[0];
320
321
  buffer[offset + 1] = vertex[1];
321
322
  buffer[offset + 2] = vertex[2];
@@ -1,7 +1,6 @@
1
1
  import { SimulationElement3d } from './graphics.js';
2
- import type { Vector2, Vector3, LerpFunc, AnySimulationElement } from './types.js';
2
+ import type { Vector2, Vector3, LerpFunc } from './types.js';
3
3
  import { Color } from './utils.js';
4
- import { SimSceneObjInfo } from './internalUtils.js';
5
4
  import { Settings } from './settings.js';
6
5
  export declare const worldProjectionMatrix: import("./types.js").Mat4;
7
6
  export declare const orthogonalMatrix: import("./types.js").Mat4;
@@ -43,20 +42,15 @@ export declare class Simulation extends Settings {
43
42
  onResize(cb: (width: number, height: number) => void): void;
44
43
  getWidth(): number;
45
44
  getHeight(): number;
46
- add(el: AnySimulationElement, id?: string): void;
45
+ add(el: SimulationElement3d, id?: string): void;
47
46
  remove(el: SimulationElement3d): void;
48
47
  removeId(id: string): void;
49
- /**
50
- * @param lifetime - ms
51
- */
52
- setLifetime(el: AnySimulationElement, lifetime: number): void;
53
48
  private applyCanvasSize;
54
49
  setCanvasSize(width: number, height: number): void;
55
50
  start(): void;
56
51
  stop(): void;
57
52
  setBackground(color: Color): void;
58
- getScene(): SimSceneObjInfo[];
59
- getSceneObjects(): SimulationElement3d[];
53
+ getScene(): SimulationElement3d[];
60
54
  private render;
61
55
  private renderScene;
62
56
  fitElement(): void;
@@ -1,7 +1,6 @@
1
1
  import { vec3 } from 'wgpu-matrix';
2
- import { SimulationElement3d } from './graphics.js';
3
2
  import { Color, matrix4, transitionValues, vector2, vector3 } from './utils.js';
4
- import { SimSceneObjInfo, buildDepthTexture, buildMultisampleTexture, updateProjectionMatrix, getVertexAndIndexSize, removeObjectId, updateOrthoProjectionMatrix, updateWorldProjectionMatrix, CachedArray } from './internalUtils.js';
3
+ import { buildDepthTexture, buildMultisampleTexture, updateProjectionMatrix, getVertexAndIndexSize, updateOrthoProjectionMatrix, updateWorldProjectionMatrix, CachedArray, addToScene, removeSceneObj, removeSceneId } from './internalUtils.js';
5
4
  import { Settings } from './settings.js';
6
5
  import { MemoBuffer } from './buffers.js';
7
6
  import { globalInfo, logger } from './globals.js';
@@ -181,10 +180,8 @@ export class Simulation extends Settings {
181
180
  this.frameRateView = new FrameRateView(showFrameRate);
182
181
  this.frameRateView.updateFrameRate(1);
183
182
  this.transparentElements = new CachedArray();
184
- // this.vertexBuffer = new MemoBuffer(GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST, 0);
185
- // this.indexBuffer = new MemoBuffer(GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST, 0);
186
- this.vertexBuffer = new MemoBuffer(GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST, 1000);
187
- this.indexBuffer = new MemoBuffer(GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST, 1000);
183
+ this.vertexBuffer = new MemoBuffer(GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST, 0);
184
+ this.indexBuffer = new MemoBuffer(GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST, 0);
188
185
  }
189
186
  handleCanvasResize(parent) {
190
187
  if (this.fittingElement) {
@@ -202,39 +199,19 @@ export class Simulation extends Settings {
202
199
  this.resizeEvents.push(cb);
203
200
  }
204
201
  getWidth() {
205
- return (this.canvasRef?.width || 0) / devicePixelRatio;
202
+ return this.canvasRef?.width || 0;
206
203
  }
207
204
  getHeight() {
208
- return (this.canvasRef?.height || 0) / devicePixelRatio;
205
+ return this.canvasRef?.height || 0;
209
206
  }
210
207
  add(el, id) {
211
- if (el instanceof SimulationElement3d) {
212
- const obj = new SimSceneObjInfo(el, id);
213
- this.scene.unshift(obj);
214
- }
215
- else {
216
- throw logger.error('Cannot add invalid SimulationElement');
217
- }
208
+ addToScene(this.scene, el, id);
218
209
  }
219
210
  remove(el) {
220
- for (let i = 0; i < this.scene.length; i++) {
221
- if (this.scene[i].getObj() === el) {
222
- this.scene.splice(i, 1);
223
- break;
224
- }
225
- }
211
+ removeSceneObj(this.scene, el);
226
212
  }
227
213
  removeId(id) {
228
- removeObjectId(this.scene, id);
229
- }
230
- /**
231
- * @param lifetime - ms
232
- */
233
- setLifetime(el, lifetime) {
234
- for (let i = 0; i < this.scene.length; i++) {
235
- if (this.scene[i].getObj() === el)
236
- this.scene[i].setLifetime(lifetime);
237
- }
214
+ removeSceneId(this.scene, id);
238
215
  }
239
216
  applyCanvasSize(width, height) {
240
217
  if (this.canvasRef === null)
@@ -291,9 +268,6 @@ export class Simulation extends Settings {
291
268
  getScene() {
292
269
  return this.scene;
293
270
  }
294
- getSceneObjects() {
295
- return this.scene.map((item) => item.getObj());
296
- }
297
271
  render(device, ctx, canvas) {
298
272
  const colorAttachment = {
299
273
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -377,21 +351,10 @@ export class Simulation extends Settings {
377
351
  renderScene(device, passEncoder, vertexBuffer, indexBuffer, startVertexOffset, startIndexOffset, scene, numElements, diff, transparent) {
378
352
  let vertexOffset = startVertexOffset;
379
353
  let indexOffset = startIndexOffset;
380
- const toRemove = [];
381
354
  for (let i = 0; i < numElements; i++) {
382
- const sceneObj = scene[i];
383
- const lifetime = sceneObj.getLifetime();
384
- if (lifetime !== null) {
385
- const complete = sceneObj.lifetimeComplete();
386
- if (complete) {
387
- toRemove.push(i);
388
- continue;
389
- }
390
- sceneObj.traverseLife(diff);
391
- }
392
- const obj = sceneObj.getObj();
355
+ const obj = scene[i];
393
356
  if (!transparent && obj.isTransparent()) {
394
- this.transparentElements.add(sceneObj);
357
+ this.transparentElements.add(obj);
395
358
  continue;
396
359
  }
397
360
  if (obj.hasChildren()) {
@@ -419,9 +382,6 @@ export class Simulation extends Settings {
419
382
  vertexOffset += vertices.byteLength;
420
383
  indexOffset += indices.byteLength;
421
384
  }
422
- for (let i = toRemove.length - 1; i >= 0; i--) {
423
- this.remove(scene.at(i).getObj());
424
- }
425
385
  return [vertexOffset - startVertexOffset, indexOffset - startIndexOffset];
426
386
  }
427
387
  fitElement() {
package/dist/types.d.ts CHANGED
@@ -1,14 +1,16 @@
1
1
  /// <reference types="@webgpu/types" />
2
2
  import { MemoBuffer } from './buffers.js';
3
- import { CubicBezierCurve2d, SimulationElement2d, SimulationElement3d, SplinePoint2d } from './graphics.js';
3
+ import { CubicBezierCurve2d, SimulationElement3d, SplinePoint2d } from './graphics.js';
4
4
  import { Color } from './utils.js';
5
5
  export type FloatArray = Float32Array | Float64Array;
6
6
  export type UintArray = Uint8Array | Uint16Array | Uint32Array;
7
7
  export type IntArray = Int8Array | Int16Array | Int32Array;
8
8
  export type ArrayTypes = FloatArray | UintArray | IntArray;
9
- export type Vector4 = FloatArray & [number, number, number, number];
10
- export type Vector3 = FloatArray & [number, number, number];
11
9
  export type Vector2 = FloatArray & [number, number];
10
+ export type Vector3 = FloatArray & [number, number, number];
11
+ export type Vector4 = FloatArray & [number, number, number, number];
12
+ export type Vector2m = Vector2 | Vector3 | Vector4;
13
+ export type Vector3m = Vector3 | Vector4;
12
14
  export type Mat4 = FloatArray & [
13
15
  number,
14
16
  number,
@@ -31,7 +33,6 @@ export type Quat = Float32Array & [number, number, number, number];
31
33
  export type LerpFunc = (n: number) => number;
32
34
  export type VertexColorMap = Record<number, Color>;
33
35
  export type ElementRotation<T extends Vector2 | Vector3> = T extends Vector2 ? number : T;
34
- export type AnySimulationElement = SimulationElement2d | SimulationElement3d;
35
36
  export type EmptyParams = object;
36
37
  export interface CubeGeometryParams {
37
38
  width: number;
package/dist/utils.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  /// <reference types="@webgpu/types" />
2
2
  import { SimulationElement3d, SplinePoint2d } from './graphics.js';
3
- import { AnySimulationElement, FloatArray, Mat4, Vector2, Vector3, Vector4 } from './types.js';
4
- import { SimSceneObjInfo } from './internalUtils.js';
3
+ import { FloatArray, Mat4, Vector2, Vector2m, Vector3, Vector3m, Vector4 } from './types.js';
5
4
  import { Shader } from './shaders.js';
6
5
  export declare class Color {
7
6
  r: number;
@@ -79,10 +78,8 @@ export declare function continuousSplinePoint2d(end: Vertex, control: Vector2, d
79
78
  * @param t - seconds
80
79
  */
81
80
  export declare function waitFor(t: number): Promise<unknown>;
82
- export declare function distance2d(vector1: Vector2, vector2: Vector2): number;
83
- export declare function distance3d(vector1: Vector3, vector2: Vector3): number;
84
- export declare function toSceneObjInfo(el: AnySimulationElement, id?: string): SimSceneObjInfo;
85
- export declare function toSceneObjInfoMany(el: AnySimulationElement[], id?: (string | undefined)[]): SimSceneObjInfo[];
81
+ export declare function distance2d(vector1: Vector2m, vector2: Vector2m): number;
82
+ export declare function distance3d(vector1: Vector3m, vector2: Vector3m): number;
86
83
  export declare function interpolateColors(colors: Color[], t: number): Color;
87
84
  export declare function createBindGroup(shader: Shader, bindGroupIndex: number, buffers: GPUBuffer[]): GPUBindGroup;
88
85
  export declare function writeUniformWorldMatrix(el: SimulationElement3d): void;
package/dist/utils.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { mat4, vec2, vec3, vec4 } from 'wgpu-matrix';
2
2
  import { SplinePoint2d } from './graphics.js';
3
- import { SimSceneObjInfo } from './internalUtils.js';
4
3
  import { globalInfo } from './globals.js';
5
4
  import { orthogonalMatrix, worldProjectionMatrix } from './simulation.js';
6
5
  import { worldProjMatOffset } from './constants.js';
@@ -234,16 +233,11 @@ export function colorf(val, a) {
234
233
  return color(val, val, val, a);
235
234
  }
236
235
  export function splinePoint2d(end, control1, control2, detail) {
237
- vec2.scale(control1, devicePixelRatio, control1);
238
- vec2.scale(control2, devicePixelRatio, control2);
239
- vec2.scale(end.getPos(), devicePixelRatio, end.getPos());
240
236
  const rawControls = [cloneBuf(control1), cloneBuf(control2)];
241
237
  vec2.add(end.getPos(), control2, control2);
242
238
  return new SplinePoint2d(null, end, control1, control2, rawControls, detail);
243
239
  }
244
240
  export function continuousSplinePoint2d(end, control, detail) {
245
- vec2.scale(control, devicePixelRatio, control);
246
- vec2.scale(end.getPos(), devicePixelRatio, end.getPos());
247
241
  const rawControls = [vector2(), cloneBuf(control)];
248
242
  vec2.add(end.getPos(), control, control);
249
243
  return new SplinePoint2d(null, end, null, control, rawControls, detail);
@@ -262,12 +256,6 @@ export function distance2d(vector1, vector2) {
262
256
  export function distance3d(vector1, vector2) {
263
257
  return vec3.distance(vector1, vector2);
264
258
  }
265
- export function toSceneObjInfo(el, id) {
266
- return new SimSceneObjInfo(el, id);
267
- }
268
- export function toSceneObjInfoMany(el, id) {
269
- return el.map((item, index) => toSceneObjInfo(item, id ? id[index] : undefined));
270
- }
271
259
  export function interpolateColors(colors, t) {
272
260
  t = Math.min(1, Math.max(0, t));
273
261
  if (colors.length === 0)
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.8.3",
8
+ "version": "0.9.0",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": "./dist/index.js",