simulationjsv2 0.10.5 → 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/README.md +4 -0
- 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 -7
- 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 -7
- package/dist/globals.js +7 -12
- package/dist/graphics.d.ts +20 -21
- package/dist/graphics.js +57 -59
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/internalUtils.d.ts +3 -4
- 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 -36
- package/dist/utils.d.ts +3 -4
- package/dist/utils.js +6 -9
- package/package.json +26 -26
- package/pnpm-workspace.yaml +2 -0
- package/dist/pipelineUtil.d.ts +0 -5
- package/dist/pipelineUtil.js +0 -22
- package/dist/shaders.d.ts +0 -37
package/dist/simulation.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { vec3 } from 'wgpu-matrix';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { matrix4, transitionValues, vector2, vector3, webGLAvailable } from './utils.js';
|
|
3
|
+
import { updateProjectionMatrix, updateOrthoProjectionMatrix, updateWorldProjectionMatrix, CachedArray, addToScene, removeSceneObj, removeSceneId } from './internalUtils.js';
|
|
4
4
|
import { Settings } from './settings.js';
|
|
5
|
-
import { MemoBuffer } from './buffers.js';
|
|
6
5
|
import { globalInfo, logger } from './globals.js';
|
|
6
|
+
import { WebGLBackend } from './backends/webgl.js';
|
|
7
|
+
import { WebGPUBackend } from './backends/webgpu.js';
|
|
7
8
|
const simjsFrameRateCss = `.simjs-frame-rate {
|
|
8
9
|
position: absolute;
|
|
9
10
|
top: 0;
|
|
@@ -142,9 +143,13 @@ export class Camera {
|
|
|
142
143
|
}
|
|
143
144
|
}
|
|
144
145
|
export let camera = new Camera(vector3());
|
|
146
|
+
const defaultSimulationOptions = {
|
|
147
|
+
sceneCamera: null,
|
|
148
|
+
showFrameRate: false,
|
|
149
|
+
backendMode: 'webgpu'
|
|
150
|
+
};
|
|
145
151
|
export class Simulation extends Settings {
|
|
146
152
|
canvasRef = null;
|
|
147
|
-
bgColor = new Color(255, 255, 255);
|
|
148
153
|
scene = [];
|
|
149
154
|
fittingElement = false;
|
|
150
155
|
running = true;
|
|
@@ -152,9 +157,9 @@ export class Simulation extends Settings {
|
|
|
152
157
|
resizeEvents;
|
|
153
158
|
frameRateView;
|
|
154
159
|
transparentElements;
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
160
|
+
backend;
|
|
161
|
+
constructor(idOrCanvasRef, options = {}) {
|
|
162
|
+
const { sceneCamera = defaultSimulationOptions.sceneCamera, showFrameRate = defaultSimulationOptions.showFrameRate, backendMode = defaultSimulationOptions.backendMode } = options;
|
|
158
163
|
super();
|
|
159
164
|
if (typeof idOrCanvasRef === 'string') {
|
|
160
165
|
const ref = document.getElementById(idOrCanvasRef);
|
|
@@ -181,8 +186,15 @@ export class Simulation extends Settings {
|
|
|
181
186
|
this.frameRateView = new FrameRateView(showFrameRate);
|
|
182
187
|
this.frameRateView.updateFrameRate(1);
|
|
183
188
|
this.transparentElements = new CachedArray();
|
|
184
|
-
|
|
185
|
-
|
|
189
|
+
if (backendMode === 'webgpu' && 'gpu' in navigator) {
|
|
190
|
+
this.backend = new WebGPUBackend();
|
|
191
|
+
}
|
|
192
|
+
else if (webGLAvailable(this.canvasRef)) {
|
|
193
|
+
this.backend = new WebGLBackend();
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
throw logger.error('WebGL and WebGPU not available');
|
|
197
|
+
}
|
|
186
198
|
}
|
|
187
199
|
handleCanvasResize(parent) {
|
|
188
200
|
if (this.fittingElement) {
|
|
@@ -199,6 +211,9 @@ export class Simulation extends Settings {
|
|
|
199
211
|
onResize(cb) {
|
|
200
212
|
this.resizeEvents.push(cb);
|
|
201
213
|
}
|
|
214
|
+
getBackend() {
|
|
215
|
+
return this.backend;
|
|
216
|
+
}
|
|
202
217
|
getWidth() {
|
|
203
218
|
return this.canvasRef?.width || 0;
|
|
204
219
|
}
|
|
@@ -242,35 +257,23 @@ export class Simulation extends Settings {
|
|
|
242
257
|
(async () => {
|
|
243
258
|
if (this.canvasRef === null)
|
|
244
259
|
return;
|
|
245
|
-
this.initialized = true;
|
|
246
|
-
this.running = true;
|
|
247
|
-
const adapter = await navigator.gpu.requestAdapter();
|
|
248
|
-
if (!adapter)
|
|
249
|
-
throw logger.error('Adapter is null');
|
|
250
|
-
const ctx = this.canvasRef.getContext('webgpu');
|
|
251
|
-
if (!ctx)
|
|
252
|
-
throw logger.error('Context is null');
|
|
253
|
-
const device = await adapter.requestDevice();
|
|
254
|
-
globalInfo.setDevice(device);
|
|
255
260
|
const screenSize = vector2(this.canvasRef.width, this.canvasRef.height);
|
|
256
261
|
camera.setScreenSize(screenSize);
|
|
257
262
|
const canvas = this.canvasRef;
|
|
258
263
|
canvas.width = canvas.clientWidth * devicePixelRatio;
|
|
259
264
|
canvas.height = canvas.clientHeight * devicePixelRatio;
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
});
|
|
266
|
-
this.render(device, ctx, canvas);
|
|
265
|
+
this.initialized = true;
|
|
266
|
+
this.running = true;
|
|
267
|
+
await this.backend.init(this.canvasRef);
|
|
268
|
+
this.backend.initShaders(globalInfo.getToInitShaders());
|
|
269
|
+
this.render(canvas, this.backend);
|
|
267
270
|
})();
|
|
268
271
|
}
|
|
269
272
|
stop() {
|
|
270
273
|
this.running = false;
|
|
271
274
|
}
|
|
272
275
|
setBackground(color) {
|
|
273
|
-
this.
|
|
276
|
+
this.backend.setClearColor(color);
|
|
274
277
|
}
|
|
275
278
|
setDefaultColor(color) {
|
|
276
279
|
globalInfo.setDefaultColor(color);
|
|
@@ -281,15 +284,7 @@ export class Simulation extends Settings {
|
|
|
281
284
|
getScene() {
|
|
282
285
|
return this.scene;
|
|
283
286
|
}
|
|
284
|
-
render(
|
|
285
|
-
const colorAttachment = {
|
|
286
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
287
|
-
// @ts-ignore
|
|
288
|
-
view: undefined, // Assigned later
|
|
289
|
-
clearValue: this.bgColor.toObject(),
|
|
290
|
-
loadOp: 'clear',
|
|
291
|
-
storeOp: 'store'
|
|
292
|
-
};
|
|
287
|
+
render(canvas, backend) {
|
|
293
288
|
const newAspectRatio = canvas.width / canvas.height;
|
|
294
289
|
if (newAspectRatio !== aspectRatio) {
|
|
295
290
|
updateProjectionMatrix(projMat, newAspectRatio);
|
|
@@ -297,23 +292,11 @@ export class Simulation extends Settings {
|
|
|
297
292
|
}
|
|
298
293
|
updateWorldProjectionMatrix(worldProjectionMatrix, projMat);
|
|
299
294
|
updateOrthoProjectionMatrix(orthogonalMatrix, camera.getScreenSize());
|
|
300
|
-
|
|
301
|
-
let depthTexture = buildDepthTexture(device, canvas.width, canvas.height);
|
|
302
|
-
const renderPassDescriptor = {
|
|
303
|
-
colorAttachments: [colorAttachment],
|
|
304
|
-
depthStencilAttachment: {
|
|
305
|
-
view: depthTexture.createView(),
|
|
306
|
-
depthClearValue: 1.0,
|
|
307
|
-
depthLoadOp: 'clear',
|
|
308
|
-
depthStoreOp: 'store'
|
|
309
|
-
}
|
|
310
|
-
};
|
|
295
|
+
backend.renderStart(canvas);
|
|
311
296
|
// sub 10 to start with a reasonable gap between starting time and next frame time
|
|
312
297
|
let prev = Date.now() - 10;
|
|
313
298
|
let prevFps = 0;
|
|
314
299
|
const frame = async () => {
|
|
315
|
-
if (!canvas)
|
|
316
|
-
return;
|
|
317
300
|
requestAnimationFrame(frame);
|
|
318
301
|
if (!this.running)
|
|
319
302
|
return;
|
|
@@ -333,36 +316,23 @@ export class Simulation extends Settings {
|
|
|
333
316
|
aspectRatio = camera.getAspectRatio();
|
|
334
317
|
updateProjectionMatrix(projMat, aspectRatio);
|
|
335
318
|
updateWorldProjectionMatrix(worldProjectionMatrix, projMat);
|
|
336
|
-
|
|
337
|
-
depthTexture = buildDepthTexture(device, screenSize[0], screenSize[1]);
|
|
338
|
-
renderPassDescriptor.depthStencilAttachment.view = depthTexture.createView();
|
|
319
|
+
backend.updateTextures(screenSize);
|
|
339
320
|
}
|
|
340
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
341
|
-
// @ts-ignore
|
|
342
|
-
renderPassDescriptor.colorAttachments[0].view = multisampleTexture.createView();
|
|
343
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
344
|
-
// @ts-ignore
|
|
345
|
-
renderPassDescriptor.colorAttachments[0].resolveTarget = ctx.getCurrentTexture().createView();
|
|
346
321
|
if (camera.hasUpdated()) {
|
|
347
322
|
updateOrthoProjectionMatrix(orthogonalMatrix, camera.getScreenSize());
|
|
348
323
|
updateWorldProjectionMatrix(worldProjectionMatrix, projMat);
|
|
349
324
|
}
|
|
350
|
-
|
|
351
|
-
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
|
|
352
|
-
const [totalVerticesSize, totalIndexSize] = getVertexAndIndexSize(this.scene);
|
|
353
|
-
this.vertexBuffer.setSize(totalVerticesSize * 4);
|
|
354
|
-
this.indexBuffer.setSize(totalIndexSize * 4);
|
|
325
|
+
backend.preRender(this.scene);
|
|
355
326
|
this.transparentElements.reset();
|
|
356
|
-
const [opaqueVertexOffset, opaqueIndexOffset] = this.renderScene(
|
|
357
|
-
this.renderScene(
|
|
327
|
+
const [opaqueVertexOffset, opaqueIndexOffset] = this.renderScene(backend, 0, 0, this.scene, this.scene.length, diff, false);
|
|
328
|
+
this.renderScene(backend, opaqueVertexOffset, opaqueIndexOffset, this.transparentElements.getArray(), this.transparentElements.length, diff, true);
|
|
358
329
|
camera.updateConsumed();
|
|
359
|
-
|
|
360
|
-
device.queue.submit([commandEncoder.finish()]);
|
|
330
|
+
backend.finishRender();
|
|
361
331
|
};
|
|
362
332
|
requestAnimationFrame(frame);
|
|
363
333
|
}
|
|
364
|
-
renderScene(
|
|
365
|
-
let
|
|
334
|
+
renderScene(backend, startVertexCallOffset, startIndexOffset, scene, numElements, diff, transparent) {
|
|
335
|
+
let vertexCallOffset = startVertexCallOffset;
|
|
366
336
|
let indexOffset = startIndexOffset;
|
|
367
337
|
for (let i = 0; i < numElements; i++) {
|
|
368
338
|
const obj = scene[i];
|
|
@@ -372,30 +342,23 @@ export class Simulation extends Settings {
|
|
|
372
342
|
}
|
|
373
343
|
if (obj.hasChildren()) {
|
|
374
344
|
const childObjects = obj.getChildrenInfos();
|
|
375
|
-
const [vertexDiff, indexDiff] = this.renderScene(
|
|
376
|
-
|
|
345
|
+
const [vertexDiff, indexDiff] = this.renderScene(backend, vertexCallOffset, indexOffset, childObjects, childObjects.length, diff, transparent);
|
|
346
|
+
vertexCallOffset += vertexDiff;
|
|
377
347
|
indexOffset += indexDiff;
|
|
378
348
|
}
|
|
379
349
|
if (obj.isEmpty)
|
|
380
350
|
continue;
|
|
381
|
-
const
|
|
351
|
+
const vertexCallBuffer = obj.getVertexCallBuffer();
|
|
382
352
|
const indices = obj.getIndexBuffer();
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
const instances = obj.isInstance ? obj.getNumInstances() : 1;
|
|
390
|
-
const bindGroups = obj.getShader().getBindGroups(obj);
|
|
391
|
-
for (let i = 0; i < bindGroups.length; i++) {
|
|
392
|
-
passEncoder.setBindGroup(i, bindGroups[i]);
|
|
393
|
-
}
|
|
394
|
-
passEncoder.drawIndexed(indices.length, instances);
|
|
395
|
-
vertexOffset += vertices.byteLength;
|
|
353
|
+
backend.draw(obj,
|
|
354
|
+
// vertex
|
|
355
|
+
vertexCallOffset, vertexCallBuffer,
|
|
356
|
+
// index
|
|
357
|
+
indexOffset, indices);
|
|
358
|
+
vertexCallOffset += vertexCallBuffer.byteLength;
|
|
396
359
|
indexOffset += indices.byteLength;
|
|
397
360
|
}
|
|
398
|
-
return [
|
|
361
|
+
return [vertexCallOffset - startVertexCallOffset, indexOffset - startIndexOffset];
|
|
399
362
|
}
|
|
400
363
|
fitElement() {
|
|
401
364
|
if (this.canvasRef === null)
|
package/dist/types.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
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';
|
|
3
6
|
import { CubicBezierCurve2d, SimulationElement3d, SplinePoint2d } from './graphics.js';
|
|
7
|
+
import { SimJSWebGLShader } from './shaders/webgl.js';
|
|
8
|
+
import { SimJSWebGPUShader } from './shaders/webgpu.js';
|
|
4
9
|
import { Color } from './utils.js';
|
|
5
10
|
export type FloatArray = Float32Array | Float64Array;
|
|
6
11
|
export type UintArray = Uint8Array | Uint16Array | Uint32Array;
|
|
@@ -33,21 +38,20 @@ export type Quat = Float32Array & [number, number, number, number];
|
|
|
33
38
|
export type LerpFunc = (n: number) => number;
|
|
34
39
|
export type VertexColorMap = Record<number, Color>;
|
|
35
40
|
export type ElementRotation<T extends Vector2 | Vector3> = T extends Vector2 ? number : T;
|
|
36
|
-
export type
|
|
37
|
-
export interface CubeGeometryParams {
|
|
41
|
+
export type CubeGeometryParams = {
|
|
38
42
|
width: number;
|
|
39
43
|
height: number;
|
|
40
44
|
depth: number;
|
|
41
|
-
}
|
|
42
|
-
export
|
|
45
|
+
};
|
|
46
|
+
export type SquareGeometryParams = {
|
|
43
47
|
width: number;
|
|
44
48
|
height: number;
|
|
45
|
-
}
|
|
46
|
-
export
|
|
49
|
+
};
|
|
50
|
+
export type CircleGeometryParams = {
|
|
47
51
|
radius: number;
|
|
48
52
|
detail: number;
|
|
49
|
-
}
|
|
50
|
-
export
|
|
53
|
+
};
|
|
54
|
+
export type Spline2dGeometryParams = {
|
|
51
55
|
points: SplinePoint2d[];
|
|
52
56
|
detail: number;
|
|
53
57
|
interpolateStart: number;
|
|
@@ -57,63 +61,77 @@ export interface Spline2dGeometryParams {
|
|
|
57
61
|
distance: number;
|
|
58
62
|
vertexInterpolations: number[];
|
|
59
63
|
curveVertexIndices: number[];
|
|
60
|
-
}
|
|
61
|
-
export
|
|
64
|
+
};
|
|
65
|
+
export type LineGeometryParams = {
|
|
62
66
|
pos: Vector3;
|
|
63
67
|
to: Vector3;
|
|
64
68
|
thickness: number;
|
|
65
|
-
}
|
|
66
|
-
export
|
|
69
|
+
};
|
|
70
|
+
export type TraceLinesParams = {
|
|
67
71
|
maxLength: number | null;
|
|
68
|
-
}
|
|
69
|
-
export
|
|
72
|
+
};
|
|
73
|
+
export type PipelineGroup = {
|
|
70
74
|
triangleList: GPURenderPipeline;
|
|
71
75
|
triangleStrip: GPURenderPipeline;
|
|
72
76
|
lineStrip: GPURenderPipeline;
|
|
73
77
|
triangleListTransparent: GPURenderPipeline;
|
|
74
78
|
triangleStripTransparent: GPURenderPipeline;
|
|
75
79
|
lineStripTransparent: GPURenderPipeline;
|
|
76
|
-
}
|
|
77
|
-
export
|
|
80
|
+
};
|
|
81
|
+
export type VertexParamGeneratorInfo = {
|
|
78
82
|
bufferSize: number;
|
|
79
83
|
createBuffer: (x: number, y: number, z: number, color: Color) => number[];
|
|
80
|
-
}
|
|
81
|
-
export
|
|
84
|
+
};
|
|
85
|
+
export type ShaderInfo = {
|
|
82
86
|
pipeline: GPURenderPipeline;
|
|
83
87
|
paramGenerator: VertexParamGeneratorInfo;
|
|
84
88
|
bufferInfo: {
|
|
85
89
|
buffers: GPUBuffer[];
|
|
86
90
|
layout: GPUBindGroupLayout;
|
|
87
91
|
} | null;
|
|
88
|
-
}
|
|
89
|
-
export
|
|
92
|
+
};
|
|
93
|
+
export type VertexParamInfo = {
|
|
90
94
|
format: GPUVertexFormat;
|
|
91
95
|
size: number;
|
|
92
|
-
}
|
|
93
|
-
export
|
|
96
|
+
};
|
|
97
|
+
export type BindGroupEntry = {
|
|
94
98
|
visibility: GPUBindGroupLayoutEntry['visibility'];
|
|
95
99
|
buffer: GPUBindGroupLayoutEntry['buffer'];
|
|
96
|
-
}
|
|
100
|
+
};
|
|
97
101
|
export type ArrayConstructors = Float32ArrayConstructor | Float64ArrayConstructor | Int8ArrayConstructor | Int16ArrayConstructor | Int32ArrayConstructor;
|
|
98
|
-
export
|
|
102
|
+
export type BindGroupValue = {
|
|
99
103
|
value: number[];
|
|
100
104
|
usage: GPUBufferDescriptor['usage'];
|
|
101
105
|
array: ArrayConstructors;
|
|
102
|
-
}
|
|
103
|
-
export
|
|
106
|
+
};
|
|
107
|
+
export type BindGroupInfo = {
|
|
104
108
|
bindings: BindGroupEntry[];
|
|
105
109
|
values: () => BindGroupValue[];
|
|
106
|
-
}
|
|
107
|
-
export
|
|
110
|
+
};
|
|
111
|
+
export type SimulationElementInfo = {
|
|
108
112
|
topology: GPUPrimitiveTopology;
|
|
109
113
|
transparent: boolean;
|
|
110
114
|
cullMode: GPUCullMode;
|
|
111
|
-
}
|
|
112
|
-
export
|
|
115
|
+
};
|
|
116
|
+
export type WebGPUBufferDecleration = {
|
|
113
117
|
usage: GPUBufferDescriptor['usage'];
|
|
114
118
|
defaultSize?: number;
|
|
115
|
-
|
|
116
|
-
|
|
119
|
+
};
|
|
120
|
+
export type WebGLBufferDecleration = {
|
|
121
|
+
target: GLenum;
|
|
122
|
+
usage: GLenum;
|
|
123
|
+
defaultCapacity?: number;
|
|
124
|
+
};
|
|
117
125
|
export type VertexBufferWriter = (element: SimulationElement3d, buffer: Float32Array, vertex: Vector3, vertexIndex: number, offset: number) => void;
|
|
118
|
-
export type
|
|
119
|
-
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,7 +1,6 @@
|
|
|
1
|
-
/// <reference types="@webgpu/types" />
|
|
2
1
|
import { SimulationElement3d, SplinePoint2d } from './graphics.js';
|
|
3
2
|
import { FloatArray, LerpFunc, Mat4, Vector2, Vector2m, Vector3, Vector3m, Vector4 } from './types.js';
|
|
4
|
-
import {
|
|
3
|
+
import { SimJSWebGPUShader } from './shaders/webgpu.js';
|
|
5
4
|
export declare class Color {
|
|
6
5
|
r: number;
|
|
7
6
|
g: number;
|
|
@@ -83,8 +82,8 @@ export declare function distance3d(vector1: Vector3m, vector2: Vector3m): number
|
|
|
83
82
|
export declare function interpolateColors(colors: Color[], t: number): Color;
|
|
84
83
|
export declare function vectorsToVertex(vectors: Vector3[]): Vertex[];
|
|
85
84
|
export declare function cloneVectors(vectors: Vector3[]): Vector3[];
|
|
86
|
-
export declare function createBindGroup(shader:
|
|
87
|
-
export declare function writeUniformWorldMatrix(el: SimulationElement3d): void;
|
|
85
|
+
export declare function createBindGroup(shader: SimJSWebGPUShader, bindGroupIndex: number, buffers: GPUBuffer[]): GPUBindGroup;
|
|
88
86
|
export declare function transform(from: SimulationElement3d, to: SimulationElement3d, t: number, f?: LerpFunc): Promise<void>;
|
|
89
87
|
export declare function defaultColor(): Color;
|
|
88
|
+
export declare function webGLAvailable(canvas: HTMLCanvasElement): boolean;
|
|
90
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,37 +0,0 @@
|
|
|
1
|
-
/// <reference types="@webgpu/types" />
|
|
2
|
-
import { SimulationElement3d } from './graphics.js';
|
|
3
|
-
import { BindGroupGenerator, BufferInfo, BufferWriter, Vector3, VertexBufferWriter, VertexParamInfo } from './types.js';
|
|
4
|
-
export declare const uniformBufferSize: number;
|
|
5
|
-
export declare class Shader {
|
|
6
|
-
private bindGroupLayoutDescriptors;
|
|
7
|
-
private bindGroupLayouts;
|
|
8
|
-
private module;
|
|
9
|
-
private code;
|
|
10
|
-
private fragmentMain;
|
|
11
|
-
private vertexMain;
|
|
12
|
-
private vertexBuffers;
|
|
13
|
-
private bufferLength;
|
|
14
|
-
private bufferWriter;
|
|
15
|
-
private vertexBufferWriter;
|
|
16
|
-
private bindGroupGenerator;
|
|
17
|
-
private buffers;
|
|
18
|
-
private bufferInfos;
|
|
19
|
-
constructor(code: string, descriptors: GPUBindGroupLayoutDescriptor[], vertexParams: VertexParamInfo[], bufferInfos: BufferInfo[], bufferWriter: BufferWriter, bindGroupGenerator: BindGroupGenerator, vertexBufferWriter: VertexBufferWriter, vertexMain?: string, fragmentMain?: string);
|
|
20
|
-
getCode(): string;
|
|
21
|
-
getBufferLength(): number;
|
|
22
|
-
getVertexBuffers(): GPUVertexBufferLayout;
|
|
23
|
-
getBindGroupLayouts(): GPUBindGroupLayout[];
|
|
24
|
-
getBindGroupLayoutDescriptors(): GPUBindGroupLayoutDescriptor[];
|
|
25
|
-
getBufferInfo(): BufferInfo[];
|
|
26
|
-
getBufferWriter(): BufferWriter;
|
|
27
|
-
getVertexBufferWriter(): VertexBufferWriter;
|
|
28
|
-
getBindGroupGenerator(): BindGroupGenerator;
|
|
29
|
-
getModule(): GPUShaderModule;
|
|
30
|
-
getVertexMain(): string;
|
|
31
|
-
getFragmentMain(): string;
|
|
32
|
-
setVertexInfo(element: SimulationElement3d, buffer: Float32Array, vertex: Vector3, vertexIndex: number, offset: number): void;
|
|
33
|
-
writeBuffers(el: SimulationElement3d): void;
|
|
34
|
-
getBindGroups(el: SimulationElement3d): GPUBindGroup[];
|
|
35
|
-
}
|
|
36
|
-
export declare const defaultShader: Shader;
|
|
37
|
-
export declare const vertexColorShader: Shader;
|