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
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { WebGLBackend } from './backends/webgl.js';
|
|
2
|
+
import { WebGPUBackend } from './backends/webgpu.js';
|
|
3
|
+
import { MemoBuffer } from './buffers/buffer.js';
|
|
4
|
+
import { WebGLMemoBuffer } from './buffers/webgl.js';
|
|
5
|
+
import { WebGPUMemoBuffer } from './buffers/webgpu.js';
|
|
2
6
|
import { CubicBezierCurve2d, SimulationElement3d, SplinePoint2d } from './graphics.js';
|
|
7
|
+
import { SimJSWebGLShader } from './shaders/webgl.js';
|
|
8
|
+
import { SimJSWebGPUShader } from './shaders/webgpu.js';
|
|
3
9
|
import { Color } from './utils.js';
|
|
4
10
|
export type FloatArray = Float32Array | Float64Array;
|
|
5
11
|
export type UintArray = Uint8Array | Uint16Array | Uint32Array;
|
|
@@ -32,21 +38,20 @@ export type Quat = Float32Array & [number, number, number, number];
|
|
|
32
38
|
export type LerpFunc = (n: number) => number;
|
|
33
39
|
export type VertexColorMap = Record<number, Color>;
|
|
34
40
|
export type ElementRotation<T extends Vector2 | Vector3> = T extends Vector2 ? number : T;
|
|
35
|
-
export type
|
|
36
|
-
export interface CubeGeometryParams {
|
|
41
|
+
export type CubeGeometryParams = {
|
|
37
42
|
width: number;
|
|
38
43
|
height: number;
|
|
39
44
|
depth: number;
|
|
40
|
-
}
|
|
41
|
-
export
|
|
45
|
+
};
|
|
46
|
+
export type SquareGeometryParams = {
|
|
42
47
|
width: number;
|
|
43
48
|
height: number;
|
|
44
|
-
}
|
|
45
|
-
export
|
|
49
|
+
};
|
|
50
|
+
export type CircleGeometryParams = {
|
|
46
51
|
radius: number;
|
|
47
52
|
detail: number;
|
|
48
|
-
}
|
|
49
|
-
export
|
|
53
|
+
};
|
|
54
|
+
export type Spline2dGeometryParams = {
|
|
50
55
|
points: SplinePoint2d[];
|
|
51
56
|
detail: number;
|
|
52
57
|
interpolateStart: number;
|
|
@@ -56,63 +61,77 @@ export interface Spline2dGeometryParams {
|
|
|
56
61
|
distance: number;
|
|
57
62
|
vertexInterpolations: number[];
|
|
58
63
|
curveVertexIndices: number[];
|
|
59
|
-
}
|
|
60
|
-
export
|
|
64
|
+
};
|
|
65
|
+
export type LineGeometryParams = {
|
|
61
66
|
pos: Vector3;
|
|
62
67
|
to: Vector3;
|
|
63
68
|
thickness: number;
|
|
64
|
-
}
|
|
65
|
-
export
|
|
69
|
+
};
|
|
70
|
+
export type TraceLinesParams = {
|
|
66
71
|
maxLength: number | null;
|
|
67
|
-
}
|
|
68
|
-
export
|
|
72
|
+
};
|
|
73
|
+
export type PipelineGroup = {
|
|
69
74
|
triangleList: GPURenderPipeline;
|
|
70
75
|
triangleStrip: GPURenderPipeline;
|
|
71
76
|
lineStrip: GPURenderPipeline;
|
|
72
77
|
triangleListTransparent: GPURenderPipeline;
|
|
73
78
|
triangleStripTransparent: GPURenderPipeline;
|
|
74
79
|
lineStripTransparent: GPURenderPipeline;
|
|
75
|
-
}
|
|
76
|
-
export
|
|
80
|
+
};
|
|
81
|
+
export type VertexParamGeneratorInfo = {
|
|
77
82
|
bufferSize: number;
|
|
78
83
|
createBuffer: (x: number, y: number, z: number, color: Color) => number[];
|
|
79
|
-
}
|
|
80
|
-
export
|
|
84
|
+
};
|
|
85
|
+
export type ShaderInfo = {
|
|
81
86
|
pipeline: GPURenderPipeline;
|
|
82
87
|
paramGenerator: VertexParamGeneratorInfo;
|
|
83
88
|
bufferInfo: {
|
|
84
89
|
buffers: GPUBuffer[];
|
|
85
90
|
layout: GPUBindGroupLayout;
|
|
86
91
|
} | null;
|
|
87
|
-
}
|
|
88
|
-
export
|
|
92
|
+
};
|
|
93
|
+
export type VertexParamInfo = {
|
|
89
94
|
format: GPUVertexFormat;
|
|
90
95
|
size: number;
|
|
91
|
-
}
|
|
92
|
-
export
|
|
96
|
+
};
|
|
97
|
+
export type BindGroupEntry = {
|
|
93
98
|
visibility: GPUBindGroupLayoutEntry['visibility'];
|
|
94
99
|
buffer: GPUBindGroupLayoutEntry['buffer'];
|
|
95
|
-
}
|
|
100
|
+
};
|
|
96
101
|
export type ArrayConstructors = Float32ArrayConstructor | Float64ArrayConstructor | Int8ArrayConstructor | Int16ArrayConstructor | Int32ArrayConstructor;
|
|
97
|
-
export
|
|
102
|
+
export type BindGroupValue = {
|
|
98
103
|
value: number[];
|
|
99
104
|
usage: GPUBufferDescriptor['usage'];
|
|
100
105
|
array: ArrayConstructors;
|
|
101
|
-
}
|
|
102
|
-
export
|
|
106
|
+
};
|
|
107
|
+
export type BindGroupInfo = {
|
|
103
108
|
bindings: BindGroupEntry[];
|
|
104
109
|
values: () => BindGroupValue[];
|
|
105
|
-
}
|
|
106
|
-
export
|
|
110
|
+
};
|
|
111
|
+
export type SimulationElementInfo = {
|
|
107
112
|
topology: GPUPrimitiveTopology;
|
|
108
113
|
transparent: boolean;
|
|
109
114
|
cullMode: GPUCullMode;
|
|
110
|
-
}
|
|
111
|
-
export
|
|
115
|
+
};
|
|
116
|
+
export type WebGPUBufferDecleration = {
|
|
112
117
|
usage: GPUBufferDescriptor['usage'];
|
|
113
118
|
defaultSize?: number;
|
|
114
|
-
|
|
115
|
-
|
|
119
|
+
};
|
|
120
|
+
export type WebGLBufferDecleration = {
|
|
121
|
+
target: GLenum;
|
|
122
|
+
usage: GLenum;
|
|
123
|
+
defaultCapacity?: number;
|
|
124
|
+
};
|
|
116
125
|
export type VertexBufferWriter = (element: SimulationElement3d, buffer: Float32Array, vertex: Vector3, vertexIndex: number, offset: number) => void;
|
|
117
|
-
export type
|
|
118
|
-
export type
|
|
126
|
+
export type BackendType = 'webgpu' | 'webgl';
|
|
127
|
+
export type BackendSpecificType<T extends BackendType, WebGPUOption, WebGLOption> = T extends 'webgpu' ? WebGPUOption : WebGLOption;
|
|
128
|
+
export type SpecificBackendType<T extends BackendType> = BackendSpecificType<T, WebGPUBackend, WebGLBackend>;
|
|
129
|
+
export type SpecificShaderType<T extends BackendType> = BackendSpecificType<T, SimJSWebGPUShader, SimJSWebGLShader<any>>;
|
|
130
|
+
export type SpecificMemoBufferType<T extends BackendType> = BackendSpecificType<T, WebGPUMemoBuffer, WebGLMemoBuffer>;
|
|
131
|
+
export type GPUBuffers<T extends BackendType | unknown> = {
|
|
132
|
+
gpuVertexCallBuffer: MemoBufferFromBackendType<T>;
|
|
133
|
+
gpuIndexBuffer: MemoBufferFromBackendType<T>;
|
|
134
|
+
};
|
|
135
|
+
export type AnyGPUBuffer = GPUBuffer | WebGLBuffer;
|
|
136
|
+
export type MemoBufferFromBackendType<T extends BackendType | unknown> = T extends BackendType ? SpecificMemoBufferType<T> : MemoBuffer;
|
|
137
|
+
export type BufferFromBackendType<T extends BackendType | unknown> = T extends BackendType ? BackendSpecificType<T, GPUBuffer, WebGLBuffer> : unknown;
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SimulationElement3d, SplinePoint2d } from './graphics.js';
|
|
2
2
|
import { FloatArray, LerpFunc, Mat4, Vector2, Vector2m, Vector3, Vector3m, Vector4 } from './types.js';
|
|
3
|
-
import {
|
|
3
|
+
import { SimJSWebGPUShader } from './shaders/webgpu.js';
|
|
4
4
|
export declare class Color {
|
|
5
5
|
r: number;
|
|
6
6
|
g: number;
|
|
@@ -82,8 +82,8 @@ export declare function distance3d(vector1: Vector3m, vector2: Vector3m): number
|
|
|
82
82
|
export declare function interpolateColors(colors: Color[], t: number): Color;
|
|
83
83
|
export declare function vectorsToVertex(vectors: Vector3[]): Vertex[];
|
|
84
84
|
export declare function cloneVectors(vectors: Vector3[]): Vector3[];
|
|
85
|
-
export declare function createBindGroup(shader:
|
|
86
|
-
export declare function writeUniformWorldMatrix(el: SimulationElement3d): void;
|
|
85
|
+
export declare function createBindGroup(shader: SimJSWebGPUShader, bindGroupIndex: number, buffers: GPUBuffer[]): GPUBindGroup;
|
|
87
86
|
export declare function transform(from: SimulationElement3d, to: SimulationElement3d, t: number, f?: LerpFunc): Promise<void>;
|
|
88
87
|
export declare function defaultColor(): Color;
|
|
88
|
+
export declare function webGLAvailable(canvas: HTMLCanvasElement): boolean;
|
|
89
89
|
export {};
|
package/dist/utils.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { mat4, vec2, vec3, vec4 } from 'wgpu-matrix';
|
|
2
2
|
import { SplinePoint2d } from './graphics.js';
|
|
3
3
|
import { globalInfo } from './globals.js';
|
|
4
|
-
import { orthogonalMatrix, worldProjectionMatrix } from './simulation.js';
|
|
5
|
-
import { worldProjMatOffset } from './constants.js';
|
|
6
4
|
export class Color {
|
|
7
5
|
r; // 0 - 255
|
|
8
6
|
g; // 0 - 255
|
|
@@ -288,7 +286,9 @@ export function cloneVectors(vectors) {
|
|
|
288
286
|
return vectors.map((vec) => cloneBuf(vec));
|
|
289
287
|
}
|
|
290
288
|
export function createBindGroup(shader, bindGroupIndex, buffers) {
|
|
291
|
-
|
|
289
|
+
// TODO - probably change
|
|
290
|
+
const backend = globalInfo.errorGetCanvas().getBackend();
|
|
291
|
+
const device = backend.getDeviceOrError();
|
|
292
292
|
const layout = shader.getBindGroupLayouts()[bindGroupIndex];
|
|
293
293
|
return device.createBindGroup({
|
|
294
294
|
layout: layout,
|
|
@@ -300,12 +300,6 @@ export function createBindGroup(shader, bindGroupIndex, buffers) {
|
|
|
300
300
|
}))
|
|
301
301
|
});
|
|
302
302
|
}
|
|
303
|
-
export function writeUniformWorldMatrix(el) {
|
|
304
|
-
const device = globalInfo.errorGetDevice();
|
|
305
|
-
const uniformBuffer = el.getUniformBuffer();
|
|
306
|
-
const projBuf = el.is3d ? worldProjectionMatrix : orthogonalMatrix;
|
|
307
|
-
device.queue.writeBuffer(uniformBuffer, worldProjMatOffset, projBuf.buffer, projBuf.byteOffset, projBuf.byteLength);
|
|
308
|
-
}
|
|
309
303
|
/// may have unexpected position behavior for nested elements, or elements with a geometry with a set triangle order
|
|
310
304
|
export function transform(from, to, t, f) {
|
|
311
305
|
const canvas = globalInfo.errorGetCanvas();
|
|
@@ -332,3 +326,6 @@ export function transform(from, to, t, f) {
|
|
|
332
326
|
export function defaultColor() {
|
|
333
327
|
return globalInfo.getDefaultColor();
|
|
334
328
|
}
|
|
329
|
+
export function webGLAvailable(canvas) {
|
|
330
|
+
return canvas.getContext('webgl2') !== null;
|
|
331
|
+
}
|
package/package.json
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
2
|
+
"name": "simulationjsv2",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"types": "./dist/index.d.ts",
|
|
6
|
+
"author": "Jackson Otto",
|
|
7
|
+
"description": "A simple graphics library using WebGPU",
|
|
8
|
+
"version": "0.11.0",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"dev": "npx nodemon --watch src -e ts --exec 'npx tsc || exit 1'",
|
|
17
|
+
"test": "vite --port 3000",
|
|
18
|
+
"build": "npx tsc"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"nodemon": "^3.1.11",
|
|
22
|
+
"typescript": "^5.9.3",
|
|
23
|
+
"vite": "^4.5.14"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@webgpu/types": "^0.1.68",
|
|
27
|
+
"wgpu-matrix": "^2.9.1"
|
|
13
28
|
}
|
|
14
|
-
},
|
|
15
|
-
"scripts": {
|
|
16
|
-
"dev": "npx nodemon --watch src -e ts --exec 'npx tsc || exit 1'",
|
|
17
|
-
"test": "vite --port 3000",
|
|
18
|
-
"build": "npx tsc"
|
|
19
|
-
},
|
|
20
|
-
"devDependencies": {
|
|
21
|
-
"nodemon": "^3.0.1",
|
|
22
|
-
"typescript": "^5.1.6",
|
|
23
|
-
"vite": "^4.4.4"
|
|
24
|
-
},
|
|
25
|
-
"dependencies": {
|
|
26
|
-
"@webgpu/types": "^0.1.34",
|
|
27
|
-
"wgpu-matrix": "^2.8.0"
|
|
28
|
-
}
|
|
29
29
|
}
|
package/dist/pipelineUtil.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
/// <reference types="@webgpu/types" />
|
|
2
|
-
import { SimulationElement3d } from './graphics.js';
|
|
3
|
-
import { Shader } from './shaders.js';
|
|
4
|
-
export declare function createBindGroup(shader: Shader, bindGroupIndex: number, buffers: GPUBuffer[]): GPUBindGroup;
|
|
5
|
-
export declare function writeUniformWorldMatrix(el: SimulationElement3d): void;
|
package/dist/pipelineUtil.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { worldProjMatOffset } from './constants.js';
|
|
2
|
-
import { globalInfo } from './globals.js';
|
|
3
|
-
import { orthogonalMatrix, worldProjectionMatrix } from './simulation.js';
|
|
4
|
-
export function createBindGroup(shader, bindGroupIndex, buffers) {
|
|
5
|
-
const device = globalInfo.errorGetDevice();
|
|
6
|
-
const layout = shader.getBindGroupLayouts()[bindGroupIndex];
|
|
7
|
-
return device.createBindGroup({
|
|
8
|
-
layout: layout,
|
|
9
|
-
entries: buffers.map((buffer, index) => ({
|
|
10
|
-
binding: index,
|
|
11
|
-
resource: {
|
|
12
|
-
buffer
|
|
13
|
-
}
|
|
14
|
-
}))
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
export function writeUniformWorldMatrix(el) {
|
|
18
|
-
const device = globalInfo.errorGetDevice();
|
|
19
|
-
const uniformBuffer = el.getUniformBuffer();
|
|
20
|
-
const projBuf = el.is3d ? worldProjectionMatrix : orthogonalMatrix;
|
|
21
|
-
device.queue.writeBuffer(uniformBuffer, worldProjMatOffset, projBuf.buffer, projBuf.byteOffset, projBuf.byteLength);
|
|
22
|
-
}
|
package/dist/shaders.d.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { SimulationElement3d } from './graphics.js';
|
|
2
|
-
import { BindGroupGenerator, BufferInfo, BufferWriter, Vector3, VertexBufferWriter, VertexParamInfo } from './types.js';
|
|
3
|
-
export declare const uniformBufferSize: number;
|
|
4
|
-
export declare class Shader {
|
|
5
|
-
private bindGroupLayoutDescriptors;
|
|
6
|
-
private bindGroupLayouts;
|
|
7
|
-
private module;
|
|
8
|
-
private code;
|
|
9
|
-
private fragmentMain;
|
|
10
|
-
private vertexMain;
|
|
11
|
-
private vertexBuffers;
|
|
12
|
-
private bufferLength;
|
|
13
|
-
private bufferWriter;
|
|
14
|
-
private vertexBufferWriter;
|
|
15
|
-
private bindGroupGenerator;
|
|
16
|
-
private buffers;
|
|
17
|
-
private bufferInfos;
|
|
18
|
-
constructor(code: string, descriptors: GPUBindGroupLayoutDescriptor[], vertexParams: VertexParamInfo[], bufferInfos: BufferInfo[], bufferWriter: BufferWriter, bindGroupGenerator: BindGroupGenerator, vertexBufferWriter: VertexBufferWriter, vertexMain?: string, fragmentMain?: string);
|
|
19
|
-
getCode(): string;
|
|
20
|
-
getBufferLength(): number;
|
|
21
|
-
getVertexBuffers(): GPUVertexBufferLayout;
|
|
22
|
-
getBindGroupLayouts(): GPUBindGroupLayout[];
|
|
23
|
-
getBindGroupLayoutDescriptors(): GPUBindGroupLayoutDescriptor[];
|
|
24
|
-
getBufferInfo(): BufferInfo[];
|
|
25
|
-
getBufferWriter(): BufferWriter;
|
|
26
|
-
getVertexBufferWriter(): VertexBufferWriter;
|
|
27
|
-
getBindGroupGenerator(): BindGroupGenerator;
|
|
28
|
-
getModule(): GPUShaderModule;
|
|
29
|
-
getVertexMain(): string;
|
|
30
|
-
getFragmentMain(): string;
|
|
31
|
-
setVertexInfo(element: SimulationElement3d, buffer: Float32Array, vertex: Vector3, vertexIndex: number, offset: number): void;
|
|
32
|
-
writeBuffers(el: SimulationElement3d): void;
|
|
33
|
-
getBindGroups(el: SimulationElement3d): GPUBindGroup[];
|
|
34
|
-
}
|
|
35
|
-
export declare const defaultShader: Shader;
|
|
36
|
-
export declare const vertexColorShader: Shader;
|