simulationjsv2 0.6.0 → 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,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;
@@ -13,19 +13,17 @@ export declare class VertexCache {
13
13
  getVertexCount(): number;
14
14
  }
15
15
  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;
16
+ export declare const updateWorldProjectionMatrix: (worldProjMat: Mat4, projMat: Mat4) => void;
17
17
  export declare const updateOrthoProjectionMatrix: (mat: Mat4, screenSize: [number, number]) => Float32Array;
18
18
  export declare const buildDepthTexture: (device: GPUDevice, width: number, height: number) => GPUTexture;
19
19
  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
20
  export declare const removeObjectId: (scene: SimSceneObjInfo[], id: string) => void;
23
21
  export declare class SimSceneObjInfo {
24
22
  private obj;
25
23
  private id;
26
24
  private lifetime;
27
25
  private currentLife;
28
- constructor(obj: AnySimulationElement, id?: string);
26
+ constructor(obj: SimulationElement3d, id?: string);
29
27
  /**
30
28
  * @param lifetime - ms
31
29
  */
@@ -36,7 +34,7 @@ export declare class SimSceneObjInfo {
36
34
  * @param amount - ms
37
35
  */
38
36
  traverseLife(amount: number): void;
39
- getObj(): AnySimulationElement;
37
+ getObj(): SimulationElement3d;
40
38
  getId(): string | null;
41
39
  }
42
40
  declare class Logger {
@@ -64,5 +62,8 @@ export declare function rotateMat4(mat: Mat4, rotation: Vector3): void;
64
62
  export declare function createPipeline(device: GPUDevice, module: GPUShaderModule, bindGroupLayouts: GPUBindGroupLayout[], presentationFormat: GPUTextureFormat, topology: GPUPrimitiveTopology, vertexParams?: VertexParamInfo[]): GPURenderPipeline;
65
63
  export declare function triangulateWireFrameOrder(len: number): number[];
66
64
  export declare function getTotalVertices(scene: SimSceneObjInfo[]): number;
67
- export declare function wrapVoidPromise(promise: Promise<unknown>): Promise<void>;
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;
68
69
  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;
@@ -27,10 +27,10 @@ export class VertexCache {
27
27
  }
28
28
  }
29
29
  export const updateProjectionMatrix = (mat, aspectRatio, zNear = 1, zFar = 500) => {
30
- const fov = (2 * Math.PI) / 5;
30
+ const fov = Math.PI / 4;
31
31
  return mat4.perspective(fov, aspectRatio, zNear, zFar, mat);
32
32
  };
33
- export const updateWorldProjectionMatrix = (worldProjMat, projMat, camera) => {
33
+ export const updateWorldProjectionMatrix = (worldProjMat, projMat) => {
34
34
  mat4.identity(worldProjMat);
35
35
  const camPos = cloneBuf(camera.getPos());
36
36
  const rotation = camera.getRotation();
@@ -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 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) {
@@ -333,21 +311,32 @@ export function getTotalVertices(scene) {
333
311
  }
334
312
  return total;
335
313
  }
336
- export function wrapVoidPromise(promise) {
337
- return new Promise((resolve) => {
338
- promise.then(() => resolve());
339
- });
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;
340
342
  }
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,10 +1,30 @@
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;
@@ -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
- 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;
63
+ export declare class ShaderGroup extends EmptyElement {
93
64
  private code;
94
65
  private module;
95
66
  private pipeline;
@@ -100,21 +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
  }
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
- }