simulationjsv2 0.2.3 → 0.2.5
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 +4 -1
- package/dist/constants.js +4 -1
- package/dist/geometry.d.ts +103 -0
- package/dist/geometry.js +407 -0
- package/dist/graphics.d.ts +70 -54
- package/dist/graphics.js +290 -476
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/simulation.d.ts +8 -3
- package/dist/simulation.js +86 -97
- package/dist/types.d.ts +57 -2
- package/dist/utils.d.ts +15 -7
- package/dist/utils.js +103 -13
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export * from './simulation.js';
|
|
2
2
|
export * from './graphics.js';
|
|
3
3
|
export * from './types.js';
|
|
4
|
-
export { Vertex, Color, cloneBuf, vector4, vector3, vector2, vector3FromVector2, colorFromVector4, randomInt, randomColor, vertex, color, colorf, transitionValues, lerp, smoothStep, linearStep, splinePoint2d, continuousSplinePoint2d, easeInOutQuad, easeInOutExpo, easeInOutQuart } from './utils.js';
|
|
4
|
+
export { Vertex, Color, cloneBuf, vector4, vector3, vector2, vector3FromVector2, colorFromVector4, randomInt, randomColor, vertex, color, colorf, transitionValues, lerp, smoothStep, linearStep, splinePoint2d, continuousSplinePoint2d, easeInOutQuad, easeInOutExpo, easeInOutQuart, waitFor } from './utils.js';
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export * from './simulation.js';
|
|
2
2
|
export * from './graphics.js';
|
|
3
3
|
export * from './types.js';
|
|
4
|
-
export { Vertex, Color, cloneBuf, vector4, vector3, vector2, vector3FromVector2, colorFromVector4, randomInt, randomColor, vertex, color, colorf, transitionValues, lerp, smoothStep, linearStep, splinePoint2d, continuousSplinePoint2d, easeInOutQuad, easeInOutExpo, easeInOutQuart } from './utils.js';
|
|
4
|
+
export { Vertex, Color, cloneBuf, vector4, vector3, vector2, vector3FromVector2, colorFromVector4, randomInt, randomColor, vertex, color, colorf, transitionValues, lerp, smoothStep, linearStep, splinePoint2d, continuousSplinePoint2d, easeInOutQuad, easeInOutExpo, easeInOutQuart, waitFor } from './utils.js';
|
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;
|
|
@@ -20,14 +21,18 @@ export declare class Simulation {
|
|
|
20
21
|
fitElement(): void;
|
|
21
22
|
private assertHasCanvas;
|
|
22
23
|
}
|
|
23
|
-
export declare class SceneCollection extends
|
|
24
|
+
export declare class SceneCollection extends SimulationElement3d {
|
|
25
|
+
protected geometry: BlankGeometry;
|
|
24
26
|
private name;
|
|
25
27
|
private scene;
|
|
26
28
|
constructor(name: string);
|
|
27
29
|
getName(): string;
|
|
28
30
|
add(el: SimulationElement<any>): void;
|
|
29
31
|
empty(): void;
|
|
30
|
-
|
|
32
|
+
getSceneBuffer(camera: Camera): number[];
|
|
33
|
+
getWireframe(camera: Camera): number[];
|
|
34
|
+
getTriangles(camera: Camera): number[];
|
|
35
|
+
protected updateMatrix(camera: Camera): void;
|
|
31
36
|
}
|
|
32
37
|
export declare class Camera {
|
|
33
38
|
private pos;
|
package/dist/simulation.js
CHANGED
|
@@ -1,11 +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
|
|
8
|
-
const is3dOffset = 40; // 4 * 10
|
|
4
|
+
import { Color, applyElementToScene, buildDepthTexture, buildMultisampleTexture, buildProjectionMatrix, createPipeline, getOrthoMatrix, getTransformationMatrix, logger, transitionValues, vector2, vector3 } from './utils.js';
|
|
5
|
+
import { BlankGeometry } from './geometry.js';
|
|
9
6
|
const shader = `
|
|
10
7
|
struct Uniforms {
|
|
11
8
|
modelViewProjectionMatrix : mat4x4<f32>,
|
|
@@ -23,19 +20,29 @@ struct VertexOutput {
|
|
|
23
20
|
}
|
|
24
21
|
|
|
25
22
|
@vertex
|
|
26
|
-
fn
|
|
23
|
+
fn vertex_main_3d(
|
|
27
24
|
@location(0) position : vec4<f32>,
|
|
28
25
|
@location(1) color : vec4<f32>,
|
|
29
26
|
@location(2) uv : vec2<f32>,
|
|
30
|
-
@location(3) is3d : f32
|
|
31
27
|
) -> VertexOutput {
|
|
32
28
|
var output : VertexOutput;
|
|
33
29
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
output.Position = uniforms.modelViewProjectionMatrix * position;
|
|
31
|
+
output.fragUV = uv;
|
|
32
|
+
output.fragPosition = position;
|
|
33
|
+
output.fragColor = color;
|
|
34
|
+
return output;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@vertex
|
|
38
|
+
fn vertex_main_2d(
|
|
39
|
+
@location(0) position : vec4<f32>,
|
|
40
|
+
@location(1) color : vec4<f32>,
|
|
41
|
+
@location(2) uv : vec2<f32>,
|
|
42
|
+
) -> VertexOutput {
|
|
43
|
+
var output : VertexOutput;
|
|
44
|
+
|
|
45
|
+
output.Position = uniforms.orthoProjectionMatrix * position;
|
|
39
46
|
output.fragUV = uv;
|
|
40
47
|
output.fragPosition = position;
|
|
41
48
|
output.fragColor = color;
|
|
@@ -131,7 +138,7 @@ export class Simulation {
|
|
|
131
138
|
this.frameRateView.updateFrameRate(1);
|
|
132
139
|
}
|
|
133
140
|
add(el) {
|
|
134
|
-
applyElementToScene(this.scene,
|
|
141
|
+
applyElementToScene(this.scene, el);
|
|
135
142
|
}
|
|
136
143
|
setCanvasSize(width, height) {
|
|
137
144
|
this.assertHasCanvas();
|
|
@@ -178,71 +185,19 @@ export class Simulation {
|
|
|
178
185
|
format: presentationFormat,
|
|
179
186
|
alphaMode: 'premultiplied'
|
|
180
187
|
});
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
{
|
|
188
|
-
arrayStride: vertexSize,
|
|
189
|
-
attributes: [
|
|
190
|
-
{
|
|
191
|
-
// position
|
|
192
|
-
shaderLocation: 0,
|
|
193
|
-
offset: 0,
|
|
194
|
-
format: 'float32x4'
|
|
195
|
-
},
|
|
196
|
-
{
|
|
197
|
-
// color
|
|
198
|
-
shaderLocation: 1,
|
|
199
|
-
offset: colorOffset,
|
|
200
|
-
format: 'float32x4'
|
|
201
|
-
},
|
|
202
|
-
{
|
|
203
|
-
// size
|
|
204
|
-
shaderLocation: 2,
|
|
205
|
-
offset: uvOffset,
|
|
206
|
-
format: 'float32x2'
|
|
207
|
-
},
|
|
208
|
-
{
|
|
209
|
-
// is3d
|
|
210
|
-
shaderLocation: 3,
|
|
211
|
-
offset: is3dOffset,
|
|
212
|
-
format: 'float32'
|
|
213
|
-
}
|
|
214
|
-
]
|
|
215
|
-
}
|
|
216
|
-
]
|
|
217
|
-
},
|
|
218
|
-
fragment: {
|
|
219
|
-
module: shaderModule,
|
|
220
|
-
entryPoint: 'fragment_main',
|
|
221
|
-
targets: [
|
|
222
|
-
{
|
|
223
|
-
format: presentationFormat
|
|
224
|
-
}
|
|
225
|
-
]
|
|
226
|
-
},
|
|
227
|
-
primitive: {
|
|
228
|
-
topology: 'triangle-list'
|
|
229
|
-
},
|
|
230
|
-
multisample: {
|
|
231
|
-
count: 4
|
|
232
|
-
},
|
|
233
|
-
depthStencil: {
|
|
234
|
-
depthWriteEnabled: true,
|
|
235
|
-
depthCompare: 'less',
|
|
236
|
-
format: 'depth24plus'
|
|
237
|
-
}
|
|
238
|
-
});
|
|
188
|
+
const pipeline2dTriangleList = createPipeline(device, shaderModule, presentationFormat, 'vertex_main_2d', 'triangle-list');
|
|
189
|
+
const pipeline2dTriangleStrip = createPipeline(device, shaderModule, presentationFormat, 'vertex_main_2d', 'triangle-strip');
|
|
190
|
+
const pipeline2dLineStrip = createPipeline(device, shaderModule, presentationFormat, 'vertex_main_2d', 'line-strip');
|
|
191
|
+
const pipeline3dTriangleList = createPipeline(device, shaderModule, presentationFormat, 'vertex_main_3d', 'triangle-list');
|
|
192
|
+
const pipeline3dTriangleStrip = createPipeline(device, shaderModule, presentationFormat, 'vertex_main_3d', 'triangle-strip');
|
|
193
|
+
const pipeline3dLineStrip = createPipeline(device, shaderModule, presentationFormat, 'vertex_main_3d', 'line-strip');
|
|
239
194
|
const uniformBufferSize = 4 * 16 + 4 * 16 + 4 * 2 + 8; // 4x4 matrix + 4x4 matrix + vec2<f32> + 8 bc 144 is cool
|
|
240
195
|
const uniformBuffer = device.createBuffer({
|
|
241
196
|
size: uniformBufferSize,
|
|
242
197
|
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
243
198
|
});
|
|
244
199
|
const uniformBindGroup = device.createBindGroup({
|
|
245
|
-
layout:
|
|
200
|
+
layout: pipeline3dTriangleList.getBindGroupLayout(0),
|
|
246
201
|
entries: [
|
|
247
202
|
{
|
|
248
203
|
binding: 0,
|
|
@@ -324,27 +279,52 @@ export class Simulation {
|
|
|
324
279
|
orthoMatrix.buffer, orthoMatrix.byteOffset, orthoMatrix.byteLength);
|
|
325
280
|
device.queue.writeBuffer(uniformBuffer, 4 * 16 + 4 * 16, // 4x4 matrix + 4x4 matrix
|
|
326
281
|
screenSize.buffer, screenSize.byteOffset, screenSize.byteLength);
|
|
327
|
-
let vertexArray = [];
|
|
328
|
-
for (let i = 0; i < this.scene.length; i++) {
|
|
329
|
-
const buffer = this.scene[i].getBuffer(this.camera, this.camera.hasUpdated());
|
|
330
|
-
vertexArray = vertexArray.concat(buffer);
|
|
331
|
-
}
|
|
332
|
-
this.camera.updateConsumed();
|
|
333
|
-
const vertexF32Array = new Float32Array(vertexArray);
|
|
334
|
-
const vertexBuffer = device.createBuffer({
|
|
335
|
-
size: vertexF32Array.byteLength,
|
|
336
|
-
usage: GPUBufferUsage.VERTEX,
|
|
337
|
-
mappedAtCreation: true
|
|
338
|
-
});
|
|
339
|
-
new Float32Array(vertexBuffer.getMappedRange()).set(vertexF32Array);
|
|
340
|
-
vertexBuffer.unmap();
|
|
341
|
-
const vertexCount = vertexF32Array.length / BUF_LEN;
|
|
342
282
|
const commandEncoder = device.createCommandEncoder();
|
|
343
283
|
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
|
|
344
|
-
passEncoder.setPipeline(
|
|
284
|
+
passEncoder.setPipeline(pipeline3dTriangleList);
|
|
345
285
|
passEncoder.setBindGroup(0, uniformBindGroup);
|
|
346
|
-
|
|
347
|
-
|
|
286
|
+
for (let i = 0; i < this.scene.length; i++) {
|
|
287
|
+
const buffer = this.scene[i].getBuffer(this.camera);
|
|
288
|
+
const vertexF32Array = new Float32Array(buffer);
|
|
289
|
+
const vertexBuffer = device.createBuffer({
|
|
290
|
+
size: vertexF32Array.byteLength,
|
|
291
|
+
usage: GPUBufferUsage.VERTEX,
|
|
292
|
+
mappedAtCreation: true
|
|
293
|
+
});
|
|
294
|
+
new Float32Array(vertexBuffer.getMappedRange()).set(vertexF32Array);
|
|
295
|
+
vertexBuffer.unmap();
|
|
296
|
+
const vertexCount = vertexF32Array.length / BUF_LEN;
|
|
297
|
+
if (this.scene[i].isWireframe()) {
|
|
298
|
+
if (this.scene[i].is3d) {
|
|
299
|
+
passEncoder.setPipeline(pipeline3dLineStrip);
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
passEncoder.setPipeline(pipeline2dLineStrip);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
const type = this.scene[i].getGeometryType();
|
|
307
|
+
if (type === 'strip') {
|
|
308
|
+
if (this.scene[i].is3d) {
|
|
309
|
+
passEncoder.setPipeline(pipeline3dTriangleStrip);
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
passEncoder.setPipeline(pipeline2dTriangleStrip);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
else if (type === 'list') {
|
|
316
|
+
if (this.scene[i].is3d) {
|
|
317
|
+
passEncoder.setPipeline(pipeline3dTriangleList);
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
passEncoder.setPipeline(pipeline2dTriangleList);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
passEncoder.setVertexBuffer(0, vertexBuffer);
|
|
325
|
+
passEncoder.draw(vertexCount);
|
|
326
|
+
}
|
|
327
|
+
this.camera.updateConsumed();
|
|
348
328
|
passEncoder.end();
|
|
349
329
|
device.queue.submit([commandEncoder.finish()]);
|
|
350
330
|
};
|
|
@@ -366,27 +346,36 @@ export class Simulation {
|
|
|
366
346
|
}
|
|
367
347
|
}
|
|
368
348
|
}
|
|
369
|
-
export class SceneCollection extends
|
|
349
|
+
export class SceneCollection extends SimulationElement3d {
|
|
350
|
+
geometry;
|
|
370
351
|
name;
|
|
371
352
|
scene;
|
|
372
353
|
constructor(name) {
|
|
373
354
|
super(vector3());
|
|
374
355
|
this.name = name;
|
|
375
356
|
this.scene = [];
|
|
357
|
+
this.geometry = new BlankGeometry();
|
|
376
358
|
}
|
|
377
359
|
getName() {
|
|
378
360
|
return this.name;
|
|
379
361
|
}
|
|
380
362
|
add(el) {
|
|
381
|
-
applyElementToScene(this.scene,
|
|
363
|
+
applyElementToScene(this.scene, el);
|
|
382
364
|
}
|
|
383
365
|
empty() {
|
|
384
366
|
this.scene = [];
|
|
385
367
|
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
368
|
+
getSceneBuffer(camera) {
|
|
369
|
+
return this.scene.map((item) => item.getBuffer(camera)).flat();
|
|
370
|
+
}
|
|
371
|
+
getWireframe(camera) {
|
|
372
|
+
return this.getSceneBuffer(camera);
|
|
373
|
+
}
|
|
374
|
+
getTriangles(camera) {
|
|
375
|
+
return this.getSceneBuffer(camera);
|
|
376
|
+
}
|
|
377
|
+
updateMatrix(camera) {
|
|
378
|
+
this.defaultUpdateMatrix(camera);
|
|
390
379
|
}
|
|
391
380
|
}
|
|
392
381
|
export class Camera {
|
package/dist/types.d.ts
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CubicBezierCurve2d, SplinePoint2d } from './graphics.js';
|
|
2
|
+
import { Color, Vertex } from './utils.js';
|
|
2
3
|
export type Vector4 = Float32Array & [number, number, number, number];
|
|
3
4
|
export type Vector3 = Float32Array & [number, number, number];
|
|
4
5
|
export type Vector2 = Float32Array & [number, number];
|
|
6
|
+
export type Mat4 = Float32Array & [
|
|
7
|
+
number,
|
|
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
|
+
];
|
|
5
24
|
export type LerpFunc = (n: number) => number;
|
|
6
|
-
export type VertexColorMap = Record<
|
|
25
|
+
export type VertexColorMap = Record<number, Color>;
|
|
26
|
+
export type ElementRotation<T extends Vector2 | Vector3> = T extends Vector2 ? number : T;
|
|
27
|
+
export type CubeGeometryParams = {
|
|
28
|
+
width: number;
|
|
29
|
+
height: number;
|
|
30
|
+
depth: number;
|
|
31
|
+
};
|
|
32
|
+
export type SquareGeometryParams = {
|
|
33
|
+
width: number;
|
|
34
|
+
height: number;
|
|
35
|
+
colorMap: VertexColorMap;
|
|
36
|
+
};
|
|
37
|
+
export type CircleGeometryParams = {
|
|
38
|
+
radius: number;
|
|
39
|
+
detail: number;
|
|
40
|
+
};
|
|
41
|
+
export type SplineGeometryParams = {
|
|
42
|
+
points: SplinePoint2d[];
|
|
43
|
+
curves: CubicBezierCurve2d[];
|
|
44
|
+
distance: number;
|
|
45
|
+
detail: number;
|
|
46
|
+
interpolateStart: number;
|
|
47
|
+
interpolateLimit: number;
|
|
48
|
+
thickness: number;
|
|
49
|
+
color: Color;
|
|
50
|
+
vertexColors: Color[];
|
|
51
|
+
};
|
|
52
|
+
export type LineGeometryParams<T extends Vector2 | Vector3> = {
|
|
53
|
+
pos: T;
|
|
54
|
+
to: T;
|
|
55
|
+
thickness: number;
|
|
56
|
+
};
|
|
57
|
+
export type Line2dGeometryParams = LineGeometryParams<Vector2>;
|
|
58
|
+
export type Line3dGeometryParams = LineGeometryParams<Vector3>;
|
|
59
|
+
export type PolygonGeometryParams = {
|
|
60
|
+
points: Vertex[];
|
|
61
|
+
};
|
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)
|
|
@@ -77,13 +76,14 @@ export declare function linearStep(t: number): number;
|
|
|
77
76
|
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
|
-
export declare function
|
|
81
|
-
export declare function
|
|
82
|
-
export declare function
|
|
79
|
+
export declare function vertexBuffer(x: number, y: number, z: number, color: Color, uv?: Vector2): number[];
|
|
80
|
+
export declare function vector3ToPixelRatio(vec: Vector3): void;
|
|
81
|
+
export declare function vector2ToPixelRatio(vec: Vector2): void;
|
|
83
82
|
export declare function cloneBuf<T extends Float32Array>(buf: T): T;
|
|
84
83
|
export declare function vector4(x?: number, y?: number, z?: number, w?: number): Vector4;
|
|
85
84
|
export declare function vector3(x?: number, y?: number, z?: number): Vector3;
|
|
86
85
|
export declare function vector2(x?: number, y?: number): Vector2;
|
|
86
|
+
export declare function matrix4(): Mat4;
|
|
87
87
|
export declare function vector3FromVector2(vec: Vector2): Vector3;
|
|
88
88
|
export declare function vector2FromVector3(vec: Vector3): Vector2;
|
|
89
89
|
export declare function colorFromVector4(vec: Vector4): Color;
|
|
@@ -95,4 +95,12 @@ export declare function colorf(val: number, a?: number): Color;
|
|
|
95
95
|
export declare function splinePoint2d(end: Vertex, control1: Vector2, control2: Vector2, detail?: number): SplinePoint2d;
|
|
96
96
|
export declare function continuousSplinePoint2d(end: Vertex, control: Vector2, detail?: number): SplinePoint2d;
|
|
97
97
|
export declare function interpolateColors(colors: Color[], t: number): Color;
|
|
98
|
+
/**
|
|
99
|
+
* @param t - seconds
|
|
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[];
|
|
98
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
|
|
@@ -100,9 +100,9 @@ export class Vertex {
|
|
|
100
100
|
}
|
|
101
101
|
toBuffer(defaultColor) {
|
|
102
102
|
if (this.is3d)
|
|
103
|
-
return
|
|
103
|
+
return vertexBuffer(this.pos[0], this.pos[1], this.pos[2], this.color || defaultColor, this.uv);
|
|
104
104
|
else
|
|
105
|
-
return
|
|
105
|
+
return vertexBuffer(this.pos[0], this.pos[1], 0, this.color || defaultColor, this.uv);
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
export const buildProjectionMatrix = (aspectRatio, zNear = 1, zFar = 500) => {
|
|
@@ -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 {
|
|
@@ -263,14 +260,17 @@ export function easeInOutQuart(t) {
|
|
|
263
260
|
export function easeInOutQuad(t) {
|
|
264
261
|
return t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2;
|
|
265
262
|
}
|
|
266
|
-
export function
|
|
267
|
-
return [x, y, z, 1, ...color.toBuffer(), ...uv
|
|
263
|
+
export function vertexBuffer(x, y, z, color, uv = vector2()) {
|
|
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;
|
|
271
270
|
}
|
|
272
|
-
export function
|
|
273
|
-
|
|
271
|
+
export function vector2ToPixelRatio(vec) {
|
|
272
|
+
vec[0] *= devicePixelRatio;
|
|
273
|
+
vec[1] *= devicePixelRatio;
|
|
274
274
|
}
|
|
275
275
|
export function cloneBuf(buf) {
|
|
276
276
|
return new Float32Array(buf);
|
|
@@ -284,6 +284,9 @@ export function vector3(x = 0, y = 0, z = 0) {
|
|
|
284
284
|
export function vector2(x = 0, y = 0) {
|
|
285
285
|
return vec2.fromValues(x, y);
|
|
286
286
|
}
|
|
287
|
+
export function matrix4() {
|
|
288
|
+
return mat4.identity();
|
|
289
|
+
}
|
|
287
290
|
export function vector3FromVector2(vec) {
|
|
288
291
|
return vector3(vec[0], vec[1]);
|
|
289
292
|
}
|
|
@@ -348,3 +351,90 @@ export function interpolateColors(colors, t) {
|
|
|
348
351
|
res.a += diff.a;
|
|
349
352
|
return res;
|
|
350
353
|
}
|
|
354
|
+
/**
|
|
355
|
+
* @param t - seconds
|
|
356
|
+
*/
|
|
357
|
+
export function waitFor(t) {
|
|
358
|
+
return new Promise((resolve) => {
|
|
359
|
+
setTimeout(resolve, t * 1000);
|
|
360
|
+
});
|
|
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
|
+
}
|