simulationjsv2 0.1.8 → 0.1.9

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.
@@ -72,8 +72,10 @@ export declare class BezierCurve2d {
72
72
  }
73
73
  export declare class CubicBezierCurve2d extends BezierCurve2d {
74
74
  private detail;
75
- constructor(points: [Vector2, Vector2, Vector2, Vector2], detail?: number);
75
+ private colors;
76
+ constructor(points: [Vector2, Vector2, Vector2, Vector2], detail?: number, colors?: Color[]);
76
77
  getDetail(): number | undefined;
78
+ getColors(): Color[];
77
79
  }
78
80
  export declare class SplinePoint2d {
79
81
  private start;
@@ -88,6 +90,7 @@ export declare class SplinePoint2d {
88
90
  getControls(): readonly [Vector2 | null, Vector2];
89
91
  getRawControls(): [Vector2, Vector2];
90
92
  getDetail(): number | undefined;
93
+ getColors(prevColor?: Color): Color[];
91
94
  getVectorArray(prevEnd: Vector2 | null, prevControl: Vector2 | null): readonly [Vector2, Vector2, Vector2, Vector2];
92
95
  }
93
96
  export declare class Spline2d extends SimulationElement {
@@ -96,7 +99,7 @@ export declare class Spline2d extends SimulationElement {
96
99
  private detail;
97
100
  private interpolateLimit;
98
101
  private distance;
99
- constructor(pos: Vector2, points: SplinePoint2d[], width?: number, color?: Color, detail?: number);
102
+ constructor(pos: Vertex, points: SplinePoint2d[], width?: number, detail?: number);
100
103
  setInterpolateLimit(limit: number, t?: number, f?: LerpFunc): Promise<void>;
101
104
  getBuffer(camera: Camera, force: boolean): number[];
102
105
  }
package/dist/graphics.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { vec3, quat, mat4, vec2, vec4 } from 'wgpu-matrix';
2
- import { Vertex, VertexCache, cloneBuf, color, colorFromVector4, lossyTriangulate, vec3ToPixelRatio, vector3FromVector2, vector2, vector3, vertex, vertexBuffer2d, vertexBuffer3d, Color, transitionValues, logger, vector2FromVector3 } from './utils.js';
2
+ import { Vertex, VertexCache, cloneBuf, color, colorFromVector4, lossyTriangulate, vec3ToPixelRatio, vector3FromVector2, vector2, vector3, vertex, vertexBuffer2d, vertexBuffer3d, Color, transitionValues, logger, vector2FromVector3, interpolateColors } from './utils.js';
3
3
  export class SimulationElement {
4
4
  pos;
5
5
  color;
@@ -480,13 +480,18 @@ export class BezierCurve2d {
480
480
  }
481
481
  export class CubicBezierCurve2d extends BezierCurve2d {
482
482
  detail;
483
- constructor(points, detail) {
483
+ colors;
484
+ constructor(points, detail, colors) {
484
485
  super(points);
485
486
  this.detail = detail;
487
+ this.colors = colors || [];
486
488
  }
487
489
  getDetail() {
488
490
  return this.detail;
489
491
  }
492
+ getColors() {
493
+ return this.colors;
494
+ }
490
495
  }
491
496
  export class SplinePoint2d {
492
497
  start;
@@ -518,6 +523,18 @@ export class SplinePoint2d {
518
523
  getDetail() {
519
524
  return this.detail;
520
525
  }
526
+ getColors(prevColor) {
527
+ const colors = [];
528
+ if (prevColor)
529
+ colors.push(prevColor);
530
+ if (this.start && this.start.getColor()) {
531
+ colors.push(this.start.getColor());
532
+ }
533
+ if (this.end.getColor()) {
534
+ colors.push(this.end.getColor());
535
+ }
536
+ return colors;
537
+ }
521
538
  getVectorArray(prevEnd, prevControl) {
522
539
  const firstControl = cloneBuf(this.control1 || prevControl || vector2());
523
540
  if (prevEnd) {
@@ -540,8 +557,8 @@ export class Spline2d extends SimulationElement {
540
557
  detail;
541
558
  interpolateLimit;
542
559
  distance;
543
- constructor(pos, points, width = 2, color, detail = 40) {
544
- super(vector3FromVector2(pos), color);
560
+ constructor(pos, points, width = 2, detail = 40) {
561
+ super(pos.getPos(), pos.getColor() || undefined);
545
562
  this.curves = [];
546
563
  this.width = width * devicePixelRatio;
547
564
  this.detail = detail;
@@ -549,13 +566,17 @@ export class Spline2d extends SimulationElement {
549
566
  this.distance = 0;
550
567
  for (let i = 0; i < points.length; i++) {
551
568
  let prevControl = null;
569
+ let prevColor = this.getColor();
552
570
  if (i > 0) {
553
571
  prevControl = cloneBuf(points[i - 1].getRawControls()[1]);
554
572
  vec2.negate(prevControl, prevControl);
555
- console.log(prevControl);
573
+ const prevColors = points[i - 1].getColors();
574
+ if (prevColors.at(-1)) {
575
+ prevColor = prevColors.at(-1);
576
+ }
556
577
  }
557
578
  const bezierPoints = points[i].getVectorArray(i > 0 ? vector2FromVector3(points[i - 1].getEnd().getPos()) : null, prevControl);
558
- const curve = new CubicBezierCurve2d(bezierPoints, points[i].getDetail());
579
+ const curve = new CubicBezierCurve2d(bezierPoints, points[i].getDetail(), points[i].getColors(prevColor));
559
580
  this.distance += curve.getLength();
560
581
  this.curves.push(curve);
561
582
  }
@@ -599,9 +620,10 @@ export class Spline2d extends SimulationElement {
599
620
  const normal = vector2(-slope[1], slope[0]);
600
621
  vec2.normalize(normal, normal);
601
622
  vec2.scale(normal, this.width / 2, normal);
602
- const vertTop = vertex(point[0] + normal[0], point[1] + normal[1]);
623
+ const vertexColor = interpolateColors(this.curves[i].getColors(), currentInterpolation);
624
+ const vertTop = vertex(point[0] + normal[0], point[1] + normal[1], 0, vertexColor);
603
625
  verticesTop.push(vertTop);
604
- const vertBottom = vertex(point[0] - normal[0], point[1] - normal[1]);
626
+ const vertBottom = vertex(point[0] - normal[0], point[1] - normal[1], 0, vertexColor);
605
627
  verticesBottom.unshift(vertBottom);
606
628
  if (atLimit) {
607
629
  break outer;
@@ -1,7 +1,7 @@
1
1
  import { vec3 } from 'wgpu-matrix';
2
2
  import { SimulationElement } from './graphics.js';
3
3
  import { BUF_LEN } from './constants.js';
4
- import { Color, applyElementToScene, buildDepthTexture, buildProjectionMatrix, getOrthoMatrix, getTransformationMatrix, logger, transitionValues, vector2, vector3 } from './utils.js';
4
+ import { Color, applyElementToScene, buildMultisampleTexture, buildProjectionMatrix, getOrthoMatrix, getTransformationMatrix, logger, transitionValues, vector2, vector3 } from './utils.js';
5
5
  const vertexSize = 44; // 4 * 10 + 1
6
6
  const colorOffset = 16; // 4 * 4
7
7
  const uvOffset = 32; // 4 * 8
@@ -227,10 +227,8 @@ export class Simulation {
227
227
  primitive: {
228
228
  topology: 'triangle-list'
229
229
  },
230
- depthStencil: {
231
- depthWriteEnabled: true,
232
- depthCompare: 'less',
233
- format: 'depth24plus'
230
+ multisample: {
231
+ count: 4
234
232
  }
235
233
  });
236
234
  const uniformBufferSize = 4 * 16 + 4 * 16 + 4 * 2 + 8; // 4x4 matrix + 4x4 matrix + vec2<f32> + 8 bc 144 is cool
@@ -268,15 +266,9 @@ export class Simulation {
268
266
  orthoMatrix = getOrthoMatrix(this.camera.getScreenSize());
269
267
  };
270
268
  updateOrthoMatrix();
271
- let depthTexture = buildDepthTexture(device, canvas.width, canvas.height);
269
+ let multisampleTexture = buildMultisampleTexture(device, ctx, canvas.width, canvas.height);
272
270
  const renderPassDescriptor = {
273
- colorAttachments: [colorAttachment],
274
- depthStencilAttachment: {
275
- view: depthTexture.createView(),
276
- depthClearValue: 1.0,
277
- depthLoadOp: 'clear',
278
- depthStoreOp: 'store'
279
- }
271
+ colorAttachments: [colorAttachment]
280
272
  };
281
273
  // sub 10 to start with a reasonable gap between starting time and next frame time
282
274
  let prev = Date.now() - 10;
@@ -303,11 +295,12 @@ export class Simulation {
303
295
  aspect = this.camera.getAspectRatio();
304
296
  projectionMatrix = buildProjectionMatrix(aspect);
305
297
  updateModelViewProjectionMatrix();
306
- depthTexture = buildDepthTexture(device, screenSize[0], screenSize[1]);
307
- renderPassDescriptor.depthStencilAttachment.view = depthTexture.createView();
298
+ multisampleTexture = buildMultisampleTexture(device, ctx, screenSize[0], screenSize[1]);
308
299
  }
309
300
  // @ts-ignore
310
- renderPassDescriptor.colorAttachments[0].view = ctx.getCurrentTexture().createView();
301
+ renderPassDescriptor.colorAttachments[0].view = multisampleTexture.createView();
302
+ // @ts-ignore
303
+ renderPassDescriptor.colorAttachments[0].resolveTarget = ctx.getCurrentTexture().createView();
311
304
  if (this.camera.hasUpdated()) {
312
305
  updateOrthoMatrix();
313
306
  updateModelViewProjectionMatrix();
package/dist/utils.d.ts CHANGED
@@ -51,7 +51,7 @@ export declare class Vertex {
51
51
  export declare const buildProjectionMatrix: (aspectRatio: number, zNear?: number, zFar?: number) => any;
52
52
  export declare const getTransformationMatrix: (pos: Vector3, rotation: Vector3, projectionMatrix: mat4) => Float32Array;
53
53
  export declare const getOrthoMatrix: (screenSize: [number, number]) => Float32Array;
54
- export declare const buildDepthTexture: (device: GPUDevice, width: number, height: number) => GPUTexture;
54
+ export declare const buildMultisampleTexture: (device: GPUDevice, ctx: GPUCanvasContext, width: number, height: number) => GPUTexture;
55
55
  export declare const applyElementToScene: (scene: SimulationElement[], camera: Camera | null, el: SimulationElement) => void;
56
56
  declare class Logger {
57
57
  constructor();
@@ -90,4 +90,5 @@ export declare function color(r?: number, g?: number, b?: number, a?: number): C
90
90
  export declare function colorf(val: number, a?: number): Color;
91
91
  export declare function splinePoint2d(end: Vertex, control1: Vector2, control2: Vector2, detail?: number): SplinePoint2d;
92
92
  export declare function continuousSplinePoint2d(end: Vertex, control: Vector2, detail?: number): SplinePoint2d;
93
+ export declare function interpolateColors(colors: Color[], t: number): Color;
93
94
  export {};
package/dist/utils.js CHANGED
@@ -125,11 +125,12 @@ export const getTransformationMatrix = (pos, rotation, projectionMatrix) => {
125
125
  export const getOrthoMatrix = (screenSize) => {
126
126
  return mat4.ortho(0, screenSize[0], 0, screenSize[1], 0, 100);
127
127
  };
128
- export const buildDepthTexture = (device, width, height) => {
128
+ export const buildMultisampleTexture = (device, ctx, width, height) => {
129
129
  return device.createTexture({
130
130
  size: [width, height],
131
- format: 'depth24plus',
132
- usage: GPUTextureUsage.RENDER_ATTACHMENT
131
+ format: ctx.getCurrentTexture().format,
132
+ usage: GPUTextureUsage.RENDER_ATTACHMENT,
133
+ sampleCount: 4
133
134
  });
134
135
  };
135
136
  export const applyElementToScene = (scene, camera, el) => {
@@ -299,3 +300,22 @@ export function continuousSplinePoint2d(end, control, detail) {
299
300
  vec2.add(end.getPos(), control, control);
300
301
  return new SplinePoint2d(null, end, null, control, rawControls, detail);
301
302
  }
303
+ export function interpolateColors(colors, t) {
304
+ const colorInterval = 1 / colors.length;
305
+ let index = Math.floor(t / colorInterval);
306
+ if (index === colors.length)
307
+ index--;
308
+ const from = index === colors.length - 1 ? colors[index - 1] : colors[index];
309
+ const to = index === colors.length - 1 ? colors[index] : colors[index + 1];
310
+ const diff = to.diff(from);
311
+ diff.r *= t / (colorInterval * colors.length);
312
+ diff.g *= t / (colorInterval * colors.length);
313
+ diff.b *= t / (colorInterval * colors.length);
314
+ diff.a *= t / (colorInterval * colors.length);
315
+ const res = from.clone();
316
+ res.r += diff.r;
317
+ res.g += diff.g;
318
+ res.b += diff.b;
319
+ res.a += diff.a;
320
+ return res;
321
+ }
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.8",
8
+ "version": "0.1.9",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": "./dist/index.js",