simulationjsv2 0.2.9 → 0.3.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.d.ts +7 -5
- package/dist/geometry.js +15 -7
- package/dist/graphics.d.ts +5 -4
- package/dist/graphics.js +25 -12
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/types.d.ts +2 -1
- package/dist/utils.js +1 -1
- package/package.json +1 -1
package/dist/geometry.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { CircleGeometryParams, CubeGeometryParams, Line2dGeometryParams, Line3dGeometryParams, Mat4, PolygonGeometryParams,
|
|
1
|
+
import { CircleGeometryParams, CubeGeometryParams, Line2dGeometryParams, Line3dGeometryParams, Mat4, PolygonGeometryParams, Spline2dGeometryParams, SquareGeometryParams, Vector2, Vector3, VertexColorMap } from './types.js';
|
|
2
2
|
import { Color, Vertex } from './utils.js';
|
|
3
|
-
import { SplinePoint2d } from './graphics.js';
|
|
3
|
+
import { CubicBezierCurve2d, SplinePoint2d } from './graphics.js';
|
|
4
4
|
export declare abstract class Geometry {
|
|
5
5
|
protected abstract wireframeOrder: number[];
|
|
6
6
|
protected abstract triangleOrder: number[];
|
|
@@ -43,7 +43,7 @@ export declare class SquareGeometry extends Geometry {
|
|
|
43
43
|
protected wireframeOrder: number[];
|
|
44
44
|
protected triangleOrder: number[];
|
|
45
45
|
protected params: SquareGeometryParams;
|
|
46
|
-
constructor(width: number, height: number);
|
|
46
|
+
constructor(width: number, height: number, centerOffset?: Vector2);
|
|
47
47
|
setVertexColorMap(colorMap: VertexColorMap): void;
|
|
48
48
|
setWidth(width: number): void;
|
|
49
49
|
setHeight(height: number): void;
|
|
@@ -67,16 +67,18 @@ export declare class CircleGeometry extends Geometry {
|
|
|
67
67
|
private updateTriangleOrder;
|
|
68
68
|
recompute(): void;
|
|
69
69
|
}
|
|
70
|
-
export declare class
|
|
70
|
+
export declare class Spline2dGeometry extends Geometry {
|
|
71
71
|
protected wireframeOrder: number[];
|
|
72
72
|
protected triangleOrder: number[];
|
|
73
|
-
protected params:
|
|
73
|
+
protected params: Spline2dGeometryParams;
|
|
74
74
|
constructor(points: SplinePoint2d[], color: Color, thickness: number, detail: number);
|
|
75
75
|
updateInterpolationStart(start: number): void;
|
|
76
76
|
updateInterpolationLimit(limit: number): void;
|
|
77
|
+
updateThickness(thickness: number): void;
|
|
77
78
|
private getVertexCount;
|
|
78
79
|
getWireframeVertexCount(): number;
|
|
79
80
|
getTriangleVertexCount(): number;
|
|
81
|
+
getCurves(): CubicBezierCurve2d[];
|
|
80
82
|
private computeCurves;
|
|
81
83
|
private updateWireframeOrder;
|
|
82
84
|
recompute(): void;
|
package/dist/geometry.js
CHANGED
|
@@ -126,12 +126,13 @@ export class SquareGeometry extends Geometry {
|
|
|
126
126
|
wireframeOrder = [0, 1, 2, 3, 0, 2];
|
|
127
127
|
triangleOrder = [0, 1, 3, 2];
|
|
128
128
|
params;
|
|
129
|
-
constructor(width, height) {
|
|
129
|
+
constructor(width, height, centerOffset) {
|
|
130
130
|
super([], 'strip');
|
|
131
131
|
this.params = {
|
|
132
132
|
width,
|
|
133
133
|
height,
|
|
134
|
-
colorMap: {}
|
|
134
|
+
colorMap: {},
|
|
135
|
+
centerOffset: centerOffset || vector2(0, 0)
|
|
135
136
|
};
|
|
136
137
|
this.recompute();
|
|
137
138
|
}
|
|
@@ -145,11 +146,12 @@ export class SquareGeometry extends Geometry {
|
|
|
145
146
|
this.params.height = height;
|
|
146
147
|
}
|
|
147
148
|
recompute() {
|
|
149
|
+
const centerOffset = this.params.centerOffset;
|
|
148
150
|
this.vertices = [
|
|
149
|
-
vector3(-this.params.width
|
|
150
|
-
vector3(this.params.width
|
|
151
|
-
vector3(this.params.width
|
|
152
|
-
vector3(-this.params.width
|
|
151
|
+
vector3(-this.params.width * centerOffset[0], this.params.height * centerOffset[1]),
|
|
152
|
+
vector3(this.params.width * (1 - centerOffset[0]), this.params.height * centerOffset[1]),
|
|
153
|
+
vector3(this.params.width * (1 - centerOffset[0]), -this.params.height * (1 - centerOffset[1])),
|
|
154
|
+
vector3(-this.params.width * centerOffset[0], -this.params.height * (1 - centerOffset[1]))
|
|
153
155
|
];
|
|
154
156
|
}
|
|
155
157
|
getTriangleBuffer(color) {
|
|
@@ -208,7 +210,7 @@ export class CircleGeometry extends Geometry {
|
|
|
208
210
|
this.updateWireframeOrder();
|
|
209
211
|
}
|
|
210
212
|
}
|
|
211
|
-
export class
|
|
213
|
+
export class Spline2dGeometry extends Geometry {
|
|
212
214
|
wireframeOrder;
|
|
213
215
|
triangleOrder;
|
|
214
216
|
params;
|
|
@@ -236,6 +238,9 @@ export class SplineGeometry extends Geometry {
|
|
|
236
238
|
updateInterpolationLimit(limit) {
|
|
237
239
|
this.params.interpolateLimit = Math.min(1, Math.max(0, limit));
|
|
238
240
|
}
|
|
241
|
+
updateThickness(thickness) {
|
|
242
|
+
this.params.thickness = thickness;
|
|
243
|
+
}
|
|
239
244
|
getVertexCount() {
|
|
240
245
|
return this.triangleOrder.length * BUF_LEN;
|
|
241
246
|
}
|
|
@@ -245,6 +250,9 @@ export class SplineGeometry extends Geometry {
|
|
|
245
250
|
getTriangleVertexCount() {
|
|
246
251
|
return this.getVertexCount();
|
|
247
252
|
}
|
|
253
|
+
getCurves() {
|
|
254
|
+
return this.params.curves;
|
|
255
|
+
}
|
|
248
256
|
computeCurves() {
|
|
249
257
|
for (let i = 0; i < this.params.points.length; i++) {
|
|
250
258
|
let prevControl = null;
|
package/dist/graphics.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { Camera } from './simulation.js';
|
|
3
3
|
import type { Vector2, Vector3, LerpFunc, VertexColorMap, ElementRotation, Mat4 } from './types.js';
|
|
4
4
|
import { Vertex, VertexCache, Color } from './utils.js';
|
|
5
|
-
import { BlankGeometry, CircleGeometry, CubeGeometry, Geometry, Line2dGeometry, Line3dGeometry, PlaneGeometry, PolygonGeometry,
|
|
5
|
+
import { BlankGeometry, CircleGeometry, CubeGeometry, Geometry, Line2dGeometry, Line3dGeometry, PlaneGeometry, PolygonGeometry, Spline2dGeometry, SquareGeometry } from './geometry.js';
|
|
6
6
|
export declare abstract class SimulationElement<T extends Vector2 | Vector3 = Vector3> {
|
|
7
7
|
protected abstract pos: T;
|
|
8
8
|
protected abstract geometry: Geometry;
|
|
@@ -63,9 +63,10 @@ export declare class Square extends SimulationElement2d {
|
|
|
63
63
|
private height;
|
|
64
64
|
private vertexColors;
|
|
65
65
|
/**
|
|
66
|
+
* @param centerOffset{Vector2} - A vector2 of values from 0 to 1
|
|
66
67
|
* @param vertexColors{Record<number, Color>} - 0 is top left vertex, numbers increase clockwise
|
|
67
68
|
*/
|
|
68
|
-
constructor(pos: Vector2, width: number, height: number, color?: Color, rotation?: number, vertexColors?: VertexColorMap);
|
|
69
|
+
constructor(pos: Vector2, width: number, height: number, color?: Color, rotation?: number, centerOffset?: Vector2, vertexColors?: VertexColorMap);
|
|
69
70
|
private cloneColorMap;
|
|
70
71
|
setVertexColors(newColorMap: VertexColorMap, t?: number, f?: LerpFunc): Promise<void>;
|
|
71
72
|
scaleWidth(amount: number, t?: number, f?: LerpFunc): Promise<void>;
|
|
@@ -154,8 +155,7 @@ export declare class SplinePoint2d {
|
|
|
154
155
|
getVectorArray(prevEnd: Vector2 | null, prevControl: Vector2 | null): readonly [Vector2, Vector2, Vector2, Vector2];
|
|
155
156
|
}
|
|
156
157
|
export declare class Spline2d extends SimulationElement2d {
|
|
157
|
-
protected geometry:
|
|
158
|
-
private curves;
|
|
158
|
+
protected geometry: Spline2dGeometry;
|
|
159
159
|
private thickness;
|
|
160
160
|
private detail;
|
|
161
161
|
private interpolateStart;
|
|
@@ -163,6 +163,7 @@ export declare class Spline2d extends SimulationElement2d {
|
|
|
163
163
|
constructor(pos: Vertex, points: SplinePoint2d[], thickness?: number, detail?: number);
|
|
164
164
|
setInterpolateStart(start: number, t?: number, f?: LerpFunc): Promise<void>;
|
|
165
165
|
setInterpolateLimit(limit: number, t?: number, f?: LerpFunc): Promise<void>;
|
|
166
|
+
setThickness(thickness: number, t?: number, f?: LerpFunc): Promise<void>;
|
|
166
167
|
interpolateSlope(t: number): readonly [Vector2, Vector2];
|
|
167
168
|
interpolate(t: number): Vector2;
|
|
168
169
|
protected updateMatrix(camera: Camera): void;
|
package/dist/graphics.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { vec3, mat4, vec2, vec4 } from 'wgpu-matrix';
|
|
2
2
|
import { Vertex, VertexCache, cloneBuf, color, colorFromVector4, vector3ToPixelRatio, vector2, vector3, vertex, Color, transitionValues, logger, vector2FromVector3, matrix4, rotateMat4, vector3FromVector2, vector2ToPixelRatio, bufferGenerator } from './utils.js';
|
|
3
|
-
import { BlankGeometry, CircleGeometry, CubeGeometry, Line2dGeometry, Line3dGeometry, PlaneGeometry, PolygonGeometry,
|
|
3
|
+
import { BlankGeometry, CircleGeometry, CubeGeometry, Line2dGeometry, Line3dGeometry, PlaneGeometry, PolygonGeometry, Spline2dGeometry, SquareGeometry } from './geometry.js';
|
|
4
4
|
export class SimulationElement {
|
|
5
5
|
color;
|
|
6
6
|
wireframe;
|
|
@@ -232,15 +232,16 @@ export class Square extends SimulationElement2d {
|
|
|
232
232
|
height;
|
|
233
233
|
vertexColors;
|
|
234
234
|
/**
|
|
235
|
+
* @param centerOffset{Vector2} - A vector2 of values from 0 to 1
|
|
235
236
|
* @param vertexColors{Record<number, Color>} - 0 is top left vertex, numbers increase clockwise
|
|
236
237
|
*/
|
|
237
|
-
constructor(pos, width, height, color, rotation, vertexColors) {
|
|
238
|
+
constructor(pos, width, height, color, rotation, centerOffset, vertexColors) {
|
|
238
239
|
super(pos, rotation, color);
|
|
239
240
|
vector2ToPixelRatio(this.pos);
|
|
240
241
|
this.width = width * devicePixelRatio;
|
|
241
242
|
this.height = height * devicePixelRatio;
|
|
242
243
|
this.vertexColors = this.cloneColorMap(vertexColors || {});
|
|
243
|
-
this.geometry = new SquareGeometry(this.width, this.height);
|
|
244
|
+
this.geometry = new SquareGeometry(this.width, this.height, centerOffset);
|
|
244
245
|
this.geometry.setVertexColorMap(this.vertexColors);
|
|
245
246
|
}
|
|
246
247
|
cloneColorMap(colorMap) {
|
|
@@ -359,8 +360,6 @@ export class Square extends SimulationElement2d {
|
|
|
359
360
|
updateMatrix(camera) {
|
|
360
361
|
const pos = cloneBuf(this.pos);
|
|
361
362
|
pos[1] = camera.getScreenSize()[1] - pos[1];
|
|
362
|
-
pos[0] += this.width / 2;
|
|
363
|
-
pos[1] -= this.height / 2;
|
|
364
363
|
const matrix = matrix4();
|
|
365
364
|
mat4.translate(matrix, vector3FromVector2(pos), matrix);
|
|
366
365
|
mat4.rotateZ(matrix, this.rotation, matrix);
|
|
@@ -763,19 +762,19 @@ export class SplinePoint2d {
|
|
|
763
762
|
}
|
|
764
763
|
export class Spline2d extends SimulationElement2d {
|
|
765
764
|
geometry;
|
|
766
|
-
curves;
|
|
767
765
|
thickness;
|
|
768
766
|
detail;
|
|
769
767
|
interpolateStart;
|
|
770
768
|
interpolateLimit;
|
|
771
769
|
constructor(pos, points, thickness = devicePixelRatio, detail = 40) {
|
|
772
|
-
|
|
773
|
-
|
|
770
|
+
const tempPos = vector2FromVector3(pos.getPos());
|
|
771
|
+
vector2ToPixelRatio(tempPos);
|
|
772
|
+
super(tempPos, 0, pos.getColor() || undefined);
|
|
774
773
|
this.thickness = thickness * devicePixelRatio;
|
|
775
774
|
this.detail = detail;
|
|
776
775
|
this.interpolateStart = 0;
|
|
777
776
|
this.interpolateLimit = 1;
|
|
778
|
-
this.geometry = new
|
|
777
|
+
this.geometry = new Spline2dGeometry(points, this.getColor(), this.thickness, this.detail);
|
|
779
778
|
}
|
|
780
779
|
setInterpolateStart(start, t = 0, f) {
|
|
781
780
|
const diff = start - this.interpolateStart;
|
|
@@ -801,13 +800,27 @@ export class Spline2d extends SimulationElement2d {
|
|
|
801
800
|
this.vertexCache.updated();
|
|
802
801
|
}, t, f);
|
|
803
802
|
}
|
|
803
|
+
setThickness(thickness, t = 0, f) {
|
|
804
|
+
thickness *= devicePixelRatio;
|
|
805
|
+
const diff = thickness - this.thickness;
|
|
806
|
+
return transitionValues((p) => {
|
|
807
|
+
this.thickness += diff * p;
|
|
808
|
+
this.geometry.updateThickness(this.thickness);
|
|
809
|
+
this.vertexCache.updated();
|
|
810
|
+
}, () => {
|
|
811
|
+
this.thickness = thickness;
|
|
812
|
+
this.geometry.updateThickness(this.thickness);
|
|
813
|
+
this.vertexCache.updated();
|
|
814
|
+
}, t, f);
|
|
815
|
+
}
|
|
804
816
|
interpolateSlope(t) {
|
|
805
|
-
const
|
|
817
|
+
const curves = this.geometry.getCurves();
|
|
818
|
+
const curveInterval = 1 / curves.length;
|
|
806
819
|
let index = Math.floor(t / curveInterval);
|
|
807
|
-
if (index ===
|
|
820
|
+
if (index === curves.length)
|
|
808
821
|
index--;
|
|
809
822
|
const diff = (t - curveInterval * index) * 2;
|
|
810
|
-
return
|
|
823
|
+
return curves[index].interpolateSlope(diff);
|
|
811
824
|
}
|
|
812
825
|
interpolate(t) {
|
|
813
826
|
const [vec] = this.interpolateSlope(t);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export * from './simulation.js';
|
|
2
2
|
export * from './graphics.js';
|
|
3
3
|
export * from './types.js';
|
|
4
|
+
export { vec2, vec3, vec4, mat3, mat4 } from 'wgpu-matrix';
|
|
4
5
|
export { Vertex, Color, cloneBuf, vector4, vector3, vector2, vector3FromVector2, colorFromVector4, randomInt, randomColor, vertex, color, colorf, transitionValues, lerp, smoothStep, linearStep, splinePoint2d, continuousSplinePoint2d, easeInOutQuad, easeInOutExpo, easeInOutQuart, waitFor, matrix4 } from './utils.js';
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export * from './simulation.js';
|
|
2
2
|
export * from './graphics.js';
|
|
3
3
|
export * from './types.js';
|
|
4
|
+
export { vec2, vec3, vec4, mat3, mat4 } from 'wgpu-matrix';
|
|
4
5
|
export { Vertex, Color, cloneBuf, vector4, vector3, vector2, vector3FromVector2, colorFromVector4, randomInt, randomColor, vertex, color, colorf, transitionValues, lerp, smoothStep, linearStep, splinePoint2d, continuousSplinePoint2d, easeInOutQuad, easeInOutExpo, easeInOutQuart, waitFor, matrix4 } from './utils.js';
|
package/dist/types.d.ts
CHANGED
|
@@ -35,12 +35,13 @@ export type SquareGeometryParams = {
|
|
|
35
35
|
width: number;
|
|
36
36
|
height: number;
|
|
37
37
|
colorMap: VertexColorMap;
|
|
38
|
+
centerOffset: Vector2;
|
|
38
39
|
};
|
|
39
40
|
export type CircleGeometryParams = {
|
|
40
41
|
radius: number;
|
|
41
42
|
detail: number;
|
|
42
43
|
};
|
|
43
|
-
export type
|
|
44
|
+
export type Spline2dGeometryParams = {
|
|
44
45
|
points: SplinePoint2d[];
|
|
45
46
|
curves: CubicBezierCurve2d[];
|
|
46
47
|
distance: number;
|
package/dist/utils.js
CHANGED
|
@@ -143,7 +143,7 @@ export const buildMultisampleTexture = (device, ctx, width, height) => {
|
|
|
143
143
|
};
|
|
144
144
|
export const applyElementToScene = (scene, el) => {
|
|
145
145
|
if (el instanceof SimulationElement) {
|
|
146
|
-
scene.
|
|
146
|
+
scene.unshift(el);
|
|
147
147
|
}
|
|
148
148
|
else {
|
|
149
149
|
throw logger.error('Cannot add invalid SimulationElement');
|