simulationjsv2 0.8.0 → 0.8.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.
package/TODO.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # TODO
2
2
 
3
- - [ ] Change render vertices to use index buffer
4
- - [ ] Add cull modes
3
+ - [x] Change render vertices to use index buffer
4
+ - [x] Add cull modes
5
5
  - [x] Fix shaders
6
6
  - [x] Clean up polygons
7
7
  - [x] Fix transparency
package/dist/geometry.js CHANGED
@@ -44,15 +44,15 @@ export class CubeGeometry extends Geometry {
44
44
  wireframeOrder = [0, 1, 2, 3, 0, 2, 6, 5, 1, 6, 7, 4, 5, 7, 3, 4, 0, 5, 6, 3];
45
45
  // prettier-ignore
46
46
  triangleOrder = [
47
- 0, 1, 3, 2,
48
- 4, 7, 5, 6,
49
- 0, 3, 4, 7,
50
- 0, 4, 1, 5,
51
- 1, 5, 2, 6,
52
- 2, 6, 3, 7
47
+ 0, 1, 2, 2, 3, 0,
48
+ 6, 5, 4, 7, 6, 4,
49
+ 4, 1, 0, 1, 4, 5,
50
+ 2, 1, 5, 5, 6, 2,
51
+ 0, 3, 4, 7, 4, 3,
52
+ 3, 6, 7, 6, 3, 2
53
53
  ];
54
54
  constructor(width, height, depth) {
55
- super([], 'strip');
55
+ super();
56
56
  this.params = {
57
57
  width,
58
58
  height,
@@ -61,10 +61,8 @@ export declare abstract class SimulationElement3d {
61
61
  getRotation(): Vector3;
62
62
  getCenterOffset(): Vector3;
63
63
  fill(newColor: Color, t?: number, f?: LerpFunc): Promise<void>;
64
- private moveChildren;
65
64
  move(amount: Vector3, t?: number, f?: LerpFunc, fromDevicePixelRatio?: boolean): Promise<void>;
66
65
  moveTo(pos: Vector3, t?: number, f?: LerpFunc, fromDevicePixelRatio?: boolean): Promise<void>;
67
- rotateChildrenTo(angle: Vector3): void;
68
66
  rotateChildren(angle: Vector3): void;
69
67
  rotate(amount: Vector3, t?: number, f?: LerpFunc): Promise<void>;
70
68
  rotateTo(rot: Vector3, t?: number, f?: LerpFunc): Promise<void>;
package/dist/graphics.js CHANGED
@@ -211,18 +211,12 @@ export class SimulationElement3d {
211
211
  this.vertexCache.updated();
212
212
  }, t, f);
213
213
  }
214
- moveChildren(amount, t = 0, f) {
215
- for (let i = 0; i < this.children.length; i++) {
216
- this.children[i].getObj().move(amount, t, f, true);
217
- }
218
- }
219
214
  move(amount, t = 0, f, fromDevicePixelRatio = false) {
220
215
  const tempAmount = cloneBuf(amount);
221
216
  if (!fromDevicePixelRatio)
222
217
  vector3ToPixelRatio(tempAmount);
223
218
  const finalPos = cloneBuf(this.pos);
224
219
  vec3.add(finalPos, tempAmount, finalPos);
225
- this.moveChildren(amount, t, f);
226
220
  return internalTransitionValues((p) => {
227
221
  this.pos[0] += tempAmount[0] * p;
228
222
  this.pos[1] += tempAmount[1] * p;
@@ -237,7 +231,6 @@ export class SimulationElement3d {
237
231
  vector3ToPixelRatio(tempPos);
238
232
  const diff = vector3();
239
233
  vec3.sub(tempPos, this.pos, diff);
240
- this.moveChildren(diff, t, f);
241
234
  return internalTransitionValues((p) => {
242
235
  this.pos[0] += diff[0] * p;
243
236
  this.pos[1] += diff[1] * p;
@@ -246,11 +239,6 @@ export class SimulationElement3d {
246
239
  this.pos = tempPos;
247
240
  }, t, f);
248
241
  }
249
- rotateChildrenTo(angle) {
250
- for (let i = 0; i < this.children.length; i++) {
251
- this.children[i].getObj().rotateTo(angle);
252
- }
253
- }
254
242
  rotateChildren(angle) {
255
243
  for (let i = 0; i < this.children.length; i++) {
256
244
  this.children[i].getObj().rotate(angle);
@@ -375,8 +363,8 @@ export class Square extends SimulationElement2d {
375
363
  */
376
364
  constructor(pos, width, height, color, rotation) {
377
365
  super(pos, vector3(0, 0, rotation), color);
378
- this.width = width;
379
- this.height = height;
366
+ this.width = width / devicePixelRatio;
367
+ this.height = height / devicePixelRatio;
380
368
  this.geometry = new SquareGeometry(this.width, this.height);
381
369
  }
382
370
  getWidth() {
@@ -34,7 +34,7 @@ export class CachedArray {
34
34
  }
35
35
  add(index) {
36
36
  if (this.length < this.data.length) {
37
- this.data[this.length - 1] = index;
37
+ this.data[this.length] = index;
38
38
  }
39
39
  else {
40
40
  this.data.push(index);
@@ -39,6 +39,7 @@ export declare class Simulation extends Settings {
39
39
  private indexBuffer;
40
40
  constructor(idOrCanvasRef: string | HTMLCanvasElement, sceneCamera?: Camera | null, showFrameRate?: boolean);
41
41
  private handleCanvasResize;
42
+ on<K extends keyof HTMLElementEventMap>(event: K, cb: (this: HTMLCanvasElement, ev: HTMLElementEventMap[K]) => void, options?: boolean | AddEventListenerOptions): void;
42
43
  onResize(cb: (width: number, height: number) => void): void;
43
44
  getWidth(): number;
44
45
  getHeight(): number;
@@ -181,8 +181,10 @@ export class Simulation extends Settings {
181
181
  this.frameRateView = new FrameRateView(showFrameRate);
182
182
  this.frameRateView.updateFrameRate(1);
183
183
  this.transparentElements = new CachedArray();
184
- this.vertexBuffer = new MemoBuffer(GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST, 0);
185
- this.indexBuffer = new MemoBuffer(GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST, 0);
184
+ // this.vertexBuffer = new MemoBuffer(GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST, 0);
185
+ // this.indexBuffer = new MemoBuffer(GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST, 0);
186
+ this.vertexBuffer = new MemoBuffer(GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST, 1000);
187
+ this.indexBuffer = new MemoBuffer(GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST, 1000);
186
188
  }
187
189
  handleCanvasResize(parent) {
188
190
  if (this.fittingElement) {
@@ -191,6 +193,11 @@ export class Simulation extends Settings {
191
193
  this.setCanvasSize(width, height);
192
194
  }
193
195
  }
196
+ on(event, cb, options) {
197
+ if (!this.canvasRef)
198
+ return;
199
+ this.canvasRef.addEventListener(event, cb, options);
200
+ }
194
201
  onResize(cb) {
195
202
  this.resizeEvents.push(cb);
196
203
  }
package/dist/utils.d.ts CHANGED
@@ -68,7 +68,7 @@ export declare function vector2(x?: number, y?: number): Vector2;
68
68
  export declare function matrix4(): Mat4;
69
69
  export declare function vector3FromVector2(vec: Vector2): Vector3;
70
70
  export declare function vector2FromVector3(vec: Vector3): Vector2;
71
- export declare function randomInt(range: number, min?: number): number;
71
+ export declare function randomInt(max: number, min?: number): number;
72
72
  export declare function randomColor(a?: number): Color;
73
73
  export declare function vertex(x?: number, y?: number, z?: number, color?: Color, uv?: Vector2): Vertex;
74
74
  export declare function color(r?: number, g?: number, b?: number, a?: number): Color;
package/dist/utils.js CHANGED
@@ -218,8 +218,8 @@ export function vector3FromVector2(vec) {
218
218
  export function vector2FromVector3(vec) {
219
219
  return vector2(vec[0], vec[1]);
220
220
  }
221
- export function randomInt(range, min = 0) {
222
- return Math.floor(Math.random() * (range - min)) + min;
221
+ export function randomInt(max, min = 0) {
222
+ return Math.floor(Math.random() * (max - min)) + min;
223
223
  }
224
224
  export function randomColor(a = 1) {
225
225
  return new Color(randomInt(255), randomInt(255), randomInt(255), a);
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.8.0",
8
+ "version": "0.8.2",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": "./dist/index.js",