simulationjsv2 0.2.4 → 0.2.6
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/constants.d.ts +3 -0
- package/dist/constants.js +3 -0
- package/dist/geometry.d.ts +103 -0
- package/dist/geometry.js +407 -0
- package/dist/graphics.d.ts +54 -48
- package/dist/graphics.js +239 -457
- package/dist/simulation.d.ts +12 -3
- package/dist/simulation.js +86 -193
- package/dist/types.d.ts +66 -2
- package/dist/utils.d.ts +10 -5
- package/dist/utils.js +92 -7
- package/package.json +1 -1
package/dist/simulation.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/// <reference types="dist" />
|
|
2
|
-
import { SimulationElement } from './graphics.js';
|
|
2
|
+
import { SimulationElement, SimulationElement3d } from './graphics.js';
|
|
3
3
|
import type { Vector2, Vector3, LerpFunc } from './types.js';
|
|
4
4
|
import { Color } from './utils.js';
|
|
5
|
+
import { BlankGeometry } from './geometry.js';
|
|
5
6
|
export declare class Simulation {
|
|
6
7
|
canvasRef: HTMLCanvasElement | null;
|
|
7
8
|
private bgColor;
|
|
@@ -10,6 +11,7 @@ export declare class Simulation {
|
|
|
10
11
|
private running;
|
|
11
12
|
private frameRateView;
|
|
12
13
|
private camera;
|
|
14
|
+
private pipelines;
|
|
13
15
|
constructor(idOrCanvasRef: string | HTMLCanvasElement, camera?: Camera | null, showFrameRate?: boolean);
|
|
14
16
|
add(el: SimulationElement<any>): void;
|
|
15
17
|
setCanvasSize(width: number, height: number): void;
|
|
@@ -17,17 +19,24 @@ export declare class Simulation {
|
|
|
17
19
|
stop(): void;
|
|
18
20
|
setBackground(color: Color): void;
|
|
19
21
|
render(device: GPUDevice, ctx: GPUCanvasContext): void;
|
|
22
|
+
private renderScene;
|
|
20
23
|
fitElement(): void;
|
|
21
24
|
private assertHasCanvas;
|
|
22
25
|
}
|
|
23
|
-
export declare class SceneCollection extends
|
|
26
|
+
export declare class SceneCollection extends SimulationElement3d {
|
|
27
|
+
protected geometry: BlankGeometry;
|
|
24
28
|
private name;
|
|
25
29
|
private scene;
|
|
26
30
|
constructor(name: string);
|
|
31
|
+
setWireframe(_: boolean): void;
|
|
27
32
|
getName(): string;
|
|
33
|
+
getScene(): SimulationElement<Vector3>[];
|
|
28
34
|
add(el: SimulationElement<any>): void;
|
|
29
35
|
empty(): void;
|
|
30
|
-
|
|
36
|
+
getSceneBuffer(camera: Camera): number[];
|
|
37
|
+
getWireframe(camera: Camera): number[];
|
|
38
|
+
getTriangles(camera: Camera): number[];
|
|
39
|
+
protected updateMatrix(camera: Camera): void;
|
|
31
40
|
}
|
|
32
41
|
export declare class Camera {
|
|
33
42
|
private pos;
|
package/dist/simulation.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { vec3 } from 'wgpu-matrix';
|
|
2
|
-
import {
|
|
2
|
+
import { SimulationElement3d } from './graphics.js';
|
|
3
3
|
import { BUF_LEN } from './constants.js';
|
|
4
|
-
import { Color, applyElementToScene, buildDepthTexture, buildMultisampleTexture, buildProjectionMatrix, getOrthoMatrix, getTransformationMatrix, logger, transitionValues, vector2, vector3 } from './utils.js';
|
|
5
|
-
|
|
6
|
-
const colorOffset = 16; // 4 * 4
|
|
7
|
-
const uvOffset = 32; // 4 * 8
|
|
4
|
+
import { Color, applyElementToScene, buildDepthTexture, buildMultisampleTexture, buildProjectionMatrix, createPipeline, getOrthoMatrix, getTransformationMatrix, logger, transitionValues, vector2, vector3 } from './utils.js';
|
|
5
|
+
import { BlankGeometry } from './geometry.js';
|
|
8
6
|
const shader = `
|
|
9
7
|
struct Uniforms {
|
|
10
8
|
modelViewProjectionMatrix : mat4x4<f32>,
|
|
@@ -108,6 +106,7 @@ export class Simulation {
|
|
|
108
106
|
running = true;
|
|
109
107
|
frameRateView;
|
|
110
108
|
camera;
|
|
109
|
+
pipelines;
|
|
111
110
|
constructor(idOrCanvasRef, camera = null, showFrameRate = false) {
|
|
112
111
|
if (typeof idOrCanvasRef === 'string') {
|
|
113
112
|
const ref = document.getElementById(idOrCanvasRef);
|
|
@@ -136,11 +135,12 @@ export class Simulation {
|
|
|
136
135
|
this.setCanvasSize(width, height);
|
|
137
136
|
}
|
|
138
137
|
});
|
|
138
|
+
this.pipelines = null;
|
|
139
139
|
this.frameRateView = new FrameRateView(showFrameRate);
|
|
140
140
|
this.frameRateView.updateFrameRate(1);
|
|
141
141
|
}
|
|
142
142
|
add(el) {
|
|
143
|
-
applyElementToScene(this.scene,
|
|
143
|
+
applyElementToScene(this.scene, el);
|
|
144
144
|
}
|
|
145
145
|
setCanvasSize(width, height) {
|
|
146
146
|
this.assertHasCanvas();
|
|
@@ -187,169 +187,21 @@ export class Simulation {
|
|
|
187
187
|
format: presentationFormat,
|
|
188
188
|
alphaMode: 'premultiplied'
|
|
189
189
|
});
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
attributes: [
|
|
199
|
-
{
|
|
200
|
-
// position
|
|
201
|
-
shaderLocation: 0,
|
|
202
|
-
offset: 0,
|
|
203
|
-
format: 'float32x4'
|
|
204
|
-
},
|
|
205
|
-
{
|
|
206
|
-
// color
|
|
207
|
-
shaderLocation: 1,
|
|
208
|
-
offset: colorOffset,
|
|
209
|
-
format: 'float32x4'
|
|
210
|
-
},
|
|
211
|
-
{
|
|
212
|
-
// size
|
|
213
|
-
shaderLocation: 2,
|
|
214
|
-
offset: uvOffset,
|
|
215
|
-
format: 'float32x2'
|
|
216
|
-
}
|
|
217
|
-
]
|
|
218
|
-
}
|
|
219
|
-
]
|
|
220
|
-
},
|
|
221
|
-
fragment: {
|
|
222
|
-
module: shaderModule,
|
|
223
|
-
entryPoint: 'fragment_main',
|
|
224
|
-
targets: [
|
|
225
|
-
{
|
|
226
|
-
format: presentationFormat
|
|
227
|
-
}
|
|
228
|
-
]
|
|
229
|
-
},
|
|
230
|
-
primitive: {
|
|
231
|
-
topology: 'triangle-list'
|
|
232
|
-
},
|
|
233
|
-
multisample: {
|
|
234
|
-
count: 4
|
|
235
|
-
},
|
|
236
|
-
depthStencil: {
|
|
237
|
-
depthWriteEnabled: true,
|
|
238
|
-
depthCompare: 'less',
|
|
239
|
-
format: 'depth24plus'
|
|
240
|
-
}
|
|
241
|
-
});
|
|
242
|
-
const pipeline3d = device.createRenderPipeline({
|
|
243
|
-
layout: 'auto',
|
|
244
|
-
vertex: {
|
|
245
|
-
module: shaderModule,
|
|
246
|
-
entryPoint: 'vertex_main_3d',
|
|
247
|
-
buffers: [
|
|
248
|
-
{
|
|
249
|
-
arrayStride: vertexSize,
|
|
250
|
-
attributes: [
|
|
251
|
-
{
|
|
252
|
-
// position
|
|
253
|
-
shaderLocation: 0,
|
|
254
|
-
offset: 0,
|
|
255
|
-
format: 'float32x4'
|
|
256
|
-
},
|
|
257
|
-
{
|
|
258
|
-
// color
|
|
259
|
-
shaderLocation: 1,
|
|
260
|
-
offset: colorOffset,
|
|
261
|
-
format: 'float32x4'
|
|
262
|
-
},
|
|
263
|
-
{
|
|
264
|
-
// size
|
|
265
|
-
shaderLocation: 2,
|
|
266
|
-
offset: uvOffset,
|
|
267
|
-
format: 'float32x2'
|
|
268
|
-
}
|
|
269
|
-
]
|
|
270
|
-
}
|
|
271
|
-
]
|
|
272
|
-
},
|
|
273
|
-
fragment: {
|
|
274
|
-
module: shaderModule,
|
|
275
|
-
entryPoint: 'fragment_main',
|
|
276
|
-
targets: [
|
|
277
|
-
{
|
|
278
|
-
format: presentationFormat
|
|
279
|
-
}
|
|
280
|
-
]
|
|
281
|
-
},
|
|
282
|
-
primitive: {
|
|
283
|
-
topology: 'triangle-list'
|
|
284
|
-
},
|
|
285
|
-
multisample: {
|
|
286
|
-
count: 4
|
|
287
|
-
},
|
|
288
|
-
depthStencil: {
|
|
289
|
-
depthWriteEnabled: true,
|
|
290
|
-
depthCompare: 'less',
|
|
291
|
-
format: 'depth24plus'
|
|
292
|
-
}
|
|
293
|
-
});
|
|
294
|
-
const wireframePipeline = device.createRenderPipeline({
|
|
295
|
-
layout: 'auto',
|
|
296
|
-
vertex: {
|
|
297
|
-
module: shaderModule,
|
|
298
|
-
entryPoint: 'vertex_main_3d',
|
|
299
|
-
buffers: [
|
|
300
|
-
{
|
|
301
|
-
arrayStride: vertexSize,
|
|
302
|
-
attributes: [
|
|
303
|
-
{
|
|
304
|
-
// position
|
|
305
|
-
shaderLocation: 0,
|
|
306
|
-
offset: 0,
|
|
307
|
-
format: 'float32x4'
|
|
308
|
-
},
|
|
309
|
-
{
|
|
310
|
-
// color
|
|
311
|
-
shaderLocation: 1,
|
|
312
|
-
offset: colorOffset,
|
|
313
|
-
format: 'float32x4'
|
|
314
|
-
},
|
|
315
|
-
{
|
|
316
|
-
// size
|
|
317
|
-
shaderLocation: 2,
|
|
318
|
-
offset: uvOffset,
|
|
319
|
-
format: 'float32x2'
|
|
320
|
-
}
|
|
321
|
-
]
|
|
322
|
-
}
|
|
323
|
-
]
|
|
324
|
-
},
|
|
325
|
-
fragment: {
|
|
326
|
-
module: shaderModule,
|
|
327
|
-
entryPoint: 'fragment_main',
|
|
328
|
-
targets: [
|
|
329
|
-
{
|
|
330
|
-
format: presentationFormat
|
|
331
|
-
}
|
|
332
|
-
]
|
|
333
|
-
},
|
|
334
|
-
primitive: {
|
|
335
|
-
topology: 'line-strip'
|
|
336
|
-
},
|
|
337
|
-
multisample: {
|
|
338
|
-
count: 4
|
|
339
|
-
},
|
|
340
|
-
depthStencil: {
|
|
341
|
-
depthWriteEnabled: true,
|
|
342
|
-
depthCompare: 'less',
|
|
343
|
-
format: 'depth24plus'
|
|
344
|
-
}
|
|
345
|
-
});
|
|
190
|
+
this.pipelines = {
|
|
191
|
+
triangleList2d: createPipeline(device, shaderModule, presentationFormat, 'vertex_main_2d', 'triangle-list'),
|
|
192
|
+
triangleStrip2d: createPipeline(device, shaderModule, presentationFormat, 'vertex_main_2d', 'triangle-strip'),
|
|
193
|
+
lineStrip2d: createPipeline(device, shaderModule, presentationFormat, 'vertex_main_2d', 'line-strip'),
|
|
194
|
+
triangleList3d: createPipeline(device, shaderModule, presentationFormat, 'vertex_main_3d', 'triangle-list'),
|
|
195
|
+
triangleStrip3d: createPipeline(device, shaderModule, presentationFormat, 'vertex_main_3d', 'triangle-strip'),
|
|
196
|
+
lineStrip3d: createPipeline(device, shaderModule, presentationFormat, 'vertex_main_3d', 'line-strip')
|
|
197
|
+
};
|
|
346
198
|
const uniformBufferSize = 4 * 16 + 4 * 16 + 4 * 2 + 8; // 4x4 matrix + 4x4 matrix + vec2<f32> + 8 bc 144 is cool
|
|
347
199
|
const uniformBuffer = device.createBuffer({
|
|
348
200
|
size: uniformBufferSize,
|
|
349
201
|
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
350
202
|
});
|
|
351
203
|
const uniformBindGroup = device.createBindGroup({
|
|
352
|
-
layout:
|
|
204
|
+
layout: this.pipelines.triangleList3d.getBindGroupLayout(0),
|
|
353
205
|
entries: [
|
|
354
206
|
{
|
|
355
207
|
binding: 0,
|
|
@@ -433,37 +285,63 @@ export class Simulation {
|
|
|
433
285
|
screenSize.buffer, screenSize.byteOffset, screenSize.byteLength);
|
|
434
286
|
const commandEncoder = device.createCommandEncoder();
|
|
435
287
|
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
|
|
436
|
-
passEncoder.setPipeline(
|
|
288
|
+
passEncoder.setPipeline(this.pipelines.triangleList3d);
|
|
437
289
|
passEncoder.setBindGroup(0, uniformBindGroup);
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
290
|
+
this.renderScene(device, passEncoder, this.scene);
|
|
291
|
+
this.camera.updateConsumed();
|
|
292
|
+
passEncoder.end();
|
|
293
|
+
device.queue.submit([commandEncoder.finish()]);
|
|
294
|
+
};
|
|
295
|
+
requestAnimationFrame(frame);
|
|
296
|
+
}
|
|
297
|
+
renderScene(device, passEncoder, scene) {
|
|
298
|
+
if (this.pipelines === null)
|
|
299
|
+
return;
|
|
300
|
+
for (let i = 0; i < scene.length; i++) {
|
|
301
|
+
if (scene[i] instanceof SceneCollection) {
|
|
302
|
+
this.renderScene(device, passEncoder, scene[i].getScene());
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
const buffer = scene[i].getBuffer(this.camera);
|
|
306
|
+
const vertexF32Array = new Float32Array(buffer);
|
|
307
|
+
const vertexBuffer = device.createBuffer({
|
|
308
|
+
size: vertexF32Array.byteLength,
|
|
309
|
+
usage: GPUBufferUsage.VERTEX,
|
|
310
|
+
mappedAtCreation: true
|
|
311
|
+
});
|
|
312
|
+
new Float32Array(vertexBuffer.getMappedRange()).set(vertexF32Array);
|
|
313
|
+
vertexBuffer.unmap();
|
|
314
|
+
const vertexCount = vertexF32Array.length / BUF_LEN;
|
|
315
|
+
if (scene[i].isWireframe()) {
|
|
316
|
+
if (scene[i].is3d) {
|
|
317
|
+
passEncoder.setPipeline(this.pipelines.lineStrip3d);
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
passEncoder.setPipeline(this.pipelines.lineStrip2d);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
const type = scene[i].getGeometryType();
|
|
325
|
+
if (type === 'strip') {
|
|
326
|
+
if (scene[i].is3d) {
|
|
327
|
+
passEncoder.setPipeline(this.pipelines.triangleStrip3d);
|
|
452
328
|
}
|
|
453
329
|
else {
|
|
454
|
-
passEncoder.setPipeline(
|
|
330
|
+
passEncoder.setPipeline(this.pipelines.triangleStrip2d);
|
|
455
331
|
}
|
|
456
332
|
}
|
|
457
|
-
else {
|
|
458
|
-
|
|
333
|
+
else if (type === 'list') {
|
|
334
|
+
if (scene[i].is3d) {
|
|
335
|
+
passEncoder.setPipeline(this.pipelines.triangleList3d);
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
passEncoder.setPipeline(this.pipelines.triangleList2d);
|
|
339
|
+
}
|
|
459
340
|
}
|
|
460
|
-
passEncoder.setVertexBuffer(0, vertexBuffer);
|
|
461
|
-
passEncoder.draw(vertexCount);
|
|
462
341
|
}
|
|
463
|
-
passEncoder.
|
|
464
|
-
|
|
465
|
-
}
|
|
466
|
-
requestAnimationFrame(frame);
|
|
342
|
+
passEncoder.setVertexBuffer(0, vertexBuffer);
|
|
343
|
+
passEncoder.draw(vertexCount);
|
|
344
|
+
}
|
|
467
345
|
}
|
|
468
346
|
fitElement() {
|
|
469
347
|
this.assertHasCanvas();
|
|
@@ -481,27 +359,42 @@ export class Simulation {
|
|
|
481
359
|
}
|
|
482
360
|
}
|
|
483
361
|
}
|
|
484
|
-
export class SceneCollection extends
|
|
362
|
+
export class SceneCollection extends SimulationElement3d {
|
|
363
|
+
geometry;
|
|
485
364
|
name;
|
|
486
365
|
scene;
|
|
487
366
|
constructor(name) {
|
|
488
367
|
super(vector3());
|
|
368
|
+
this.wireframe = false;
|
|
489
369
|
this.name = name;
|
|
490
370
|
this.scene = [];
|
|
371
|
+
this.geometry = new BlankGeometry();
|
|
491
372
|
}
|
|
373
|
+
setWireframe(_) { }
|
|
492
374
|
getName() {
|
|
493
375
|
return this.name;
|
|
494
376
|
}
|
|
377
|
+
getScene() {
|
|
378
|
+
return this.scene;
|
|
379
|
+
}
|
|
495
380
|
add(el) {
|
|
496
|
-
applyElementToScene(this.scene,
|
|
381
|
+
applyElementToScene(this.scene, el);
|
|
497
382
|
}
|
|
498
383
|
empty() {
|
|
499
384
|
this.scene = [];
|
|
500
385
|
}
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
386
|
+
getSceneBuffer(camera) {
|
|
387
|
+
return this.scene.map((item) => item.getBuffer(camera)).flat();
|
|
388
|
+
}
|
|
389
|
+
getWireframe(camera) {
|
|
390
|
+
return this.getSceneBuffer(camera);
|
|
391
|
+
}
|
|
392
|
+
getTriangles(camera) {
|
|
393
|
+
console.log('here');
|
|
394
|
+
return this.getSceneBuffer(camera);
|
|
395
|
+
}
|
|
396
|
+
updateMatrix(camera) {
|
|
397
|
+
this.defaultUpdateMatrix(camera);
|
|
505
398
|
}
|
|
506
399
|
}
|
|
507
400
|
export class Camera {
|
package/dist/types.d.ts
CHANGED
|
@@ -1,6 +1,70 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="dist" />
|
|
2
|
+
import { CubicBezierCurve2d, SplinePoint2d } from './graphics.js';
|
|
3
|
+
import { Color, Vertex } from './utils.js';
|
|
2
4
|
export type Vector4 = Float32Array & [number, number, number, number];
|
|
3
5
|
export type Vector3 = Float32Array & [number, number, number];
|
|
4
6
|
export type Vector2 = Float32Array & [number, number];
|
|
7
|
+
export type Mat4 = Float32Array & [
|
|
8
|
+
number,
|
|
9
|
+
number,
|
|
10
|
+
number,
|
|
11
|
+
number,
|
|
12
|
+
number,
|
|
13
|
+
number,
|
|
14
|
+
number,
|
|
15
|
+
number,
|
|
16
|
+
number,
|
|
17
|
+
number,
|
|
18
|
+
number,
|
|
19
|
+
number,
|
|
20
|
+
number,
|
|
21
|
+
number,
|
|
22
|
+
number,
|
|
23
|
+
number
|
|
24
|
+
];
|
|
5
25
|
export type LerpFunc = (n: number) => number;
|
|
6
|
-
export type VertexColorMap = Record<
|
|
26
|
+
export type VertexColorMap = Record<number, Color>;
|
|
27
|
+
export type ElementRotation<T extends Vector2 | Vector3> = T extends Vector2 ? number : T;
|
|
28
|
+
export type CubeGeometryParams = {
|
|
29
|
+
width: number;
|
|
30
|
+
height: number;
|
|
31
|
+
depth: number;
|
|
32
|
+
};
|
|
33
|
+
export type SquareGeometryParams = {
|
|
34
|
+
width: number;
|
|
35
|
+
height: number;
|
|
36
|
+
colorMap: VertexColorMap;
|
|
37
|
+
};
|
|
38
|
+
export type CircleGeometryParams = {
|
|
39
|
+
radius: number;
|
|
40
|
+
detail: number;
|
|
41
|
+
};
|
|
42
|
+
export type SplineGeometryParams = {
|
|
43
|
+
points: SplinePoint2d[];
|
|
44
|
+
curves: CubicBezierCurve2d[];
|
|
45
|
+
distance: number;
|
|
46
|
+
detail: number;
|
|
47
|
+
interpolateStart: number;
|
|
48
|
+
interpolateLimit: number;
|
|
49
|
+
thickness: number;
|
|
50
|
+
color: Color;
|
|
51
|
+
vertexColors: Color[];
|
|
52
|
+
};
|
|
53
|
+
export type LineGeometryParams<T extends Vector2 | Vector3> = {
|
|
54
|
+
pos: T;
|
|
55
|
+
to: T;
|
|
56
|
+
thickness: number;
|
|
57
|
+
};
|
|
58
|
+
export type Line2dGeometryParams = LineGeometryParams<Vector2>;
|
|
59
|
+
export type Line3dGeometryParams = LineGeometryParams<Vector3>;
|
|
60
|
+
export type PolygonGeometryParams = {
|
|
61
|
+
points: Vertex[];
|
|
62
|
+
};
|
|
63
|
+
export type PipelineGroup = {
|
|
64
|
+
triangleList2d: GPURenderPipeline;
|
|
65
|
+
triangleStrip2d: GPURenderPipeline;
|
|
66
|
+
lineStrip2d: GPURenderPipeline;
|
|
67
|
+
triangleList3d: GPURenderPipeline;
|
|
68
|
+
triangleStrip3d: GPURenderPipeline;
|
|
69
|
+
lineStrip3d: GPURenderPipeline;
|
|
70
|
+
};
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/// <reference types="dist" />
|
|
2
2
|
import { SimulationElement, SplinePoint2d } from './graphics.js';
|
|
3
|
-
import { Vector2, Vector3, Vector4 } from './types.js';
|
|
4
|
-
import { Camera } from './simulation.js';
|
|
3
|
+
import { Mat4, Vector2, Vector3, Vector4 } from './types.js';
|
|
5
4
|
export declare class Color {
|
|
6
5
|
r: number;
|
|
7
6
|
g: number;
|
|
@@ -53,7 +52,7 @@ export declare const getTransformationMatrix: (pos: Vector3, rotation: Vector3,
|
|
|
53
52
|
export declare const getOrthoMatrix: (screenSize: [number, number]) => Float32Array;
|
|
54
53
|
export declare const buildDepthTexture: (device: GPUDevice, width: number, height: number) => GPUTexture;
|
|
55
54
|
export declare const buildMultisampleTexture: (device: GPUDevice, ctx: GPUCanvasContext, width: number, height: number) => GPUTexture;
|
|
56
|
-
export declare const applyElementToScene: (scene: SimulationElement[],
|
|
55
|
+
export declare const applyElementToScene: (scene: SimulationElement[], el: SimulationElement) => void;
|
|
57
56
|
declare class Logger {
|
|
58
57
|
constructor();
|
|
59
58
|
private fmt;
|
|
@@ -63,7 +62,7 @@ declare class Logger {
|
|
|
63
62
|
log_error(msg: string): void;
|
|
64
63
|
}
|
|
65
64
|
export declare const logger: Logger;
|
|
66
|
-
export declare function lossyTriangulate(vertices:
|
|
65
|
+
export declare function lossyTriangulate<T>(vertices: T[]): (readonly [T, T, T])[];
|
|
67
66
|
/**
|
|
68
67
|
* @param callback1 - called every frame until the animation is finished
|
|
69
68
|
* @param callback2 - called after animation is finished (called immediately when t = 0)
|
|
@@ -78,11 +77,13 @@ export declare function easeInOutExpo(t: number): number;
|
|
|
78
77
|
export declare function easeInOutQuart(t: number): number;
|
|
79
78
|
export declare function easeInOutQuad(t: number): number;
|
|
80
79
|
export declare function vertexBuffer(x: number, y: number, z: number, color: Color, uv?: Vector2): number[];
|
|
81
|
-
export declare function
|
|
80
|
+
export declare function vector3ToPixelRatio(vec: Vector3): void;
|
|
81
|
+
export declare function vector2ToPixelRatio(vec: Vector2): void;
|
|
82
82
|
export declare function cloneBuf<T extends Float32Array>(buf: T): T;
|
|
83
83
|
export declare function vector4(x?: number, y?: number, z?: number, w?: number): Vector4;
|
|
84
84
|
export declare function vector3(x?: number, y?: number, z?: number): Vector3;
|
|
85
85
|
export declare function vector2(x?: number, y?: number): Vector2;
|
|
86
|
+
export declare function matrix4(): Mat4;
|
|
86
87
|
export declare function vector3FromVector2(vec: Vector2): Vector3;
|
|
87
88
|
export declare function vector2FromVector3(vec: Vector3): Vector2;
|
|
88
89
|
export declare function colorFromVector4(vec: Vector4): Color;
|
|
@@ -98,4 +99,8 @@ export declare function interpolateColors(colors: Color[], t: number): Color;
|
|
|
98
99
|
* @param t - seconds
|
|
99
100
|
*/
|
|
100
101
|
export declare function waitFor(t: number): Promise<unknown>;
|
|
102
|
+
export declare function matrixFromRotation(rotation: Vector3): Mat4;
|
|
103
|
+
export declare function rotateMat4(mat: Mat4, rotation: Vector3): void;
|
|
104
|
+
export declare function createPipeline(device: GPUDevice, module: GPUShaderModule, presentationFormat: GPUTextureFormat, entryPoint: string, topology: GPUPrimitiveTopology): GPURenderPipeline;
|
|
105
|
+
export declare function triangulateWireFrameOrder(len: number): number[];
|
|
101
106
|
export {};
|
package/dist/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { mat4, vec2, vec3, vec4 } from 'wgpu-matrix';
|
|
2
2
|
import { SimulationElement, SplinePoint2d } from './graphics.js';
|
|
3
|
-
import { BUF_LEN } from './constants.js';
|
|
3
|
+
import { BUF_LEN, colorOffset, uvOffset, vertexSize } from './constants.js';
|
|
4
4
|
export class Color {
|
|
5
5
|
r; // 0 - 255
|
|
6
6
|
g; // 0 - 255
|
|
@@ -141,11 +141,8 @@ export const buildMultisampleTexture = (device, ctx, width, height) => {
|
|
|
141
141
|
sampleCount: 4
|
|
142
142
|
});
|
|
143
143
|
};
|
|
144
|
-
export const applyElementToScene = (scene,
|
|
145
|
-
if (!camera)
|
|
146
|
-
throw logger.error('Camera is not initialized in element');
|
|
144
|
+
export const applyElementToScene = (scene, el) => {
|
|
147
145
|
if (el instanceof SimulationElement) {
|
|
148
|
-
el.setCamera(camera);
|
|
149
146
|
scene.push(el);
|
|
150
147
|
}
|
|
151
148
|
else {
|
|
@@ -266,8 +263,14 @@ export function easeInOutQuad(t) {
|
|
|
266
263
|
export function vertexBuffer(x, y, z, color, uv = vector2()) {
|
|
267
264
|
return [x, y, z, 1, ...color.toBuffer(), ...uv];
|
|
268
265
|
}
|
|
269
|
-
export function
|
|
270
|
-
|
|
266
|
+
export function vector3ToPixelRatio(vec) {
|
|
267
|
+
vec[0] *= devicePixelRatio;
|
|
268
|
+
vec[1] *= devicePixelRatio;
|
|
269
|
+
vec[2] *= devicePixelRatio;
|
|
270
|
+
}
|
|
271
|
+
export function vector2ToPixelRatio(vec) {
|
|
272
|
+
vec[0] *= devicePixelRatio;
|
|
273
|
+
vec[1] *= devicePixelRatio;
|
|
271
274
|
}
|
|
272
275
|
export function cloneBuf(buf) {
|
|
273
276
|
return new Float32Array(buf);
|
|
@@ -281,6 +284,9 @@ export function vector3(x = 0, y = 0, z = 0) {
|
|
|
281
284
|
export function vector2(x = 0, y = 0) {
|
|
282
285
|
return vec2.fromValues(x, y);
|
|
283
286
|
}
|
|
287
|
+
export function matrix4() {
|
|
288
|
+
return mat4.identity();
|
|
289
|
+
}
|
|
284
290
|
export function vector3FromVector2(vec) {
|
|
285
291
|
return vector3(vec[0], vec[1]);
|
|
286
292
|
}
|
|
@@ -353,3 +359,82 @@ export function waitFor(t) {
|
|
|
353
359
|
setTimeout(resolve, t * 1000);
|
|
354
360
|
});
|
|
355
361
|
}
|
|
362
|
+
export function matrixFromRotation(rotation) {
|
|
363
|
+
let rotMatrix = mat4.identity();
|
|
364
|
+
mat4.rotateZ(rotMatrix, rotation[2], rotMatrix);
|
|
365
|
+
mat4.rotateY(rotMatrix, rotation[1], rotMatrix);
|
|
366
|
+
mat4.rotateX(rotMatrix, rotation[0], rotMatrix);
|
|
367
|
+
return rotMatrix;
|
|
368
|
+
}
|
|
369
|
+
export function rotateMat4(mat, rotation) {
|
|
370
|
+
mat4.rotateZ(mat, rotation[2], mat);
|
|
371
|
+
mat4.rotateY(mat, rotation[1], mat);
|
|
372
|
+
mat4.rotateX(mat, rotation[0], mat);
|
|
373
|
+
}
|
|
374
|
+
export function createPipeline(device, module, presentationFormat, entryPoint, topology) {
|
|
375
|
+
return device.createRenderPipeline({
|
|
376
|
+
layout: 'auto',
|
|
377
|
+
vertex: {
|
|
378
|
+
module,
|
|
379
|
+
entryPoint,
|
|
380
|
+
buffers: [
|
|
381
|
+
{
|
|
382
|
+
arrayStride: vertexSize,
|
|
383
|
+
attributes: [
|
|
384
|
+
{
|
|
385
|
+
// position
|
|
386
|
+
shaderLocation: 0,
|
|
387
|
+
offset: 0,
|
|
388
|
+
format: 'float32x4'
|
|
389
|
+
},
|
|
390
|
+
{
|
|
391
|
+
// color
|
|
392
|
+
shaderLocation: 1,
|
|
393
|
+
offset: colorOffset,
|
|
394
|
+
format: 'float32x4'
|
|
395
|
+
},
|
|
396
|
+
{
|
|
397
|
+
// size
|
|
398
|
+
shaderLocation: 2,
|
|
399
|
+
offset: uvOffset,
|
|
400
|
+
format: 'float32x2'
|
|
401
|
+
}
|
|
402
|
+
]
|
|
403
|
+
}
|
|
404
|
+
]
|
|
405
|
+
},
|
|
406
|
+
fragment: {
|
|
407
|
+
module,
|
|
408
|
+
entryPoint: 'fragment_main',
|
|
409
|
+
targets: [
|
|
410
|
+
{
|
|
411
|
+
format: presentationFormat
|
|
412
|
+
}
|
|
413
|
+
]
|
|
414
|
+
},
|
|
415
|
+
primitive: {
|
|
416
|
+
topology
|
|
417
|
+
},
|
|
418
|
+
multisample: {
|
|
419
|
+
count: 4
|
|
420
|
+
},
|
|
421
|
+
depthStencil: {
|
|
422
|
+
depthWriteEnabled: true,
|
|
423
|
+
depthCompare: 'less',
|
|
424
|
+
format: 'depth24plus'
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
export function triangulateWireFrameOrder(len) {
|
|
429
|
+
const order = Array(len)
|
|
430
|
+
.fill(0)
|
|
431
|
+
.map((_, index) => index);
|
|
432
|
+
let front = 0;
|
|
433
|
+
let back = len - 1;
|
|
434
|
+
while (front < back) {
|
|
435
|
+
order.push(front, back);
|
|
436
|
+
front++;
|
|
437
|
+
back--;
|
|
438
|
+
}
|
|
439
|
+
return order;
|
|
440
|
+
}
|