simulationjsv2 0.5.2 → 0.7.1

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.
@@ -1,30 +1,29 @@
1
1
  /// <reference types="@webgpu/types" />
2
- import { AnySimulationElement, VertexParamGeneratorInfo, Mat4, Vector2, Vector3, VertexParamInfo } from './types.js';
2
+ import { VertexParamGeneratorInfo, Mat4, Vector2, Vector3, VertexParamInfo } from './types.js';
3
3
  import { Color } from './utils.js';
4
+ import { SimulationElement3d } from './graphics.js';
4
5
  export declare class VertexCache {
5
6
  private vertices;
6
7
  private hasUpdated;
7
8
  constructor();
8
- setCache(vertices: number[]): void;
9
- getCache(): number[];
9
+ setCache(vertices: Float32Array | number[]): void;
10
+ getCache(): Float32Array;
10
11
  updated(): void;
11
12
  shouldUpdate(): boolean;
12
13
  getVertexCount(): number;
13
14
  }
14
- export declare const buildProjectionMatrix: (aspectRatio: number, zNear?: number, zFar?: number) => any;
15
- export declare const getTransformationMatrix: (pos: Vector3, rotation: Vector3, projectionMatrix: mat4) => Float32Array;
16
- export declare const getOrthoMatrix: (screenSize: [number, number]) => Float32Array;
15
+ export declare const updateProjectionMatrix: (mat: Mat4, aspectRatio: number, zNear?: number, zFar?: number) => any;
16
+ export declare const updateWorldProjectionMatrix: (worldProjMat: Mat4, projMat: Mat4) => void;
17
+ export declare const updateOrthoProjectionMatrix: (mat: Mat4, screenSize: [number, number]) => Float32Array;
17
18
  export declare const buildDepthTexture: (device: GPUDevice, width: number, height: number) => GPUTexture;
18
19
  export declare const buildMultisampleTexture: (device: GPUDevice, ctx: GPUCanvasContext, width: number, height: number) => GPUTexture;
19
- export declare const addObject: (scene: SimSceneObjInfo[], el: AnySimulationElement, device: GPUDevice | null, id?: string) => void;
20
- export declare const removeObject: (scene: SimSceneObjInfo[], el: AnySimulationElement) => void;
21
20
  export declare const removeObjectId: (scene: SimSceneObjInfo[], id: string) => void;
22
21
  export declare class SimSceneObjInfo {
23
22
  private obj;
24
23
  private id;
25
24
  private lifetime;
26
25
  private currentLife;
27
- constructor(obj: AnySimulationElement, id?: string);
26
+ constructor(obj: SimulationElement3d, id?: string);
28
27
  /**
29
28
  * @param lifetime - ms
30
29
  */
@@ -35,7 +34,7 @@ export declare class SimSceneObjInfo {
35
34
  * @param amount - ms
36
35
  */
37
36
  traverseLife(amount: number): void;
38
- getObj(): AnySimulationElement;
37
+ getObj(): SimulationElement3d;
39
38
  getId(): string | null;
40
39
  }
41
40
  declare class Logger {
@@ -48,6 +47,7 @@ declare class Logger {
48
47
  }
49
48
  export declare const logger: Logger;
50
49
  export declare function lossyTriangulate<T>(vertices: T[]): (readonly [T, T, T])[];
50
+ export declare function lossyTriangulateStrip<T>(vertices: T[]): T[];
51
51
  declare class BufferGenerator {
52
52
  private instancing;
53
53
  constructor();
@@ -59,7 +59,11 @@ export declare function vector3ToPixelRatio(vec: Vector3): void;
59
59
  export declare function vector2ToPixelRatio(vec: Vector2): void;
60
60
  export declare function matrixFromRotation(rotation: Vector3): Mat4;
61
61
  export declare function rotateMat4(mat: Mat4, rotation: Vector3): void;
62
- export declare function createPipeline(device: GPUDevice, module: GPUShaderModule, bindGroupLayouts: GPUBindGroupLayout[], presentationFormat: GPUTextureFormat, entryPoint: string, topology: GPUPrimitiveTopology, vertexParams?: VertexParamInfo[]): GPURenderPipeline;
62
+ export declare function createPipeline(device: GPUDevice, module: GPUShaderModule, bindGroupLayouts: GPUBindGroupLayout[], presentationFormat: GPUTextureFormat, topology: GPUPrimitiveTopology, vertexParams?: VertexParamInfo[]): GPURenderPipeline;
63
63
  export declare function triangulateWireFrameOrder(len: number): number[];
64
64
  export declare function getTotalVertices(scene: SimSceneObjInfo[]): number;
65
+ export declare function vectorCompAngle(a: number, b: number): number;
66
+ export declare function angleBetween(pos1: Vector3, pos2: Vector3): Vector3;
67
+ export declare function internalTransitionValues(onFrame: (deltaT: number, t: number, total: number) => void, adjustment: () => void, transitionLength: number, func?: (n: number) => number): Promise<void>;
68
+ export declare function posTo2dScreen(pos: Vector3): Vector3;
65
69
  export {};
@@ -1,14 +1,16 @@
1
1
  import { mat4, vec3 } from 'wgpu-matrix';
2
2
  import { BUF_LEN, colorOffset, drawingInstancesOffset, uvOffset, vertexSize } from './constants.js';
3
- import { vector2, vector3 } from './utils.js';
4
- import { Instance, SimulationElement } from './graphics.js';
5
- import { SceneCollection } from './simulation.js';
3
+ import { cloneBuf, transitionValues, vector2, vector3 } from './utils.js';
4
+ import { camera } from './simulation.js';
5
+ import { settings } from './settings.js';
6
6
  export class VertexCache {
7
- vertices = [];
7
+ vertices;
8
8
  hasUpdated = true;
9
- constructor() { }
9
+ constructor() {
10
+ this.vertices = new Float32Array();
11
+ }
10
12
  setCache(vertices) {
11
- this.vertices = vertices;
13
+ this.vertices = Array.isArray(vertices) ? new Float32Array(vertices) : vertices;
12
14
  this.hasUpdated = false;
13
15
  }
14
16
  getCache() {
@@ -24,25 +26,23 @@ export class VertexCache {
24
26
  return this.vertices.length / BUF_LEN;
25
27
  }
26
28
  }
27
- export const buildProjectionMatrix = (aspectRatio, zNear = 1, zFar = 500) => {
28
- const fov = (2 * Math.PI) / 5;
29
- return mat4.perspective(fov, aspectRatio, zNear, zFar);
29
+ export const updateProjectionMatrix = (mat, aspectRatio, zNear = 1, zFar = 500) => {
30
+ const fov = Math.PI / 4;
31
+ return mat4.perspective(fov, aspectRatio, zNear, zFar, mat);
30
32
  };
31
- export const getTransformationMatrix = (pos, rotation, projectionMatrix) => {
32
- const modelViewProjectionMatrix = mat4.create();
33
- const viewMatrix = mat4.identity();
34
- const camPos = vector3();
35
- vec3.clone(pos, camPos);
33
+ export const updateWorldProjectionMatrix = (worldProjMat, projMat) => {
34
+ mat4.identity(worldProjMat);
35
+ const camPos = cloneBuf(camera.getPos());
36
+ const rotation = camera.getRotation();
36
37
  vec3.scale(camPos, -1, camPos);
37
- mat4.rotateZ(viewMatrix, rotation[2], viewMatrix);
38
- mat4.rotateY(viewMatrix, rotation[1], viewMatrix);
39
- mat4.rotateX(viewMatrix, rotation[0], viewMatrix);
40
- mat4.translate(viewMatrix, camPos, viewMatrix);
41
- mat4.multiply(projectionMatrix, viewMatrix, modelViewProjectionMatrix);
42
- return modelViewProjectionMatrix;
38
+ mat4.rotateZ(worldProjMat, rotation[2], worldProjMat);
39
+ mat4.rotateY(worldProjMat, rotation[1], worldProjMat);
40
+ mat4.rotateX(worldProjMat, rotation[0], worldProjMat);
41
+ mat4.translate(worldProjMat, camPos, worldProjMat);
42
+ mat4.multiply(projMat, worldProjMat, worldProjMat);
43
43
  };
44
- export const getOrthoMatrix = (screenSize) => {
45
- return mat4.ortho(0, screenSize[0], 0, screenSize[1], 0, 100);
44
+ export const updateOrthoProjectionMatrix = (mat, screenSize) => {
45
+ return mat4.ortho(0, screenSize[0], 0, screenSize[1], 0, 100, mat);
46
46
  };
47
47
  export const buildDepthTexture = (device, width, height) => {
48
48
  return device.createTexture({
@@ -60,28 +60,6 @@ export const buildMultisampleTexture = (device, ctx, width, height) => {
60
60
  sampleCount: 4
61
61
  });
62
62
  };
63
- export const addObject = (scene, el, device, id) => {
64
- if (el instanceof SimulationElement) {
65
- if (device !== null && (el instanceof Instance || el instanceof SceneCollection)) {
66
- el.setDevice(device);
67
- }
68
- const obj = new SimSceneObjInfo(el, id);
69
- scene.unshift(obj);
70
- }
71
- else {
72
- throw logger.error('Cannot add invalid SimulationElement');
73
- }
74
- };
75
- export const removeObject = (scene, el) => {
76
- if (!(el instanceof SimulationElement))
77
- return;
78
- for (let i = 0; i < scene.length; i++) {
79
- if (scene[i].getObj() === el) {
80
- scene.splice(i, 1);
81
- break;
82
- }
83
- }
84
- };
85
63
  export const removeObjectId = (scene, id) => {
86
64
  for (let i = 0; i < scene.length; i++) {
87
65
  if (scene[i].getId() === id) {
@@ -176,6 +154,25 @@ export function lossyTriangulate(vertices) {
176
154
  }
177
155
  return res;
178
156
  }
157
+ export function lossyTriangulateStrip(vertices) {
158
+ const res = [];
159
+ let upper = vertices.length - 1;
160
+ let lower = 0;
161
+ let onLower = true;
162
+ while (upper > lower) {
163
+ if (onLower) {
164
+ res.push(vertices[lower]);
165
+ lower++;
166
+ }
167
+ else {
168
+ res.push(vertices[upper]);
169
+ upper--;
170
+ }
171
+ onLower = !onLower;
172
+ }
173
+ res.push(vertices[upper]);
174
+ return res;
175
+ }
179
176
  class BufferGenerator {
180
177
  instancing = false;
181
178
  constructor() { }
@@ -191,7 +188,7 @@ class BufferGenerator {
191
188
  }
192
189
  return buf;
193
190
  }
194
- return [x, y, z, 1, ...color.toBuffer(), ...uv, this.instancing ? 1 : 0];
191
+ return [x, y, z, ...color.toBuffer(), ...uv, this.instancing ? 1 : 0];
195
192
  }
196
193
  }
197
194
  export const bufferGenerator = new BufferGenerator();
@@ -216,7 +213,7 @@ export function rotateMat4(mat, rotation) {
216
213
  mat4.rotateY(mat, rotation[1], mat);
217
214
  mat4.rotateX(mat, rotation[0], mat);
218
215
  }
219
- export function createPipeline(device, module, bindGroupLayouts, presentationFormat, entryPoint, topology, vertexParams) {
216
+ export function createPipeline(device, module, bindGroupLayouts, presentationFormat, topology, vertexParams) {
220
217
  let params = [
221
218
  {
222
219
  // position
@@ -263,7 +260,7 @@ export function createPipeline(device, module, bindGroupLayouts, presentationFor
263
260
  }),
264
261
  vertex: {
265
262
  module,
266
- entryPoint,
263
+ entryPoint: 'vertex_main',
267
264
  buffers: [
268
265
  {
269
266
  arrayStride: stride,
@@ -314,3 +311,32 @@ export function getTotalVertices(scene) {
314
311
  }
315
312
  return total;
316
313
  }
314
+ export function vectorCompAngle(a, b) {
315
+ if (a === 0)
316
+ return 0;
317
+ else {
318
+ if (b === 0)
319
+ return 0;
320
+ else
321
+ return Math.atan2(a, b);
322
+ }
323
+ }
324
+ export function angleBetween(pos1, pos2) {
325
+ const diff = vec3.sub(pos1, pos2);
326
+ const angleZ = vectorCompAngle(diff[0], diff[1]);
327
+ const angleY = vectorCompAngle(diff[2], diff[0]);
328
+ const angleX = vectorCompAngle(diff[2], diff[1]);
329
+ return vector3(angleX, angleY, angleZ);
330
+ }
331
+ export function internalTransitionValues(onFrame, adjustment, transitionLength, func) {
332
+ const newAdjustment = () => {
333
+ if (settings.transformAdjustments)
334
+ adjustment();
335
+ };
336
+ return transitionValues(onFrame, newAdjustment, transitionLength, func);
337
+ }
338
+ export function posTo2dScreen(pos) {
339
+ const newPos = cloneBuf(pos);
340
+ newPos[1] = camera.getScreenSize()[1] + newPos[1];
341
+ return newPos;
342
+ }
@@ -0,0 +1,7 @@
1
+ export declare const settings: {
2
+ transformAdjustments: boolean;
3
+ };
4
+ export declare class Settings {
5
+ constructor();
6
+ setTransformAdjustments(value: boolean): void;
7
+ }
@@ -0,0 +1,9 @@
1
+ export const settings = {
2
+ transformAdjustments: true
3
+ };
4
+ export class Settings {
5
+ constructor() { }
6
+ setTransformAdjustments(value) {
7
+ settings.transformAdjustments = value;
8
+ }
9
+ }
@@ -1,10 +1,30 @@
1
1
  /// <reference types="@webgpu/types" />
2
- import { SimulationElement3d } from './graphics.js';
2
+ import { EmptyElement, SimulationElement3d } from './graphics.js';
3
3
  import type { Vector2, Vector3, LerpFunc, AnySimulationElement, VertexParamGeneratorInfo, VertexParamInfo, BindGroupInfo } from './types.js';
4
4
  import { Color } from './utils.js';
5
- import { BlankGeometry } from './geometry.js';
6
5
  import { SimSceneObjInfo } from './internalUtils.js';
7
- export declare class Simulation {
6
+ import { Settings } from './settings.js';
7
+ export declare class Camera {
8
+ private pos;
9
+ private rotation;
10
+ private aspectRatio;
11
+ private updated;
12
+ private screenSize;
13
+ constructor(pos: Vector3, rotation?: Vector3);
14
+ setScreenSize(size: Vector2): void;
15
+ getScreenSize(): Vector2;
16
+ hasUpdated(): boolean;
17
+ updateConsumed(): void;
18
+ move(amount: Vector3, t?: number, f?: LerpFunc): Promise<void>;
19
+ moveTo(pos: Vector3, t?: number, f?: LerpFunc): Promise<void>;
20
+ rotateTo(value: Vector3, t?: number, f?: LerpFunc): Promise<void>;
21
+ rotate(amount: Vector3, t?: number, f?: LerpFunc): Promise<void>;
22
+ getRotation(): Vector3;
23
+ getPos(): Vector3;
24
+ getAspectRatio(): number;
25
+ }
26
+ export declare let camera: Camera;
27
+ export declare class Simulation extends Settings {
8
28
  canvasRef: HTMLCanvasElement | null;
9
29
  private bgColor;
10
30
  private scene;
@@ -12,18 +32,17 @@ export declare class Simulation {
12
32
  private running;
13
33
  private initialized;
14
34
  private frameRateView;
15
- private camera;
16
35
  private device;
17
36
  private pipelines;
18
37
  private renderInfo;
19
38
  private resizeEvents;
20
- constructor(idOrCanvasRef: string | HTMLCanvasElement, camera?: Camera | null, showFrameRate?: boolean);
39
+ constructor(idOrCanvasRef: string | HTMLCanvasElement, sceneCamera?: Camera | null, showFrameRate?: boolean);
21
40
  private handleCanvasResize;
22
41
  onResize(cb: (width: number, height: number) => void): void;
23
42
  getWidth(): number;
24
43
  getHeight(): number;
25
44
  add(el: AnySimulationElement, id?: string): void;
26
- remove(el: AnySimulationElement): void;
45
+ remove(el: SimulationElement3d): void;
27
46
  removeId(id: string): void;
28
47
  /**
29
48
  * @param lifetime - ms
@@ -36,60 +55,12 @@ export declare class Simulation {
36
55
  stop(): void;
37
56
  setBackground(color: Color): void;
38
57
  getScene(): SimSceneObjInfo[];
39
- getSceneObjects(): AnySimulationElement[];
58
+ getSceneObjects(): SimulationElement3d[];
40
59
  private render;
41
60
  private renderScene;
42
61
  fitElement(): void;
43
62
  }
44
- export declare class SceneCollection extends SimulationElement3d {
45
- protected geometry: BlankGeometry;
46
- private name;
47
- private scene;
48
- protected device: GPUDevice | null;
49
- constructor(name?: string);
50
- setWireframe(_: boolean): void;
51
- getName(): string | null;
52
- getScene(): SimSceneObjInfo[];
53
- setDevice(device: GPUDevice): void;
54
- protected propagateDevice(device: GPUDevice): void;
55
- getVertexCount(): number;
56
- getSceneObjects(): AnySimulationElement[];
57
- setSceneObjects(newScene: AnySimulationElement[]): void;
58
- setScene(newScene: SimSceneObjInfo[]): void;
59
- add(el: AnySimulationElement, id?: string): void;
60
- remove(el: AnySimulationElement): void;
61
- removeId(id: string): void;
62
- /**
63
- * @param lifetime - ms
64
- */
65
- setLifetime(el: AnySimulationElement, lifetime: number): void;
66
- empty(): void;
67
- getSceneBuffer(camera: Camera): number[];
68
- getWireframe(camera: Camera): number[];
69
- getTriangles(camera: Camera): number[];
70
- protected updateMatrix(camera: Camera): void;
71
- }
72
- export declare class Camera {
73
- private pos;
74
- private rotation;
75
- private aspectRatio;
76
- private updated;
77
- private screenSize;
78
- constructor(pos: Vector3, rotation?: Vector3);
79
- setScreenSize(size: Vector2): void;
80
- getScreenSize(): Vector2;
81
- hasUpdated(): boolean;
82
- updateConsumed(): void;
83
- move(amount: Vector3, t?: number, f?: LerpFunc): Promise<void>;
84
- moveTo(pos: Vector3, t?: number, f?: LerpFunc): Promise<void>;
85
- rotateTo(value: Vector3, t?: number, f?: LerpFunc): Promise<void>;
86
- rotate(amount: Vector3, t?: number, f?: LerpFunc): Promise<void>;
87
- getRotation(): Vector3;
88
- getPos(): Vector3;
89
- getAspectRatio(): number;
90
- }
91
- export declare class ShaderGroup extends SceneCollection {
92
- protected geometry: BlankGeometry;
63
+ export declare class ShaderGroup extends EmptyElement {
93
64
  private code;
94
65
  private module;
95
66
  private pipeline;
@@ -100,12 +71,11 @@ export declare class ShaderGroup extends SceneCollection {
100
71
  private bindGroup;
101
72
  private valueBuffers;
102
73
  constructor(shaderCode: string, topology: GPUPrimitiveTopology | undefined, vertexParams: VertexParamInfo[], paramGenerator: VertexParamGeneratorInfo, bindGroup?: BindGroupInfo);
103
- protected propagateDevice(device: GPUDevice): void;
74
+ protected onDeviceChange(device: GPUDevice): void;
104
75
  getBindGroupLayout(): GPUBindGroupLayout | null;
105
76
  getPipeline(): GPURenderPipeline | null;
106
- getBindGroupBuffers(): GPUBuffer[] | null;
77
+ getBindGroupBuffers(device: GPUDevice): GPUBuffer[] | null;
107
78
  private createBuffer;
108
- protected updateMatrix(camera: Camera): void;
109
79
  getVertexParamGenerator(): VertexParamGeneratorInfo;
110
80
  hasBindGroup(): boolean;
111
81
  }