simulationjsv2 0.2.5 → 0.2.7

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.
@@ -9,7 +9,7 @@ export declare abstract class SimulationElement<T extends Vector2 | Vector3 = Ve
9
9
  protected wireframe: boolean;
10
10
  protected vertexCache: VertexCache;
11
11
  protected rotation: ElementRotation<T>;
12
- is3d: boolean;
12
+ readonly is3d: boolean;
13
13
  /**
14
14
  * @param pos - Expected to be adjusted to devicePixelRatio before reaching constructor
15
15
  */
package/dist/graphics.js CHANGED
@@ -219,8 +219,8 @@ export class Square extends SimulationElement2d {
219
219
  */
220
220
  constructor(pos, width, height, color, rotation, vertexColors) {
221
221
  super(pos, rotation, color);
222
- this.width = width * devicePixelRatio;
223
- this.height = height * devicePixelRatio;
222
+ this.width = width;
223
+ this.height = height;
224
224
  this.vertexColors = this.cloneColorMap(vertexColors || {});
225
225
  this.geometry = new SquareGeometry(this.width, this.height);
226
226
  this.geometry.setVertexColorMap(this.vertexColors);
@@ -11,6 +11,7 @@ export declare class Simulation {
11
11
  private running;
12
12
  private frameRateView;
13
13
  private camera;
14
+ private pipelines;
14
15
  constructor(idOrCanvasRef: string | HTMLCanvasElement, camera?: Camera | null, showFrameRate?: boolean);
15
16
  add(el: SimulationElement<any>): void;
16
17
  setCanvasSize(width: number, height: number): void;
@@ -18,6 +19,7 @@ export declare class Simulation {
18
19
  stop(): void;
19
20
  setBackground(color: Color): void;
20
21
  render(device: GPUDevice, ctx: GPUCanvasContext): void;
22
+ private renderScene;
21
23
  fitElement(): void;
22
24
  private assertHasCanvas;
23
25
  }
@@ -26,7 +28,9 @@ export declare class SceneCollection extends SimulationElement3d {
26
28
  private name;
27
29
  private scene;
28
30
  constructor(name: string);
31
+ setWireframe(_: boolean): void;
29
32
  getName(): string;
33
+ getScene(): SimulationElement<Vector3>[];
30
34
  add(el: SimulationElement<any>): void;
31
35
  empty(): void;
32
36
  getSceneBuffer(camera: Camera): number[];
@@ -106,6 +106,7 @@ export class Simulation {
106
106
  running = true;
107
107
  frameRateView;
108
108
  camera;
109
+ pipelines;
109
110
  constructor(idOrCanvasRef, camera = null, showFrameRate = false) {
110
111
  if (typeof idOrCanvasRef === 'string') {
111
112
  const ref = document.getElementById(idOrCanvasRef);
@@ -134,6 +135,7 @@ export class Simulation {
134
135
  this.setCanvasSize(width, height);
135
136
  }
136
137
  });
138
+ this.pipelines = null;
137
139
  this.frameRateView = new FrameRateView(showFrameRate);
138
140
  this.frameRateView.updateFrameRate(1);
139
141
  }
@@ -185,19 +187,21 @@ export class Simulation {
185
187
  format: presentationFormat,
186
188
  alphaMode: 'premultiplied'
187
189
  });
188
- const pipeline2dTriangleList = createPipeline(device, shaderModule, presentationFormat, 'vertex_main_2d', 'triangle-list');
189
- const pipeline2dTriangleStrip = createPipeline(device, shaderModule, presentationFormat, 'vertex_main_2d', 'triangle-strip');
190
- const pipeline2dLineStrip = createPipeline(device, shaderModule, presentationFormat, 'vertex_main_2d', 'line-strip');
191
- const pipeline3dTriangleList = createPipeline(device, shaderModule, presentationFormat, 'vertex_main_3d', 'triangle-list');
192
- const pipeline3dTriangleStrip = createPipeline(device, shaderModule, presentationFormat, 'vertex_main_3d', 'triangle-strip');
193
- const pipeline3dLineStrip = createPipeline(device, shaderModule, presentationFormat, 'vertex_main_3d', 'line-strip');
190
+ this.pipelines = {
191
+ triangleList2d: createPipeline(device, shaderModule, presentationFormat, 'vertex_main_2d', 'triangle-list'),
192
+ triangleStrip2d: createPipeline(device, shaderModule, presentationFormat, 'vertex_main_2d', 'triangle-strip'),
193
+ lineStrip2d: createPipeline(device, shaderModule, presentationFormat, 'vertex_main_2d', 'line-strip'),
194
+ triangleList3d: createPipeline(device, shaderModule, presentationFormat, 'vertex_main_3d', 'triangle-list'),
195
+ triangleStrip3d: createPipeline(device, shaderModule, presentationFormat, 'vertex_main_3d', 'triangle-strip'),
196
+ lineStrip3d: createPipeline(device, shaderModule, presentationFormat, 'vertex_main_3d', 'line-strip')
197
+ };
194
198
  const uniformBufferSize = 4 * 16 + 4 * 16 + 4 * 2 + 8; // 4x4 matrix + 4x4 matrix + vec2<f32> + 8 bc 144 is cool
195
199
  const uniformBuffer = device.createBuffer({
196
200
  size: uniformBufferSize,
197
201
  usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
198
202
  });
199
203
  const uniformBindGroup = device.createBindGroup({
200
- layout: pipeline3dTriangleList.getBindGroupLayout(0),
204
+ layout: this.pipelines.triangleList3d.getBindGroupLayout(0),
201
205
  entries: [
202
206
  {
203
207
  binding: 0,
@@ -281,54 +285,63 @@ export class Simulation {
281
285
  screenSize.buffer, screenSize.byteOffset, screenSize.byteLength);
282
286
  const commandEncoder = device.createCommandEncoder();
283
287
  const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
284
- passEncoder.setPipeline(pipeline3dTriangleList);
288
+ passEncoder.setPipeline(this.pipelines.triangleList3d);
285
289
  passEncoder.setBindGroup(0, uniformBindGroup);
286
- for (let i = 0; i < this.scene.length; i++) {
287
- const buffer = this.scene[i].getBuffer(this.camera);
288
- const vertexF32Array = new Float32Array(buffer);
289
- const vertexBuffer = device.createBuffer({
290
- size: vertexF32Array.byteLength,
291
- usage: GPUBufferUsage.VERTEX,
292
- mappedAtCreation: true
293
- });
294
- new Float32Array(vertexBuffer.getMappedRange()).set(vertexF32Array);
295
- vertexBuffer.unmap();
296
- const vertexCount = vertexF32Array.length / BUF_LEN;
297
- if (this.scene[i].isWireframe()) {
298
- if (this.scene[i].is3d) {
299
- passEncoder.setPipeline(pipeline3dLineStrip);
290
+ this.renderScene(device, passEncoder, this.scene);
291
+ this.camera.updateConsumed();
292
+ passEncoder.end();
293
+ device.queue.submit([commandEncoder.finish()]);
294
+ };
295
+ requestAnimationFrame(frame);
296
+ }
297
+ renderScene(device, passEncoder, scene) {
298
+ if (this.pipelines === null)
299
+ return;
300
+ for (let i = 0; i < scene.length; i++) {
301
+ if (scene[i] instanceof SceneCollection) {
302
+ this.renderScene(device, passEncoder, scene[i].getScene());
303
+ continue;
304
+ }
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);
313
+ vertexBuffer.unmap();
314
+ const vertexCount = vertexF32Array.length / BUF_LEN;
315
+ if (scene[i].isWireframe()) {
316
+ if (scene[i].is3d) {
317
+ passEncoder.setPipeline(this.pipelines.lineStrip3d);
318
+ }
319
+ else {
320
+ passEncoder.setPipeline(this.pipelines.lineStrip2d);
321
+ }
322
+ }
323
+ else {
324
+ const type = scene[i].getGeometryType();
325
+ if (type === 'strip') {
326
+ if (scene[i].is3d) {
327
+ passEncoder.setPipeline(this.pipelines.triangleStrip3d);
300
328
  }
301
329
  else {
302
- passEncoder.setPipeline(pipeline2dLineStrip);
330
+ passEncoder.setPipeline(this.pipelines.triangleStrip2d);
303
331
  }
304
332
  }
305
- else {
306
- const type = this.scene[i].getGeometryType();
307
- if (type === 'strip') {
308
- if (this.scene[i].is3d) {
309
- passEncoder.setPipeline(pipeline3dTriangleStrip);
310
- }
311
- else {
312
- passEncoder.setPipeline(pipeline2dTriangleStrip);
313
- }
333
+ else if (type === 'list') {
334
+ if (scene[i].is3d) {
335
+ passEncoder.setPipeline(this.pipelines.triangleList3d);
314
336
  }
315
- else if (type === 'list') {
316
- if (this.scene[i].is3d) {
317
- passEncoder.setPipeline(pipeline3dTriangleList);
318
- }
319
- else {
320
- passEncoder.setPipeline(pipeline2dTriangleList);
321
- }
337
+ else {
338
+ passEncoder.setPipeline(this.pipelines.triangleList2d);
322
339
  }
323
340
  }
324
- passEncoder.setVertexBuffer(0, vertexBuffer);
325
- passEncoder.draw(vertexCount);
326
341
  }
327
- this.camera.updateConsumed();
328
- passEncoder.end();
329
- device.queue.submit([commandEncoder.finish()]);
330
- };
331
- requestAnimationFrame(frame);
342
+ passEncoder.setVertexBuffer(0, vertexBuffer);
343
+ passEncoder.draw(vertexCount);
344
+ }
332
345
  }
333
346
  fitElement() {
334
347
  this.assertHasCanvas();
@@ -352,13 +365,18 @@ export class SceneCollection extends SimulationElement3d {
352
365
  scene;
353
366
  constructor(name) {
354
367
  super(vector3());
368
+ this.wireframe = false;
355
369
  this.name = name;
356
370
  this.scene = [];
357
371
  this.geometry = new BlankGeometry();
358
372
  }
373
+ setWireframe(_) { }
359
374
  getName() {
360
375
  return this.name;
361
376
  }
377
+ getScene() {
378
+ return this.scene;
379
+ }
362
380
  add(el) {
363
381
  applyElementToScene(this.scene, el);
364
382
  }
@@ -372,6 +390,7 @@ export class SceneCollection extends SimulationElement3d {
372
390
  return this.getSceneBuffer(camera);
373
391
  }
374
392
  getTriangles(camera) {
393
+ console.log('here');
375
394
  return this.getSceneBuffer(camera);
376
395
  }
377
396
  updateMatrix(camera) {
package/dist/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference types="dist" />
1
2
  import { CubicBezierCurve2d, SplinePoint2d } from './graphics.js';
2
3
  import { Color, Vertex } from './utils.js';
3
4
  export type Vector4 = Float32Array & [number, number, number, number];
@@ -59,3 +60,11 @@ export type Line3dGeometryParams = LineGeometryParams<Vector3>;
59
60
  export type PolygonGeometryParams = {
60
61
  points: Vertex[];
61
62
  };
63
+ export type PipelineGroup = {
64
+ triangleList2d: GPURenderPipeline;
65
+ triangleStrip2d: GPURenderPipeline;
66
+ lineStrip2d: GPURenderPipeline;
67
+ triangleList3d: GPURenderPipeline;
68
+ triangleStrip3d: GPURenderPipeline;
69
+ lineStrip3d: GPURenderPipeline;
70
+ };
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.5",
8
+ "version": "0.2.7",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": "./dist/index.js",