simulationjsv2 0.4.4 → 0.4.5

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.
@@ -184,7 +184,11 @@ export declare class Instance<T extends SimulationElement2d | SimulationElement3
184
184
  private matrixBuffer;
185
185
  private device;
186
186
  readonly isInstance = true;
187
+ private baseMat;
187
188
  constructor(obj: T, numInstances: number);
189
+ setNumInstances(numInstances: number): void;
190
+ setInstance(instance: number, transformation: Mat4): void;
191
+ private mapBuffer;
188
192
  private setMatrixBuffer;
189
193
  getInstances(): Mat4[];
190
194
  getNumInstances(): number;
package/dist/graphics.js CHANGED
@@ -895,6 +895,7 @@ export class Instance extends SimulationElement3d {
895
895
  matrixBuffer;
896
896
  device;
897
897
  isInstance = true;
898
+ baseMat;
898
899
  constructor(obj, numInstances) {
899
900
  super(vector3());
900
901
  this.device = null;
@@ -904,18 +905,55 @@ export class Instance extends SimulationElement3d {
904
905
  this.instanceMatrix = [];
905
906
  this.is3d = Boolean(obj.is3d);
906
907
  this.geometry = new BlankGeometry();
907
- const mat = matrix4();
908
+ this.baseMat = matrix4();
908
909
  if (typeof obj.getRotation() === 'number') {
909
- mat4.rotateZ(mat, obj.getRotation(), mat);
910
+ mat4.rotateZ(this.baseMat, obj.getRotation(), this.baseMat);
910
911
  }
911
912
  else {
912
- rotateMat4(mat, obj.getRotation());
913
+ rotateMat4(this.baseMat, obj.getRotation());
913
914
  }
914
915
  for (let i = 0; i < numInstances; i++) {
915
- const clone = cloneBuf(mat);
916
+ const clone = cloneBuf(this.baseMat);
916
917
  this.instanceMatrix.push(clone);
917
918
  }
918
919
  }
920
+ setNumInstances(numInstances) {
921
+ if (numInstances < 0)
922
+ throw logger.error('Num instances is less than 0');
923
+ const oldLen = this.instanceMatrix.length;
924
+ if (numInstances < oldLen) {
925
+ const diff = oldLen - numInstances;
926
+ this.instanceMatrix.splice(oldLen - diff, diff);
927
+ return;
928
+ }
929
+ const oldArr = this.instanceMatrix;
930
+ this.instanceMatrix = Array(numInstances);
931
+ for (let i = 0; i < numInstances; i++) {
932
+ if (i < oldLen) {
933
+ this.instanceMatrix[i] = oldArr[i];
934
+ continue;
935
+ }
936
+ const clone = cloneBuf(this.baseMat);
937
+ this.instanceMatrix[i] = clone;
938
+ }
939
+ if (this.device) {
940
+ this.setMatrixBuffer();
941
+ this.mapBuffer();
942
+ }
943
+ }
944
+ setInstance(instance, transformation) {
945
+ if (instance >= this.instanceMatrix.length)
946
+ return;
947
+ this.instanceMatrix[instance] = transformation;
948
+ this.mapBuffer();
949
+ }
950
+ mapBuffer() {
951
+ if (!this.device || this.matrixBuffer === null)
952
+ return;
953
+ const buf = new Float32Array(this.instanceMatrix.map((mat) => [...mat]).flat());
954
+ this.device.queue.writeBuffer(this.matrixBuffer, 0, buf.buffer, buf.byteOffset, buf.byteLength);
955
+ this.matrixBuffer.unmap();
956
+ }
919
957
  setMatrixBuffer() {
920
958
  if (!this.device || this.instanceMatrix.length === 0)
921
959
  return;
@@ -923,12 +961,9 @@ export class Instance extends SimulationElement3d {
923
961
  const size = Math.max(minSize, this.instanceMatrix[0].byteLength * this.instanceMatrix.length);
924
962
  this.matrixBuffer = this.device.createBuffer({
925
963
  size,
926
- usage: GPUBufferUsage.STORAGE,
927
- mappedAtCreation: true
964
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
928
965
  });
929
- const buf = this.instanceMatrix.map((mat) => [...mat]).flat();
930
- new Float32Array(this.matrixBuffer.getMappedRange()).set(buf);
931
- this.matrixBuffer.unmap();
966
+ this.mapBuffer();
932
967
  }
933
968
  getInstances() {
934
969
  return this.instanceMatrix;
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.4",
8
+ "version": "0.4.5",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": "./dist/index.js",