simulationjsv2 0.6.0 → 0.7.2

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,7 +1,7 @@
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 { Camera } from './simulation.js';
4
+ import { SimulationElement3d } from './graphics.js';
5
5
  export declare class VertexCache {
6
6
  private vertices;
7
7
  private hasUpdated;
@@ -12,20 +12,26 @@ export declare class VertexCache {
12
12
  shouldUpdate(): boolean;
13
13
  getVertexCount(): number;
14
14
  }
15
+ export declare class GlobalInfo {
16
+ private device;
17
+ constructor();
18
+ setDevice(device: GPUDevice): void;
19
+ errorGetDevice(): GPUDevice;
20
+ getDevice(): GPUDevice | null;
21
+ }
22
+ export declare const globalInfo: GlobalInfo;
15
23
  export declare const updateProjectionMatrix: (mat: Mat4, aspectRatio: number, zNear?: number, zFar?: number) => any;
16
- export declare const updateWorldProjectionMatrix: (worldProjMat: Mat4, projMat: Mat4, camera: Camera) => void;
24
+ export declare const updateWorldProjectionMatrix: (worldProjMat: Mat4, projMat: Mat4) => void;
17
25
  export declare const updateOrthoProjectionMatrix: (mat: Mat4, screenSize: [number, number]) => Float32Array;
18
26
  export declare const buildDepthTexture: (device: GPUDevice, width: number, height: number) => GPUTexture;
19
27
  export declare const buildMultisampleTexture: (device: GPUDevice, ctx: GPUCanvasContext, width: number, height: number) => GPUTexture;
20
- export declare const addObject: (scene: SimSceneObjInfo[], el: AnySimulationElement, device: GPUDevice | null, id?: string) => void;
21
- export declare const removeObject: (scene: SimSceneObjInfo[], el: AnySimulationElement) => void;
22
28
  export declare const removeObjectId: (scene: SimSceneObjInfo[], id: string) => void;
23
29
  export declare class SimSceneObjInfo {
24
30
  private obj;
25
31
  private id;
26
32
  private lifetime;
27
33
  private currentLife;
28
- constructor(obj: AnySimulationElement, id?: string);
34
+ constructor(obj: SimulationElement3d, id?: string);
29
35
  /**
30
36
  * @param lifetime - ms
31
37
  */
@@ -36,7 +42,7 @@ export declare class SimSceneObjInfo {
36
42
  * @param amount - ms
37
43
  */
38
44
  traverseLife(amount: number): void;
39
- getObj(): AnySimulationElement;
45
+ getObj(): SimulationElement3d;
40
46
  getId(): string | null;
41
47
  }
42
48
  declare class Logger {
@@ -59,10 +65,11 @@ declare class BufferGenerator {
59
65
  export declare const bufferGenerator: BufferGenerator;
60
66
  export declare function vector3ToPixelRatio(vec: Vector3): void;
61
67
  export declare function vector2ToPixelRatio(vec: Vector2): void;
62
- export declare function matrixFromRotation(rotation: Vector3): Mat4;
63
- export declare function rotateMat4(mat: Mat4, rotation: Vector3): void;
64
68
  export declare function createPipeline(device: GPUDevice, module: GPUShaderModule, bindGroupLayouts: GPUBindGroupLayout[], presentationFormat: GPUTextureFormat, topology: GPUPrimitiveTopology, vertexParams?: VertexParamInfo[]): GPURenderPipeline;
65
69
  export declare function triangulateWireFrameOrder(len: number): number[];
66
70
  export declare function getTotalVertices(scene: SimSceneObjInfo[]): number;
67
- export declare function wrapVoidPromise(promise: Promise<unknown>): Promise<void>;
71
+ export declare function vectorCompAngle(a: number, b: number): number;
72
+ export declare function angleBetween(pos1: Vector3, pos2: Vector3): Vector3;
73
+ export declare function internalTransitionValues(onFrame: (deltaT: number, t: number, total: number) => void, adjustment: () => void, transitionLength: number, func?: (n: number) => number): Promise<void>;
74
+ export declare function posTo2dScreen(pos: Vector3): Vector3;
68
75
  export {};
@@ -1,8 +1,8 @@
1
1
  import { mat4, vec3 } from 'wgpu-matrix';
2
2
  import { BUF_LEN, colorOffset, drawingInstancesOffset, uvOffset, vertexSize } from './constants.js';
3
- import { cloneBuf, vector2 } from './utils.js';
4
- import { 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
7
  vertices;
8
8
  hasUpdated = true;
@@ -26,11 +26,29 @@ export class VertexCache {
26
26
  return this.vertices.length / BUF_LEN;
27
27
  }
28
28
  }
29
+ export class GlobalInfo {
30
+ device;
31
+ constructor() {
32
+ this.device = null;
33
+ }
34
+ setDevice(device) {
35
+ this.device = device;
36
+ }
37
+ errorGetDevice() {
38
+ if (!this.device)
39
+ throw logger.error('GPUDevice is null');
40
+ return this.device;
41
+ }
42
+ getDevice() {
43
+ return this.device;
44
+ }
45
+ }
46
+ export const globalInfo = new GlobalInfo();
29
47
  export const updateProjectionMatrix = (mat, aspectRatio, zNear = 1, zFar = 500) => {
30
- const fov = (2 * Math.PI) / 5;
48
+ const fov = Math.PI / 4;
31
49
  return mat4.perspective(fov, aspectRatio, zNear, zFar, mat);
32
50
  };
33
- export const updateWorldProjectionMatrix = (worldProjMat, projMat, camera) => {
51
+ export const updateWorldProjectionMatrix = (worldProjMat, projMat) => {
34
52
  mat4.identity(worldProjMat);
35
53
  const camPos = cloneBuf(camera.getPos());
36
54
  const rotation = camera.getRotation();
@@ -60,28 +78,6 @@ export const buildMultisampleTexture = (device, ctx, width, height) => {
60
78
  sampleCount: 4
61
79
  });
62
80
  };
63
- export const addObject = (scene, el, device, id) => {
64
- if (el instanceof SimulationElement) {
65
- if (device !== null && 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
81
  export const removeObjectId = (scene, id) => {
86
82
  for (let i = 0; i < scene.length; i++) {
87
83
  if (scene[i].getId() === id) {
@@ -223,18 +219,6 @@ export function vector2ToPixelRatio(vec) {
223
219
  vec[0] *= devicePixelRatio;
224
220
  vec[1] *= devicePixelRatio;
225
221
  }
226
- export function matrixFromRotation(rotation) {
227
- const rotMatrix = mat4.identity();
228
- mat4.rotateZ(rotMatrix, rotation[2], rotMatrix);
229
- mat4.rotateY(rotMatrix, rotation[1], rotMatrix);
230
- mat4.rotateX(rotMatrix, rotation[0], rotMatrix);
231
- return rotMatrix;
232
- }
233
- export function rotateMat4(mat, rotation) {
234
- mat4.rotateZ(mat, rotation[2], mat);
235
- mat4.rotateY(mat, rotation[1], mat);
236
- mat4.rotateX(mat, rotation[0], mat);
237
- }
238
222
  export function createPipeline(device, module, bindGroupLayouts, presentationFormat, topology, vertexParams) {
239
223
  let params = [
240
224
  {
@@ -333,21 +317,32 @@ export function getTotalVertices(scene) {
333
317
  }
334
318
  return total;
335
319
  }
336
- export function wrapVoidPromise(promise) {
337
- return new Promise((resolve) => {
338
- promise.then(() => resolve());
339
- });
320
+ export function vectorCompAngle(a, b) {
321
+ if (a === 0)
322
+ return 0;
323
+ else {
324
+ if (b === 0)
325
+ return 0;
326
+ else
327
+ return Math.atan2(a, b);
328
+ }
329
+ }
330
+ export function angleBetween(pos1, pos2) {
331
+ const diff = vec3.sub(pos1, pos2);
332
+ const angleZ = vectorCompAngle(diff[0], diff[1]);
333
+ const angleY = vectorCompAngle(diff[2], diff[0]);
334
+ const angleX = vectorCompAngle(diff[2], diff[1]);
335
+ return vector3(angleX, angleY, angleZ);
336
+ }
337
+ export function internalTransitionValues(onFrame, adjustment, transitionLength, func) {
338
+ const newAdjustment = () => {
339
+ if (settings.transformAdjustments)
340
+ adjustment();
341
+ };
342
+ return transitionValues(onFrame, newAdjustment, transitionLength, func);
343
+ }
344
+ export function posTo2dScreen(pos) {
345
+ const newPos = cloneBuf(pos);
346
+ newPos[1] = camera.getScreenSize()[1] + newPos[1];
347
+ return newPos;
340
348
  }
341
- // export function vec3FromQuat(vec: Vector3, quat: Quat, matrix: Mat4) {
342
- // const mat = matrix || matrix4();
343
- // const threshold = 0.9999999;
344
- // mat4.fromQuat(quat, mat);
345
- // vec[1] = clamp(mat[8], -1, 1);
346
- // if (Math.abs(mat[8]) < threshold) {
347
- // vec[0] = Math.atan2(-mat[9], mat[10]);
348
- // vec[2] = Math.atan2(-mat[4], mat[0]);
349
- // } else {
350
- // vec[0] = Math.atan2(-mat[6], mat[5]);
351
- // vec[2] = 0;
352
- // }
353
- // }
@@ -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,29 +1,47 @@
1
1
  /// <reference types="@webgpu/types" />
2
- import { SimulationElement3d } from './graphics.js';
3
- import type { Vector2, Vector3, LerpFunc, AnySimulationElement, VertexParamGeneratorInfo, VertexParamInfo, BindGroupInfo, ElementRotation } from './types.js';
2
+ import { EmptyElement, SimulationElement3d } from './graphics.js';
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;
11
31
  private fittingElement;
12
32
  private running;
13
33
  private initialized;
14
- private frameRateView;
15
- private camera;
16
- private device;
17
34
  private pipelines;
18
35
  private renderInfo;
19
36
  private resizeEvents;
20
- constructor(idOrCanvasRef: string | HTMLCanvasElement, camera?: Camera | null, showFrameRate?: boolean);
37
+ private frameRateView;
38
+ constructor(idOrCanvasRef: string | HTMLCanvasElement, sceneCamera?: Camera | null, showFrameRate?: boolean);
21
39
  private handleCanvasResize;
22
40
  onResize(cb: (width: number, height: number) => void): void;
23
41
  getWidth(): number;
24
42
  getHeight(): number;
25
43
  add(el: AnySimulationElement, id?: string): void;
26
- remove(el: AnySimulationElement): void;
44
+ remove(el: SimulationElement3d): void;
27
45
  removeId(id: string): void;
28
46
  /**
29
47
  * @param lifetime - ms
@@ -32,64 +50,15 @@ export declare class Simulation {
32
50
  private applyCanvasSize;
33
51
  setCanvasSize(width: number, height: number): void;
34
52
  start(): void;
35
- private propagateDevice;
36
53
  stop(): void;
37
54
  setBackground(color: Color): void;
38
55
  getScene(): SimSceneObjInfo[];
39
- getSceneObjects(): AnySimulationElement[];
56
+ getSceneObjects(): SimulationElement3d[];
40
57
  private render;
41
58
  private renderScene;
42
59
  fitElement(): void;
43
60
  }
44
- export declare class SceneCollection extends SimulationElement3d {
45
- protected geometry: BlankGeometry;
46
- private name;
47
- protected scene: SimSceneObjInfo[];
48
- protected device: GPUDevice | null;
49
- constructor(name?: string);
50
- setWireframe(wireframe: 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(): (number | Float32Array)[];
68
- getWireframe(): (number | Float32Array)[];
69
- getTriangles(): (number | Float32Array)[];
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;
61
+ export declare class ShaderGroup extends EmptyElement {
93
62
  private code;
94
63
  private module;
95
64
  private pipeline;
@@ -100,21 +69,11 @@ export declare class ShaderGroup extends SceneCollection {
100
69
  private bindGroup;
101
70
  private valueBuffers;
102
71
  constructor(shaderCode: string, topology: GPUPrimitiveTopology | undefined, vertexParams: VertexParamInfo[], paramGenerator: VertexParamGeneratorInfo, bindGroup?: BindGroupInfo);
103
- protected propagateDevice(device: GPUDevice): void;
72
+ private initPipeline;
104
73
  getBindGroupLayout(): GPUBindGroupLayout | null;
105
74
  getPipeline(): GPURenderPipeline | null;
106
- getBindGroupBuffers(): GPUBuffer[] | null;
75
+ getBindGroupBuffers(device: GPUDevice): GPUBuffer[] | null;
107
76
  private createBuffer;
108
- protected updateMatrix(camera: Camera): void;
109
77
  getVertexParamGenerator(): VertexParamGeneratorInfo;
110
78
  hasBindGroup(): boolean;
111
79
  }
112
- export declare class Group extends SceneCollection {
113
- constructor(name?: string);
114
- move(amount: Vector2 | Vector3, t?: number, f?: LerpFunc): Promise<void>;
115
- moveTo(pos: Vector2 | Vector3, t?: number, f?: LerpFunc): Promise<void>;
116
- rotate(amount: ElementRotation<Vector2 | Vector3>, t?: number, f?: LerpFunc): Promise<void>;
117
- rotateTo(rotation: ElementRotation<Vector2 | Vector3>, t?: number, f?: LerpFunc): Promise<void>;
118
- fill(newColor: Color, t?: number, f?: LerpFunc | undefined): Promise<void>;
119
- private loopElements;
120
- }