simulationjsv2 0.8.3 → 0.8.4
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 +1 -0
- package/dist/geometry.js +4 -0
- package/dist/graphics.d.ts +13 -6
- package/dist/graphics.js +45 -16
- package/dist/internalUtils.d.ts +5 -22
- package/dist/internalUtils.js +29 -47
- package/dist/shaders.js +8 -7
- package/dist/simulation.d.ts +3 -9
- package/dist/simulation.js +8 -48
- package/dist/types.d.ts +5 -4
- package/dist/utils.d.ts +3 -6
- package/dist/utils.js +0 -7
- package/package.json +1 -1
package/dist/geometry.d.ts
CHANGED
package/dist/geometry.js
CHANGED
package/dist/graphics.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/// <reference types="@webgpu/types" />
|
|
2
|
-
import type { Vector2, Vector3, LerpFunc, Mat4
|
|
2
|
+
import type { Vector2, Vector3, LerpFunc, Mat4 } from './types.js';
|
|
3
3
|
import { Vertex, Color } from './utils.js';
|
|
4
4
|
import { BlankGeometry, CircleGeometry, CubeGeometry, Geometry, Line2dGeometry, Line3dGeometry, PlaneGeometry, PolygonGeometry, Spline2dGeometry, SquareGeometry, TraceLines2dGeometry as TraceLinesGeometry } from './geometry.js';
|
|
5
|
-
import {
|
|
5
|
+
import { Float32ArrayCache } from './internalUtils.js';
|
|
6
6
|
import { Shader } from './shaders.js';
|
|
7
7
|
import { Material } from './materials.js';
|
|
8
8
|
export declare abstract class SimulationElement3d {
|
|
@@ -10,6 +10,7 @@ export declare abstract class SimulationElement3d {
|
|
|
10
10
|
private uniformBuffer;
|
|
11
11
|
private prevInfo;
|
|
12
12
|
private pipeline;
|
|
13
|
+
protected id: string | null;
|
|
13
14
|
protected shader: Shader;
|
|
14
15
|
protected material: Material;
|
|
15
16
|
protected cullMode: GPUCullMode;
|
|
@@ -29,10 +30,15 @@ export declare abstract class SimulationElement3d {
|
|
|
29
30
|
* @param pos - Expected to be adjusted to devicePixelRatio before reaching constructor
|
|
30
31
|
*/
|
|
31
32
|
constructor(pos: Vector3, rotation: Vector3, color?: Color);
|
|
33
|
+
getId(): string | null;
|
|
34
|
+
setId(id: string): void;
|
|
35
|
+
getColor(): Color;
|
|
32
36
|
add(el: SimulationElement3d, id?: string): void;
|
|
33
37
|
remove(el: SimulationElement3d): void;
|
|
38
|
+
removeId(id: string): void;
|
|
34
39
|
getChildren(): SimulationElement3d[];
|
|
35
|
-
|
|
40
|
+
setChildren(elements: SimulationElement3d[]): void;
|
|
41
|
+
getChildrenInfos(): SimulationElement3d[];
|
|
36
42
|
hasChildren(): boolean;
|
|
37
43
|
setParent(parent: SimulationElement3d): void;
|
|
38
44
|
getParent(): SimulationElement3d | null;
|
|
@@ -205,7 +211,7 @@ export declare class Spline2d extends SimulationElement2d {
|
|
|
205
211
|
interpolateSlope(t: number): Vector2[] | readonly [Vector2, Vector2];
|
|
206
212
|
interpolate(t: number): Vector2;
|
|
207
213
|
}
|
|
208
|
-
export declare class Instance<T extends
|
|
214
|
+
export declare class Instance<T extends SimulationElement3d> extends SimulationElement3d {
|
|
209
215
|
protected geometry: BlankGeometry;
|
|
210
216
|
private obj;
|
|
211
217
|
private instanceMatrix;
|
|
@@ -231,12 +237,13 @@ export declare class Instance<T extends AnySimulationElement> extends Simulation
|
|
|
231
237
|
export declare class TraceLines2d extends SimulationElement2d {
|
|
232
238
|
protected geometry: TraceLinesGeometry;
|
|
233
239
|
constructor(color?: Color, maxLen?: number);
|
|
234
|
-
addPoint(vert: Vector2 | Vector3,
|
|
240
|
+
addPoint(vert: Vector2 | Vector3, color?: Color): void;
|
|
241
|
+
clear(): void;
|
|
235
242
|
isWireframe(): boolean;
|
|
236
243
|
}
|
|
237
244
|
export declare class TraceLines3d extends SimulationElement3d {
|
|
238
245
|
protected geometry: TraceLinesGeometry;
|
|
239
246
|
constructor(color?: Color, maxLen?: number);
|
|
240
|
-
addPoint(vert: Vector2 | Vector3,
|
|
247
|
+
addPoint(vert: Vector2 | Vector3, color?: Color): void;
|
|
241
248
|
isWireframe(): boolean;
|
|
242
249
|
}
|
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 {
|
|
4
|
+
import { Float32ArrayCache, internalTransitionValues, posTo2dScreen, vector3ToPixelRatio } 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';
|
|
@@ -12,6 +12,7 @@ export class SimulationElement3d {
|
|
|
12
12
|
uniformBuffer;
|
|
13
13
|
prevInfo;
|
|
14
14
|
pipeline;
|
|
15
|
+
id;
|
|
15
16
|
shader;
|
|
16
17
|
material;
|
|
17
18
|
cullMode;
|
|
@@ -44,21 +45,44 @@ export class SimulationElement3d {
|
|
|
44
45
|
this.shader = defaultShader;
|
|
45
46
|
this.material = new BasicMaterial(color);
|
|
46
47
|
this.cullMode = 'back';
|
|
48
|
+
this.id = null;
|
|
49
|
+
}
|
|
50
|
+
getId() {
|
|
51
|
+
return this.id;
|
|
52
|
+
}
|
|
53
|
+
setId(id) {
|
|
54
|
+
this.id = id;
|
|
55
|
+
}
|
|
56
|
+
getColor() {
|
|
57
|
+
return this.material.getColor();
|
|
47
58
|
}
|
|
48
59
|
add(el, id) {
|
|
49
60
|
el.setParent(this);
|
|
50
|
-
|
|
51
|
-
|
|
61
|
+
if (id)
|
|
62
|
+
el.setId(id);
|
|
63
|
+
this.children.push(el);
|
|
52
64
|
}
|
|
53
65
|
remove(el) {
|
|
54
66
|
for (let i = 0; i < this.children.length; i++) {
|
|
55
|
-
if (this.children[i]
|
|
67
|
+
if (this.children[i] === el) {
|
|
56
68
|
this.children.splice(i, 1);
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
removeId(id) {
|
|
74
|
+
for (let i = 0; i < this.children.length; i++) {
|
|
75
|
+
if (this.children[i].getId() === id) {
|
|
76
|
+
this.children.splice(i, 1);
|
|
77
|
+
break;
|
|
57
78
|
}
|
|
58
79
|
}
|
|
59
80
|
}
|
|
60
81
|
getChildren() {
|
|
61
|
-
return this.children
|
|
82
|
+
return this.children;
|
|
83
|
+
}
|
|
84
|
+
setChildren(elements) {
|
|
85
|
+
this.children = elements;
|
|
62
86
|
}
|
|
63
87
|
getChildrenInfos() {
|
|
64
88
|
return this.children;
|
|
@@ -241,7 +265,7 @@ export class SimulationElement3d {
|
|
|
241
265
|
}
|
|
242
266
|
rotateChildren(angle) {
|
|
243
267
|
for (let i = 0; i < this.children.length; i++) {
|
|
244
|
-
this.children[i].
|
|
268
|
+
this.children[i].rotate(angle);
|
|
245
269
|
}
|
|
246
270
|
}
|
|
247
271
|
rotate(amount, t = 0, f) {
|
|
@@ -275,14 +299,14 @@ export class SimulationElement3d {
|
|
|
275
299
|
}
|
|
276
300
|
let vertexCount = this.geometry.getIndexes(this.isWireframe()).length;
|
|
277
301
|
for (let i = 0; i < this.children.length; i++) {
|
|
278
|
-
vertexCount += this.children[i].
|
|
302
|
+
vertexCount += this.children[i].getVertexCount();
|
|
279
303
|
}
|
|
280
304
|
return vertexCount;
|
|
281
305
|
}
|
|
282
306
|
getIndexCount() {
|
|
283
307
|
let indexCount = this.geometry.getIndexes(this.isWireframe()).length;
|
|
284
308
|
for (let i = 0; i < this.children.length; i++) {
|
|
285
|
-
indexCount += this.children[i].
|
|
309
|
+
indexCount += this.children[i].getIndexCount();
|
|
286
310
|
}
|
|
287
311
|
return indexCount;
|
|
288
312
|
}
|
|
@@ -295,9 +319,9 @@ export class SimulationElement3d {
|
|
|
295
319
|
const vertices = this.geometry.getVertices();
|
|
296
320
|
const stride = this.shader.getBufferLength();
|
|
297
321
|
const vertexBuffer = new Float32Array(vertices.length * stride);
|
|
298
|
-
const shader = this.isWireframe() ? defaultShader : this.shader;
|
|
322
|
+
// const shader = this.isWireframe() ? defaultShader : this.shader;
|
|
299
323
|
for (let i = 0; i < vertices.length; i++) {
|
|
300
|
-
shader.setVertexInfo(this, vertexBuffer, vertices[i], i, i * stride);
|
|
324
|
+
this.shader.setVertexInfo(this, vertexBuffer, vertices[i], i, i * stride);
|
|
301
325
|
}
|
|
302
326
|
this.vertexCache.setCache(vertexBuffer);
|
|
303
327
|
return vertexBuffer;
|
|
@@ -1099,14 +1123,20 @@ export class TraceLines2d extends SimulationElement2d {
|
|
|
1099
1123
|
constructor(color, maxLen) {
|
|
1100
1124
|
super(vector2(), vector3(), color);
|
|
1101
1125
|
this.geometry = new TraceLinesGeometry(maxLen);
|
|
1126
|
+
this.material = new VertexColorMaterial();
|
|
1127
|
+
if (color)
|
|
1128
|
+
this.material.setColor(color);
|
|
1129
|
+
this.shader = vertexColorShader;
|
|
1102
1130
|
}
|
|
1103
|
-
|
|
1104
|
-
addPoint(vert, _color) {
|
|
1131
|
+
addPoint(vert, color) {
|
|
1105
1132
|
const newVert = vert.length < 3 ? vector3(vert[0] ?? 0, vert[1] ?? 0, 0) : vert;
|
|
1106
|
-
// const vert = vertex(point[0], point[1], point?.[2] || 0, color);
|
|
1107
1133
|
this.geometry.addVertex(newVert);
|
|
1134
|
+
this.material.addVertexColor(color ?? this.material.getColor());
|
|
1108
1135
|
this.vertexCache.updated();
|
|
1109
1136
|
}
|
|
1137
|
+
clear() {
|
|
1138
|
+
this.geometry.clear();
|
|
1139
|
+
}
|
|
1110
1140
|
// always being wireframe means that triangleOrder
|
|
1111
1141
|
// in in the geometry does not need to be a duplicate
|
|
1112
1142
|
// of wireframeOrder
|
|
@@ -1120,11 +1150,10 @@ export class TraceLines3d extends SimulationElement3d {
|
|
|
1120
1150
|
super(vector3(), vector3(), color);
|
|
1121
1151
|
this.geometry = new TraceLinesGeometry(maxLen);
|
|
1122
1152
|
}
|
|
1123
|
-
|
|
1124
|
-
addPoint(vert, _color) {
|
|
1125
|
-
// const vert = vertex(point[0], point[1], point?.[2] || 0, color);
|
|
1153
|
+
addPoint(vert, color) {
|
|
1126
1154
|
const newVert = vert.length < 3 ? vector3(vert[0] ?? 0, vert[1] ?? 0, 0) : vert;
|
|
1127
1155
|
this.geometry.addVertex(newVert);
|
|
1156
|
+
this.material.addVertexColor(color ?? this.material.getColor());
|
|
1128
1157
|
this.vertexCache.updated();
|
|
1129
1158
|
}
|
|
1130
1159
|
// always being wireframe means that triangleOrder
|
package/dist/internalUtils.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="@webgpu/types" />
|
|
2
2
|
import { Mat4, Vector2, Vector3 } from './types.js';
|
|
3
|
-
import { SimulationElement3d } from './graphics.js';
|
|
4
3
|
import { Shader } from './shaders.js';
|
|
4
|
+
import { SimulationElement3d } from './graphics.js';
|
|
5
5
|
export declare class Float32ArrayCache {
|
|
6
6
|
private vertices;
|
|
7
7
|
private hasUpdated;
|
|
@@ -26,33 +26,16 @@ export declare const updateWorldProjectionMatrix: (worldProjMat: Mat4, projMat:
|
|
|
26
26
|
export declare const updateOrthoProjectionMatrix: (mat: Mat4, screenSize: [number, number]) => Float32Array;
|
|
27
27
|
export declare const buildDepthTexture: (device: GPUDevice, width: number, height: number) => GPUTexture;
|
|
28
28
|
export declare const buildMultisampleTexture: (device: GPUDevice, ctx: GPUCanvasContext, width: number, height: number) => GPUTexture;
|
|
29
|
-
export declare const removeObjectId: (scene: SimSceneObjInfo[], id: string) => void;
|
|
30
|
-
export declare class SimSceneObjInfo {
|
|
31
|
-
private obj;
|
|
32
|
-
private id;
|
|
33
|
-
private lifetime;
|
|
34
|
-
private currentLife;
|
|
35
|
-
constructor(obj: SimulationElement3d, id?: string);
|
|
36
|
-
/**
|
|
37
|
-
* @param lifetime - ms
|
|
38
|
-
*/
|
|
39
|
-
setLifetime(lifetime: number): void;
|
|
40
|
-
getLifetime(): number | null;
|
|
41
|
-
lifetimeComplete(): boolean;
|
|
42
|
-
/**
|
|
43
|
-
* @param amount - ms
|
|
44
|
-
*/
|
|
45
|
-
traverseLife(amount: number): void;
|
|
46
|
-
getObj(): SimulationElement3d;
|
|
47
|
-
getId(): string | null;
|
|
48
|
-
}
|
|
49
29
|
export declare function lossyTriangulate<T>(vertices: T[]): (readonly [T, T, T])[];
|
|
50
30
|
export declare function lossyTriangulateStrip<T>(vertices: T[]): T[];
|
|
51
31
|
export declare function createIndexArray(length: number): number[];
|
|
52
32
|
export declare function vector3ToPixelRatio(vec: Vector3): void;
|
|
53
33
|
export declare function vector2ToPixelRatio(vec: Vector2): void;
|
|
54
34
|
export declare function triangulateWireFrameOrder(len: number): number[];
|
|
55
|
-
export declare function getVertexAndIndexSize(scene:
|
|
35
|
+
export declare function getVertexAndIndexSize(scene: SimulationElement3d[]): readonly [number, number];
|
|
56
36
|
export declare function internalTransitionValues(onFrame: (deltaT: number, t: number, total: number) => void, adjustment: () => void, transitionLength: number, func?: (n: number) => number): Promise<void>;
|
|
57
37
|
export declare function posTo2dScreen(pos: Vector3): Vector3;
|
|
58
38
|
export declare function createPipeline(device: GPUDevice, info: string, shader: Shader): GPURenderPipeline;
|
|
39
|
+
export declare function addToScene(scene: SimulationElement3d[], el: SimulationElement3d, id?: string): void;
|
|
40
|
+
export declare function removeSceneObj(scene: SimulationElement3d[], el: SimulationElement3d): void;
|
|
41
|
+
export declare function removeSceneId(scene: SimulationElement3d[], id: string): void;
|
package/dist/internalUtils.js
CHANGED
|
@@ -2,6 +2,8 @@ import { mat4, vec3 } from 'wgpu-matrix';
|
|
|
2
2
|
import { cloneBuf, transitionValues } from './utils.js';
|
|
3
3
|
import { camera } from './simulation.js';
|
|
4
4
|
import { settings } from './settings.js';
|
|
5
|
+
import { SimulationElement3d } from './graphics.js';
|
|
6
|
+
import { logger } from './globals.js';
|
|
5
7
|
export class Float32ArrayCache {
|
|
6
8
|
vertices;
|
|
7
9
|
hasUpdated = true;
|
|
@@ -86,52 +88,6 @@ export const buildMultisampleTexture = (device, ctx, width, height) => {
|
|
|
86
88
|
sampleCount: 4
|
|
87
89
|
});
|
|
88
90
|
};
|
|
89
|
-
export const removeObjectId = (scene, id) => {
|
|
90
|
-
for (let i = 0; i < scene.length; i++) {
|
|
91
|
-
if (scene[i].getId() === id) {
|
|
92
|
-
scene.splice(i, 1);
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
export class SimSceneObjInfo {
|
|
98
|
-
obj;
|
|
99
|
-
id;
|
|
100
|
-
lifetime; // ms
|
|
101
|
-
currentLife;
|
|
102
|
-
constructor(obj, id) {
|
|
103
|
-
this.obj = obj;
|
|
104
|
-
this.id = id || null;
|
|
105
|
-
this.lifetime = null;
|
|
106
|
-
this.currentLife = 0;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* @param lifetime - ms
|
|
110
|
-
*/
|
|
111
|
-
setLifetime(lifetime) {
|
|
112
|
-
this.lifetime = lifetime;
|
|
113
|
-
}
|
|
114
|
-
getLifetime() {
|
|
115
|
-
return this.lifetime;
|
|
116
|
-
}
|
|
117
|
-
lifetimeComplete() {
|
|
118
|
-
if (this.lifetime === null)
|
|
119
|
-
return false;
|
|
120
|
-
return this.currentLife >= this.lifetime;
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* @param amount - ms
|
|
124
|
-
*/
|
|
125
|
-
traverseLife(amount) {
|
|
126
|
-
this.currentLife += amount;
|
|
127
|
-
}
|
|
128
|
-
getObj() {
|
|
129
|
-
return this.obj;
|
|
130
|
-
}
|
|
131
|
-
getId() {
|
|
132
|
-
return this.id;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
91
|
// optomized for speed, depending on orientation of vertices as input, shape may not be preserved
|
|
136
92
|
export function lossyTriangulate(vertices) {
|
|
137
93
|
const res = [];
|
|
@@ -211,7 +167,7 @@ export function getVertexAndIndexSize(scene) {
|
|
|
211
167
|
let vertexSize = 0;
|
|
212
168
|
let indexSize = 0;
|
|
213
169
|
for (let i = 0; i < scene.length; i++) {
|
|
214
|
-
const obj = scene[i]
|
|
170
|
+
const obj = scene[i];
|
|
215
171
|
vertexSize += obj.getVertexCount() * obj.getShader().getBufferLength();
|
|
216
172
|
indexSize += obj.getIndexCount();
|
|
217
173
|
}
|
|
@@ -276,3 +232,29 @@ export function createPipeline(device, info, shader) {
|
|
|
276
232
|
}
|
|
277
233
|
});
|
|
278
234
|
}
|
|
235
|
+
export function addToScene(scene, el, id) {
|
|
236
|
+
if (el instanceof SimulationElement3d) {
|
|
237
|
+
if (id)
|
|
238
|
+
el.setId(id);
|
|
239
|
+
scene.unshift(el);
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
throw logger.error('Cannot add invalid SimulationElement');
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
export function removeSceneObj(scene, el) {
|
|
246
|
+
for (let i = 0; i < scene.length; i++) {
|
|
247
|
+
if (scene[i] === el) {
|
|
248
|
+
scene.splice(i, 1);
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
export function removeSceneId(scene, id) {
|
|
254
|
+
for (let i = 0; i < scene.length; i++) {
|
|
255
|
+
if (scene[i].getId() === id) {
|
|
256
|
+
scene.splice(i, 1);
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
package/dist/shaders.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { MemoBuffer } from './buffers.js';
|
|
2
2
|
import { mat4ByteLength } from './constants.js';
|
|
3
3
|
import { globalInfo } from './globals.js';
|
|
4
|
-
import {
|
|
4
|
+
import { createBindGroup, writeUniformWorldMatrix } from './utils.js';
|
|
5
5
|
export const uniformBufferSize = mat4ByteLength * 2 + 4 * 2 + 8; // 4x4 matrix * 2 + vec2<f32> + 8 bc 144 is cool
|
|
6
6
|
const defaultInfos = [
|
|
7
7
|
{
|
|
@@ -216,14 +216,14 @@ fn fragment_main(
|
|
|
216
216
|
}
|
|
217
217
|
], defaultInfos, defaultBufferWriter, defaultBindGroupGenerator, (el, buffer, vertex, _, offset) => {
|
|
218
218
|
const material = el.getMaterial();
|
|
219
|
-
const
|
|
219
|
+
const vertexColor = material.getColor();
|
|
220
220
|
buffer[offset] = vertex[0];
|
|
221
221
|
buffer[offset + 1] = vertex[1];
|
|
222
222
|
buffer[offset + 2] = vertex[2];
|
|
223
|
-
buffer[offset + 3] =
|
|
224
|
-
buffer[offset + 4] =
|
|
225
|
-
buffer[offset + 5] =
|
|
226
|
-
buffer[offset + 6] =
|
|
223
|
+
buffer[offset + 3] = vertexColor.r / 255;
|
|
224
|
+
buffer[offset + 4] = vertexColor.g / 255;
|
|
225
|
+
buffer[offset + 5] = vertexColor.b / 255;
|
|
226
|
+
buffer[offset + 6] = vertexColor.a;
|
|
227
227
|
// TODO possibly change uv for textures
|
|
228
228
|
buffer[offset + 7] = 0;
|
|
229
229
|
buffer[offset + 8] = 0;
|
|
@@ -315,7 +315,8 @@ fn fragment_main(
|
|
|
315
315
|
], defaultInfos, defaultBufferWriter, defaultBindGroupGenerator, (el, buffer, vertex, vertexIndex, offset) => {
|
|
316
316
|
const material = el.getMaterial();
|
|
317
317
|
const colors = material.getVertexColors();
|
|
318
|
-
const vertexColor = colors[vertexIndex] ??
|
|
318
|
+
const vertexColor = colors[vertexIndex] ?? el.getColor();
|
|
319
|
+
// const vertexColor = color(0, 255, 255);
|
|
319
320
|
buffer[offset] = vertex[0];
|
|
320
321
|
buffer[offset + 1] = vertex[1];
|
|
321
322
|
buffer[offset + 2] = vertex[2];
|
package/dist/simulation.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { SimulationElement3d } from './graphics.js';
|
|
2
|
-
import type { Vector2, Vector3, LerpFunc
|
|
2
|
+
import type { Vector2, Vector3, LerpFunc } from './types.js';
|
|
3
3
|
import { Color } from './utils.js';
|
|
4
|
-
import { SimSceneObjInfo } from './internalUtils.js';
|
|
5
4
|
import { Settings } from './settings.js';
|
|
6
5
|
export declare const worldProjectionMatrix: import("./types.js").Mat4;
|
|
7
6
|
export declare const orthogonalMatrix: import("./types.js").Mat4;
|
|
@@ -43,20 +42,15 @@ export declare class Simulation extends Settings {
|
|
|
43
42
|
onResize(cb: (width: number, height: number) => void): void;
|
|
44
43
|
getWidth(): number;
|
|
45
44
|
getHeight(): number;
|
|
46
|
-
add(el:
|
|
45
|
+
add(el: SimulationElement3d, id?: string): void;
|
|
47
46
|
remove(el: SimulationElement3d): void;
|
|
48
47
|
removeId(id: string): void;
|
|
49
|
-
/**
|
|
50
|
-
* @param lifetime - ms
|
|
51
|
-
*/
|
|
52
|
-
setLifetime(el: AnySimulationElement, lifetime: number): void;
|
|
53
48
|
private applyCanvasSize;
|
|
54
49
|
setCanvasSize(width: number, height: number): void;
|
|
55
50
|
start(): void;
|
|
56
51
|
stop(): void;
|
|
57
52
|
setBackground(color: Color): void;
|
|
58
|
-
getScene():
|
|
59
|
-
getSceneObjects(): SimulationElement3d[];
|
|
53
|
+
getScene(): SimulationElement3d[];
|
|
60
54
|
private render;
|
|
61
55
|
private renderScene;
|
|
62
56
|
fitElement(): void;
|
package/dist/simulation.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { vec3 } from 'wgpu-matrix';
|
|
2
|
-
import { SimulationElement3d } from './graphics.js';
|
|
3
2
|
import { Color, matrix4, transitionValues, vector2, vector3 } from './utils.js';
|
|
4
|
-
import {
|
|
3
|
+
import { buildDepthTexture, buildMultisampleTexture, updateProjectionMatrix, getVertexAndIndexSize, updateOrthoProjectionMatrix, updateWorldProjectionMatrix, CachedArray, addToScene, removeSceneObj, removeSceneId } from './internalUtils.js';
|
|
5
4
|
import { Settings } from './settings.js';
|
|
6
5
|
import { MemoBuffer } from './buffers.js';
|
|
7
6
|
import { globalInfo, logger } from './globals.js';
|
|
@@ -181,10 +180,8 @@ export class Simulation extends Settings {
|
|
|
181
180
|
this.frameRateView = new FrameRateView(showFrameRate);
|
|
182
181
|
this.frameRateView.updateFrameRate(1);
|
|
183
182
|
this.transparentElements = new CachedArray();
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
this.vertexBuffer = new MemoBuffer(GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST, 1000);
|
|
187
|
-
this.indexBuffer = new MemoBuffer(GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST, 1000);
|
|
183
|
+
this.vertexBuffer = new MemoBuffer(GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST, 0);
|
|
184
|
+
this.indexBuffer = new MemoBuffer(GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST, 0);
|
|
188
185
|
}
|
|
189
186
|
handleCanvasResize(parent) {
|
|
190
187
|
if (this.fittingElement) {
|
|
@@ -208,33 +205,13 @@ export class Simulation extends Settings {
|
|
|
208
205
|
return (this.canvasRef?.height || 0) / devicePixelRatio;
|
|
209
206
|
}
|
|
210
207
|
add(el, id) {
|
|
211
|
-
|
|
212
|
-
const obj = new SimSceneObjInfo(el, id);
|
|
213
|
-
this.scene.unshift(obj);
|
|
214
|
-
}
|
|
215
|
-
else {
|
|
216
|
-
throw logger.error('Cannot add invalid SimulationElement');
|
|
217
|
-
}
|
|
208
|
+
addToScene(this.scene, el, id);
|
|
218
209
|
}
|
|
219
210
|
remove(el) {
|
|
220
|
-
|
|
221
|
-
if (this.scene[i].getObj() === el) {
|
|
222
|
-
this.scene.splice(i, 1);
|
|
223
|
-
break;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
211
|
+
removeSceneObj(this.scene, el);
|
|
226
212
|
}
|
|
227
213
|
removeId(id) {
|
|
228
|
-
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* @param lifetime - ms
|
|
232
|
-
*/
|
|
233
|
-
setLifetime(el, lifetime) {
|
|
234
|
-
for (let i = 0; i < this.scene.length; i++) {
|
|
235
|
-
if (this.scene[i].getObj() === el)
|
|
236
|
-
this.scene[i].setLifetime(lifetime);
|
|
237
|
-
}
|
|
214
|
+
removeSceneId(this.scene, id);
|
|
238
215
|
}
|
|
239
216
|
applyCanvasSize(width, height) {
|
|
240
217
|
if (this.canvasRef === null)
|
|
@@ -291,9 +268,6 @@ export class Simulation extends Settings {
|
|
|
291
268
|
getScene() {
|
|
292
269
|
return this.scene;
|
|
293
270
|
}
|
|
294
|
-
getSceneObjects() {
|
|
295
|
-
return this.scene.map((item) => item.getObj());
|
|
296
|
-
}
|
|
297
271
|
render(device, ctx, canvas) {
|
|
298
272
|
const colorAttachment = {
|
|
299
273
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
@@ -377,21 +351,10 @@ export class Simulation extends Settings {
|
|
|
377
351
|
renderScene(device, passEncoder, vertexBuffer, indexBuffer, startVertexOffset, startIndexOffset, scene, numElements, diff, transparent) {
|
|
378
352
|
let vertexOffset = startVertexOffset;
|
|
379
353
|
let indexOffset = startIndexOffset;
|
|
380
|
-
const toRemove = [];
|
|
381
354
|
for (let i = 0; i < numElements; i++) {
|
|
382
|
-
const
|
|
383
|
-
const lifetime = sceneObj.getLifetime();
|
|
384
|
-
if (lifetime !== null) {
|
|
385
|
-
const complete = sceneObj.lifetimeComplete();
|
|
386
|
-
if (complete) {
|
|
387
|
-
toRemove.push(i);
|
|
388
|
-
continue;
|
|
389
|
-
}
|
|
390
|
-
sceneObj.traverseLife(diff);
|
|
391
|
-
}
|
|
392
|
-
const obj = sceneObj.getObj();
|
|
355
|
+
const obj = scene[i];
|
|
393
356
|
if (!transparent && obj.isTransparent()) {
|
|
394
|
-
this.transparentElements.add(
|
|
357
|
+
this.transparentElements.add(obj);
|
|
395
358
|
continue;
|
|
396
359
|
}
|
|
397
360
|
if (obj.hasChildren()) {
|
|
@@ -419,9 +382,6 @@ export class Simulation extends Settings {
|
|
|
419
382
|
vertexOffset += vertices.byteLength;
|
|
420
383
|
indexOffset += indices.byteLength;
|
|
421
384
|
}
|
|
422
|
-
for (let i = toRemove.length - 1; i >= 0; i--) {
|
|
423
|
-
this.remove(scene.at(i).getObj());
|
|
424
|
-
}
|
|
425
385
|
return [vertexOffset - startVertexOffset, indexOffset - startIndexOffset];
|
|
426
386
|
}
|
|
427
387
|
fitElement() {
|
package/dist/types.d.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
/// <reference types="@webgpu/types" />
|
|
2
2
|
import { MemoBuffer } from './buffers.js';
|
|
3
|
-
import { CubicBezierCurve2d,
|
|
3
|
+
import { CubicBezierCurve2d, SimulationElement3d, SplinePoint2d } from './graphics.js';
|
|
4
4
|
import { Color } from './utils.js';
|
|
5
5
|
export type FloatArray = Float32Array | Float64Array;
|
|
6
6
|
export type UintArray = Uint8Array | Uint16Array | Uint32Array;
|
|
7
7
|
export type IntArray = Int8Array | Int16Array | Int32Array;
|
|
8
8
|
export type ArrayTypes = FloatArray | UintArray | IntArray;
|
|
9
|
-
export type Vector4 = FloatArray & [number, number, number, number];
|
|
10
|
-
export type Vector3 = FloatArray & [number, number, number];
|
|
11
9
|
export type Vector2 = FloatArray & [number, number];
|
|
10
|
+
export type Vector3 = FloatArray & [number, number, number];
|
|
11
|
+
export type Vector4 = FloatArray & [number, number, number, number];
|
|
12
|
+
export type Vector2m = Vector2 | Vector3 | Vector4;
|
|
13
|
+
export type Vector3m = Vector3 | Vector4;
|
|
12
14
|
export type Mat4 = FloatArray & [
|
|
13
15
|
number,
|
|
14
16
|
number,
|
|
@@ -31,7 +33,6 @@ export type Quat = Float32Array & [number, number, number, number];
|
|
|
31
33
|
export type LerpFunc = (n: number) => number;
|
|
32
34
|
export type VertexColorMap = Record<number, Color>;
|
|
33
35
|
export type ElementRotation<T extends Vector2 | Vector3> = T extends Vector2 ? number : T;
|
|
34
|
-
export type AnySimulationElement = SimulationElement2d | SimulationElement3d;
|
|
35
36
|
export type EmptyParams = object;
|
|
36
37
|
export interface CubeGeometryParams {
|
|
37
38
|
width: number;
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/// <reference types="@webgpu/types" />
|
|
2
2
|
import { SimulationElement3d, SplinePoint2d } from './graphics.js';
|
|
3
|
-
import {
|
|
4
|
-
import { SimSceneObjInfo } from './internalUtils.js';
|
|
3
|
+
import { FloatArray, Mat4, Vector2, Vector2m, Vector3, Vector3m, Vector4 } from './types.js';
|
|
5
4
|
import { Shader } from './shaders.js';
|
|
6
5
|
export declare class Color {
|
|
7
6
|
r: number;
|
|
@@ -79,10 +78,8 @@ export declare function continuousSplinePoint2d(end: Vertex, control: Vector2, d
|
|
|
79
78
|
* @param t - seconds
|
|
80
79
|
*/
|
|
81
80
|
export declare function waitFor(t: number): Promise<unknown>;
|
|
82
|
-
export declare function distance2d(vector1:
|
|
83
|
-
export declare function distance3d(vector1:
|
|
84
|
-
export declare function toSceneObjInfo(el: AnySimulationElement, id?: string): SimSceneObjInfo;
|
|
85
|
-
export declare function toSceneObjInfoMany(el: AnySimulationElement[], id?: (string | undefined)[]): SimSceneObjInfo[];
|
|
81
|
+
export declare function distance2d(vector1: Vector2m, vector2: Vector2m): number;
|
|
82
|
+
export declare function distance3d(vector1: Vector3m, vector2: Vector3m): number;
|
|
86
83
|
export declare function interpolateColors(colors: Color[], t: number): Color;
|
|
87
84
|
export declare function createBindGroup(shader: Shader, bindGroupIndex: number, buffers: GPUBuffer[]): GPUBindGroup;
|
|
88
85
|
export declare function writeUniformWorldMatrix(el: SimulationElement3d): void;
|
package/dist/utils.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { mat4, vec2, vec3, vec4 } from 'wgpu-matrix';
|
|
2
2
|
import { SplinePoint2d } from './graphics.js';
|
|
3
|
-
import { SimSceneObjInfo } from './internalUtils.js';
|
|
4
3
|
import { globalInfo } from './globals.js';
|
|
5
4
|
import { orthogonalMatrix, worldProjectionMatrix } from './simulation.js';
|
|
6
5
|
import { worldProjMatOffset } from './constants.js';
|
|
@@ -262,12 +261,6 @@ export function distance2d(vector1, vector2) {
|
|
|
262
261
|
export function distance3d(vector1, vector2) {
|
|
263
262
|
return vec3.distance(vector1, vector2);
|
|
264
263
|
}
|
|
265
|
-
export function toSceneObjInfo(el, id) {
|
|
266
|
-
return new SimSceneObjInfo(el, id);
|
|
267
|
-
}
|
|
268
|
-
export function toSceneObjInfoMany(el, id) {
|
|
269
|
-
return el.map((item, index) => toSceneObjInfo(item, id ? id[index] : undefined));
|
|
270
|
-
}
|
|
271
264
|
export function interpolateColors(colors, t) {
|
|
272
265
|
t = Math.min(1, Math.max(0, t));
|
|
273
266
|
if (colors.length === 0)
|