simulationjsv2 0.4.2 → 0.4.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.js +2 -2
- package/dist/graphics.js +1 -0
- package/dist/internalUtils.d.ts +0 -1
- package/dist/internalUtils.js +1 -28
- package/dist/simulation.d.ts +1 -0
- package/dist/simulation.js +25 -11
- package/dist/types.d.ts +1 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +25 -0
- package/package.json +1 -1
package/dist/geometry.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { mat4, vec2, vec3 } from 'wgpu-matrix';
|
|
2
|
-
import { cloneBuf, matrix4, vector2, vector2FromVector3, vector3, vector3FromVector2, vertex } from './utils.js';
|
|
2
|
+
import { cloneBuf, interpolateColors, matrix4, vector2, vector2FromVector3, vector3, vector3FromVector2, vertex } from './utils.js';
|
|
3
3
|
import { CubicBezierCurve2d } from './graphics.js';
|
|
4
4
|
import { BUF_LEN } from './constants.js';
|
|
5
|
-
import { bufferGenerator,
|
|
5
|
+
import { bufferGenerator, lossyTriangulate, triangulateWireFrameOrder } from './internalUtils.js';
|
|
6
6
|
export class Geometry {
|
|
7
7
|
vertices;
|
|
8
8
|
matrix;
|
package/dist/graphics.js
CHANGED
|
@@ -373,6 +373,7 @@ export class Circle extends SimulationElement2d {
|
|
|
373
373
|
detail;
|
|
374
374
|
constructor(pos, radius, color, detail = 50) {
|
|
375
375
|
super(pos, 0, color);
|
|
376
|
+
vector2ToPixelRatio(this.pos);
|
|
376
377
|
this.radius = radius;
|
|
377
378
|
this.detail = detail;
|
|
378
379
|
this.geometry = new CircleGeometry(this.radius, this.detail);
|
package/dist/internalUtils.d.ts
CHANGED
|
@@ -58,7 +58,6 @@ declare class BufferGenerator {
|
|
|
58
58
|
export declare const bufferGenerator: BufferGenerator;
|
|
59
59
|
export declare function vector3ToPixelRatio(vec: Vector3): void;
|
|
60
60
|
export declare function vector2ToPixelRatio(vec: Vector2): void;
|
|
61
|
-
export declare function interpolateColors(colors: Color[], t: number): Color;
|
|
62
61
|
export declare function matrixFromRotation(rotation: Vector3): Mat4;
|
|
63
62
|
export declare function rotateMat4(mat: Mat4, rotation: Vector3): void;
|
|
64
63
|
export declare function createPipeline(device: GPUDevice, module: GPUShaderModule, bindGroupLayout: GPUBindGroupLayout, presentationFormat: GPUTextureFormat, entryPoint: string, topology: GPUPrimitiveTopology): GPURenderPipeline;
|
package/dist/internalUtils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { mat4, vec3 } from 'wgpu-matrix';
|
|
2
2
|
import { BUF_LEN, colorOffset, drawingInstancesOffset, uvOffset, vertexSize } from './constants.js';
|
|
3
|
-
import {
|
|
3
|
+
import { vector2, vector3 } from './utils.js';
|
|
4
4
|
import { SimulationElement } from './graphics.js';
|
|
5
5
|
export class VertexCache {
|
|
6
6
|
vertices = [];
|
|
@@ -192,31 +192,6 @@ export function vector2ToPixelRatio(vec) {
|
|
|
192
192
|
vec[0] *= devicePixelRatio;
|
|
193
193
|
vec[1] *= devicePixelRatio;
|
|
194
194
|
}
|
|
195
|
-
export function interpolateColors(colors, t) {
|
|
196
|
-
t = Math.min(1, Math.max(0, t));
|
|
197
|
-
if (colors.length === 0)
|
|
198
|
-
return color();
|
|
199
|
-
if (colors.length === 1)
|
|
200
|
-
return colors[0];
|
|
201
|
-
const colorInterval = 1 / colors.length;
|
|
202
|
-
let index = Math.floor(t / colorInterval);
|
|
203
|
-
if (index >= colors.length)
|
|
204
|
-
index = colors.length - 1;
|
|
205
|
-
const from = index === colors.length - 1 ? colors[index - 1] : colors[index];
|
|
206
|
-
const to = index === colors.length - 1 ? colors[index] : colors[index + 1];
|
|
207
|
-
const diff = to.diff(from);
|
|
208
|
-
const scale = t / (colorInterval * colors.length);
|
|
209
|
-
diff.r *= scale;
|
|
210
|
-
diff.g *= scale;
|
|
211
|
-
diff.b *= scale;
|
|
212
|
-
diff.a *= scale;
|
|
213
|
-
const res = from.clone();
|
|
214
|
-
res.r += diff.r;
|
|
215
|
-
res.g += diff.g;
|
|
216
|
-
res.b += diff.b;
|
|
217
|
-
res.a += diff.a;
|
|
218
|
-
return res;
|
|
219
|
-
}
|
|
220
195
|
export function matrixFromRotation(rotation) {
|
|
221
196
|
let rotMatrix = mat4.identity();
|
|
222
197
|
mat4.rotateZ(rotMatrix, rotation[2], rotMatrix);
|
|
@@ -308,8 +283,6 @@ export function getTotalVertices(scene) {
|
|
|
308
283
|
let total = 0;
|
|
309
284
|
for (let i = 0; i < scene.length; i++) {
|
|
310
285
|
const obj = scene[i].getObj();
|
|
311
|
-
if (obj.isCollection)
|
|
312
|
-
continue;
|
|
313
286
|
total += obj.getVertexCount();
|
|
314
287
|
}
|
|
315
288
|
return total;
|
package/dist/simulation.d.ts
CHANGED
|
@@ -44,6 +44,7 @@ export declare class SceneCollection extends SimulationElement3d {
|
|
|
44
44
|
setWireframe(_: boolean): void;
|
|
45
45
|
getName(): string;
|
|
46
46
|
getScene(): SimSceneObjInfo[];
|
|
47
|
+
getVertexCount(): number;
|
|
47
48
|
getSceneObjects(): SimulationElement<Vector3 | Vector2>[];
|
|
48
49
|
setSceneObjects(newScene: SimulationElement<any>[]): void;
|
|
49
50
|
setScene(newScene: SimSceneObjInfo[]): void;
|
package/dist/simulation.js
CHANGED
|
@@ -266,7 +266,8 @@ export class Simulation {
|
|
|
266
266
|
this.renderInfo = {
|
|
267
267
|
uniformBuffer,
|
|
268
268
|
bindGroupLayout,
|
|
269
|
-
instanceBuffer
|
|
269
|
+
instanceBuffer,
|
|
270
|
+
vertexBuffer: null
|
|
270
271
|
};
|
|
271
272
|
this.pipelines = {
|
|
272
273
|
triangleList2d: createPipeline(device, shaderModule, bindGroupLayout, presentationFormat, 'vertex_main_2d', 'triangle-list'),
|
|
@@ -329,6 +330,8 @@ export class Simulation {
|
|
|
329
330
|
const frame = async () => {
|
|
330
331
|
if (!canvas)
|
|
331
332
|
return;
|
|
333
|
+
if (!this.renderInfo)
|
|
334
|
+
return;
|
|
332
335
|
requestAnimationFrame(frame);
|
|
333
336
|
if (!this.running)
|
|
334
337
|
return;
|
|
@@ -369,22 +372,25 @@ export class Simulation {
|
|
|
369
372
|
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
|
|
370
373
|
passEncoder.setPipeline(this.pipelines.triangleList3d);
|
|
371
374
|
passEncoder.setBindGroup(0, uniformBindGroup);
|
|
372
|
-
|
|
375
|
+
const totalVertices = getTotalVertices(this.scene);
|
|
376
|
+
if (this.renderInfo.vertexBuffer === null ||
|
|
377
|
+
this.renderInfo.vertexBuffer.size / (4 * BUF_LEN) < totalVertices) {
|
|
378
|
+
this.renderInfo.vertexBuffer = device.createBuffer({
|
|
379
|
+
size: totalVertices * 4 * BUF_LEN,
|
|
380
|
+
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
this.renderScene(device, passEncoder, this.renderInfo.vertexBuffer, this.scene, 0, diff);
|
|
373
384
|
this.camera.updateConsumed();
|
|
374
385
|
passEncoder.end();
|
|
375
386
|
device.queue.submit([commandEncoder.finish()]);
|
|
376
387
|
};
|
|
377
388
|
requestAnimationFrame(frame);
|
|
378
389
|
}
|
|
379
|
-
|
|
390
|
+
renderScene(device, passEncoder, vertexBuffer, scene, startOffset, diff) {
|
|
380
391
|
if (this.pipelines === null)
|
|
381
|
-
return;
|
|
382
|
-
let
|
|
383
|
-
const vertexBuffer = device.createBuffer({
|
|
384
|
-
size: totalVertices * 4 * BUF_LEN,
|
|
385
|
-
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST
|
|
386
|
-
});
|
|
387
|
-
let currentOffset = 0;
|
|
392
|
+
return 0;
|
|
393
|
+
let currentOffset = startOffset;
|
|
388
394
|
let toRemove = [];
|
|
389
395
|
for (let i = 0; i < scene.length; i++) {
|
|
390
396
|
const lifetime = scene[i].getLifetime();
|
|
@@ -398,7 +404,7 @@ export class Simulation {
|
|
|
398
404
|
}
|
|
399
405
|
const obj = scene[i].getObj();
|
|
400
406
|
if (obj.isCollection) {
|
|
401
|
-
this.renderScene(device, passEncoder, obj.getScene(), diff);
|
|
407
|
+
currentOffset += this.renderScene(device, passEncoder, vertexBuffer, obj.getScene(), currentOffset, diff);
|
|
402
408
|
continue;
|
|
403
409
|
}
|
|
404
410
|
const buffer = new Float32Array(obj.getBuffer(this.camera));
|
|
@@ -465,6 +471,7 @@ export class Simulation {
|
|
|
465
471
|
for (let i = toRemove.length - 1; i >= 0; i--) {
|
|
466
472
|
removeObject(scene, scene[i].getObj());
|
|
467
473
|
}
|
|
474
|
+
return currentOffset - startOffset;
|
|
468
475
|
}
|
|
469
476
|
fitElement() {
|
|
470
477
|
this.assertHasCanvas();
|
|
@@ -501,6 +508,13 @@ export class SceneCollection extends SimulationElement3d {
|
|
|
501
508
|
getScene() {
|
|
502
509
|
return this.scene;
|
|
503
510
|
}
|
|
511
|
+
getVertexCount() {
|
|
512
|
+
let total = 0;
|
|
513
|
+
for (let i = 0; i < this.scene.length; i++) {
|
|
514
|
+
total += this.scene[i].getObj().getVertexCount();
|
|
515
|
+
}
|
|
516
|
+
return total;
|
|
517
|
+
}
|
|
504
518
|
getSceneObjects() {
|
|
505
519
|
return this.scene.map((item) => item.getObj());
|
|
506
520
|
}
|
package/dist/types.d.ts
CHANGED
package/dist/utils.d.ts
CHANGED
|
@@ -80,3 +80,4 @@ export declare function distance2d(vector1: Vector2, vector2: Vector2): number;
|
|
|
80
80
|
export declare function distance3d(vector1: Vector3, vector2: Vector3): number;
|
|
81
81
|
export declare function toSceneObjInfo(el: SimulationElement<any>, id?: string): SimSceneObjInfo;
|
|
82
82
|
export declare function toSceneObjInfoMany(el: SimulationElement<any>[], id?: (string | undefined)[]): SimSceneObjInfo[];
|
|
83
|
+
export declare function interpolateColors(colors: Color[], t: number): Color;
|
package/dist/utils.js
CHANGED
|
@@ -260,3 +260,28 @@ export function toSceneObjInfo(el, id) {
|
|
|
260
260
|
export function toSceneObjInfoMany(el, id) {
|
|
261
261
|
return el.map((item, index) => toSceneObjInfo(item, id ? id[index] : undefined));
|
|
262
262
|
}
|
|
263
|
+
export function interpolateColors(colors, t) {
|
|
264
|
+
t = Math.min(1, Math.max(0, t));
|
|
265
|
+
if (colors.length === 0)
|
|
266
|
+
return color();
|
|
267
|
+
if (colors.length === 1)
|
|
268
|
+
return colors[0];
|
|
269
|
+
const colorInterval = 1 / colors.length;
|
|
270
|
+
let index = Math.floor(t / colorInterval);
|
|
271
|
+
if (index >= colors.length)
|
|
272
|
+
index = colors.length - 1;
|
|
273
|
+
const from = index === colors.length - 1 ? colors[index - 1] : colors[index];
|
|
274
|
+
const to = index === colors.length - 1 ? colors[index] : colors[index + 1];
|
|
275
|
+
const diff = to.diff(from);
|
|
276
|
+
const scale = t / (colorInterval * colors.length);
|
|
277
|
+
diff.r *= scale;
|
|
278
|
+
diff.g *= scale;
|
|
279
|
+
diff.b *= scale;
|
|
280
|
+
diff.a *= scale;
|
|
281
|
+
const res = from.clone();
|
|
282
|
+
res.r += diff.r;
|
|
283
|
+
res.g += diff.g;
|
|
284
|
+
res.b += diff.b;
|
|
285
|
+
res.a += diff.a;
|
|
286
|
+
return res;
|
|
287
|
+
}
|