simulationjsv2 0.4.5 → 0.4.6

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.
@@ -183,7 +183,6 @@ export declare class Instance<T extends SimulationElement2d | SimulationElement3
183
183
  private instanceMatrix;
184
184
  private matrixBuffer;
185
185
  private device;
186
- readonly isInstance = true;
187
186
  private baseMat;
188
187
  constructor(obj: T, numInstances: number);
189
188
  setNumInstances(numInstances: number): void;
package/dist/graphics.js CHANGED
@@ -894,7 +894,6 @@ export class Instance extends SimulationElement3d {
894
894
  instanceMatrix;
895
895
  matrixBuffer;
896
896
  device;
897
- isInstance = true;
898
897
  baseMat;
899
898
  constructor(obj, numInstances) {
900
899
  super(vector3());
@@ -942,7 +941,7 @@ export class Instance extends SimulationElement3d {
942
941
  }
943
942
  }
944
943
  setInstance(instance, transformation) {
945
- if (instance >= this.instanceMatrix.length)
944
+ if (instance >= this.instanceMatrix.length || instance < 0)
946
945
  return;
947
946
  this.instanceMatrix[instance] = transformation;
948
947
  this.mapBuffer();
@@ -17,7 +17,7 @@ export declare const getTransformationMatrix: (pos: Vector3, rotation: Vector3,
17
17
  export declare const getOrthoMatrix: (screenSize: [number, number]) => Float32Array;
18
18
  export declare const buildDepthTexture: (device: GPUDevice, width: number, height: number) => GPUTexture;
19
19
  export declare const buildMultisampleTexture: (device: GPUDevice, ctx: GPUCanvasContext, width: number, height: number) => GPUTexture;
20
- export declare const addObject: (scene: SimSceneObjInfo[], el: SimulationElement<any>, id?: string) => void;
20
+ export declare const addObject: (scene: SimSceneObjInfo[], el: SimulationElement<any>, device: GPUDevice | null, id?: string) => void;
21
21
  export declare const removeObject: (scene: SimSceneObjInfo[], el: SimulationElement<any>) => void;
22
22
  export declare const removeObjectId: (scene: SimSceneObjInfo[], id: string) => void;
23
23
  export declare class SimSceneObjInfo {
@@ -1,7 +1,8 @@
1
1
  import { mat4, vec3 } from 'wgpu-matrix';
2
2
  import { BUF_LEN, colorOffset, drawingInstancesOffset, uvOffset, vertexSize } from './constants.js';
3
3
  import { vector2, vector3 } from './utils.js';
4
- import { SimulationElement } from './graphics.js';
4
+ import { Instance, SimulationElement } from './graphics.js';
5
+ import { SceneCollection } from './simulation.js';
5
6
  export class VertexCache {
6
7
  vertices = [];
7
8
  hasUpdated = true;
@@ -59,8 +60,11 @@ export const buildMultisampleTexture = (device, ctx, width, height) => {
59
60
  sampleCount: 4
60
61
  });
61
62
  };
62
- export const addObject = (scene, el, id) => {
63
+ export const addObject = (scene, el, device, id) => {
63
64
  if (el instanceof SimulationElement) {
65
+ if (device !== null && (el instanceof Instance || el instanceof SceneCollection)) {
66
+ el.setDevice(device);
67
+ }
64
68
  const obj = new SimSceneObjInfo(el, id);
65
69
  scene.unshift(obj);
66
70
  }
@@ -13,6 +13,7 @@ export declare class Simulation {
13
13
  private initialized;
14
14
  private frameRateView;
15
15
  private camera;
16
+ private device;
16
17
  private pipelines;
17
18
  private renderInfo;
18
19
  constructor(idOrCanvasRef: string | HTMLCanvasElement, camera?: Camera | null, showFrameRate?: boolean);
@@ -25,31 +26,33 @@ export declare class Simulation {
25
26
  setLifetime(el: SimulationElement<any>, lifetime: number): void;
26
27
  setCanvasSize(width: number, height: number): void;
27
28
  start(): void;
29
+ private propagateDevice;
28
30
  stop(): void;
29
31
  setBackground(color: Color): void;
30
32
  getScene(): SimSceneObjInfo[];
31
33
  getSceneObjects(): SimulationElement<Vector3 | Vector2>[];
32
- private propagateDevice;
33
- render(device: GPUDevice, ctx: GPUCanvasContext): void;
34
+ private render;
34
35
  private renderScene;
35
36
  fitElement(): void;
36
- private assertHasCanvas;
37
37
  }
38
38
  export declare class SceneCollection extends SimulationElement3d {
39
39
  protected geometry: BlankGeometry;
40
40
  private name;
41
41
  private scene;
42
- readonly isCollection = true;
42
+ private device;
43
43
  constructor(name: string);
44
44
  setWireframe(_: boolean): void;
45
45
  getName(): string;
46
46
  getScene(): SimSceneObjInfo[];
47
+ setDevice(device: GPUDevice): void;
48
+ private propagateDevice;
47
49
  getVertexCount(): number;
48
50
  getSceneObjects(): SimulationElement<Vector3 | Vector2>[];
49
51
  setSceneObjects(newScene: SimulationElement<any>[]): void;
50
52
  setScene(newScene: SimSceneObjInfo[]): void;
51
- add(el: SimulationElement<any>): void;
53
+ add(el: SimulationElement<any>, id?: string): void;
52
54
  remove(el: SimulationElement<any>): void;
55
+ removeId(id: string): void;
53
56
  /**
54
57
  * @param lifetime - ms
55
58
  */
@@ -1,5 +1,5 @@
1
1
  import { vec3 } from 'wgpu-matrix';
2
- import { SimulationElement3d } from './graphics.js';
2
+ import { Instance, SimulationElement3d } from './graphics.js';
3
3
  import { BUF_LEN } from './constants.js';
4
4
  import { Color, toSceneObjInfoMany, transitionValues, vector2, vector3 } from './utils.js';
5
5
  import { BlankGeometry } from './geometry.js';
@@ -118,8 +118,9 @@ export class Simulation {
118
118
  initialized = false;
119
119
  frameRateView;
120
120
  camera;
121
- pipelines;
122
- renderInfo;
121
+ device = null;
122
+ pipelines = null;
123
+ renderInfo = null;
123
124
  constructor(idOrCanvasRef, camera = null, showFrameRate = false) {
124
125
  if (typeof idOrCanvasRef === 'string') {
125
126
  const ref = document.getElementById(idOrCanvasRef);
@@ -148,13 +149,11 @@ export class Simulation {
148
149
  this.setCanvasSize(width, height);
149
150
  }
150
151
  });
151
- this.renderInfo = null;
152
- this.pipelines = null;
153
152
  this.frameRateView = new FrameRateView(showFrameRate);
154
153
  this.frameRateView.updateFrameRate(1);
155
154
  }
156
155
  add(el, id) {
157
- addObject(this.scene, el, id);
156
+ addObject(this.scene, el, this.device, id);
158
157
  }
159
158
  remove(el) {
160
159
  removeObject(this.scene, el);
@@ -172,7 +171,8 @@ export class Simulation {
172
171
  }
173
172
  }
174
173
  setCanvasSize(width, height) {
175
- this.assertHasCanvas();
174
+ if (this.canvasRef === null)
175
+ return;
176
176
  this.canvasRef.width = width * devicePixelRatio;
177
177
  this.canvasRef.height = height * devicePixelRatio;
178
178
  this.canvasRef.style.width = width + 'px';
@@ -184,7 +184,8 @@ export class Simulation {
184
184
  return;
185
185
  }
186
186
  (async () => {
187
- this.assertHasCanvas();
187
+ if (this.canvasRef === null)
188
+ return;
188
189
  this.initialized = true;
189
190
  this.running = true;
190
191
  const adapter = await navigator.gpu.requestAdapter();
@@ -194,6 +195,7 @@ export class Simulation {
194
195
  if (!ctx)
195
196
  throw logger.error('Context is null');
196
197
  const device = await adapter.requestDevice();
198
+ this.device = device;
197
199
  this.propagateDevice(device);
198
200
  ctx.configure({
199
201
  device,
@@ -201,9 +203,17 @@ export class Simulation {
201
203
  });
202
204
  const screenSize = vector2(this.canvasRef.width, this.canvasRef.height);
203
205
  this.camera.setScreenSize(screenSize);
204
- this.render(device, ctx);
206
+ this.render(ctx);
205
207
  })();
206
208
  }
209
+ propagateDevice(device) {
210
+ for (let i = 0; i < this.scene.length; i++) {
211
+ const el = this.scene[i].getObj();
212
+ if (el instanceof Instance || el instanceof SceneCollection) {
213
+ el.setDevice(device);
214
+ }
215
+ }
216
+ }
207
217
  stop() {
208
218
  this.running = false;
209
219
  }
@@ -216,17 +226,11 @@ export class Simulation {
216
226
  getSceneObjects() {
217
227
  return this.scene.map((item) => item.getObj());
218
228
  }
219
- propagateDevice(device) {
220
- for (let i = 0; i < this.scene.length; i++) {
221
- const obj = this.scene[i].getObj();
222
- if (obj.isInstance) {
223
- obj.setDevice(device);
224
- }
225
- }
226
- }
227
- render(device, ctx) {
228
- this.assertHasCanvas();
229
+ render(ctx) {
230
+ if (this.canvasRef === null || this.device === null)
231
+ return;
229
232
  const canvas = this.canvasRef;
233
+ const device = this.device;
230
234
  canvas.width = canvas.clientWidth * devicePixelRatio;
231
235
  canvas.height = canvas.clientHeight * devicePixelRatio;
232
236
  const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
@@ -403,7 +407,7 @@ export class Simulation {
403
407
  scene[i].traverseLife(diff);
404
408
  }
405
409
  const obj = scene[i].getObj();
406
- if (obj.isCollection) {
410
+ if (obj instanceof SceneCollection) {
407
411
  currentOffset += this.renderScene(device, passEncoder, vertexBuffer, obj.getScene(), currentOffset, diff);
408
412
  continue;
409
413
  }
@@ -440,7 +444,7 @@ export class Simulation {
440
444
  }
441
445
  }
442
446
  let instances = 1;
443
- if (obj.isInstance) {
447
+ if (obj instanceof Instance) {
444
448
  instances = obj.getNumInstances();
445
449
  const buf = obj.getMatrixBuffer();
446
450
  if (buf && this.renderInfo) {
@@ -474,7 +478,8 @@ export class Simulation {
474
478
  return currentOffset - startOffset;
475
479
  }
476
480
  fitElement() {
477
- this.assertHasCanvas();
481
+ if (this.canvasRef === null)
482
+ return;
478
483
  this.fittingElement = true;
479
484
  const parent = this.canvasRef.parentElement;
480
485
  if (parent !== null) {
@@ -483,17 +488,12 @@ export class Simulation {
483
488
  this.setCanvasSize(width, height);
484
489
  }
485
490
  }
486
- assertHasCanvas() {
487
- if (this.canvasRef === null) {
488
- throw logger.error(`cannot complete action, canvas is null`);
489
- }
490
- }
491
491
  }
492
492
  export class SceneCollection extends SimulationElement3d {
493
493
  geometry;
494
494
  name;
495
495
  scene;
496
- isCollection = true;
496
+ device = null;
497
497
  constructor(name) {
498
498
  super(vector3());
499
499
  this.wireframe = false;
@@ -508,6 +508,18 @@ export class SceneCollection extends SimulationElement3d {
508
508
  getScene() {
509
509
  return this.scene;
510
510
  }
511
+ setDevice(device) {
512
+ this.device = device;
513
+ this.propagateDevice(device);
514
+ }
515
+ propagateDevice(device) {
516
+ for (let i = 0; i < this.scene.length; i++) {
517
+ const el = this.scene[i].getObj();
518
+ if (el instanceof Instance || el instanceof SceneCollection) {
519
+ el.setDevice(device);
520
+ }
521
+ }
522
+ }
511
523
  getVertexCount() {
512
524
  let total = 0;
513
525
  for (let i = 0; i < this.scene.length; i++) {
@@ -524,12 +536,15 @@ export class SceneCollection extends SimulationElement3d {
524
536
  setScene(newScene) {
525
537
  this.scene = newScene;
526
538
  }
527
- add(el) {
528
- addObject(this.scene, el);
539
+ add(el, id) {
540
+ addObject(this.scene, el, this.device, id);
529
541
  }
530
542
  remove(el) {
531
543
  removeObject(this.scene, el);
532
544
  }
545
+ removeId(id) {
546
+ removeObjectId(this.scene, id);
547
+ }
533
548
  /**
534
549
  * @param lifetime - ms
535
550
  */
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.5",
8
+ "version": "0.4.6",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": "./dist/index.js",