simulationjsv2 0.7.1 → 0.7.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.
@@ -6,6 +6,7 @@ export declare const drawingInstancesOffset = 36;
6
6
  export declare const BUF_LEN: number;
7
7
  export declare const worldProjMatOffset = 0;
8
8
  export declare const modelProjMatOffset: number;
9
+ export declare const mat4ByteLength = 64;
9
10
  export declare const xAxis: import("./types.js").Vector3;
10
11
  export declare const yAxis: import("./types.js").Vector3;
11
12
  export declare const zAxis: import("./types.js").Vector3;
package/dist/constants.js CHANGED
@@ -7,6 +7,7 @@ export const drawingInstancesOffset = 36;
7
7
  export const BUF_LEN = vertexSize / 4;
8
8
  export const worldProjMatOffset = 0;
9
9
  export const modelProjMatOffset = 4 * 16;
10
+ export const mat4ByteLength = 64;
10
11
  export const xAxis = vector3(1);
11
12
  export const yAxis = vector3(0, 1);
12
13
  export const zAxis = vector3(0, 0, 1);
@@ -34,9 +34,8 @@ export declare abstract class SimulationElement3d {
34
34
  setCenterOffset(offset: Vector3): void;
35
35
  setRotationOffset(offset: Vector3): void;
36
36
  resetCenterOffset(): void;
37
- propagateDevice(device: GPUDevice): void;
38
37
  getModelMatrix(): Mat4;
39
- getUniformBuffer(device: GPUDevice, mat: Mat4): GPUBuffer;
38
+ getUniformBuffer(mat: Mat4): GPUBuffer;
40
39
  protected mirrorParentTransforms3d(mat: Mat4): void;
41
40
  protected updateModelMatrix3d(): void;
42
41
  protected mirrorParentTransforms2d(mat: Mat4): void;
@@ -59,7 +58,6 @@ export declare abstract class SimulationElement3d {
59
58
  rotateTo(rot: Vector3, t?: number, f?: LerpFunc): Promise<void>;
60
59
  getVertexCount(): number;
61
60
  getBuffer(vertexParamGenerator?: VertexParamGeneratorInfo): Float32Array | number[];
62
- protected abstract onDeviceChange(device: GPUDevice): void;
63
61
  }
64
62
  export declare class EmptyElement extends SimulationElement3d {
65
63
  protected geometry: BlankGeometry;
@@ -67,7 +65,6 @@ export declare class EmptyElement extends SimulationElement3d {
67
65
  isEmpty: boolean;
68
66
  constructor(label?: string);
69
67
  getLabel(): string | null;
70
- protected onDeviceChange(_device: GPUDevice): void;
71
68
  }
72
69
  export declare abstract class SimulationElement2d extends SimulationElement3d {
73
70
  is3d: boolean;
@@ -81,7 +78,6 @@ export declare class Plane extends SimulationElement3d {
81
78
  points: Vertex[];
82
79
  constructor(pos: Vector3, points: Vertex[], color?: Color, rotation?: Vector3);
83
80
  setPoints(newPoints: Vertex[]): void;
84
- protected onDeviceChange(_device: GPUDevice): void;
85
81
  }
86
82
  export declare class Square extends SimulationElement2d {
87
83
  protected geometry: SquareGeometry;
@@ -102,7 +98,6 @@ export declare class Square extends SimulationElement2d {
102
98
  scale(amount: number, t?: number, f?: LerpFunc): Promise<void>;
103
99
  setWidth(num: number, t?: number, f?: LerpFunc): Promise<void>;
104
100
  setHeight(num: number, t?: number, f?: LerpFunc): Promise<void>;
105
- protected onDeviceChange(_device: GPUDevice): void;
106
101
  }
107
102
  export declare class Circle extends SimulationElement2d {
108
103
  protected geometry: CircleGeometry;
@@ -111,7 +106,6 @@ export declare class Circle extends SimulationElement2d {
111
106
  constructor(pos: Vector2, radius: number, color?: Color, detail?: number);
112
107
  setRadius(num: number, t?: number, f?: LerpFunc): Promise<void>;
113
108
  scale(amount: number, t?: number, f?: LerpFunc): Promise<void>;
114
- protected onDeviceChange(_device: GPUDevice): void;
115
109
  }
116
110
  export declare class Polygon extends SimulationElement2d {
117
111
  protected geometry: PolygonGeometry;
@@ -119,7 +113,6 @@ export declare class Polygon extends SimulationElement2d {
119
113
  constructor(pos: Vector2, points: Vertex[], color?: Color, rotation?: number);
120
114
  getVertices(): Vertex[];
121
115
  setVertices(newVertices: Vertex[], t?: number, f?: LerpFunc): Promise<void>;
122
- protected onDeviceChange(_device: GPUDevice): void;
123
116
  }
124
117
  export declare class Line3d extends SimulationElement3d {
125
118
  protected geometry: Line3dGeometry;
@@ -128,7 +121,6 @@ export declare class Line3d extends SimulationElement3d {
128
121
  constructor(pos: Vertex, to: Vertex, thickness: number);
129
122
  setStart(pos: Vector3, t?: number, f?: LerpFunc): Promise<void>;
130
123
  setEnd(pos: Vector3, t?: number, f?: LerpFunc): Promise<void>;
131
- protected onDeviceChange(_device: GPUDevice): void;
132
124
  }
133
125
  export declare class Line2d extends SimulationElement2d {
134
126
  protected geometry: Line2dGeometry;
@@ -137,7 +129,6 @@ export declare class Line2d extends SimulationElement2d {
137
129
  constructor(from: Vertex, to: Vertex, thickness?: number);
138
130
  setStart(pos: Vector3, t?: number, f?: LerpFunc): Promise<void>;
139
131
  setEnd(pos: Vector3, t?: number, f?: LerpFunc): Promise<void>;
140
- protected onDeviceChange(_device: GPUDevice): void;
141
132
  }
142
133
  export declare class Cube extends SimulationElement3d {
143
134
  protected geometry: CubeGeometry;
@@ -149,7 +140,6 @@ export declare class Cube extends SimulationElement3d {
149
140
  setHeight(height: number, t?: number, f?: LerpFunc): Promise<void>;
150
141
  setDepth(depth: number, t?: number, f?: LerpFunc): Promise<void>;
151
142
  scale(amount: number, t?: number, f?: LerpFunc): Promise<void>;
152
- protected onDeviceChange(_device: GPUDevice): void;
153
143
  }
154
144
  export declare class BezierCurve2d {
155
145
  private points;
@@ -202,7 +192,6 @@ export declare class Spline2d extends SimulationElement2d {
202
192
  setThickness(thickness: number, t?: number, f?: LerpFunc): Promise<void>;
203
193
  interpolateSlope(t: number): Vector2[] | readonly [Vector2, Vector2];
204
194
  interpolate(t: number): Vector2;
205
- protected onDeviceChange(_device: GPUDevice): void;
206
195
  }
207
196
  export declare class Instance<T extends AnySimulationElement> extends SimulationElement3d {
208
197
  protected geometry: BlankGeometry;
@@ -210,18 +199,19 @@ export declare class Instance<T extends AnySimulationElement> extends Simulation
210
199
  private instanceMatrix;
211
200
  private matrixBuffer;
212
201
  private baseMat;
202
+ private maxInstances;
213
203
  isInstance: boolean;
214
204
  constructor(obj: T, numInstances: number);
215
205
  setNumInstances(numInstances: number): void;
216
206
  setInstance(instance: number, transformation: Mat4): void;
207
+ private allocBuffer;
217
208
  private mapBuffer;
218
209
  getInstances(): Mat4[];
219
210
  getNumInstances(): number;
220
- getMatrixBuffer(device: GPUDevice): GPUBuffer;
211
+ getMatrixBuffer(): GPUBuffer;
221
212
  getVertexCount(): number;
222
213
  getGeometryType(): "list" | "strip";
223
214
  getBuffer(): Float32Array | number[];
224
- protected onDeviceChange(device: GPUDevice): void;
225
215
  getModelMatrix(): Mat4;
226
216
  }
227
217
  export declare class TraceLines2d extends SimulationElement2d {
@@ -229,12 +219,10 @@ export declare class TraceLines2d extends SimulationElement2d {
229
219
  constructor(color?: Color, maxLen?: number);
230
220
  addPoint(point: Vector2 | Vector3, color?: Color): void;
231
221
  isWireframe(): boolean;
232
- protected onDeviceChange(_: GPUDevice): void;
233
222
  }
234
223
  export declare class TraceLines3d extends SimulationElement3d {
235
224
  protected geometry: TraceLinesGeometry;
236
225
  constructor(color?: Color, maxLen?: number);
237
226
  addPoint(point: Vector2 | Vector3, color?: Color): void;
238
227
  isWireframe(): boolean;
239
- protected onDeviceChange(_: GPUDevice): void;
240
228
  }
package/dist/graphics.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { vec3, mat4, vec2, vec4 } from 'wgpu-matrix';
2
2
  import { Vertex, cloneBuf, color, colorFromVector4, vector2, vector3, vertex, Color, vector2FromVector3, matrix4, vector3FromVector2, distance2d } from './utils.js';
3
3
  import { BlankGeometry, CircleGeometry, CubeGeometry, Line2dGeometry, Line3dGeometry, PlaneGeometry, PolygonGeometry, Spline2dGeometry, SquareGeometry, TraceLines2dGeometry as TraceLinesGeometry } from './geometry.js';
4
- import { SimSceneObjInfo, VertexCache, bufferGenerator, internalTransitionValues, logger, posTo2dScreen, rotateMat4, vector3ToPixelRatio } from './internalUtils.js';
5
- import { modelProjMatOffset } from './constants.js';
4
+ import { SimSceneObjInfo, VertexCache, bufferGenerator, globalInfo, internalTransitionValues, logger, posTo2dScreen, vector3ToPixelRatio } from './internalUtils.js';
5
+ import { mat4ByteLength, modelProjMatOffset } from './constants.js';
6
6
  export class SimulationElement3d {
7
7
  children;
8
8
  uniformBuffer;
@@ -74,17 +74,12 @@ export class SimulationElement3d {
74
74
  this.centerOffset[1] = 0;
75
75
  this.centerOffset[2] = 0;
76
76
  }
77
- propagateDevice(device) {
78
- this.onDeviceChange(device);
79
- for (let i = 0; i < this.children.length; i++) {
80
- this.children[i].getObj().propagateDevice(device);
81
- }
82
- }
83
77
  getModelMatrix() {
84
78
  this.updateModelMatrix3d();
85
79
  return this.modelMatrix;
86
80
  }
87
- getUniformBuffer(device, mat) {
81
+ getUniformBuffer(mat) {
82
+ const device = globalInfo.errorGetDevice();
88
83
  if (!this.uniformBuffer) {
89
84
  const uniformBufferSize = 4 * 16 + 4 * 16 + 4 * 2 + 8; // 4x4 matrix + 4x4 matrix + vec2<f32> + 8 bc 144 is cool
90
85
  this.uniformBuffer = device.createBuffer({
@@ -302,8 +297,6 @@ export class EmptyElement extends SimulationElement3d {
302
297
  getLabel() {
303
298
  return this.label;
304
299
  }
305
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
306
- onDeviceChange(_device) { }
307
300
  }
308
301
  export class SimulationElement2d extends SimulationElement3d {
309
302
  is3d = false;
@@ -335,8 +328,6 @@ export class Plane extends SimulationElement3d {
335
328
  this.points = newPoints;
336
329
  this.vertexCache.updated();
337
330
  }
338
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
339
- onDeviceChange(_device) { }
340
331
  }
341
332
  export class Square extends SimulationElement2d {
342
333
  geometry;
@@ -478,8 +469,6 @@ export class Square extends SimulationElement2d {
478
469
  this.vertexCache.updated();
479
470
  }, t, f);
480
471
  }
481
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
482
- onDeviceChange(_device) { }
483
472
  }
484
473
  export class Circle extends SimulationElement2d {
485
474
  geometry;
@@ -517,8 +506,6 @@ export class Circle extends SimulationElement2d {
517
506
  this.vertexCache.updated();
518
507
  }, t, f);
519
508
  }
520
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
521
- onDeviceChange(_device) { }
522
509
  }
523
510
  export class Polygon extends SimulationElement2d {
524
511
  geometry;
@@ -597,8 +584,6 @@ export class Polygon extends SimulationElement2d {
597
584
  this.vertexCache.updated();
598
585
  }, t, f);
599
586
  }
600
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
601
- onDeviceChange(_device) { }
602
587
  }
603
588
  export class Line3d extends SimulationElement3d {
604
589
  geometry;
@@ -630,8 +615,6 @@ export class Line3d extends SimulationElement3d {
630
615
  this.vertexCache.updated();
631
616
  }, t, f);
632
617
  }
633
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
634
- onDeviceChange(_device) { }
635
618
  }
636
619
  export class Line2d extends SimulationElement2d {
637
620
  geometry;
@@ -663,8 +646,6 @@ export class Line2d extends SimulationElement2d {
663
646
  this.vertexCache.updated();
664
647
  }, t, f);
665
648
  }
666
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
667
- onDeviceChange(_device) { }
668
649
  }
669
650
  export class Cube extends SimulationElement3d {
670
651
  geometry;
@@ -743,8 +724,6 @@ export class Cube extends SimulationElement3d {
743
724
  this.vertexCache.updated();
744
725
  }, t, f);
745
726
  }
746
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
747
- onDeviceChange(_device) { }
748
727
  }
749
728
  export class BezierCurve2d {
750
729
  points;
@@ -992,8 +971,6 @@ export class Spline2d extends SimulationElement2d {
992
971
  const [vec] = this.interpolateSlope(t);
993
972
  return vec;
994
973
  }
995
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
996
- onDeviceChange(_device) { }
997
974
  }
998
975
  export class Instance extends SimulationElement3d {
999
976
  geometry;
@@ -1001,9 +978,12 @@ export class Instance extends SimulationElement3d {
1001
978
  instanceMatrix;
1002
979
  matrixBuffer;
1003
980
  baseMat;
981
+ maxInstances;
1004
982
  isInstance = true;
1005
983
  constructor(obj, numInstances) {
1006
984
  super(vector3(), vector3());
985
+ // 32 matrices
986
+ this.maxInstances = 32;
1007
987
  this.matrixBuffer = null;
1008
988
  obj.isInstanced = true;
1009
989
  this.obj = obj;
@@ -1011,7 +991,6 @@ export class Instance extends SimulationElement3d {
1011
991
  this.is3d = obj.is3d;
1012
992
  this.geometry = new BlankGeometry();
1013
993
  this.baseMat = matrix4();
1014
- rotateMat4(this.baseMat, obj.getRotation());
1015
994
  for (let i = 0; i < numInstances; i++) {
1016
995
  const clone = cloneBuf(this.baseMat);
1017
996
  this.instanceMatrix.push(clone);
@@ -1020,6 +999,10 @@ export class Instance extends SimulationElement3d {
1020
999
  setNumInstances(numInstances) {
1021
1000
  if (numInstances < 0)
1022
1001
  throw logger.error('Num instances is less than 0');
1002
+ if (numInstances > this.maxInstances) {
1003
+ this.maxInstances = numInstances;
1004
+ this.allocBuffer(numInstances);
1005
+ }
1023
1006
  const oldLen = this.instanceMatrix.length;
1024
1007
  if (numInstances < oldLen) {
1025
1008
  const diff = oldLen - numInstances;
@@ -1042,9 +1025,25 @@ export class Instance extends SimulationElement3d {
1042
1025
  return;
1043
1026
  this.instanceMatrix[instance] = transformation;
1044
1027
  }
1045
- mapBuffer(device) {
1046
- if (this.matrixBuffer === null)
1028
+ allocBuffer(size) {
1029
+ const device = globalInfo.getDevice();
1030
+ if (!device)
1031
+ return;
1032
+ const byteSize = size * mat4ByteLength;
1033
+ this.matrixBuffer = device.createBuffer({
1034
+ size: byteSize,
1035
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
1036
+ });
1037
+ }
1038
+ mapBuffer() {
1039
+ const device = globalInfo.getDevice();
1040
+ if (!device)
1047
1041
  return;
1042
+ if (!this.matrixBuffer) {
1043
+ const minSize = this.maxInstances * mat4ByteLength;
1044
+ const size = Math.max(minSize, this.instanceMatrix.length);
1045
+ this.allocBuffer(size);
1046
+ }
1048
1047
  const buf = new Float32Array(this.instanceMatrix.map((mat) => [...mat]).flat());
1049
1048
  device.queue.writeBuffer(this.matrixBuffer, 0, buf.buffer, buf.byteOffset, buf.byteLength);
1050
1049
  this.matrixBuffer.unmap();
@@ -1055,16 +1054,8 @@ export class Instance extends SimulationElement3d {
1055
1054
  getNumInstances() {
1056
1055
  return this.instanceMatrix.length;
1057
1056
  }
1058
- getMatrixBuffer(device) {
1059
- if (!this.matrixBuffer) {
1060
- const minSize = 512;
1061
- const size = Math.max(minSize, this.instanceMatrix[0].byteLength * this.instanceMatrix.length);
1062
- this.matrixBuffer = device.createBuffer({
1063
- size,
1064
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
1065
- });
1066
- }
1067
- this.mapBuffer(device);
1057
+ getMatrixBuffer() {
1058
+ this.mapBuffer();
1068
1059
  return this.matrixBuffer;
1069
1060
  }
1070
1061
  getVertexCount() {
@@ -1076,9 +1067,6 @@ export class Instance extends SimulationElement3d {
1076
1067
  getBuffer() {
1077
1068
  return this.obj.getBuffer();
1078
1069
  }
1079
- onDeviceChange(device) {
1080
- this.obj.propagateDevice(device);
1081
- }
1082
1070
  getModelMatrix() {
1083
1071
  return this.obj.getModelMatrix();
1084
1072
  }
@@ -1100,8 +1088,6 @@ export class TraceLines2d extends SimulationElement2d {
1100
1088
  isWireframe() {
1101
1089
  return true;
1102
1090
  }
1103
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
1104
- onDeviceChange(_) { }
1105
1091
  }
1106
1092
  export class TraceLines3d extends SimulationElement3d {
1107
1093
  geometry;
@@ -1120,6 +1106,4 @@ export class TraceLines3d extends SimulationElement3d {
1120
1106
  isWireframe() {
1121
1107
  return true;
1122
1108
  }
1123
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
1124
- onDeviceChange(_) { }
1125
1109
  }
@@ -12,6 +12,14 @@ export declare class VertexCache {
12
12
  shouldUpdate(): boolean;
13
13
  getVertexCount(): number;
14
14
  }
15
+ export declare class GlobalInfo {
16
+ private device;
17
+ constructor();
18
+ setDevice(device: GPUDevice): void;
19
+ errorGetDevice(): GPUDevice;
20
+ getDevice(): GPUDevice | null;
21
+ }
22
+ export declare const globalInfo: GlobalInfo;
15
23
  export declare const updateProjectionMatrix: (mat: Mat4, aspectRatio: number, zNear?: number, zFar?: number) => any;
16
24
  export declare const updateWorldProjectionMatrix: (worldProjMat: Mat4, projMat: Mat4) => void;
17
25
  export declare const updateOrthoProjectionMatrix: (mat: Mat4, screenSize: [number, number]) => Float32Array;
@@ -57,8 +65,6 @@ declare class BufferGenerator {
57
65
  export declare const bufferGenerator: BufferGenerator;
58
66
  export declare function vector3ToPixelRatio(vec: Vector3): void;
59
67
  export declare function vector2ToPixelRatio(vec: Vector2): void;
60
- export declare function matrixFromRotation(rotation: Vector3): Mat4;
61
- export declare function rotateMat4(mat: Mat4, rotation: Vector3): void;
62
68
  export declare function createPipeline(device: GPUDevice, module: GPUShaderModule, bindGroupLayouts: GPUBindGroupLayout[], presentationFormat: GPUTextureFormat, topology: GPUPrimitiveTopology, vertexParams?: VertexParamInfo[]): GPURenderPipeline;
63
69
  export declare function triangulateWireFrameOrder(len: number): number[];
64
70
  export declare function getTotalVertices(scene: SimSceneObjInfo[]): number;
@@ -26,6 +26,24 @@ export class VertexCache {
26
26
  return this.vertices.length / BUF_LEN;
27
27
  }
28
28
  }
29
+ export class GlobalInfo {
30
+ device;
31
+ constructor() {
32
+ this.device = null;
33
+ }
34
+ setDevice(device) {
35
+ this.device = device;
36
+ }
37
+ errorGetDevice() {
38
+ if (!this.device)
39
+ throw logger.error('GPUDevice is null');
40
+ return this.device;
41
+ }
42
+ getDevice() {
43
+ return this.device;
44
+ }
45
+ }
46
+ export const globalInfo = new GlobalInfo();
29
47
  export const updateProjectionMatrix = (mat, aspectRatio, zNear = 1, zFar = 500) => {
30
48
  const fov = Math.PI / 4;
31
49
  return mat4.perspective(fov, aspectRatio, zNear, zFar, mat);
@@ -201,18 +219,6 @@ export function vector2ToPixelRatio(vec) {
201
219
  vec[0] *= devicePixelRatio;
202
220
  vec[1] *= devicePixelRatio;
203
221
  }
204
- export function matrixFromRotation(rotation) {
205
- const rotMatrix = mat4.identity();
206
- mat4.rotateZ(rotMatrix, rotation[2], rotMatrix);
207
- mat4.rotateY(rotMatrix, rotation[1], rotMatrix);
208
- mat4.rotateX(rotMatrix, rotation[0], rotMatrix);
209
- return rotMatrix;
210
- }
211
- export function rotateMat4(mat, rotation) {
212
- mat4.rotateZ(mat, rotation[2], mat);
213
- mat4.rotateY(mat, rotation[1], mat);
214
- mat4.rotateX(mat, rotation[0], mat);
215
- }
216
222
  export function createPipeline(device, module, bindGroupLayouts, presentationFormat, topology, vertexParams) {
217
223
  let params = [
218
224
  {
@@ -31,11 +31,10 @@ export declare class Simulation extends Settings {
31
31
  private fittingElement;
32
32
  private running;
33
33
  private initialized;
34
- private frameRateView;
35
- private device;
36
34
  private pipelines;
37
35
  private renderInfo;
38
36
  private resizeEvents;
37
+ private frameRateView;
39
38
  constructor(idOrCanvasRef: string | HTMLCanvasElement, sceneCamera?: Camera | null, showFrameRate?: boolean);
40
39
  private handleCanvasResize;
41
40
  onResize(cb: (width: number, height: number) => void): void;
@@ -51,7 +50,6 @@ export declare class Simulation extends Settings {
51
50
  private applyCanvasSize;
52
51
  setCanvasSize(width: number, height: number): void;
53
52
  start(): void;
54
- private propagateDevice;
55
53
  stop(): void;
56
54
  setBackground(color: Color): void;
57
55
  getScene(): SimSceneObjInfo[];
@@ -71,7 +69,7 @@ export declare class ShaderGroup extends EmptyElement {
71
69
  private bindGroup;
72
70
  private valueBuffers;
73
71
  constructor(shaderCode: string, topology: GPUPrimitiveTopology | undefined, vertexParams: VertexParamInfo[], paramGenerator: VertexParamGeneratorInfo, bindGroup?: BindGroupInfo);
74
- protected onDeviceChange(device: GPUDevice): void;
72
+ private initPipeline;
75
73
  getBindGroupLayout(): GPUBindGroupLayout | null;
76
74
  getPipeline(): GPURenderPipeline | null;
77
75
  getBindGroupBuffers(device: GPUDevice): GPUBuffer[] | null;
@@ -3,7 +3,7 @@ import { EmptyElement, SimulationElement3d } from './graphics.js';
3
3
  import { BUF_LEN, worldProjMatOffset } from './constants.js';
4
4
  import { Color, matrix4, transitionValues, vector2, vector3 } from './utils.js';
5
5
  import { BlankGeometry } from './geometry.js';
6
- import { SimSceneObjInfo, buildDepthTexture, buildMultisampleTexture, updateProjectionMatrix, createPipeline, getTotalVertices, logger, removeObjectId, updateOrthoProjectionMatrix, updateWorldProjectionMatrix } from './internalUtils.js';
6
+ import { SimSceneObjInfo, buildDepthTexture, buildMultisampleTexture, updateProjectionMatrix, createPipeline, getTotalVertices, logger, removeObjectId, updateOrthoProjectionMatrix, updateWorldProjectionMatrix, globalInfo } from './internalUtils.js';
7
7
  import { Settings } from './settings.js';
8
8
  const shader = `
9
9
  struct Uniforms {
@@ -32,7 +32,6 @@ fn vertex_main(
32
32
  ) -> VertexOutput {
33
33
  var output: VertexOutput;
34
34
 
35
-
36
35
  if (drawingInstance == 1) {
37
36
  output.Position = uniforms.worldProjectionMatrix * uniforms.modelProjectionMatrix * instanceMatrices[instanceIdx] * vec4(position, 1.0);
38
37
  } else {
@@ -212,11 +211,10 @@ export class Simulation extends Settings {
212
211
  fittingElement = false;
213
212
  running = true;
214
213
  initialized = false;
215
- frameRateView;
216
- device = null;
217
214
  pipelines = null;
218
215
  renderInfo = null;
219
216
  resizeEvents;
217
+ frameRateView;
220
218
  constructor(idOrCanvasRef, sceneCamera = null, showFrameRate = false) {
221
219
  super();
222
220
  if (typeof idOrCanvasRef === 'string') {
@@ -263,9 +261,6 @@ export class Simulation extends Settings {
263
261
  }
264
262
  add(el, id) {
265
263
  if (el instanceof SimulationElement3d) {
266
- if (this.device !== null) {
267
- el.propagateDevice(this.device);
268
- }
269
264
  const obj = new SimSceneObjInfo(el, id);
270
265
  this.scene.unshift(obj);
271
266
  }
@@ -324,8 +319,7 @@ export class Simulation extends Settings {
324
319
  if (!ctx)
325
320
  throw logger.error('Context is null');
326
321
  const device = await adapter.requestDevice();
327
- this.device = device;
328
- this.propagateDevice(device);
322
+ globalInfo.setDevice(device);
329
323
  ctx.configure({
330
324
  device,
331
325
  format: 'bgra8unorm'
@@ -335,12 +329,6 @@ export class Simulation extends Settings {
335
329
  this.render(ctx);
336
330
  })();
337
331
  }
338
- propagateDevice(device) {
339
- for (let i = 0; i < this.scene.length; i++) {
340
- const el = this.scene[i].getObj();
341
- el.propagateDevice(device);
342
- }
343
- }
344
332
  stop() {
345
333
  this.running = false;
346
334
  }
@@ -354,10 +342,10 @@ export class Simulation extends Settings {
354
342
  return this.scene.map((item) => item.getObj());
355
343
  }
356
344
  render(ctx) {
357
- if (this.canvasRef === null || this.device === null)
345
+ const device = globalInfo.getDevice();
346
+ if (this.canvasRef === null || device === null)
358
347
  return;
359
348
  const canvas = this.canvasRef;
360
- const device = this.device;
361
349
  canvas.width = canvas.clientWidth * devicePixelRatio;
362
350
  canvas.height = canvas.clientHeight * devicePixelRatio;
363
351
  const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
@@ -513,7 +501,7 @@ export class Simulation extends Settings {
513
501
  vertexBuffer.unmap();
514
502
  passEncoder.setVertexBuffer(0, vertexBuffer, currentOffset, buffer.byteLength);
515
503
  const modelMatrix = obj.getModelMatrix();
516
- const uniformBuffer = obj.getUniformBuffer(device, modelMatrix);
504
+ const uniformBuffer = obj.getUniformBuffer(modelMatrix);
517
505
  const projBuf = obj.is3d ? worldProjMat : orthoMatrix;
518
506
  device.queue.writeBuffer(uniformBuffer, worldProjMatOffset, projBuf.buffer, projBuf.byteOffset, projBuf.byteLength);
519
507
  if (shaderInfo) {
@@ -536,7 +524,8 @@ export class Simulation extends Settings {
536
524
  let instanceBuffer;
537
525
  if (obj.isInstance) {
538
526
  instances = obj.getNumInstances();
539
- instanceBuffer = obj.getMatrixBuffer(device);
527
+ instanceBuffer =
528
+ obj.getMatrixBuffer() ?? this.renderInfo.instanceBuffer;
540
529
  }
541
530
  else {
542
531
  instanceBuffer = this.renderInfo.instanceBuffer;
@@ -627,7 +616,8 @@ export class ShaderGroup extends EmptyElement {
627
616
  this.vertexParams = vertexParams;
628
617
  this.valueBuffers = null;
629
618
  }
630
- onDeviceChange(device) {
619
+ initPipeline() {
620
+ const device = globalInfo.errorGetDevice();
631
621
  this.module = device.createShaderModule({ code: this.code });
632
622
  const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
633
623
  const bindGroupLayout = device.createBindGroupLayout(baseBindGroupLayout);
@@ -649,6 +639,8 @@ export class ShaderGroup extends EmptyElement {
649
639
  return this.bindGroupLayout;
650
640
  }
651
641
  getPipeline() {
642
+ if (!this.pipeline)
643
+ this.initPipeline();
652
644
  return this.pipeline;
653
645
  }
654
646
  getBindGroupBuffers(device) {
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.7.1",
8
+ "version": "0.7.2",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": "./dist/index.js",