simulationjsv2 0.2.6 → 0.2.8

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.
@@ -12,6 +12,8 @@ export declare abstract class Geometry {
12
12
  updateMatrix(matrix: Mat4): void;
13
13
  getType(): "list" | "strip";
14
14
  abstract recompute(): void;
15
+ getTriangleVertexCount(): number;
16
+ getWireframeVertexCount(): number;
15
17
  protected bufferFromOrder(order: number[], color: Color): number[];
16
18
  getWireframeBuffer(color: Color): number[];
17
19
  getTriangleBuffer(color: Color): number[];
package/dist/geometry.js CHANGED
@@ -16,6 +16,12 @@ export class Geometry {
16
16
  getType() {
17
17
  return this.geometryType;
18
18
  }
19
+ getTriangleVertexCount() {
20
+ return this.triangleOrder.length;
21
+ }
22
+ getWireframeVertexCount() {
23
+ return this.wireframeOrder.length;
24
+ }
19
25
  bufferFromOrder(order, color) {
20
26
  return order
21
27
  .map((vertexIndex) => {
@@ -25,6 +25,7 @@ export declare abstract class SimulationElement<T extends Vector2 | Vector3 = Ve
25
25
  abstract rotate(amount: ElementRotation<T>, t?: number, f?: LerpFunc): Promise<void>;
26
26
  abstract rotateTo(rotation: ElementRotation<T>, t?: number, f?: LerpFunc): Promise<void>;
27
27
  protected abstract updateMatrix(camera: Camera): void;
28
+ getVertexCount(): number;
28
29
  protected defaultUpdateMatrix(camera: Camera): void;
29
30
  getBuffer(camera: Camera): number[];
30
31
  }
package/dist/graphics.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { vec3, mat4, vec2, vec4 } from 'wgpu-matrix';
2
- import { Vertex, VertexCache, cloneBuf, color, colorFromVector4, vector3ToPixelRatio, vector2, vector3, vertex, Color, transitionValues, logger, vector2FromVector3, matrix4, rotateMat4, vector3FromVector2 } from './utils.js';
2
+ import { Vertex, VertexCache, cloneBuf, color, colorFromVector4, vector3ToPixelRatio, vector2, vector3, vertex, Color, transitionValues, logger, vector2FromVector3, matrix4, rotateMat4, vector3FromVector2, vector2ToPixelRatio } from './utils.js';
3
3
  import { CircleGeometry, CubeGeometry, Line2dGeometry, Line3dGeometry, PlaneGeometry, PolygonGeometry, SplineGeometry, SquareGeometry } from './geometry.js';
4
4
  export class SimulationElement {
5
5
  color;
@@ -46,6 +46,12 @@ export class SimulationElement {
46
46
  this.vertexCache.updated();
47
47
  }, t, f);
48
48
  }
49
+ getVertexCount() {
50
+ if (this.isWireframe()) {
51
+ return this.geometry.getWireframeVertexCount();
52
+ }
53
+ return this.geometry.getTriangleVertexCount();
54
+ }
49
55
  defaultUpdateMatrix(camera) {
50
56
  const matrix = matrix4();
51
57
  if (typeof this.rotation === 'number') {
@@ -219,6 +225,7 @@ export class Square extends SimulationElement2d {
219
225
  */
220
226
  constructor(pos, width, height, color, rotation, vertexColors) {
221
227
  super(pos, rotation, color);
228
+ vector2ToPixelRatio(this.pos);
222
229
  this.width = width * devicePixelRatio;
223
230
  this.height = height * devicePixelRatio;
224
231
  this.vertexColors = this.cloneColorMap(vertexColors || {});
@@ -19,6 +19,7 @@ export declare class Simulation {
19
19
  stop(): void;
20
20
  setBackground(color: Color): void;
21
21
  render(device: GPUDevice, ctx: GPUCanvasContext): void;
22
+ private getVertexCount;
22
23
  private renderScene;
23
24
  fitElement(): void;
24
25
  private assertHasCanvas;
@@ -294,24 +294,33 @@ export class Simulation {
294
294
  };
295
295
  requestAnimationFrame(frame);
296
296
  }
297
- renderScene(device, passEncoder, scene) {
297
+ getVertexCount(scene) {
298
+ let total = 0;
299
+ for (let i = 0; i < scene.length; i++) {
300
+ if (scene[i] instanceof SceneCollection)
301
+ continue;
302
+ total += scene[i].getVertexCount();
303
+ }
304
+ return total;
305
+ }
306
+ async renderScene(device, passEncoder, scene) {
298
307
  if (this.pipelines === null)
299
308
  return;
309
+ let totalVertices = this.getVertexCount(scene);
310
+ const vertexBuffer = device.createBuffer({
311
+ size: totalVertices * 40,
312
+ usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST
313
+ });
314
+ let currentOffset = 0;
300
315
  for (let i = 0; i < scene.length; i++) {
301
316
  if (scene[i] instanceof SceneCollection) {
302
317
  this.renderScene(device, passEncoder, scene[i].getScene());
303
318
  continue;
304
319
  }
305
- const buffer = scene[i].getBuffer(this.camera);
306
- const vertexF32Array = new Float32Array(buffer);
307
- const vertexBuffer = device.createBuffer({
308
- size: vertexF32Array.byteLength,
309
- usage: GPUBufferUsage.VERTEX,
310
- mappedAtCreation: true
311
- });
312
- new Float32Array(vertexBuffer.getMappedRange()).set(vertexF32Array);
320
+ const buffer = new Float32Array(scene[i].getBuffer(this.camera));
321
+ const vertexCount = buffer.length / BUF_LEN;
322
+ device.queue.writeBuffer(vertexBuffer, currentOffset, buffer);
313
323
  vertexBuffer.unmap();
314
- const vertexCount = vertexF32Array.length / BUF_LEN;
315
324
  if (scene[i].isWireframe()) {
316
325
  if (scene[i].is3d) {
317
326
  passEncoder.setPipeline(this.pipelines.lineStrip3d);
@@ -339,8 +348,9 @@ export class Simulation {
339
348
  }
340
349
  }
341
350
  }
342
- passEncoder.setVertexBuffer(0, vertexBuffer);
351
+ passEncoder.setVertexBuffer(0, vertexBuffer, currentOffset, buffer.byteLength);
343
352
  passEncoder.draw(vertexCount);
353
+ currentOffset += buffer.byteLength;
344
354
  }
345
355
  }
346
356
  fitElement() {
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.2.6",
8
+ "version": "0.2.8",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": "./dist/index.js",