simulationjsv2 0.1.8 → 0.1.10
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.
- package/dist/graphics.d.ts +5 -2
- package/dist/graphics.js +35 -8
- package/dist/simulation.js +9 -16
- package/dist/utils.d.ts +2 -1
- package/dist/utils.js +27 -3
- package/package.json +1 -1
package/dist/graphics.d.ts
CHANGED
|
@@ -72,8 +72,10 @@ export declare class BezierCurve2d {
|
|
|
72
72
|
}
|
|
73
73
|
export declare class CubicBezierCurve2d extends BezierCurve2d {
|
|
74
74
|
private detail;
|
|
75
|
-
|
|
75
|
+
private colors;
|
|
76
|
+
constructor(points: [Vector2, Vector2, Vector2, Vector2], detail?: number, colors?: (Color | null)[]);
|
|
76
77
|
getDetail(): number | undefined;
|
|
78
|
+
getColors(): (Color | null)[];
|
|
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 | null): (Color | null)[];
|
|
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:
|
|
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
|
-
|
|
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,22 @@ export class SplinePoint2d {
|
|
|
518
523
|
getDetail() {
|
|
519
524
|
return this.detail;
|
|
520
525
|
}
|
|
526
|
+
getColors(prevColor) {
|
|
527
|
+
const colors = [null, null];
|
|
528
|
+
if (this.start && this.start.getColor()) {
|
|
529
|
+
colors[0] = this.start.getColor();
|
|
530
|
+
}
|
|
531
|
+
if (this.end.getColor()) {
|
|
532
|
+
colors[1] = this.end.getColor();
|
|
533
|
+
}
|
|
534
|
+
if (prevColor) {
|
|
535
|
+
colors.unshift(prevColor);
|
|
536
|
+
}
|
|
537
|
+
if (colors.at(-1) === null) {
|
|
538
|
+
colors.pop();
|
|
539
|
+
}
|
|
540
|
+
return colors;
|
|
541
|
+
}
|
|
521
542
|
getVectorArray(prevEnd, prevControl) {
|
|
522
543
|
const firstControl = cloneBuf(this.control1 || prevControl || vector2());
|
|
523
544
|
if (prevEnd) {
|
|
@@ -540,8 +561,8 @@ export class Spline2d extends SimulationElement {
|
|
|
540
561
|
detail;
|
|
541
562
|
interpolateLimit;
|
|
542
563
|
distance;
|
|
543
|
-
constructor(pos, points, width = 2,
|
|
544
|
-
super(
|
|
564
|
+
constructor(pos, points, width = 2, detail = 40) {
|
|
565
|
+
super(pos.getPos(), pos.getColor() || undefined);
|
|
545
566
|
this.curves = [];
|
|
546
567
|
this.width = width * devicePixelRatio;
|
|
547
568
|
this.detail = detail;
|
|
@@ -549,13 +570,17 @@ export class Spline2d extends SimulationElement {
|
|
|
549
570
|
this.distance = 0;
|
|
550
571
|
for (let i = 0; i < points.length; i++) {
|
|
551
572
|
let prevControl = null;
|
|
573
|
+
let prevColor = null;
|
|
552
574
|
if (i > 0) {
|
|
553
575
|
prevControl = cloneBuf(points[i - 1].getRawControls()[1]);
|
|
554
576
|
vec2.negate(prevControl, prevControl);
|
|
555
|
-
|
|
577
|
+
const prevColors = points[i - 1].getColors();
|
|
578
|
+
if (prevColors.at(-1)) {
|
|
579
|
+
prevColor = prevColors.at(-1) || null;
|
|
580
|
+
}
|
|
556
581
|
}
|
|
557
582
|
const bezierPoints = points[i].getVectorArray(i > 0 ? vector2FromVector3(points[i - 1].getEnd().getPos()) : null, prevControl);
|
|
558
|
-
const curve = new CubicBezierCurve2d(bezierPoints, points[i].getDetail());
|
|
583
|
+
const curve = new CubicBezierCurve2d(bezierPoints, points[i].getDetail(), points[i].getColors(prevColor));
|
|
559
584
|
this.distance += curve.getLength();
|
|
560
585
|
this.curves.push(curve);
|
|
561
586
|
}
|
|
@@ -599,9 +624,11 @@ export class Spline2d extends SimulationElement {
|
|
|
599
624
|
const normal = vector2(-slope[1], slope[0]);
|
|
600
625
|
vec2.normalize(normal, normal);
|
|
601
626
|
vec2.scale(normal, this.width / 2, normal);
|
|
602
|
-
const
|
|
627
|
+
const colors = this.curves[i].getColors().map((c) => (c ? c : this.getColor()));
|
|
628
|
+
const vertexColor = interpolateColors(colors, currentInterpolation);
|
|
629
|
+
const vertTop = vertex(point[0] + normal[0], point[1] + normal[1], 0, vertexColor);
|
|
603
630
|
verticesTop.push(vertTop);
|
|
604
|
-
const vertBottom = vertex(point[0] - normal[0], point[1] - normal[1]);
|
|
631
|
+
const vertBottom = vertex(point[0] - normal[0], point[1] - normal[1], 0, vertexColor);
|
|
605
632
|
verticesBottom.unshift(vertBottom);
|
|
606
633
|
if (atLimit) {
|
|
607
634
|
break outer;
|
package/dist/simulation.js
CHANGED
|
@@ -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,
|
|
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
|
-
|
|
231
|
-
|
|
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
|
|
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
|
-
|
|
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 =
|
|
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
|
|
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
|
|
128
|
+
export const buildMultisampleTexture = (device, ctx, width, height) => {
|
|
129
129
|
return device.createTexture({
|
|
130
130
|
size: [width, height],
|
|
131
|
-
format:
|
|
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,26 @@ 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
|
+
if (colors.length === 0)
|
|
305
|
+
return color();
|
|
306
|
+
if (colors.length === 1)
|
|
307
|
+
return colors[0];
|
|
308
|
+
const colorInterval = 1 / colors.length;
|
|
309
|
+
let index = Math.floor(t / colorInterval);
|
|
310
|
+
if (index === colors.length)
|
|
311
|
+
index--;
|
|
312
|
+
const from = index === colors.length - 1 ? colors[index - 1] : colors[index];
|
|
313
|
+
const to = index === colors.length - 1 ? colors[index] : colors[index + 1];
|
|
314
|
+
const diff = to.diff(from);
|
|
315
|
+
diff.r *= t / (colorInterval * colors.length);
|
|
316
|
+
diff.g *= t / (colorInterval * colors.length);
|
|
317
|
+
diff.b *= t / (colorInterval * colors.length);
|
|
318
|
+
diff.a *= t / (colorInterval * colors.length);
|
|
319
|
+
const res = from.clone();
|
|
320
|
+
res.r += diff.r;
|
|
321
|
+
res.g += diff.g;
|
|
322
|
+
res.b += diff.b;
|
|
323
|
+
res.a += diff.a;
|
|
324
|
+
return res;
|
|
325
|
+
}
|