simulationjsv2 0.5.2 → 0.6.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.
@@ -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';
3
+ import { cloneBuf, vector2 } from './utils.js';
4
+ import { SimulationElement } from './graphics.js';
5
5
  import { SceneCollection } from './simulation.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) => {
29
+ export const updateProjectionMatrix = (mat, aspectRatio, zNear = 1, zFar = 500) => {
28
30
  const fov = (2 * Math.PI) / 5;
29
- return mat4.perspective(fov, aspectRatio, zNear, zFar);
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, camera) => {
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({
@@ -62,7 +62,7 @@ export const buildMultisampleTexture = (device, ctx, width, height) => {
62
62
  };
63
63
  export const addObject = (scene, el, device, id) => {
64
64
  if (el instanceof SimulationElement) {
65
- if (device !== null && (el instanceof Instance || el instanceof SceneCollection)) {
65
+ if (device !== null && el instanceof SceneCollection) {
66
66
  el.setDevice(device);
67
67
  }
68
68
  const obj = new SimSceneObjInfo(el, id);
@@ -176,6 +176,25 @@ export function lossyTriangulate(vertices) {
176
176
  }
177
177
  return res;
178
178
  }
179
+ export function lossyTriangulateStrip(vertices) {
180
+ const res = [];
181
+ let upper = vertices.length - 1;
182
+ let lower = 0;
183
+ let onLower = true;
184
+ while (upper > lower) {
185
+ if (onLower) {
186
+ res.push(vertices[lower]);
187
+ lower++;
188
+ }
189
+ else {
190
+ res.push(vertices[upper]);
191
+ upper--;
192
+ }
193
+ onLower = !onLower;
194
+ }
195
+ res.push(vertices[upper]);
196
+ return res;
197
+ }
179
198
  class BufferGenerator {
180
199
  instancing = false;
181
200
  constructor() { }
@@ -191,7 +210,7 @@ class BufferGenerator {
191
210
  }
192
211
  return buf;
193
212
  }
194
- return [x, y, z, 1, ...color.toBuffer(), ...uv, this.instancing ? 1 : 0];
213
+ return [x, y, z, ...color.toBuffer(), ...uv, this.instancing ? 1 : 0];
195
214
  }
196
215
  }
197
216
  export const bufferGenerator = new BufferGenerator();
@@ -216,7 +235,7 @@ export function rotateMat4(mat, rotation) {
216
235
  mat4.rotateY(mat, rotation[1], mat);
217
236
  mat4.rotateX(mat, rotation[0], mat);
218
237
  }
219
- export function createPipeline(device, module, bindGroupLayouts, presentationFormat, entryPoint, topology, vertexParams) {
238
+ export function createPipeline(device, module, bindGroupLayouts, presentationFormat, topology, vertexParams) {
220
239
  let params = [
221
240
  {
222
241
  // position
@@ -263,7 +282,7 @@ export function createPipeline(device, module, bindGroupLayouts, presentationFor
263
282
  }),
264
283
  vertex: {
265
284
  module,
266
- entryPoint,
285
+ entryPoint: 'vertex_main',
267
286
  buffers: [
268
287
  {
269
288
  arrayStride: stride,
@@ -314,3 +333,21 @@ export function getTotalVertices(scene) {
314
333
  }
315
334
  return total;
316
335
  }
336
+ export function wrapVoidPromise(promise) {
337
+ return new Promise((resolve) => {
338
+ promise.then(() => resolve());
339
+ });
340
+ }
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
+ // }
@@ -1,6 +1,6 @@
1
1
  /// <reference types="@webgpu/types" />
2
2
  import { SimulationElement3d } from './graphics.js';
3
- import type { Vector2, Vector3, LerpFunc, AnySimulationElement, VertexParamGeneratorInfo, VertexParamInfo, BindGroupInfo } from './types.js';
3
+ import type { Vector2, Vector3, LerpFunc, AnySimulationElement, VertexParamGeneratorInfo, VertexParamInfo, BindGroupInfo, ElementRotation } from './types.js';
4
4
  import { Color } from './utils.js';
5
5
  import { BlankGeometry } from './geometry.js';
6
6
  import { SimSceneObjInfo } from './internalUtils.js';
@@ -44,10 +44,10 @@ export declare class Simulation {
44
44
  export declare class SceneCollection extends SimulationElement3d {
45
45
  protected geometry: BlankGeometry;
46
46
  private name;
47
- private scene;
47
+ protected scene: SimSceneObjInfo[];
48
48
  protected device: GPUDevice | null;
49
49
  constructor(name?: string);
50
- setWireframe(_: boolean): void;
50
+ setWireframe(wireframe: boolean): void;
51
51
  getName(): string | null;
52
52
  getScene(): SimSceneObjInfo[];
53
53
  setDevice(device: GPUDevice): void;
@@ -64,9 +64,9 @@ export declare class SceneCollection extends SimulationElement3d {
64
64
  */
65
65
  setLifetime(el: AnySimulationElement, lifetime: number): void;
66
66
  empty(): void;
67
- getSceneBuffer(camera: Camera): number[];
68
- getWireframe(camera: Camera): number[];
69
- getTriangles(camera: Camera): number[];
67
+ getSceneBuffer(): (number | Float32Array)[];
68
+ getWireframe(): (number | Float32Array)[];
69
+ getTriangles(): (number | Float32Array)[];
70
70
  protected updateMatrix(camera: Camera): void;
71
71
  }
72
72
  export declare class Camera {
@@ -109,3 +109,12 @@ export declare class ShaderGroup extends SceneCollection {
109
109
  getVertexParamGenerator(): VertexParamGeneratorInfo;
110
110
  hasBindGroup(): boolean;
111
111
  }
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
+ }