simulationjsv2 0.3.1 → 0.3.3

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.
@@ -74,6 +74,7 @@ export declare class Spline2dGeometry extends Geometry {
74
74
  constructor(points: SplinePoint2d[], color: Color, thickness: number, detail: number);
75
75
  updateInterpolationStart(start: number): void;
76
76
  updateInterpolationLimit(limit: number): void;
77
+ updatePoint(pointIndex: number, newPoint: SplinePoint2d): void;
77
78
  updateThickness(thickness: number): void;
78
79
  private getVertexCount;
79
80
  getWireframeVertexCount(): number;
package/dist/geometry.js CHANGED
@@ -239,6 +239,20 @@ export class Spline2dGeometry extends Geometry {
239
239
  updateInterpolationLimit(limit) {
240
240
  this.params.interpolateLimit = Math.min(1, Math.max(0, limit));
241
241
  }
242
+ updatePoint(pointIndex, newPoint) {
243
+ if (pointIndex < 0 && pointIndex >= this.params.points.length)
244
+ return;
245
+ const start = newPoint.getStart();
246
+ const end = newPoint.getEnd();
247
+ const [startControl, endControl] = newPoint.getControls();
248
+ const rawControls = newPoint.getRawControls();
249
+ vec3.add(end.getPos(), rawControls[1], endControl);
250
+ if (start && startControl) {
251
+ vec3.add(start.getPos(), rawControls[0], startControl);
252
+ }
253
+ this.params.points[pointIndex] = newPoint;
254
+ this.computeCurves();
255
+ }
242
256
  updateThickness(thickness) {
243
257
  this.params.thickness = thickness;
244
258
  }
@@ -255,6 +269,8 @@ export class Spline2dGeometry extends Geometry {
255
269
  return this.params.curves;
256
270
  }
257
271
  computeCurves() {
272
+ this.params.curves = [];
273
+ this.params.distance = 0;
258
274
  for (let i = 0; i < this.params.points.length; i++) {
259
275
  let prevControl = null;
260
276
  let prevColor = null;
@@ -156,6 +156,7 @@ export declare class SplinePoint2d {
156
156
  getDetail(): number | undefined;
157
157
  getColors(prevColor?: Color | null): (Color | null)[];
158
158
  getVectorArray(prevEnd: Vector2 | null, prevControl: Vector2 | null): readonly [Vector2, Vector2, Vector2, Vector2];
159
+ clone(): SplinePoint2d;
159
160
  }
160
161
  export declare class Spline2d extends SimulationElement2d {
161
162
  protected geometry: Spline2dGeometry;
@@ -169,8 +170,10 @@ export declare class Spline2d extends SimulationElement2d {
169
170
  getLength(): number;
170
171
  setInterpolateStart(start: number, t?: number, f?: LerpFunc): Promise<void>;
171
172
  setInterpolateLimit(limit: number, t?: number, f?: LerpFunc): Promise<void>;
173
+ updatePoint(pointIndex: number, newPoint: SplinePoint2d): void;
174
+ updatePointAbsolute(pointIndex: number, newPoint: SplinePoint2d): void;
172
175
  setThickness(thickness: number, t?: number, f?: LerpFunc): Promise<void>;
173
- interpolateSlope(t: number): readonly [Vector2, Vector2];
176
+ interpolateSlope(t: number): Vector2[] | readonly [Vector2, Vector2];
174
177
  interpolate(t: number): Vector2;
175
178
  protected updateMatrix(camera: Camera): void;
176
179
  }
package/dist/graphics.js CHANGED
@@ -773,6 +773,9 @@ export class SplinePoint2d {
773
773
  vector2FromVector3(this.end.getPos())
774
774
  ];
775
775
  }
776
+ clone() {
777
+ return new SplinePoint2d(this.start, this.end, this.control1, this.control2, this.rawControls, this.detail);
778
+ }
776
779
  }
777
780
  export class Spline2d extends SimulationElement2d {
778
781
  geometry;
@@ -794,6 +797,7 @@ export class Spline2d extends SimulationElement2d {
794
797
  this.estimateLength();
795
798
  }
796
799
  estimateLength() {
800
+ this.length = 0;
797
801
  const curves = this.geometry.getCurves();
798
802
  for (let i = 0; i < curves.length; i++) {
799
803
  this.length += curves[i].getLength();
@@ -826,6 +830,22 @@ export class Spline2d extends SimulationElement2d {
826
830
  this.vertexCache.updated();
827
831
  }, t, f);
828
832
  }
833
+ updatePoint(pointIndex, newPoint) {
834
+ this.geometry.updatePoint(pointIndex, newPoint);
835
+ this.estimateLength();
836
+ this.vertexCache.updated();
837
+ }
838
+ updatePointAbsolute(pointIndex, newPoint) {
839
+ const clonePoint = newPoint.clone();
840
+ const start = clonePoint.getStart()?.getPos() || vector3();
841
+ const end = clonePoint.getEnd().getPos();
842
+ const pos = vector3FromVector2(this.getPos());
843
+ vec3.sub(start, pos, start);
844
+ vec3.sub(end, pos, end);
845
+ this.geometry.updatePoint(pointIndex, clonePoint);
846
+ this.estimateLength();
847
+ this.vertexCache.updated();
848
+ }
829
849
  setThickness(thickness, t = 0, f) {
830
850
  thickness *= devicePixelRatio;
831
851
  const diff = thickness - this.thickness;
@@ -855,6 +875,8 @@ export class Spline2d extends SimulationElement2d {
855
875
  }
856
876
  currentLength += curves[i].getLength();
857
877
  }
878
+ if (curves.length === 0)
879
+ return [vector2(), vector2()];
858
880
  return curves[index].interpolateSlope(diff);
859
881
  }
860
882
  interpolate(t) {
@@ -896,8 +918,10 @@ export class Instance extends SimulationElement3d {
896
918
  setMatrixBuffer() {
897
919
  if (!this.device || this.instanceMatrix.length === 0)
898
920
  return;
921
+ const minSize = 640;
922
+ const size = Math.max(minSize, this.instanceMatrix[0].byteLength * this.instanceMatrix.length);
899
923
  this.matrixBuffer = this.device.createBuffer({
900
- size: this.instanceMatrix[0].length * 4 * this.instanceMatrix.length,
924
+ size,
901
925
  usage: GPUBufferUsage.STORAGE,
902
926
  mappedAtCreation: true
903
927
  });
@@ -34,6 +34,7 @@ export declare class SceneCollection extends SimulationElement3d {
34
34
  setWireframe(_: boolean): void;
35
35
  getName(): string;
36
36
  getScene(): SimulationElement<Vector3>[];
37
+ setScene(newScene: SimulationElement<any>[]): void;
37
38
  add(el: SimulationElement<any>): void;
38
39
  empty(): void;
39
40
  getSceneBuffer(camera: Camera): number[];
@@ -214,7 +214,7 @@ export class Simulation {
214
214
  usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
215
215
  });
216
216
  const instanceBuffer = device.createBuffer({
217
- size: 16 * 10 * 4,
217
+ size: 10 * 4 * 16,
218
218
  usage: GPUBufferUsage.STORAGE
219
219
  });
220
220
  const bindGroupLayout = device.createBindGroupLayout({
@@ -457,6 +457,9 @@ export class SceneCollection extends SimulationElement3d {
457
457
  getScene() {
458
458
  return this.scene;
459
459
  }
460
+ setScene(newScene) {
461
+ this.scene = newScene;
462
+ }
460
463
  add(el) {
461
464
  applyElementToScene(this.scene, el);
462
465
  }
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.3.1",
8
+ "version": "0.3.3",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": "./dist/index.js",