simulationjsv2 0.8.4 → 0.9.0
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/geometry.js +5 -5
- package/dist/graphics.d.ts +2 -5
- package/dist/graphics.js +15 -32
- package/dist/internalUtils.d.ts +1 -3
- package/dist/internalUtils.js +0 -9
- package/dist/simulation.js +2 -2
- package/dist/utils.js +0 -5
- package/package.json +1 -1
package/dist/geometry.js
CHANGED
|
@@ -110,10 +110,10 @@ export class SquareGeometry extends Geometry {
|
|
|
110
110
|
}
|
|
111
111
|
recompute() {
|
|
112
112
|
this.vertices = [
|
|
113
|
-
vector3(-this.params.width, this.params.height),
|
|
114
|
-
vector3(this.params.width, this.params.height),
|
|
115
|
-
vector3(this.params.width, -this.params.height),
|
|
116
|
-
vector3(-this.params.width, -this.params.height)
|
|
113
|
+
vector3(-this.params.width / 2, this.params.height / 2),
|
|
114
|
+
vector3(this.params.width / 2, this.params.height / 2),
|
|
115
|
+
vector3(this.params.width / 2, -this.params.height / 2),
|
|
116
|
+
vector3(-this.params.width / 2, -this.params.height / 2)
|
|
117
117
|
];
|
|
118
118
|
}
|
|
119
119
|
}
|
|
@@ -310,7 +310,7 @@ export class Line2dGeometry extends Geometry {
|
|
|
310
310
|
recompute() {
|
|
311
311
|
const normal = vector2(-this.params.to[1], this.params.to[0]);
|
|
312
312
|
vec2.normalize(normal, normal);
|
|
313
|
-
vec2.scale(normal, this.params.thickness
|
|
313
|
+
vec2.scale(normal, this.params.thickness, normal);
|
|
314
314
|
this.vertices = [
|
|
315
315
|
vector3(-normal[0], -normal[1]),
|
|
316
316
|
vector3(normal[0], normal[1]),
|
package/dist/graphics.d.ts
CHANGED
|
@@ -26,9 +26,6 @@ export declare abstract class SimulationElement3d {
|
|
|
26
26
|
isInstanced: boolean;
|
|
27
27
|
is3d: boolean;
|
|
28
28
|
isEmpty: boolean;
|
|
29
|
-
/**
|
|
30
|
-
* @param pos - Expected to be adjusted to devicePixelRatio before reaching constructor
|
|
31
|
-
*/
|
|
32
29
|
constructor(pos: Vector3, rotation: Vector3, color?: Color);
|
|
33
30
|
getId(): string | null;
|
|
34
31
|
setId(id: string): void;
|
|
@@ -67,8 +64,8 @@ export declare abstract class SimulationElement3d {
|
|
|
67
64
|
getRotation(): Vector3;
|
|
68
65
|
getCenterOffset(): Vector3;
|
|
69
66
|
fill(newColor: Color, t?: number, f?: LerpFunc): Promise<void>;
|
|
70
|
-
move(amount: Vector3, t?: number, f?: LerpFunc
|
|
71
|
-
moveTo(pos: Vector3, t?: number, f?: LerpFunc
|
|
67
|
+
move(amount: Vector3, t?: number, f?: LerpFunc): Promise<void>;
|
|
68
|
+
moveTo(pos: Vector3, t?: number, f?: LerpFunc): Promise<void>;
|
|
72
69
|
rotateChildren(angle: Vector3): void;
|
|
73
70
|
rotate(amount: Vector3, t?: number, f?: LerpFunc): Promise<void>;
|
|
74
71
|
rotateTo(rot: Vector3, t?: number, f?: LerpFunc): Promise<void>;
|
package/dist/graphics.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { vec3, mat4, vec2 } from 'wgpu-matrix';
|
|
2
2
|
import { cloneBuf, vector2, vector3, Color, vector2FromVector3, matrix4, vector3FromVector2, distance2d, color, interpolateColors } from './utils.js';
|
|
3
3
|
import { BlankGeometry, CircleGeometry, CubeGeometry, Line2dGeometry, Line3dGeometry, PlaneGeometry, PolygonGeometry, Spline2dGeometry, SquareGeometry, TraceLines2dGeometry as TraceLinesGeometry } from './geometry.js';
|
|
4
|
-
import { Float32ArrayCache, internalTransitionValues, posTo2dScreen
|
|
4
|
+
import { Float32ArrayCache, internalTransitionValues, posTo2dScreen } from './internalUtils.js';
|
|
5
5
|
import { mat4ByteLength, modelProjMatOffset } from './constants.js';
|
|
6
6
|
import { MemoBuffer } from './buffers.js';
|
|
7
7
|
import { globalInfo, logger, pipelineCache } from './globals.js';
|
|
@@ -27,9 +27,6 @@ export class SimulationElement3d {
|
|
|
27
27
|
isInstanced = false;
|
|
28
28
|
is3d = true;
|
|
29
29
|
isEmpty = false;
|
|
30
|
-
/**
|
|
31
|
-
* @param pos - Expected to be adjusted to devicePixelRatio before reaching constructor
|
|
32
|
-
*/
|
|
33
30
|
constructor(pos, rotation, color = new Color()) {
|
|
34
31
|
this.pos = pos;
|
|
35
32
|
this.centerOffset = vector3();
|
|
@@ -150,7 +147,8 @@ export class SimulationElement3d {
|
|
|
150
147
|
if (!this.parent)
|
|
151
148
|
return;
|
|
152
149
|
this.parent.mirrorParentTransforms3d(mat);
|
|
153
|
-
|
|
150
|
+
const pos = cloneBuf(this.parent.getRelativePos());
|
|
151
|
+
mat4.translate(mat, pos, mat);
|
|
154
152
|
const parentRot = this.parent.getRotation();
|
|
155
153
|
mat4.rotateZ(mat, parentRot[2], mat);
|
|
156
154
|
mat4.rotateY(mat, parentRot[1], mat);
|
|
@@ -161,7 +159,8 @@ export class SimulationElement3d {
|
|
|
161
159
|
if (this.parent) {
|
|
162
160
|
this.mirrorParentTransforms3d(this.modelMatrix);
|
|
163
161
|
}
|
|
164
|
-
|
|
162
|
+
const pos = cloneBuf(this.pos);
|
|
163
|
+
mat4.translate(this.modelMatrix, pos, this.modelMatrix);
|
|
165
164
|
mat4.rotateZ(this.modelMatrix, this.rotation[2], this.modelMatrix);
|
|
166
165
|
mat4.rotateY(this.modelMatrix, this.rotation[1], this.modelMatrix);
|
|
167
166
|
mat4.rotateX(this.modelMatrix, this.rotation[0], this.modelMatrix);
|
|
@@ -176,7 +175,8 @@ export class SimulationElement3d {
|
|
|
176
175
|
this.parent.mirrorParentTransforms2d(mat);
|
|
177
176
|
const parentRot = this.parent.getRotation();
|
|
178
177
|
mat4.rotateZ(mat, parentRot[2], mat);
|
|
179
|
-
|
|
178
|
+
const pos = cloneBuf(this.pos);
|
|
179
|
+
mat4.translate(mat, pos, mat);
|
|
180
180
|
}
|
|
181
181
|
updateModelMatrix2d() {
|
|
182
182
|
mat4.identity(this.modelMatrix);
|
|
@@ -235,10 +235,8 @@ export class SimulationElement3d {
|
|
|
235
235
|
this.vertexCache.updated();
|
|
236
236
|
}, t, f);
|
|
237
237
|
}
|
|
238
|
-
move(amount, t = 0, f
|
|
238
|
+
move(amount, t = 0, f) {
|
|
239
239
|
const tempAmount = cloneBuf(amount);
|
|
240
|
-
if (!fromDevicePixelRatio)
|
|
241
|
-
vector3ToPixelRatio(tempAmount);
|
|
242
240
|
const finalPos = cloneBuf(this.pos);
|
|
243
241
|
vec3.add(finalPos, tempAmount, finalPos);
|
|
244
242
|
return internalTransitionValues((p) => {
|
|
@@ -249,10 +247,8 @@ export class SimulationElement3d {
|
|
|
249
247
|
this.pos = finalPos;
|
|
250
248
|
}, t, f);
|
|
251
249
|
}
|
|
252
|
-
moveTo(pos, t = 0, f
|
|
250
|
+
moveTo(pos, t = 0, f) {
|
|
253
251
|
const tempPos = cloneBuf(pos);
|
|
254
|
-
if (!fromDevicePixelRatio)
|
|
255
|
-
vector3ToPixelRatio(tempPos);
|
|
256
252
|
const diff = vector3();
|
|
257
253
|
vec3.sub(tempPos, this.pos, diff);
|
|
258
254
|
return internalTransitionValues((p) => {
|
|
@@ -319,7 +315,6 @@ export class SimulationElement3d {
|
|
|
319
315
|
const vertices = this.geometry.getVertices();
|
|
320
316
|
const stride = this.shader.getBufferLength();
|
|
321
317
|
const vertexBuffer = new Float32Array(vertices.length * stride);
|
|
322
|
-
// const shader = this.isWireframe() ? defaultShader : this.shader;
|
|
323
318
|
for (let i = 0; i < vertices.length; i++) {
|
|
324
319
|
this.shader.setVertexInfo(this, vertexBuffer, vertices[i], i, i * stride);
|
|
325
320
|
}
|
|
@@ -349,7 +344,6 @@ export class SimulationElement2d extends SimulationElement3d {
|
|
|
349
344
|
is3d = false;
|
|
350
345
|
constructor(pos, rotation = vector3(), color) {
|
|
351
346
|
super(vector3FromVector2(pos), rotation, color);
|
|
352
|
-
vector3ToPixelRatio(this.pos);
|
|
353
347
|
}
|
|
354
348
|
rotate2d(amount, t = 0, f) {
|
|
355
349
|
return super.rotate(vector3(0, 0, amount), t, f);
|
|
@@ -387,8 +381,8 @@ export class Square extends SimulationElement2d {
|
|
|
387
381
|
*/
|
|
388
382
|
constructor(pos, width, height, color, rotation) {
|
|
389
383
|
super(pos, vector3(0, 0, rotation), color);
|
|
390
|
-
this.width = width
|
|
391
|
-
this.height = height
|
|
384
|
+
this.width = width;
|
|
385
|
+
this.height = height;
|
|
392
386
|
this.geometry = new SquareGeometry(this.width, this.height);
|
|
393
387
|
}
|
|
394
388
|
getWidth() {
|
|
@@ -443,7 +437,6 @@ export class Square extends SimulationElement2d {
|
|
|
443
437
|
}, t, f);
|
|
444
438
|
}
|
|
445
439
|
setWidth(num, t = 0, f) {
|
|
446
|
-
num *= devicePixelRatio;
|
|
447
440
|
const diffWidth = num - this.width;
|
|
448
441
|
return internalTransitionValues((p) => {
|
|
449
442
|
this.width += diffWidth * p;
|
|
@@ -456,7 +449,6 @@ export class Square extends SimulationElement2d {
|
|
|
456
449
|
}, t, f);
|
|
457
450
|
}
|
|
458
451
|
setHeight(num, t = 0, f) {
|
|
459
|
-
num *= devicePixelRatio;
|
|
460
452
|
const diffHeight = num - this.height;
|
|
461
453
|
return internalTransitionValues((p) => {
|
|
462
454
|
this.height += diffHeight * p;
|
|
@@ -475,12 +467,11 @@ export class Circle extends SimulationElement2d {
|
|
|
475
467
|
detail;
|
|
476
468
|
constructor(pos, radius, color, detail = 50) {
|
|
477
469
|
super(pos, vector3(), color);
|
|
478
|
-
this.radius = radius
|
|
470
|
+
this.radius = radius;
|
|
479
471
|
this.detail = detail;
|
|
480
472
|
this.geometry = new CircleGeometry(this.radius, this.detail);
|
|
481
473
|
}
|
|
482
474
|
setRadius(num, t = 0, f) {
|
|
483
|
-
num *= devicePixelRatio;
|
|
484
475
|
const diff = num - this.radius;
|
|
485
476
|
return internalTransitionValues((p) => {
|
|
486
477
|
this.radius += diff * p;
|
|
@@ -592,7 +583,6 @@ export class Line3d extends SimulationElement3d {
|
|
|
592
583
|
super(pos.getPos(), vector3(), to.getColor() ?? undefined);
|
|
593
584
|
this.thickness = thickness;
|
|
594
585
|
this.to = to.getPos();
|
|
595
|
-
vec3.scale(this.to, devicePixelRatio, this.to);
|
|
596
586
|
vec3.sub(this.to, this.pos, this.to);
|
|
597
587
|
this.geometry = new Line3dGeometry(this.pos, this.to, this.thickness);
|
|
598
588
|
}
|
|
@@ -621,7 +611,7 @@ export class Line2d extends SimulationElement2d {
|
|
|
621
611
|
thickness;
|
|
622
612
|
constructor(from, to, thickness = 1) {
|
|
623
613
|
super(vector2FromVector3(from.getPos()), vector3(), from.getColor() ?? undefined);
|
|
624
|
-
this.thickness = thickness
|
|
614
|
+
this.thickness = thickness;
|
|
625
615
|
this.to = to.getPos();
|
|
626
616
|
vec2.sub(this.to, this.pos, this.to);
|
|
627
617
|
this.geometry = new Line2dGeometry(this.pos, this.to, this.thickness);
|
|
@@ -631,8 +621,6 @@ export class Line2d extends SimulationElement2d {
|
|
|
631
621
|
}
|
|
632
622
|
setEnd(pos, t = 0, f) {
|
|
633
623
|
const tempPos = cloneBuf(pos);
|
|
634
|
-
vector3ToPixelRatio(tempPos);
|
|
635
|
-
// vec2.sub(tempPos, this.getPos(), tempPos);
|
|
636
624
|
const diff = vector3();
|
|
637
625
|
vec2.sub(tempPos, this.to, diff);
|
|
638
626
|
return internalTransitionValues((p) => {
|
|
@@ -660,7 +648,6 @@ export class Cube extends SimulationElement3d {
|
|
|
660
648
|
this.geometry = new CubeGeometry(this.width, this.height, this.depth);
|
|
661
649
|
}
|
|
662
650
|
setWidth(width, t = 0, f) {
|
|
663
|
-
width *= devicePixelRatio;
|
|
664
651
|
const diff = width - this.width;
|
|
665
652
|
return internalTransitionValues((p) => {
|
|
666
653
|
this.width += diff * p;
|
|
@@ -673,7 +660,6 @@ export class Cube extends SimulationElement3d {
|
|
|
673
660
|
}, t, f);
|
|
674
661
|
}
|
|
675
662
|
setHeight(height, t = 0, f) {
|
|
676
|
-
height *= devicePixelRatio;
|
|
677
663
|
const diff = height - this.width;
|
|
678
664
|
return internalTransitionValues((p) => {
|
|
679
665
|
this.height += diff * p;
|
|
@@ -686,7 +672,6 @@ export class Cube extends SimulationElement3d {
|
|
|
686
672
|
}, t, f);
|
|
687
673
|
}
|
|
688
674
|
setDepth(depth, t = 0, f) {
|
|
689
|
-
depth *= devicePixelRatio;
|
|
690
675
|
const diff = depth - this.width;
|
|
691
676
|
return internalTransitionValues((p) => {
|
|
692
677
|
this.depth += diff * p;
|
|
@@ -872,10 +857,10 @@ export class Spline2d extends SimulationElement2d {
|
|
|
872
857
|
interpolateStart;
|
|
873
858
|
interpolateLimit;
|
|
874
859
|
length;
|
|
875
|
-
constructor(pos, points, thickness =
|
|
860
|
+
constructor(pos, points, thickness = 1, detail = 40) {
|
|
876
861
|
const tempPos = vector2FromVector3(pos.getPos());
|
|
877
862
|
super(tempPos, vector3(), pos.getColor() ?? undefined);
|
|
878
|
-
this.thickness = thickness
|
|
863
|
+
this.thickness = thickness;
|
|
879
864
|
this.detail = detail;
|
|
880
865
|
this.interpolateStart = 0;
|
|
881
866
|
this.interpolateLimit = 1;
|
|
@@ -974,7 +959,6 @@ export class Spline2d extends SimulationElement2d {
|
|
|
974
959
|
this.vertexCache.updated();
|
|
975
960
|
}
|
|
976
961
|
setThickness(thickness, t = 0, f) {
|
|
977
|
-
thickness *= devicePixelRatio;
|
|
978
962
|
const diff = thickness - this.thickness;
|
|
979
963
|
return internalTransitionValues((p) => {
|
|
980
964
|
this.thickness += diff * p;
|
|
@@ -1068,7 +1052,6 @@ export class Instance extends SimulationElement3d {
|
|
|
1068
1052
|
const device = globalInfo.getDevice();
|
|
1069
1053
|
if (!device)
|
|
1070
1054
|
return;
|
|
1071
|
-
// this.allocBuffer(size);
|
|
1072
1055
|
const gpuBuffer = this.matrixBuffer.getBuffer();
|
|
1073
1056
|
const buf = new Float32Array(transformation);
|
|
1074
1057
|
device.queue.writeBuffer(gpuBuffer, instance * mat4ByteLength, buf.buffer, buf.byteOffset, buf.byteLength);
|
package/dist/internalUtils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="@webgpu/types" />
|
|
2
|
-
import { Mat4,
|
|
2
|
+
import { Mat4, Vector3 } from './types.js';
|
|
3
3
|
import { Shader } from './shaders.js';
|
|
4
4
|
import { SimulationElement3d } from './graphics.js';
|
|
5
5
|
export declare class Float32ArrayCache {
|
|
@@ -29,8 +29,6 @@ export declare const buildMultisampleTexture: (device: GPUDevice, ctx: GPUCanvas
|
|
|
29
29
|
export declare function lossyTriangulate<T>(vertices: T[]): (readonly [T, T, T])[];
|
|
30
30
|
export declare function lossyTriangulateStrip<T>(vertices: T[]): T[];
|
|
31
31
|
export declare function createIndexArray(length: number): number[];
|
|
32
|
-
export declare function vector3ToPixelRatio(vec: Vector3): void;
|
|
33
|
-
export declare function vector2ToPixelRatio(vec: Vector2): void;
|
|
34
32
|
export declare function triangulateWireFrameOrder(len: number): number[];
|
|
35
33
|
export declare function getVertexAndIndexSize(scene: SimulationElement3d[]): readonly [number, number];
|
|
36
34
|
export declare function internalTransitionValues(onFrame: (deltaT: number, t: number, total: number) => void, adjustment: () => void, transitionLength: number, func?: (n: number) => number): Promise<void>;
|
package/dist/internalUtils.js
CHANGED
|
@@ -141,15 +141,6 @@ export function createIndexArray(length) {
|
|
|
141
141
|
.fill(0)
|
|
142
142
|
.map((_, index) => index);
|
|
143
143
|
}
|
|
144
|
-
export function vector3ToPixelRatio(vec) {
|
|
145
|
-
vec[0] *= devicePixelRatio;
|
|
146
|
-
vec[1] *= devicePixelRatio;
|
|
147
|
-
vec[2] *= devicePixelRatio;
|
|
148
|
-
}
|
|
149
|
-
export function vector2ToPixelRatio(vec) {
|
|
150
|
-
vec[0] *= devicePixelRatio;
|
|
151
|
-
vec[1] *= devicePixelRatio;
|
|
152
|
-
}
|
|
153
144
|
export function triangulateWireFrameOrder(len) {
|
|
154
145
|
const order = Array(len)
|
|
155
146
|
.fill(0)
|
package/dist/simulation.js
CHANGED
|
@@ -199,10 +199,10 @@ export class Simulation extends Settings {
|
|
|
199
199
|
this.resizeEvents.push(cb);
|
|
200
200
|
}
|
|
201
201
|
getWidth() {
|
|
202
|
-
return
|
|
202
|
+
return this.canvasRef?.width || 0;
|
|
203
203
|
}
|
|
204
204
|
getHeight() {
|
|
205
|
-
return
|
|
205
|
+
return this.canvasRef?.height || 0;
|
|
206
206
|
}
|
|
207
207
|
add(el, id) {
|
|
208
208
|
addToScene(this.scene, el, id);
|
package/dist/utils.js
CHANGED
|
@@ -233,16 +233,11 @@ export function colorf(val, a) {
|
|
|
233
233
|
return color(val, val, val, a);
|
|
234
234
|
}
|
|
235
235
|
export function splinePoint2d(end, control1, control2, detail) {
|
|
236
|
-
vec2.scale(control1, devicePixelRatio, control1);
|
|
237
|
-
vec2.scale(control2, devicePixelRatio, control2);
|
|
238
|
-
vec2.scale(end.getPos(), devicePixelRatio, end.getPos());
|
|
239
236
|
const rawControls = [cloneBuf(control1), cloneBuf(control2)];
|
|
240
237
|
vec2.add(end.getPos(), control2, control2);
|
|
241
238
|
return new SplinePoint2d(null, end, control1, control2, rawControls, detail);
|
|
242
239
|
}
|
|
243
240
|
export function continuousSplinePoint2d(end, control, detail) {
|
|
244
|
-
vec2.scale(control, devicePixelRatio, control);
|
|
245
|
-
vec2.scale(end.getPos(), devicePixelRatio, end.getPos());
|
|
246
241
|
const rawControls = [vector2(), cloneBuf(control)];
|
|
247
242
|
vec2.add(end.getPos(), control, control);
|
|
248
243
|
return new SplinePoint2d(null, end, null, control, rawControls, detail);
|