simulationjsv2 0.1.2 → 0.1.4

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.
@@ -49,6 +49,7 @@ export declare class Square extends SimulationElement {
49
49
  private height;
50
50
  private rotation;
51
51
  private vertexColors;
52
+ private points;
52
53
  /**
53
54
  * @param vertexColors{Record<number, Color>} - 0 is top left vertex, numbers increase clockwise
54
55
  */
package/dist/graphics.js CHANGED
@@ -180,6 +180,7 @@ export class Square extends SimulationElement {
180
180
  height;
181
181
  rotation;
182
182
  vertexColors;
183
+ points;
183
184
  /**
184
185
  * @param vertexColors{Record<number, Color>} - 0 is top left vertex, numbers increase clockwise
185
186
  */
@@ -189,6 +190,12 @@ export class Square extends SimulationElement {
189
190
  this.height = height * devicePixelRatio;
190
191
  this.rotation = rotation || 0;
191
192
  this.vertexColors = vertexColors || {};
193
+ this.points = [
194
+ vector2(this.width / 2, this.height / 2),
195
+ vector2(-this.width / 2, this.height / 2),
196
+ vector2(-this.width / 2, -this.height / 2),
197
+ vector2(this.width / 2, -this.height / 2)
198
+ ];
192
199
  }
193
200
  scaleWidth(amount, t = 0, f) {
194
201
  const finalWidth = this.width * amount;
@@ -271,24 +278,20 @@ export class Square extends SimulationElement {
271
278
  }
272
279
  getBuffer(camera, force) {
273
280
  const resBuffer = [];
281
+ const vertexOrder = [0, 1, 2, 0, 2, 3];
274
282
  if (this.vertexCache.shouldUpdate() || force) {
275
- const points = [
276
- vector2(this.width / 2, this.height / 2),
277
- vector2(-this.width / 2, this.height / 2),
278
- vector2(-this.width / 2, -this.height / 2),
279
- vector2(this.width / 2, -this.height / 2)
280
- ].map((vec) => {
281
- const mat = mat4.identity();
282
- mat4.rotateZ(mat, this.rotation, mat);
283
- vec2.transformMat4(vec, mat, vec);
283
+ const rotationMat = mat4.identity();
284
+ mat4.rotateZ(rotationMat, this.rotation, rotationMat);
285
+ const points = this.points.map((vec) => {
286
+ vec2.transformMat4(vec, rotationMat, vec);
284
287
  const pos = vector2();
285
288
  vec2.clone(this.getPos(), pos);
286
289
  pos[1] = camera.getScreenSize()[1] - pos[1];
287
- vec2.add(pos, vector2(this.width / 2, -this.height / 2), pos);
290
+ pos[0] += this.width / 2;
291
+ pos[1] -= this.height / 2;
288
292
  vec2.add(vec, pos, vec);
289
293
  return vec;
290
294
  });
291
- const vertexOrder = [0, 1, 2, 0, 2, 3];
292
295
  vertexOrder.forEach((vertex) => {
293
296
  let vertexColor = this.vertexColors[vertex];
294
297
  vertexColor = vertexColor ? vertexColor : this.getColor();
@@ -25,6 +25,7 @@ export declare class SceneCollection extends SimulationElement {
25
25
  constructor(name: string);
26
26
  getName(): string;
27
27
  add(el: SimulationElement): void;
28
+ empty(): void;
28
29
  getBuffer(camera: Camera, force: boolean): number[];
29
30
  }
30
31
  export declare class Camera {
@@ -281,7 +281,7 @@ export class Simulation {
281
281
  // sub 10 to start with a reasonable gap between starting time and next frame time
282
282
  let prev = Date.now() - 10;
283
283
  let prevFps = 0;
284
- const frame = () => {
284
+ const frame = async () => {
285
285
  if (!this.running || !canvas)
286
286
  return;
287
287
  requestAnimationFrame(frame);
@@ -317,11 +317,11 @@ export class Simulation {
317
317
  orthoMatrix.buffer, orthoMatrix.byteOffset, orthoMatrix.byteLength);
318
318
  device.queue.writeBuffer(uniformBuffer, 4 * 16 + 4 * 16, // 4x4 matrix + 4x4 matrix
319
319
  screenSize.buffer, screenSize.byteOffset, screenSize.byteLength);
320
- const vertexArray = [];
321
- this.scene.forEach((obj) => {
322
- const buffer = obj.getBuffer(this.camera, this.camera.hasUpdated());
323
- buffer.forEach((vertex) => vertexArray.push(vertex));
324
- });
320
+ let vertexArray = [];
321
+ for (let i = 0; i < this.scene.length; i++) {
322
+ const buffer = this.scene[i].getBuffer(this.camera, this.camera.hasUpdated());
323
+ vertexArray = vertexArray.concat(buffer);
324
+ }
325
325
  this.camera.updateConsumed();
326
326
  const vertexF32Array = new Float32Array(vertexArray);
327
327
  const vertexBuffer = device.createBuffer({
@@ -373,6 +373,9 @@ export class SceneCollection extends SimulationElement {
373
373
  add(el) {
374
374
  applyElementToScene(this.scene, this.camera, el);
375
375
  }
376
+ empty() {
377
+ this.scene = [];
378
+ }
376
379
  getBuffer(camera, force) {
377
380
  const res = [];
378
381
  this.scene.forEach((item) => res.push(...item.getBuffer(camera, force)));
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.1.2",
8
+ "version": "0.1.4",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": "./dist/index.js",