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/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);
|
|
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;
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,9 @@ export * from './simulation.js';
|
|
|
2
2
|
export * from './graphics.js';
|
|
3
3
|
export * from './utils.js';
|
|
4
4
|
export * from './types.js';
|
|
5
|
-
export * from './shaders.js';
|
|
5
|
+
export * as webGPUShaderUtils from './shaders/webgpu.js';
|
|
6
6
|
export * from './materials.js';
|
|
7
7
|
export * from './constants.js';
|
|
8
|
+
export * from './backends/backend.js';
|
|
9
|
+
export * from './shaders/shader.js';
|
|
8
10
|
export { vec2, vec3, vec4, mat3, mat4, quat } from 'wgpu-matrix';
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,9 @@ export * from './simulation.js';
|
|
|
2
2
|
export * from './graphics.js';
|
|
3
3
|
export * from './utils.js';
|
|
4
4
|
export * from './types.js';
|
|
5
|
-
export * from './shaders.js';
|
|
5
|
+
export * as webGPUShaderUtils from './shaders/webgpu.js';
|
|
6
6
|
export * from './materials.js';
|
|
7
7
|
export * from './constants.js';
|
|
8
|
+
export * from './backends/backend.js';
|
|
9
|
+
export * from './shaders/shader.js';
|
|
8
10
|
export { vec2, vec3, vec4, mat3, mat4, quat } from 'wgpu-matrix';
|
package/dist/internalUtils.d.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
/// <reference types="@webgpu/types" />
|
|
2
1
|
import { Mat4, Vector3 } from './types.js';
|
|
3
|
-
import {
|
|
2
|
+
import { SimJSWebGPUShader } from './shaders/webgpu.js';
|
|
4
3
|
import { SimulationElement3d } from './graphics.js';
|
|
5
4
|
export declare class Float32ArrayCache {
|
|
6
5
|
private vertices;
|
|
7
6
|
private hasUpdated;
|
|
8
7
|
constructor();
|
|
9
8
|
setCache(vertices: Float32Array | number[]): void;
|
|
10
|
-
getCache(): Float32Array
|
|
9
|
+
getCache(): Float32Array<ArrayBufferLike>;
|
|
11
10
|
updated(): void;
|
|
12
11
|
shouldUpdate(): boolean;
|
|
13
12
|
getVertexCount(stride?: number): number;
|
|
@@ -33,7 +32,7 @@ export declare function triangulateWireFrameOrder(len: number): number[];
|
|
|
33
32
|
export declare function getVertexAndIndexSize(scene: SimulationElement3d[]): readonly [number, number];
|
|
34
33
|
export declare function internalTransitionValues(onFrame: (deltaT: number, t: number, total: number) => void, adjustment: () => void, transitionLength: number, func?: (n: number) => number): Promise<void>;
|
|
35
34
|
export declare function posTo2dScreen(pos: Vector3): Vector3;
|
|
36
|
-
export declare function createPipeline(device: GPUDevice, info: string, shader:
|
|
35
|
+
export declare function createPipeline(device: GPUDevice, info: string, shader: SimJSWebGPUShader): GPURenderPipeline;
|
|
37
36
|
export declare function addToScene(scene: SimulationElement3d[], el: SimulationElement3d, id?: string): void;
|
|
38
37
|
export declare function removeSceneObj(scene: SimulationElement3d[], el: SimulationElement3d): void;
|
|
39
38
|
export declare function removeSceneId(scene: SimulationElement3d[], id: string): void;
|
package/dist/internalUtils.js
CHANGED
|
@@ -88,7 +88,7 @@ export const buildMultisampleTexture = (device, ctx, width, height) => {
|
|
|
88
88
|
sampleCount: 4
|
|
89
89
|
});
|
|
90
90
|
};
|
|
91
|
-
//
|
|
91
|
+
// optimized for speed, depending on orientation of vertices as input, shape may not be preserved
|
|
92
92
|
export function lossyTriangulate(vertices) {
|
|
93
93
|
const res = [];
|
|
94
94
|
let facingRight = true;
|
|
@@ -236,6 +236,7 @@ export function addToScene(scene, el, id) {
|
|
|
236
236
|
export function removeSceneObj(scene, el) {
|
|
237
237
|
for (let i = 0; i < scene.length; i++) {
|
|
238
238
|
if (scene[i] === el) {
|
|
239
|
+
scene[i].delete();
|
|
239
240
|
scene.splice(i, 1);
|
|
240
241
|
break;
|
|
241
242
|
}
|
|
@@ -244,6 +245,7 @@ export function removeSceneObj(scene, el) {
|
|
|
244
245
|
export function removeSceneId(scene, id) {
|
|
245
246
|
for (let i = 0; i < scene.length; i++) {
|
|
246
247
|
if (scene[i].getId() === id) {
|
|
248
|
+
scene[i].delete();
|
|
247
249
|
scene.splice(i, 1);
|
|
248
250
|
break;
|
|
249
251
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { MemoBuffer } from '../buffers/buffer.js';
|
|
2
|
+
import { SimulationElement3d } from '../graphics.js';
|
|
3
|
+
import { BackendType, SpecificShaderType, Vector3, VertexBufferWriter } from '../types.js';
|
|
4
|
+
export declare abstract class SimJSShader {
|
|
5
|
+
protected abstract buffers: MemoBuffer[];
|
|
6
|
+
protected vertexBufferWriter: VertexBufferWriter;
|
|
7
|
+
protected compatableBackend: BackendType;
|
|
8
|
+
protected bufferLength: number;
|
|
9
|
+
constructor(conpatableBackend: BackendType, vertexBufferWriter: VertexBufferWriter);
|
|
10
|
+
abstract writeUniformBuffers(obj: SimulationElement3d): void;
|
|
11
|
+
setVertexInfo(element: SimulationElement3d, buffer: Float32Array, vertex: Vector3, vertexIndex: number, offset: number): void;
|
|
12
|
+
getCompatableBackend(): BackendType;
|
|
13
|
+
compatableWith(backendType: BackendType): boolean;
|
|
14
|
+
getBufferLength(): number;
|
|
15
|
+
as<T extends BackendType>(type: T): SpecificShaderType<T>;
|
|
16
|
+
}
|
|
17
|
+
export declare const defaultVertexBufferWriter: VertexBufferWriter;
|
|
18
|
+
export declare const defaultVertexColorBufferWriter: VertexBufferWriter;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { logger } from '../globals.js';
|
|
2
|
+
export class SimJSShader {
|
|
3
|
+
vertexBufferWriter;
|
|
4
|
+
compatableBackend;
|
|
5
|
+
bufferLength;
|
|
6
|
+
constructor(conpatableBackend, vertexBufferWriter) {
|
|
7
|
+
this.bufferLength = 0;
|
|
8
|
+
this.vertexBufferWriter = vertexBufferWriter;
|
|
9
|
+
this.compatableBackend = conpatableBackend;
|
|
10
|
+
}
|
|
11
|
+
setVertexInfo(element, buffer, vertex, vertexIndex, offset) {
|
|
12
|
+
this.vertexBufferWriter(element, buffer, vertex, vertexIndex, offset);
|
|
13
|
+
}
|
|
14
|
+
getCompatableBackend() {
|
|
15
|
+
return this.compatableBackend;
|
|
16
|
+
}
|
|
17
|
+
compatableWith(backendType) {
|
|
18
|
+
return this.compatableBackend === backendType;
|
|
19
|
+
}
|
|
20
|
+
// returns number of floats (4 bytes)
|
|
21
|
+
getBufferLength() {
|
|
22
|
+
return this.bufferLength;
|
|
23
|
+
}
|
|
24
|
+
as(type) {
|
|
25
|
+
if (this.compatableBackend !== type)
|
|
26
|
+
throw logger.error('Incompatible shader cast');
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export const defaultVertexBufferWriter = (el, buffer, vertex, vertexIndex, offset) => {
|
|
31
|
+
const material = el.getMaterial();
|
|
32
|
+
const colors = material.getVertexColors();
|
|
33
|
+
const vertexColor = colors[vertexIndex] ?? el.getColor();
|
|
34
|
+
// const vertexColor = color(0, 255, 255);
|
|
35
|
+
buffer[offset] = vertex[0];
|
|
36
|
+
buffer[offset + 1] = vertex[1];
|
|
37
|
+
buffer[offset + 2] = vertex[2];
|
|
38
|
+
buffer[offset + 3] = vertexColor.r / 255;
|
|
39
|
+
buffer[offset + 4] = vertexColor.g / 255;
|
|
40
|
+
buffer[offset + 5] = vertexColor.b / 255;
|
|
41
|
+
buffer[offset + 6] = vertexColor.a;
|
|
42
|
+
// TODO possibly change uv for textures
|
|
43
|
+
buffer[offset + 7] = 0;
|
|
44
|
+
buffer[offset + 8] = 0;
|
|
45
|
+
buffer[offset + 9] = el.isInstanced ? 1 : 0;
|
|
46
|
+
};
|
|
47
|
+
export const defaultVertexColorBufferWriter = (el, buffer, vertex, vertexIndex, offset) => {
|
|
48
|
+
const material = el.getMaterial();
|
|
49
|
+
const colors = material.getVertexColors();
|
|
50
|
+
const vertexColor = colors[vertexIndex] ?? el.getColor();
|
|
51
|
+
// const vertexColor = color(0, 255, 255);
|
|
52
|
+
buffer[offset] = vertex[0];
|
|
53
|
+
buffer[offset + 1] = vertex[1];
|
|
54
|
+
buffer[offset + 2] = vertex[2];
|
|
55
|
+
buffer[offset + 3] = vertexColor.r / 255;
|
|
56
|
+
buffer[offset + 4] = vertexColor.g / 255;
|
|
57
|
+
buffer[offset + 5] = vertexColor.b / 255;
|
|
58
|
+
buffer[offset + 6] = vertexColor.a;
|
|
59
|
+
// TODO possibly change uv for textures
|
|
60
|
+
buffer[offset + 7] = 0;
|
|
61
|
+
buffer[offset + 8] = 0;
|
|
62
|
+
buffer[offset + 9] = el.isInstanced ? 1 : 0;
|
|
63
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { BackendType } from '../types.js';
|
|
2
|
+
export declare function getDefaultShaderForBackend(backendType: BackendType): import("./webgpu.js").SimJSWebGPUShader | import("./webgl.js").SimJSWebGLShader<{
|
|
3
|
+
attributeLocations: [{
|
|
4
|
+
readonly position: 3;
|
|
5
|
+
}, {
|
|
6
|
+
readonly color: 4;
|
|
7
|
+
}, {
|
|
8
|
+
readonly uv: 2;
|
|
9
|
+
}, {
|
|
10
|
+
readonly drawingInstance: 1;
|
|
11
|
+
}];
|
|
12
|
+
uniformLocations: [{
|
|
13
|
+
readonly worldProjectionMatrix: 64;
|
|
14
|
+
}, {
|
|
15
|
+
readonly modelProjectionMatrix: 64;
|
|
16
|
+
}];
|
|
17
|
+
}>;
|
|
18
|
+
export declare function getDefaultVertexColorShaderForBackend(backendType: BackendType): import("./webgpu.js").SimJSWebGPUShader | import("./webgl.js").SimJSWebGLShader<{
|
|
19
|
+
attributeLocations: [{
|
|
20
|
+
readonly position: 3;
|
|
21
|
+
}, {
|
|
22
|
+
readonly color: 4;
|
|
23
|
+
}, {
|
|
24
|
+
readonly uv: 2;
|
|
25
|
+
}, {
|
|
26
|
+
readonly drawingInstance: 1;
|
|
27
|
+
}];
|
|
28
|
+
uniformLocations: [{
|
|
29
|
+
readonly worldProjectionMatrix: 64;
|
|
30
|
+
}, {
|
|
31
|
+
readonly modelProjectionMatrix: 64;
|
|
32
|
+
}];
|
|
33
|
+
}>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { logger } from '../globals.js';
|
|
2
|
+
import { defaultWebGLShader } from './webgl.js';
|
|
3
|
+
import { defaultWebGPUShader, defaultWebGPUVertexColorShader } from './webgpu.js';
|
|
4
|
+
export function getDefaultShaderForBackend(backendType) {
|
|
5
|
+
if (backendType === 'webgpu') {
|
|
6
|
+
return defaultWebGPUShader;
|
|
7
|
+
}
|
|
8
|
+
else if (backendType === 'webgl') {
|
|
9
|
+
return defaultWebGLShader;
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
throw logger.error('Unknown backend');
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export function getDefaultVertexColorShaderForBackend(backendType) {
|
|
16
|
+
if (backendType === 'webgpu') {
|
|
17
|
+
return defaultWebGPUVertexColorShader;
|
|
18
|
+
}
|
|
19
|
+
else if (backendType === 'webgl') {
|
|
20
|
+
return defaultWebGLShader;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
throw logger.error('Unknown backend');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { WebGLMemoBuffer } from '../buffers/webgl.js';
|
|
2
|
+
import { SimulationElement3d } from '../graphics.js';
|
|
3
|
+
import { VertexBufferWriter, WebGLBufferDecleration } from '../types.js';
|
|
4
|
+
import { SimJSShader } from './shader.js';
|
|
5
|
+
type WebGLUniformBufferWriter<T> = (programInfo: T, gl: WebGL2RenderingContext, element: SimulationElement3d, buffers: WebGLMemoBuffer[]) => void;
|
|
6
|
+
type ProgramInfoLayout = {
|
|
7
|
+
attributeLocations: {
|
|
8
|
+
[key: string]: number;
|
|
9
|
+
}[];
|
|
10
|
+
uniformLocations: {
|
|
11
|
+
[key: string]: number;
|
|
12
|
+
}[];
|
|
13
|
+
};
|
|
14
|
+
type ToTuple<T> = [keyof T, T[keyof T]];
|
|
15
|
+
type ToTupleList<T> = T extends [infer First, ...infer Rest] ? [ToTuple<First>, ...ToTupleList<Rest>] : [];
|
|
16
|
+
type ProgramInfoLayoutToInfo<T extends ProgramInfoLayout> = {
|
|
17
|
+
program: WebGLProgram;
|
|
18
|
+
attributeLocations: ToTupleList<T['attributeLocations']>;
|
|
19
|
+
uniformLocations: ToTupleList<T['uniformLocations']>;
|
|
20
|
+
};
|
|
21
|
+
export declare class SimJSWebGLShader<T extends ProgramInfoLayout> extends SimJSShader {
|
|
22
|
+
protected buffers: WebGLMemoBuffer[];
|
|
23
|
+
private shaderProgram;
|
|
24
|
+
private shaderProgramInfoLayout;
|
|
25
|
+
private shaderProgramInfo;
|
|
26
|
+
private gl;
|
|
27
|
+
private uniformBufferWriter;
|
|
28
|
+
private getShaderProgramInfoFn;
|
|
29
|
+
private vertexSource;
|
|
30
|
+
private fragmentSource;
|
|
31
|
+
private bufferDeclerations;
|
|
32
|
+
constructor(vertexSource: string, fragmentSource: string, bufferDeclerations: WebGLBufferDecleration[], uniformBufferWriter: WebGLUniformBufferWriter<ProgramInfoLayoutToInfo<T>>, vertexBufferWriter: VertexBufferWriter, getShaderProgramInfoFn: () => T);
|
|
33
|
+
init(gl: WebGL2RenderingContext): void;
|
|
34
|
+
programInfoLayoutToProgramInfo(program: WebGLProgram, gl: WebGL2RenderingContext, layout: T): ProgramInfoLayoutToInfo<T>;
|
|
35
|
+
private initShaderProgram;
|
|
36
|
+
private loadShader;
|
|
37
|
+
getShaderProgram(): WebGLProgram;
|
|
38
|
+
getShaderProgramInfo(): ProgramInfoLayoutToInfo<T>;
|
|
39
|
+
writeUniformBuffers(obj: SimulationElement3d): void;
|
|
40
|
+
writeShaderProgramAttributes(buffer: WebGLMemoBuffer, vertexCallOffset: number, vertexCallBuffer: Float32Array): void;
|
|
41
|
+
}
|
|
42
|
+
export declare const defaultWebGLShader: SimJSWebGLShader<{
|
|
43
|
+
attributeLocations: [{
|
|
44
|
+
readonly position: 3;
|
|
45
|
+
}, {
|
|
46
|
+
readonly color: 4;
|
|
47
|
+
}, {
|
|
48
|
+
readonly uv: 2;
|
|
49
|
+
}, {
|
|
50
|
+
readonly drawingInstance: 1;
|
|
51
|
+
}];
|
|
52
|
+
uniformLocations: [{
|
|
53
|
+
readonly worldProjectionMatrix: 64;
|
|
54
|
+
}, {
|
|
55
|
+
readonly modelProjectionMatrix: 64;
|
|
56
|
+
}];
|
|
57
|
+
}>;
|
|
58
|
+
export declare const defaultWebGLVertexColorShader: SimJSWebGLShader<{
|
|
59
|
+
attributeLocations: [{
|
|
60
|
+
readonly position: 3;
|
|
61
|
+
}, {
|
|
62
|
+
readonly color: 4;
|
|
63
|
+
}, {
|
|
64
|
+
readonly uv: 2;
|
|
65
|
+
}, {
|
|
66
|
+
readonly drawingInstance: 1;
|
|
67
|
+
}];
|
|
68
|
+
uniformLocations: [{
|
|
69
|
+
readonly worldProjectionMatrix: 64;
|
|
70
|
+
}, {
|
|
71
|
+
readonly modelProjectionMatrix: 64;
|
|
72
|
+
}];
|
|
73
|
+
}>;
|
|
74
|
+
export {};
|