simulationjsv2 0.10.6 → 0.11.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/TODO.md +4 -20
- package/dist/backend.d.ts +38 -0
- package/dist/backend.js +127 -0
- package/dist/backends/backend.d.ts +22 -0
- package/dist/backends/backend.js +21 -0
- package/dist/backends/webgl.d.ts +19 -0
- package/dist/backends/webgl.js +112 -0
- package/dist/backends/webgpu.d.ts +25 -0
- package/dist/backends/webgpu.js +134 -0
- package/dist/buffers/buffer.d.ts +15 -0
- package/dist/buffers/buffer.js +42 -0
- package/dist/buffers/webgl.d.ts +13 -0
- package/dist/buffers/webgl.js +46 -0
- package/dist/buffers/webgpu.d.ts +12 -0
- package/dist/buffers/webgpu.js +40 -0
- package/dist/buffers.d.ts +20 -6
- package/dist/buffers.js +54 -20
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +1 -0
- package/dist/geometry.d.ts +3 -2
- package/dist/geometry.js +8 -4
- package/dist/globals.d.ts +6 -6
- package/dist/globals.js +7 -12
- package/dist/graphics.d.ts +18 -18
- package/dist/graphics.js +57 -59
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/internalUtils.d.ts +2 -2
- package/dist/internalUtils.js +3 -1
- package/dist/shaders/shader.d.ts +18 -0
- package/dist/shaders/shader.js +63 -0
- package/dist/shaders/utils.d.ts +33 -0
- package/dist/shaders/utils.js +25 -0
- package/dist/shaders/webgl.d.ts +74 -0
- package/dist/shaders/webgl.js +242 -0
- package/dist/shaders/webgpu.d.ts +40 -0
- package/dist/{shaders.js → shaders/webgpu.js} +73 -114
- package/dist/simulation.d.ts +11 -5
- package/dist/simulation.js +49 -86
- package/dist/types.d.ts +54 -35
- package/dist/utils.d.ts +3 -3
- package/dist/utils.js +6 -9
- package/package.json +26 -26
- package/dist/pipelineUtil.d.ts +0 -5
- package/dist/pipelineUtil.js +0 -22
- package/dist/shaders.d.ts +0 -36
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { MemoBuffer } from './buffer.js';
|
|
2
|
+
export class WebGPUMemoBuffer extends MemoBuffer {
|
|
3
|
+
device;
|
|
4
|
+
buffer = null;
|
|
5
|
+
usage;
|
|
6
|
+
constructor(device, usage, initCapacity) {
|
|
7
|
+
super('webgpu', initCapacity);
|
|
8
|
+
if (device === null)
|
|
9
|
+
throw new Error('Device is null');
|
|
10
|
+
this.device = device;
|
|
11
|
+
this.usage = usage;
|
|
12
|
+
}
|
|
13
|
+
allocBuffer() {
|
|
14
|
+
if (this.buffer)
|
|
15
|
+
this.buffer.destroy();
|
|
16
|
+
this.buffer = this.device.createBuffer({
|
|
17
|
+
size: this.bufferCapacity,
|
|
18
|
+
usage: this.usage
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
getBuffer() {
|
|
22
|
+
if (!this.buffer)
|
|
23
|
+
this.allocBuffer();
|
|
24
|
+
return this.buffer;
|
|
25
|
+
}
|
|
26
|
+
write(buf, offset = 0) {
|
|
27
|
+
const neededSize = offset + buf.byteLength;
|
|
28
|
+
if (!this.buffer || neededSize > this.bufferCapacity) {
|
|
29
|
+
this.bufferCapacity = neededSize;
|
|
30
|
+
this.allocBuffer();
|
|
31
|
+
}
|
|
32
|
+
this.device.queue.writeBuffer(this.buffer, offset, buf.buffer, buf.byteOffset, buf.byteLength);
|
|
33
|
+
}
|
|
34
|
+
destroy() {
|
|
35
|
+
if (this.buffer) {
|
|
36
|
+
this.buffer.destroy();
|
|
37
|
+
this.buffer = null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
package/dist/buffers.d.ts
CHANGED
|
@@ -1,12 +1,26 @@
|
|
|
1
1
|
import { ArrayTypes } from './types.js';
|
|
2
|
-
export declare class MemoBuffer {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
export declare abstract class MemoBuffer<T> {
|
|
3
|
+
protected buffer: T | null;
|
|
4
|
+
protected bufferCapacity: number;
|
|
5
|
+
constructor(initCapacity: number);
|
|
6
|
+
abstract allocBuffer(): void;
|
|
7
|
+
abstract destroy(): void;
|
|
8
|
+
write(_buf: ArrayTypes, _offset?: number): void;
|
|
9
|
+
getBuffer(): NonNullable<T>;
|
|
10
|
+
private growCapacity;
|
|
11
|
+
ensureCapacity(capacity: number): void;
|
|
12
|
+
setCapacityPrecise(capacity: number): void;
|
|
13
|
+
}
|
|
14
|
+
export declare class WebGPUMemoBuffer extends MemoBuffer<GPUBuffer> {
|
|
15
|
+
protected device: GPUDevice;
|
|
5
16
|
private usage;
|
|
6
|
-
constructor(usage: GPUBufferDescriptor['usage'],
|
|
7
|
-
|
|
17
|
+
constructor(device: GPUDevice, usage: GPUBufferDescriptor['usage'], initCapacity: number);
|
|
18
|
+
allocBuffer(): void;
|
|
8
19
|
getBuffer(): GPUBuffer;
|
|
9
|
-
setSize(size: number): void;
|
|
10
20
|
write(buf: ArrayTypes, offset?: number): void;
|
|
11
21
|
destroy(): void;
|
|
12
22
|
}
|
|
23
|
+
export declare class WebGLMemoBuffer extends MemoBuffer<WebGLBuffer> {
|
|
24
|
+
allocBuffer(): void;
|
|
25
|
+
destroy(): void;
|
|
26
|
+
}
|
package/dist/buffers.js
CHANGED
|
@@ -1,19 +1,55 @@
|
|
|
1
1
|
import { globalInfo } from './globals.js';
|
|
2
2
|
export class MemoBuffer {
|
|
3
3
|
buffer;
|
|
4
|
-
|
|
4
|
+
bufferCapacity;
|
|
5
|
+
constructor(initCapacity) {
|
|
6
|
+
this.bufferCapacity = initCapacity;
|
|
7
|
+
this.buffer = null;
|
|
8
|
+
}
|
|
9
|
+
// cant be abstract because offset should be default param to 0
|
|
10
|
+
// which it wont allow (even though it could)
|
|
11
|
+
write(_buf, _offset = 0) { }
|
|
12
|
+
getBuffer() {
|
|
13
|
+
if (!this.buffer)
|
|
14
|
+
this.allocBuffer();
|
|
15
|
+
return this.buffer;
|
|
16
|
+
}
|
|
17
|
+
growCapacity(current, target) {
|
|
18
|
+
let res = Math.max(1, current);
|
|
19
|
+
while (res < target) {
|
|
20
|
+
res += Math.ceil(res / 2);
|
|
21
|
+
}
|
|
22
|
+
return res;
|
|
23
|
+
}
|
|
24
|
+
ensureCapacity(capacity) {
|
|
25
|
+
this.setCapacityPrecise(this.growCapacity(this.bufferCapacity, capacity));
|
|
26
|
+
}
|
|
27
|
+
setCapacityPrecise(capacity) {
|
|
28
|
+
if (!this.buffer) {
|
|
29
|
+
this.bufferCapacity = capacity;
|
|
30
|
+
this.allocBuffer();
|
|
31
|
+
}
|
|
32
|
+
if (capacity <= this.bufferCapacity)
|
|
33
|
+
return;
|
|
34
|
+
this.bufferCapacity = capacity;
|
|
35
|
+
this.allocBuffer();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export class WebGPUMemoBuffer extends MemoBuffer {
|
|
39
|
+
device;
|
|
5
40
|
usage;
|
|
6
|
-
constructor(usage,
|
|
41
|
+
constructor(device, usage, initCapacity) {
|
|
42
|
+
super(initCapacity);
|
|
43
|
+
if (device === null)
|
|
44
|
+
throw new Error('Device is null');
|
|
45
|
+
this.device = device;
|
|
7
46
|
this.usage = usage;
|
|
8
|
-
this.bufferSize = size;
|
|
9
|
-
this.buffer = null;
|
|
10
47
|
}
|
|
11
48
|
allocBuffer() {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
size: this.bufferSize,
|
|
49
|
+
if (this.buffer)
|
|
50
|
+
this.buffer.destroy();
|
|
51
|
+
this.buffer = this.device.createBuffer({
|
|
52
|
+
size: this.bufferCapacity,
|
|
17
53
|
usage: this.usage
|
|
18
54
|
});
|
|
19
55
|
}
|
|
@@ -22,18 +58,12 @@ export class MemoBuffer {
|
|
|
22
58
|
this.allocBuffer();
|
|
23
59
|
return this.buffer;
|
|
24
60
|
}
|
|
25
|
-
setSize(size) {
|
|
26
|
-
if (!this.buffer)
|
|
27
|
-
this.allocBuffer();
|
|
28
|
-
if (size > this.bufferSize) {
|
|
29
|
-
this.bufferSize = size;
|
|
30
|
-
this.allocBuffer();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
61
|
write(buf, offset = 0) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
62
|
+
// TODO - probably change
|
|
63
|
+
const backend = globalInfo.errorGetCanvas().getBackend();
|
|
64
|
+
const device = backend.getDeviceOrError();
|
|
65
|
+
if (!this.buffer || buf.byteLength > this.bufferCapacity) {
|
|
66
|
+
this.bufferCapacity = buf.byteLength;
|
|
37
67
|
this.allocBuffer();
|
|
38
68
|
}
|
|
39
69
|
device.queue.writeBuffer(this.buffer, offset, buf.buffer, buf.byteOffset, buf.byteLength);
|
|
@@ -42,3 +72,7 @@ export class MemoBuffer {
|
|
|
42
72
|
this.buffer?.destroy();
|
|
43
73
|
}
|
|
44
74
|
}
|
|
75
|
+
export class WebGLMemoBuffer extends MemoBuffer {
|
|
76
|
+
allocBuffer() { }
|
|
77
|
+
destroy() { }
|
|
78
|
+
}
|
package/dist/constants.d.ts
CHANGED
|
@@ -5,3 +5,4 @@ export declare const xAxis: import("./types.js").Vector3;
|
|
|
5
5
|
export declare const yAxis: import("./types.js").Vector3;
|
|
6
6
|
export declare const zAxis: import("./types.js").Vector3;
|
|
7
7
|
export declare const origin0: import("./types.js").Vector3;
|
|
8
|
+
export declare const FLOAT_SIZE = 4;
|
package/dist/constants.js
CHANGED
package/dist/geometry.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { CircleGeometryParams, CubeGeometryParams,
|
|
1
|
+
import { CircleGeometryParams, CubeGeometryParams, Spline2dGeometryParams, SquareGeometryParams, Vector3, LineGeometryParams, TraceLinesParams, LerpFunc } from './types.js';
|
|
2
2
|
import { CubicBezierCurve2d, SplinePoint2d } from './graphics.js';
|
|
3
|
+
export type EmptyParams = object;
|
|
3
4
|
export declare abstract class Geometry<T extends EmptyParams> {
|
|
4
5
|
private subdivision;
|
|
5
6
|
private subdivisionVertexLimit;
|
|
@@ -12,7 +13,7 @@ export declare abstract class Geometry<T extends EmptyParams> {
|
|
|
12
13
|
protected vertices: Vector3[];
|
|
13
14
|
protected topology: 'list' | 'strip';
|
|
14
15
|
constructor(geometryType?: 'list' | 'strip');
|
|
15
|
-
getTopology(): "
|
|
16
|
+
getTopology(): "strip" | "list";
|
|
16
17
|
computeVertices(): void;
|
|
17
18
|
compute(): void;
|
|
18
19
|
triangulate(): void;
|
package/dist/geometry.js
CHANGED
|
@@ -32,7 +32,8 @@ export class Geometry {
|
|
|
32
32
|
outer: for (let i = 0; i < this.subdivision; i++) {
|
|
33
33
|
const initialLength = initialVertices.length;
|
|
34
34
|
for (let j = 0; j < initialLength - 1; j++) {
|
|
35
|
-
if (this.subdivisionVertexLimit &&
|
|
35
|
+
if (this.subdivisionVertexLimit &&
|
|
36
|
+
this.vertices.length >= this.subdivisionVertexLimit)
|
|
36
37
|
break outer;
|
|
37
38
|
const vert = initialVertices[j];
|
|
38
39
|
const nextVert = initialVertices[j + 1];
|
|
@@ -368,14 +369,17 @@ export class Spline2dGeometry extends Geometry {
|
|
|
368
369
|
let atLimit = false;
|
|
369
370
|
if (step * j * sectionRatio + distanceRatio > this.params.interpolateLimit) {
|
|
370
371
|
atLimit = true;
|
|
371
|
-
currentInterpolation =
|
|
372
|
+
currentInterpolation =
|
|
373
|
+
(this.params.interpolateLimit - distanceRatio) / sectionRatio;
|
|
372
374
|
}
|
|
373
|
-
if (currentInterpolation * sectionRatio + distanceRatio <
|
|
375
|
+
if (currentInterpolation * sectionRatio + distanceRatio <
|
|
376
|
+
this.params.interpolateStart) {
|
|
374
377
|
continue;
|
|
375
378
|
}
|
|
376
379
|
if (!interpolationStarted) {
|
|
377
380
|
interpolationStarted = true;
|
|
378
|
-
currentInterpolation =
|
|
381
|
+
currentInterpolation =
|
|
382
|
+
(this.params.interpolateStart - distanceRatio) / sectionRatio;
|
|
379
383
|
j--;
|
|
380
384
|
}
|
|
381
385
|
if (!curveVertexIndexSet) {
|
package/dist/globals.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SimJSShader } from './shaders/shader.js';
|
|
2
|
+
import { SimJSWebGPUShader } from './shaders/webgpu.js';
|
|
2
3
|
import { Simulation } from './simulation.js';
|
|
3
4
|
import { Color } from './utils.js';
|
|
4
5
|
declare class Logger {
|
|
@@ -12,23 +13,22 @@ declare class Logger {
|
|
|
12
13
|
export declare const logger: Logger;
|
|
13
14
|
export declare class GlobalInfo {
|
|
14
15
|
private canvas;
|
|
15
|
-
private device;
|
|
16
16
|
private defaultColor;
|
|
17
|
+
private toInitShaders;
|
|
17
18
|
constructor();
|
|
18
19
|
setDefaultColor(color: Color): void;
|
|
19
20
|
getDefaultColor(): Color;
|
|
20
21
|
setCanvas(canvas: Simulation): void;
|
|
21
22
|
errorGetCanvas(): Simulation;
|
|
22
23
|
getCanvas(): Simulation | null;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
getDevice(): GPUDevice | null;
|
|
24
|
+
addToInitShader(shader: SimJSShader): void;
|
|
25
|
+
getToInitShaders(): SimJSShader[];
|
|
26
26
|
}
|
|
27
27
|
export declare const globalInfo: GlobalInfo;
|
|
28
28
|
export declare class PipelineCache {
|
|
29
29
|
private pipelines;
|
|
30
30
|
constructor();
|
|
31
|
-
getPipeline(device: GPUDevice, info: string, shader:
|
|
31
|
+
getPipeline(device: GPUDevice, info: string, shader: SimJSWebGPUShader): GPURenderPipeline;
|
|
32
32
|
}
|
|
33
33
|
export declare const pipelineCache: PipelineCache;
|
|
34
34
|
export {};
|
package/dist/globals.js
CHANGED
|
@@ -3,7 +3,7 @@ import { color } from './utils.js';
|
|
|
3
3
|
class Logger {
|
|
4
4
|
constructor() { }
|
|
5
5
|
fmt(msg) {
|
|
6
|
-
return `SimJS
|
|
6
|
+
return `(SimJS) ${msg}`;
|
|
7
7
|
}
|
|
8
8
|
log(msg) {
|
|
9
9
|
console.log(this.fmt(msg));
|
|
@@ -21,12 +21,12 @@ class Logger {
|
|
|
21
21
|
export const logger = new Logger();
|
|
22
22
|
export class GlobalInfo {
|
|
23
23
|
canvas;
|
|
24
|
-
device;
|
|
25
24
|
defaultColor;
|
|
25
|
+
toInitShaders;
|
|
26
26
|
constructor() {
|
|
27
27
|
this.canvas = null;
|
|
28
|
-
this.device = null;
|
|
29
28
|
this.defaultColor = null;
|
|
29
|
+
this.toInitShaders = [];
|
|
30
30
|
}
|
|
31
31
|
setDefaultColor(color) {
|
|
32
32
|
this.defaultColor = color;
|
|
@@ -45,16 +45,11 @@ export class GlobalInfo {
|
|
|
45
45
|
getCanvas() {
|
|
46
46
|
return this.canvas;
|
|
47
47
|
}
|
|
48
|
-
|
|
49
|
-
this.
|
|
48
|
+
addToInitShader(shader) {
|
|
49
|
+
this.toInitShaders.push(shader);
|
|
50
50
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
throw logger.error('GPUDevice is null');
|
|
54
|
-
return this.device;
|
|
55
|
-
}
|
|
56
|
-
getDevice() {
|
|
57
|
-
return this.device;
|
|
51
|
+
getToInitShaders() {
|
|
52
|
+
return this.toInitShaders;
|
|
58
53
|
}
|
|
59
54
|
}
|
|
60
55
|
export const globalInfo = new GlobalInfo();
|
package/dist/graphics.d.ts
CHANGED
|
@@ -2,15 +2,15 @@ import type { Vector2, Vector3, LerpFunc, Mat4 } from './types.js';
|
|
|
2
2
|
import { Vertex, Color } from './utils.js';
|
|
3
3
|
import { BlankGeometry, CircleGeometry, CubeGeometry, Geometry, Line2dGeometry, Line3dGeometry, PlaneGeometry, PolygonGeometry, Spline2dGeometry, SquareGeometry, TraceLinesGeometry as TraceLinesGeometry } from './geometry.js';
|
|
4
4
|
import { Float32ArrayCache } from './internalUtils.js';
|
|
5
|
-
import {
|
|
5
|
+
import { SimJSShader } from './shaders/shader.js';
|
|
6
6
|
import { Material } from './materials.js';
|
|
7
|
+
import { MemoBuffer } from './buffers/buffer.js';
|
|
7
8
|
export declare abstract class SimulationElement3d {
|
|
8
9
|
private children;
|
|
9
|
-
private uniformBuffer;
|
|
10
10
|
private prevInfo;
|
|
11
11
|
private pipeline;
|
|
12
12
|
protected id: string | null;
|
|
13
|
-
protected shader:
|
|
13
|
+
protected shader: SimJSShader;
|
|
14
14
|
protected material: Material;
|
|
15
15
|
protected cullMode: GPUCullMode;
|
|
16
16
|
protected parent: SimulationElement3d | null;
|
|
@@ -21,11 +21,15 @@ export declare abstract class SimulationElement3d {
|
|
|
21
21
|
protected vertexCache: Float32ArrayCache;
|
|
22
22
|
protected rotation: Vector3;
|
|
23
23
|
protected modelMatrix: Mat4;
|
|
24
|
+
protected uniformBuffer: MemoBuffer | null;
|
|
24
25
|
isInstance: boolean;
|
|
25
26
|
isInstanced: boolean;
|
|
26
27
|
is3d: boolean;
|
|
27
28
|
isEmpty: boolean;
|
|
28
29
|
constructor(pos: Vector3, rotation: Vector3, color?: Color);
|
|
30
|
+
delete(): void;
|
|
31
|
+
setUniformBuffer(buffer: MemoBuffer): void;
|
|
32
|
+
getUniformBuffer(): MemoBuffer | null;
|
|
29
33
|
emptyShallow(): void;
|
|
30
34
|
empty(): void;
|
|
31
35
|
getId(): string | null;
|
|
@@ -48,19 +52,18 @@ export declare abstract class SimulationElement3d {
|
|
|
48
52
|
setSubdivisionVertexLimit(limit: number): void;
|
|
49
53
|
clearSubdivisionVertexLimit(): void;
|
|
50
54
|
setCenterOffset(offset: Vector3): void;
|
|
51
|
-
getShader():
|
|
52
|
-
setShader(shader:
|
|
55
|
+
getShader(): SimJSShader;
|
|
56
|
+
setShader(shader: SimJSShader): void;
|
|
53
57
|
resetCenterOffset(): void;
|
|
54
58
|
getModelMatrix(): Mat4;
|
|
55
59
|
isTransparent(): boolean;
|
|
56
60
|
getObjectInfo(): string;
|
|
57
|
-
getUniformBuffer(): GPUBuffer;
|
|
58
61
|
getPipeline(): GPURenderPipeline;
|
|
59
62
|
protected mirrorParentTransforms3d(mat: Mat4): void;
|
|
60
63
|
protected updateModelMatrix3d(): void;
|
|
61
64
|
protected mirrorParentTransforms2d(mat: Mat4): void;
|
|
62
65
|
protected updateModelMatrix2d(): void;
|
|
63
|
-
getGeometryTopology(): "
|
|
66
|
+
getGeometryTopology(): "strip" | "list";
|
|
64
67
|
setWireframe(wireframe: boolean): void;
|
|
65
68
|
isWireframe(): boolean;
|
|
66
69
|
getMaterial(): Material;
|
|
@@ -80,8 +83,7 @@ export declare abstract class SimulationElement3d {
|
|
|
80
83
|
getVertexCount(): number;
|
|
81
84
|
getTreeVertexCount(): number;
|
|
82
85
|
getIndexCount(): number;
|
|
83
|
-
|
|
84
|
-
getVertexBuffer(): Float32Array<ArrayBufferLike>;
|
|
86
|
+
getVertexCallBuffer(): Float32Array<ArrayBufferLike>;
|
|
85
87
|
getIndexBuffer(): Uint32Array<ArrayBuffer>;
|
|
86
88
|
}
|
|
87
89
|
export declare class EmptyElement extends SimulationElement3d {
|
|
@@ -207,7 +209,7 @@ export declare class Spline2d extends SimulationElement2d {
|
|
|
207
209
|
private length;
|
|
208
210
|
constructor(pos: Vertex, points: SplinePoint2d[], thickness?: number, detail?: number);
|
|
209
211
|
private setVertexColors;
|
|
210
|
-
|
|
212
|
+
getVertexCallBuffer(): Float32Array<ArrayBufferLike>;
|
|
211
213
|
isTransparent(): boolean;
|
|
212
214
|
private estimateLength;
|
|
213
215
|
getLength(): number;
|
|
@@ -226,19 +228,17 @@ export declare class Instance<T extends SimulationElement3d> extends SimulationE
|
|
|
226
228
|
private matrixBuffer;
|
|
227
229
|
private baseMat;
|
|
228
230
|
private maxInstances;
|
|
229
|
-
private hasMapped;
|
|
230
231
|
isInstance: boolean;
|
|
231
232
|
constructor(obj: T, numInstances: number);
|
|
232
|
-
|
|
233
|
+
setInstanceCount(numInstances: number, forceResizeBuffer?: boolean): void;
|
|
233
234
|
setInstance(instance: number, transformation: Mat4): void;
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
getInstanceBuffer(): GPUBuffer;
|
|
235
|
+
getInstanceMatrices(): Mat4[];
|
|
236
|
+
getInstanceCount(): number;
|
|
237
|
+
getInstanceBuffer(): Float32Array<ArrayBufferLike>;
|
|
238
238
|
getTreeVertexCount(): number;
|
|
239
239
|
getIndexCount(): number;
|
|
240
|
-
getGeometryTopology(): "
|
|
241
|
-
|
|
240
|
+
getGeometryTopology(): "strip" | "list";
|
|
241
|
+
getVertexCallBuffer(): Float32Array<ArrayBufferLike>;
|
|
242
242
|
getIndexBuffer(): Uint32Array<ArrayBuffer>;
|
|
243
243
|
getModelMatrix(): Mat4;
|
|
244
244
|
}
|
package/dist/graphics.js
CHANGED
|
@@ -2,14 +2,12 @@ import { vec3, mat4, vec2 } from 'wgpu-matrix';
|
|
|
2
2
|
import { cloneBuf, vector2, vector3, vector2FromVector3, matrix4, vector3FromVector2, distance2d, interpolateColors } from './utils.js';
|
|
3
3
|
import { BlankGeometry, CircleGeometry, CubeGeometry, Line2dGeometry, Line3dGeometry, PlaneGeometry, PolygonGeometry, Spline2dGeometry, SquareGeometry, TraceLinesGeometry as TraceLinesGeometry } from './geometry.js';
|
|
4
4
|
import { Float32ArrayCache, internalTransitionValues, posTo2dScreen } from './internalUtils.js';
|
|
5
|
-
import { mat4ByteLength
|
|
6
|
-
import { MemoBuffer } from './buffers.js';
|
|
5
|
+
import { mat4ByteLength } from './constants.js';
|
|
7
6
|
import { globalInfo, logger, pipelineCache } from './globals.js';
|
|
8
|
-
import { defaultShader, uniformBufferSize, vertexColorShader } from './shaders.js';
|
|
9
7
|
import { BasicMaterial, VertexColorMaterial } from './materials.js';
|
|
8
|
+
import { getDefaultShaderForBackend, getDefaultVertexColorShaderForBackend } from './shaders/utils.js';
|
|
10
9
|
export class SimulationElement3d {
|
|
11
10
|
children;
|
|
12
|
-
uniformBuffer;
|
|
13
11
|
prevInfo;
|
|
14
12
|
pipeline;
|
|
15
13
|
id;
|
|
@@ -23,6 +21,7 @@ export class SimulationElement3d {
|
|
|
23
21
|
vertexCache;
|
|
24
22
|
rotation;
|
|
25
23
|
modelMatrix;
|
|
24
|
+
uniformBuffer = null;
|
|
26
25
|
isInstance = false;
|
|
27
26
|
isInstanced = false;
|
|
28
27
|
is3d = true;
|
|
@@ -33,16 +32,28 @@ export class SimulationElement3d {
|
|
|
33
32
|
this.vertexCache = new Float32ArrayCache();
|
|
34
33
|
this.wireframe = false;
|
|
35
34
|
this.rotation = cloneBuf(rotation);
|
|
36
|
-
this.uniformBuffer = new MemoBuffer(GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, uniformBufferSize);
|
|
37
35
|
this.children = [];
|
|
38
36
|
this.modelMatrix = matrix4();
|
|
39
37
|
this.parent = null;
|
|
40
38
|
this.pipeline = null;
|
|
41
39
|
this.prevInfo = null;
|
|
42
|
-
this.shader = defaultShader;
|
|
43
40
|
this.material = new BasicMaterial(color);
|
|
44
41
|
this.cullMode = 'none';
|
|
45
42
|
this.id = null;
|
|
43
|
+
const canvas = globalInfo.getCanvas();
|
|
44
|
+
if (!canvas)
|
|
45
|
+
throw logger.error('Canvas is null');
|
|
46
|
+
const backendType = canvas.getBackend().getBackendType();
|
|
47
|
+
this.shader = getDefaultShaderForBackend(backendType);
|
|
48
|
+
}
|
|
49
|
+
delete() {
|
|
50
|
+
this.uniformBuffer?.destroy();
|
|
51
|
+
}
|
|
52
|
+
setUniformBuffer(buffer) {
|
|
53
|
+
this.uniformBuffer = buffer;
|
|
54
|
+
}
|
|
55
|
+
getUniformBuffer() {
|
|
56
|
+
return this.uniformBuffer;
|
|
46
57
|
}
|
|
47
58
|
emptyShallow() {
|
|
48
59
|
this.children = [];
|
|
@@ -113,7 +124,7 @@ export class SimulationElement3d {
|
|
|
113
124
|
this.vertexCache.updated();
|
|
114
125
|
this.geometry.compute();
|
|
115
126
|
}
|
|
116
|
-
/// may have
|
|
127
|
+
/// may have unexpected behavior for 3d shapes
|
|
117
128
|
setSubdivisions(divisions, vertexLimit) {
|
|
118
129
|
this.geometry.setSubdivisions(divisions, vertexLimit);
|
|
119
130
|
this.vertexCache.updated();
|
|
@@ -158,20 +169,18 @@ export class SimulationElement3d {
|
|
|
158
169
|
return this.material.isTransparent();
|
|
159
170
|
}
|
|
160
171
|
getObjectInfo() {
|
|
161
|
-
const topologyString = this.isWireframe()
|
|
172
|
+
const topologyString = this.isWireframe()
|
|
173
|
+
? 'line-strip'
|
|
174
|
+
: 'triangle-' + this.getGeometryTopology();
|
|
162
175
|
return `{ "topology": "${topologyString}", "transparent": ${this.isTransparent()}, "cullMode": "${this.cullMode}" }`;
|
|
163
176
|
}
|
|
164
|
-
getUniformBuffer() {
|
|
165
|
-
const mat = this.getModelMatrix();
|
|
166
|
-
const device = globalInfo.errorGetDevice();
|
|
167
|
-
const buffer = this.uniformBuffer.getBuffer();
|
|
168
|
-
device.queue.writeBuffer(buffer, modelProjMatOffset, mat.buffer);
|
|
169
|
-
return buffer;
|
|
170
|
-
}
|
|
171
177
|
getPipeline() {
|
|
172
|
-
|
|
178
|
+
// TODO - probably change
|
|
179
|
+
const backend = globalInfo.errorGetCanvas().getBackend();
|
|
180
|
+
const device = backend.as('webgpu').getDeviceOrError();
|
|
173
181
|
const objInfo = this.getObjectInfo();
|
|
174
182
|
if (!this.pipeline || !this.prevInfo || this.prevInfo !== objInfo) {
|
|
183
|
+
// @ts-ignore
|
|
175
184
|
this.pipeline = pipelineCache.getPipeline(device, objInfo, this.shader);
|
|
176
185
|
this.prevInfo = objInfo;
|
|
177
186
|
}
|
|
@@ -402,10 +411,7 @@ export class SimulationElement3d {
|
|
|
402
411
|
}
|
|
403
412
|
return indexCount;
|
|
404
413
|
}
|
|
405
|
-
|
|
406
|
-
this.shader.writeBuffers(this);
|
|
407
|
-
}
|
|
408
|
-
getVertexBuffer() {
|
|
414
|
+
getVertexCallBuffer() {
|
|
409
415
|
if (this.vertexCache.shouldUpdate() || this.geometry.hasUpdated()) {
|
|
410
416
|
this.geometry.compute();
|
|
411
417
|
const vertices = this.geometry.getVertices();
|
|
@@ -605,7 +611,6 @@ export class Polygon extends SimulationElement2d {
|
|
|
605
611
|
super(pos, vector3(0, 0, rotation), color);
|
|
606
612
|
const vectors = vertices.map((vert) => vert.getPos());
|
|
607
613
|
const prevColor = this.getColor();
|
|
608
|
-
this.shader = vertexColorShader;
|
|
609
614
|
this.geometry = new PolygonGeometry(vectors);
|
|
610
615
|
this.material = new VertexColorMaterial([]);
|
|
611
616
|
this.material.setColor(prevColor);
|
|
@@ -970,8 +975,9 @@ export class Spline2d extends SimulationElement2d {
|
|
|
970
975
|
this.geometry = new Spline2dGeometry(points, this.thickness, this.detail);
|
|
971
976
|
this.material = new VertexColorMaterial([]);
|
|
972
977
|
this.material.setColor(pos.getColor() ?? globalInfo.getDefaultColor());
|
|
978
|
+
const backend = globalInfo.getCanvas()?.getBackend().getBackendType();
|
|
979
|
+
this.shader = getDefaultVertexColorShaderForBackend(backend);
|
|
973
980
|
this.setVertexColors();
|
|
974
|
-
this.shader = vertexColorShader;
|
|
975
981
|
this.estimateLength();
|
|
976
982
|
}
|
|
977
983
|
setVertexColors() {
|
|
@@ -993,11 +999,11 @@ export class Spline2d extends SimulationElement2d {
|
|
|
993
999
|
}
|
|
994
1000
|
this.material.setVertexColors(colorArray);
|
|
995
1001
|
}
|
|
996
|
-
|
|
1002
|
+
getVertexCallBuffer() {
|
|
997
1003
|
if (this.vertexCache.shouldUpdate()) {
|
|
998
1004
|
this.setVertexColors();
|
|
999
1005
|
}
|
|
1000
|
-
return super.
|
|
1006
|
+
return super.getVertexCallBuffer();
|
|
1001
1007
|
}
|
|
1002
1008
|
isTransparent() {
|
|
1003
1009
|
const curves = this.geometry.getCurves();
|
|
@@ -1104,31 +1110,29 @@ export class Instance extends SimulationElement3d {
|
|
|
1104
1110
|
matrixBuffer;
|
|
1105
1111
|
baseMat;
|
|
1106
1112
|
maxInstances;
|
|
1107
|
-
hasMapped;
|
|
1108
1113
|
isInstance = true;
|
|
1109
1114
|
constructor(obj, numInstances) {
|
|
1110
1115
|
super(vector3(), vector3());
|
|
1111
1116
|
// 32 matrices
|
|
1112
1117
|
this.maxInstances = 32;
|
|
1113
|
-
this.matrixBuffer = new
|
|
1118
|
+
this.matrixBuffer = new Float32Array(this.maxInstances * mat4ByteLength);
|
|
1114
1119
|
obj.isInstanced = true;
|
|
1115
1120
|
this.obj = obj;
|
|
1116
1121
|
this.instanceMatrix = [];
|
|
1117
1122
|
this.is3d = obj.is3d;
|
|
1118
1123
|
this.geometry = new BlankGeometry();
|
|
1119
|
-
this.hasMapped = false;
|
|
1120
1124
|
this.baseMat = matrix4();
|
|
1121
1125
|
for (let i = 0; i < numInstances; i++) {
|
|
1122
1126
|
const clone = cloneBuf(this.baseMat);
|
|
1123
1127
|
this.instanceMatrix.push(clone);
|
|
1124
1128
|
}
|
|
1125
1129
|
}
|
|
1126
|
-
|
|
1130
|
+
setInstanceCount(numInstances, forceResizeBuffer = false) {
|
|
1127
1131
|
if (numInstances < 0)
|
|
1128
1132
|
throw logger.error('Num instances is less than 0');
|
|
1129
1133
|
if (numInstances > this.maxInstances || forceResizeBuffer) {
|
|
1130
1134
|
this.maxInstances = numInstances;
|
|
1131
|
-
this.matrixBuffer
|
|
1135
|
+
this.matrixBuffer = new Float32Array(numInstances * mat4ByteLength);
|
|
1132
1136
|
}
|
|
1133
1137
|
const oldLen = this.instanceMatrix.length;
|
|
1134
1138
|
if (numInstances < oldLen) {
|
|
@@ -1151,38 +1155,31 @@ export class Instance extends SimulationElement3d {
|
|
|
1151
1155
|
if (instance >= this.instanceMatrix.length || instance < 0)
|
|
1152
1156
|
return;
|
|
1153
1157
|
this.instanceMatrix[instance] = transformation;
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
gpuBuffer.unmap();
|
|
1173
|
-
this.hasMapped = true;
|
|
1174
|
-
}
|
|
1175
|
-
getInstances() {
|
|
1158
|
+
for (let i = 0; i < transformation.length; i++) {
|
|
1159
|
+
this.matrixBuffer[i + instance * transformation.length] = transformation[i];
|
|
1160
|
+
}
|
|
1161
|
+
console.log(this.matrixBuffer);
|
|
1162
|
+
}
|
|
1163
|
+
// private mapBuffer() {
|
|
1164
|
+
// const device = globalInfo.getDevice();
|
|
1165
|
+
// if (!device) return;
|
|
1166
|
+
// const minSize = this.maxInstances * mat4ByteLength;
|
|
1167
|
+
// const size = Math.max(minSize, this.instanceMatrix.length);
|
|
1168
|
+
// this.matrixBuffer.setSize(size);
|
|
1169
|
+
// const gpuBuffer = this.matrixBuffer.getBuffer();
|
|
1170
|
+
// const buf = new Float32Array(this.instanceMatrix.map((mat) => [...mat]).flat());
|
|
1171
|
+
// device.queue.writeBuffer(gpuBuffer, 0, buf.buffer, buf.byteOffset, buf.byteLength);
|
|
1172
|
+
// gpuBuffer.unmap();
|
|
1173
|
+
// this.hasMapped = true;
|
|
1174
|
+
// }
|
|
1175
|
+
getInstanceMatrices() {
|
|
1176
1176
|
return this.instanceMatrix;
|
|
1177
1177
|
}
|
|
1178
|
-
|
|
1178
|
+
getInstanceCount() {
|
|
1179
1179
|
return this.instanceMatrix.length;
|
|
1180
1180
|
}
|
|
1181
1181
|
getInstanceBuffer() {
|
|
1182
|
-
|
|
1183
|
-
this.mapBuffer();
|
|
1184
|
-
}
|
|
1185
|
-
return this.matrixBuffer.getBuffer();
|
|
1182
|
+
return this.matrixBuffer;
|
|
1186
1183
|
}
|
|
1187
1184
|
getTreeVertexCount() {
|
|
1188
1185
|
return this.obj.getTreeVertexCount();
|
|
@@ -1193,8 +1190,8 @@ export class Instance extends SimulationElement3d {
|
|
|
1193
1190
|
getGeometryTopology() {
|
|
1194
1191
|
return this.obj.getGeometryTopology();
|
|
1195
1192
|
}
|
|
1196
|
-
|
|
1197
|
-
return this.obj.
|
|
1193
|
+
getVertexCallBuffer() {
|
|
1194
|
+
return this.obj.getVertexCallBuffer();
|
|
1198
1195
|
}
|
|
1199
1196
|
getIndexBuffer() {
|
|
1200
1197
|
return this.obj.getIndexBuffer();
|
|
@@ -1211,7 +1208,8 @@ export class TraceLines2d extends SimulationElement2d {
|
|
|
1211
1208
|
this.material = new VertexColorMaterial([]);
|
|
1212
1209
|
if (color)
|
|
1213
1210
|
this.material.setColor(color);
|
|
1214
|
-
|
|
1211
|
+
const backend = globalInfo.getCanvas()?.getBackend().getBackendType();
|
|
1212
|
+
this.shader = getDefaultShaderForBackend(backend);
|
|
1215
1213
|
}
|
|
1216
1214
|
addPoint(vert, color) {
|
|
1217
1215
|
const newVert = vert.length < 3 ? vector3(vert[0] ?? 0, vert[1] ?? 0, 0) : vert;
|