simulationjsv2 0.4.1 → 0.4.3

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/dist/geometry.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { mat4, vec2, vec3 } from 'wgpu-matrix';
2
- import { cloneBuf, matrix4, vector2, vector2FromVector3, vector3, vector3FromVector2, vertex } from './utils.js';
2
+ import { cloneBuf, interpolateColors, matrix4, vector2, vector2FromVector3, vector3, vector3FromVector2, vertex } from './utils.js';
3
3
  import { CubicBezierCurve2d } from './graphics.js';
4
4
  import { BUF_LEN } from './constants.js';
5
- import { bufferGenerator, interpolateColors, lossyTriangulate, triangulateWireFrameOrder } from './internalUtils.js';
5
+ import { bufferGenerator, lossyTriangulate, triangulateWireFrameOrder } from './internalUtils.js';
6
6
  export class Geometry {
7
7
  vertices;
8
8
  matrix;
@@ -58,7 +58,6 @@ declare class BufferGenerator {
58
58
  export declare const bufferGenerator: BufferGenerator;
59
59
  export declare function vector3ToPixelRatio(vec: Vector3): void;
60
60
  export declare function vector2ToPixelRatio(vec: Vector2): void;
61
- export declare function interpolateColors(colors: Color[], t: number): Color;
62
61
  export declare function matrixFromRotation(rotation: Vector3): Mat4;
63
62
  export declare function rotateMat4(mat: Mat4, rotation: Vector3): void;
64
63
  export declare function createPipeline(device: GPUDevice, module: GPUShaderModule, bindGroupLayout: GPUBindGroupLayout, presentationFormat: GPUTextureFormat, entryPoint: string, topology: GPUPrimitiveTopology): GPURenderPipeline;
@@ -1,6 +1,6 @@
1
1
  import { mat4, vec3 } from 'wgpu-matrix';
2
2
  import { BUF_LEN, colorOffset, drawingInstancesOffset, uvOffset, vertexSize } from './constants.js';
3
- import { color, vector2, vector3 } from './utils.js';
3
+ import { vector2, vector3 } from './utils.js';
4
4
  import { SimulationElement } from './graphics.js';
5
5
  export class VertexCache {
6
6
  vertices = [];
@@ -192,31 +192,6 @@ export function vector2ToPixelRatio(vec) {
192
192
  vec[0] *= devicePixelRatio;
193
193
  vec[1] *= devicePixelRatio;
194
194
  }
195
- export function interpolateColors(colors, t) {
196
- t = Math.min(1, Math.max(0, t));
197
- if (colors.length === 0)
198
- return color();
199
- if (colors.length === 1)
200
- return colors[0];
201
- const colorInterval = 1 / colors.length;
202
- let index = Math.floor(t / colorInterval);
203
- if (index >= colors.length)
204
- index = colors.length - 1;
205
- const from = index === colors.length - 1 ? colors[index - 1] : colors[index];
206
- const to = index === colors.length - 1 ? colors[index] : colors[index + 1];
207
- const diff = to.diff(from);
208
- const scale = t / (colorInterval * colors.length);
209
- diff.r *= scale;
210
- diff.g *= scale;
211
- diff.b *= scale;
212
- diff.a *= scale;
213
- const res = from.clone();
214
- res.r += diff.r;
215
- res.g += diff.g;
216
- res.b += diff.b;
217
- res.a += diff.a;
218
- return res;
219
- }
220
195
  export function matrixFromRotation(rotation) {
221
196
  let rotMatrix = mat4.identity();
222
197
  mat4.rotateZ(rotMatrix, rotation[2], rotMatrix);
@@ -27,6 +27,8 @@ export declare class Simulation {
27
27
  start(): void;
28
28
  stop(): void;
29
29
  setBackground(color: Color): void;
30
+ getScene(): SimSceneObjInfo[];
31
+ getSceneObjects(): SimulationElement<Vector3 | Vector2>[];
30
32
  private propagateDevice;
31
33
  render(device: GPUDevice, ctx: GPUCanvasContext): void;
32
34
  private renderScene;
@@ -42,6 +44,7 @@ export declare class SceneCollection extends SimulationElement3d {
42
44
  setWireframe(_: boolean): void;
43
45
  getName(): string;
44
46
  getScene(): SimSceneObjInfo[];
47
+ getSceneObjects(): SimulationElement<Vector3 | Vector2>[];
45
48
  setSceneObjects(newScene: SimulationElement<any>[]): void;
46
49
  setScene(newScene: SimSceneObjInfo[]): void;
47
50
  add(el: SimulationElement<any>): void;
@@ -1,9 +1,9 @@
1
1
  import { vec3 } from 'wgpu-matrix';
2
2
  import { SimulationElement3d } from './graphics.js';
3
3
  import { BUF_LEN } from './constants.js';
4
- import { Color, transitionValues, vector2, vector3 } from './utils.js';
4
+ import { Color, toSceneObjInfoMany, transitionValues, vector2, vector3 } from './utils.js';
5
5
  import { BlankGeometry } from './geometry.js';
6
- import { SimSceneObjInfo, addObject, buildDepthTexture, buildMultisampleTexture, buildProjectionMatrix, createPipeline, getOrthoMatrix, getTotalVertices, getTransformationMatrix, logger, removeObject, removeObjectId } from './internalUtils.js';
6
+ import { addObject, buildDepthTexture, buildMultisampleTexture, buildProjectionMatrix, createPipeline, getOrthoMatrix, getTotalVertices, getTransformationMatrix, logger, removeObject, removeObjectId } from './internalUtils.js';
7
7
  const shader = `
8
8
  struct Uniforms {
9
9
  modelViewProjectionMatrix : mat4x4<f32>,
@@ -210,6 +210,12 @@ export class Simulation {
210
210
  setBackground(color) {
211
211
  this.bgColor = color;
212
212
  }
213
+ getScene() {
214
+ return this.scene;
215
+ }
216
+ getSceneObjects() {
217
+ return this.scene.map((item) => item.getObj());
218
+ }
213
219
  propagateDevice(device) {
214
220
  for (let i = 0; i < this.scene.length; i++) {
215
221
  const obj = this.scene[i].getObj();
@@ -495,8 +501,11 @@ export class SceneCollection extends SimulationElement3d {
495
501
  getScene() {
496
502
  return this.scene;
497
503
  }
504
+ getSceneObjects() {
505
+ return this.scene.map((item) => item.getObj());
506
+ }
498
507
  setSceneObjects(newScene) {
499
- this.scene = newScene.map((item) => new SimSceneObjInfo(item));
508
+ this.scene = toSceneObjInfoMany(newScene);
500
509
  }
501
510
  setScene(newScene) {
502
511
  this.scene = newScene;
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- import { SplinePoint2d } from './graphics.js';
1
+ import { SimulationElement, SplinePoint2d } from './graphics.js';
2
2
  import { FloatArray, Mat4, Shift, Vector2, Vector3, Vector4 } from './types.js';
3
+ import { SimSceneObjInfo } from './internalUtils.js';
3
4
  export declare class Color {
4
5
  r: number;
5
6
  g: number;
@@ -50,6 +51,12 @@ export declare function linearStep(t: number): number;
50
51
  export declare function easeInOutExpo(t: number): number;
51
52
  export declare function easeInOutQuart(t: number): number;
52
53
  export declare function easeInOutQuad(t: number): number;
54
+ export declare function easeInQuad(x: number): number;
55
+ export declare function easeOutQuad(x: number): number;
56
+ export declare function easeInQuart(x: number): number;
57
+ export declare function easeOutQuart(x: number): number;
58
+ export declare function easeInExpo(x: number): number;
59
+ export declare function easeOutExpo(x: number): number;
53
60
  export declare function cloneBuf<T extends FloatArray>(buf: T): T;
54
61
  export declare function vector4(x?: number, y?: number, z?: number, w?: number): Vector4;
55
62
  export declare function vector3(x?: number, y?: number, z?: number): Vector3;
@@ -71,3 +78,6 @@ export declare function continuousSplinePoint2d(end: Vertex, control: Vector2, d
71
78
  export declare function waitFor(t: number): Promise<unknown>;
72
79
  export declare function distance2d(vector1: Vector2, vector2: Vector2): number;
73
80
  export declare function distance3d(vector1: Vector3, vector2: Vector3): number;
81
+ export declare function toSceneObjInfo(el: SimulationElement<any>, id?: string): SimSceneObjInfo;
82
+ export declare function toSceneObjInfoMany(el: SimulationElement<any>[], id?: (string | undefined)[]): SimSceneObjInfo[];
83
+ export declare function interpolateColors(colors: Color[], t: number): Color;
package/dist/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { mat4, vec2, vec3, vec4 } from 'wgpu-matrix';
2
2
  import { SplinePoint2d } from './graphics.js';
3
- import { bufferGenerator } from './internalUtils.js';
3
+ import { SimSceneObjInfo, bufferGenerator } from './internalUtils.js';
4
4
  export class Color {
5
5
  r; // 0 - 255
6
6
  g; // 0 - 255
@@ -168,6 +168,24 @@ export function easeInOutQuart(t) {
168
168
  export function easeInOutQuad(t) {
169
169
  return t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2;
170
170
  }
171
+ export function easeInQuad(x) {
172
+ return x * x;
173
+ }
174
+ export function easeOutQuad(x) {
175
+ return 1 - (1 - x) * (1 - x);
176
+ }
177
+ export function easeInQuart(x) {
178
+ return x * x * x * x;
179
+ }
180
+ export function easeOutQuart(x) {
181
+ return 1 - Math.pow(1 - x, 4);
182
+ }
183
+ export function easeInExpo(x) {
184
+ return x === 0 ? 0 : Math.pow(2, 10 * x - 10);
185
+ }
186
+ export function easeOutExpo(x) {
187
+ return x === 1 ? 1 : 1 - Math.pow(2, -10 * x);
188
+ }
171
189
  export function cloneBuf(buf) {
172
190
  return new Float32Array(buf);
173
191
  }
@@ -236,3 +254,34 @@ export function distance2d(vector1, vector2) {
236
254
  export function distance3d(vector1, vector2) {
237
255
  return vec3.distance(vector1, vector2);
238
256
  }
257
+ export function toSceneObjInfo(el, id) {
258
+ return new SimSceneObjInfo(el, id);
259
+ }
260
+ export function toSceneObjInfoMany(el, id) {
261
+ return el.map((item, index) => toSceneObjInfo(item, id ? id[index] : undefined));
262
+ }
263
+ export function interpolateColors(colors, t) {
264
+ t = Math.min(1, Math.max(0, t));
265
+ if (colors.length === 0)
266
+ return color();
267
+ if (colors.length === 1)
268
+ return colors[0];
269
+ const colorInterval = 1 / colors.length;
270
+ let index = Math.floor(t / colorInterval);
271
+ if (index >= colors.length)
272
+ index = colors.length - 1;
273
+ const from = index === colors.length - 1 ? colors[index - 1] : colors[index];
274
+ const to = index === colors.length - 1 ? colors[index] : colors[index + 1];
275
+ const diff = to.diff(from);
276
+ const scale = t / (colorInterval * colors.length);
277
+ diff.r *= scale;
278
+ diff.g *= scale;
279
+ diff.b *= scale;
280
+ diff.a *= scale;
281
+ const res = from.clone();
282
+ res.r += diff.r;
283
+ res.g += diff.g;
284
+ res.b += diff.b;
285
+ res.a += diff.a;
286
+ return res;
287
+ }
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Jackson Otto",
7
7
  "description": "A simple graphics library using WebGPU",
8
- "version": "0.4.1",
8
+ "version": "0.4.3",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": "./dist/index.js",