@pretty-chitty/core 1.2.17 → 1.2.18

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.
Files changed (47) hide show
  1. package/dist/components/PanelSpark.js +1 -1
  2. package/dist/components/PlayerImage.d.ts.map +1 -1
  3. package/dist/components/PlayerImage.js +21 -1
  4. package/dist/components/PlayerImage.js.map +1 -1
  5. package/dist/game/PlayerChit.d.ts +1 -0
  6. package/dist/game/PlayerChit.d.ts.map +1 -1
  7. package/dist/game/PlayerChit.js +5 -2
  8. package/dist/game/PlayerChit.js.map +1 -1
  9. package/dist/game/Turn.d.ts +6 -2
  10. package/dist/game/Turn.d.ts.map +1 -1
  11. package/dist/game/Turn.js +100 -61
  12. package/dist/game/Turn.js.map +1 -1
  13. package/dist/hooks/useWebGlRenderer.d.ts +1 -0
  14. package/dist/hooks/useWebGlRenderer.d.ts.map +1 -1
  15. package/dist/hooks/useWebGlRenderer.js +33 -21
  16. package/dist/hooks/useWebGlRenderer.js.map +1 -1
  17. package/dist/utilities/CanvasStack/CanvasOperations.d.ts +15 -1
  18. package/dist/utilities/CanvasStack/CanvasOperations.d.ts.map +1 -1
  19. package/dist/utilities/CanvasStack/CanvasOperations.js +65 -6
  20. package/dist/utilities/CanvasStack/CanvasOperations.js.map +1 -1
  21. package/dist/utilities/CanvasStack/CanvasStack.d.ts +7 -1
  22. package/dist/utilities/CanvasStack/CanvasStack.d.ts.map +1 -1
  23. package/dist/utilities/CanvasStack/CanvasStack.js +72 -6
  24. package/dist/utilities/CanvasStack/CanvasStack.js.map +1 -1
  25. package/dist/utilities/CanvasStack/PlayerCanvas.d.ts +2 -1
  26. package/dist/utilities/CanvasStack/PlayerCanvas.d.ts.map +1 -1
  27. package/dist/utilities/CanvasStack/PlayerCanvas.js +3 -2
  28. package/dist/utilities/CanvasStack/PlayerCanvas.js.map +1 -1
  29. package/dist/utilities/CanvasStack/ReactCanvas.d.ts +2 -1
  30. package/dist/utilities/CanvasStack/ReactCanvas.d.ts.map +1 -1
  31. package/dist/utilities/CanvasStack/ReactCanvas.js +2 -2
  32. package/dist/utilities/CanvasStack/ReactCanvas.js.map +1 -1
  33. package/dist/utilities/ParameterizedCanvas.d.ts +1 -0
  34. package/dist/utilities/ParameterizedCanvas.d.ts.map +1 -1
  35. package/dist/utilities/ParameterizedCanvas.js +4 -1
  36. package/dist/utilities/ParameterizedCanvas.js.map +1 -1
  37. package/package.json +1 -1
  38. package/src/library/components/PanelSpark.tsx +1 -1
  39. package/src/library/components/PlayerImage.tsx +26 -2
  40. package/src/library/game/PlayerChit.ts +6 -2
  41. package/src/library/game/Turn.ts +112 -69
  42. package/src/library/hooks/useWebGlRenderer.tsx +44 -24
  43. package/src/library/utilities/CanvasStack/CanvasOperations.tsx +87 -17
  44. package/src/library/utilities/CanvasStack/CanvasStack.ts +86 -12
  45. package/src/library/utilities/CanvasStack/PlayerCanvas.tsx +5 -2
  46. package/src/library/utilities/CanvasStack/ReactCanvas.tsx +2 -2
  47. package/src/library/utilities/ParameterizedCanvas.ts +5 -1
@@ -39,6 +39,9 @@ export class CanvasStack implements IUpdatingCanvas {
39
39
  private static imageCache = new ImageCache(50); // 50? Is that good? No idea.
40
40
  private _outlets: { [id: string]: Coord } = {};
41
41
 
42
+ private _mipCanvases: HTMLCanvasElement[] = [];
43
+ private _mipContexts: CanvasRenderingContext2D[] = [];
44
+
42
45
  public createdAt = Date.now();
43
46
 
44
47
  public get hasBuiltTexture(): boolean {
@@ -90,6 +93,7 @@ export class CanvasStack implements IUpdatingCanvas {
90
93
  this._outlets[id] = coords;
91
94
  },
92
95
  );
96
+ this._renderMipCanvases();
93
97
  this.cbs.forEach((cb) => cb());
94
98
  }
95
99
 
@@ -98,6 +102,8 @@ export class CanvasStack implements IUpdatingCanvas {
98
102
  this.canvas!.title = `Disposed, mat ${this._material?.uuid} texture ${this._texture?.uuid}`;
99
103
  this.canvas = undefined;
100
104
  this.context = undefined;
105
+ this._mipCanvases = [];
106
+ this._mipContexts = [];
101
107
  CanvasStack._liveStacks.delete(this);
102
108
 
103
109
  if (this._material) {
@@ -114,6 +120,7 @@ export class CanvasStack implements IUpdatingCanvas {
114
120
  public width: number,
115
121
  public height: number,
116
122
  public operation: CanvasOperation,
123
+ private _mipLevelsOverride?: number,
117
124
  ) {
118
125
  this.canvas = document.createElement("canvas");
119
126
  this.canvas.width = width;
@@ -129,6 +136,56 @@ export class CanvasStack implements IUpdatingCanvas {
129
136
  CanvasStack.ensureContextRestoreSubscription();
130
137
 
131
138
  this.render();
139
+ this._initMipCanvases();
140
+ }
141
+
142
+ get mipMaps(): number {
143
+ if (this._mipLevelsOverride !== undefined) return this._mipLevelsOverride;
144
+ return this.operation.benefitsFromMipMap() ? 2 : 0;
145
+ }
146
+
147
+ private _initMipCanvases() {
148
+ this._mipCanvases = [];
149
+ this._mipContexts = [];
150
+ // Build the full mipmap chain down to 1×1. WebGL requires a complete chain
151
+ // when using a mipmap filter — a partial chain makes the texture incomplete
152
+ // and WebGL renders it as transparent.
153
+ let w = this.width;
154
+ let h = this.height;
155
+ while (w > 1 || h > 1) {
156
+ w = Math.max(1, Math.floor(w / 2));
157
+ h = Math.max(1, Math.floor(h / 2));
158
+ const c = document.createElement("canvas");
159
+ c.width = w;
160
+ c.height = h;
161
+ this._mipCanvases.push(c);
162
+ this._mipContexts.push(c.getContext("2d")!);
163
+ }
164
+ this._renderMipCanvases();
165
+ }
166
+
167
+ private _renderMipCanvases() {
168
+ const noop = () => {};
169
+ const numCustomLevels = this.mipMaps;
170
+ for (let i = 0; i < this._mipCanvases.length; i++) {
171
+ const ctx = this._mipContexts[i];
172
+ const c = this._mipCanvases[i];
173
+ if (i < numCustomLevels) {
174
+ // Custom render: canvas scale so text is rasterized natively at the
175
+ // smaller effective size rather than downsampled from the base canvas.
176
+ const scale = c.width / this.width;
177
+ ctx.save();
178
+ ctx.scale(scale, scale);
179
+ ctx.clearRect(0, 0, this.width, this.height);
180
+ this.operation.render(ctx, { x: 0, y: 0, w: this.width, h: this.height }, this.loadUrl.bind(this), noop);
181
+ ctx.restore();
182
+ } else {
183
+ // Complete the chain by box-filtering down from the previous level.
184
+ const prev = i === 0 ? this.canvas! : this._mipCanvases[i - 1];
185
+ ctx.clearRect(0, 0, c.width, c.height);
186
+ ctx.drawImage(prev, 0, 0, c.width, c.height);
187
+ }
188
+ }
132
189
  }
133
190
 
134
191
  private static _liveStacks: Set<CanvasStack> = new Set();
@@ -175,18 +232,35 @@ export class CanvasStack implements IUpdatingCanvas {
175
232
  }
176
233
 
177
234
  if (!this._texture) {
178
- // WebGL1 only supports mipmaps on power-of-two textures, and these
179
- // canvases are arbitrarily sized. Skip mipmap generation outside WebGL2.
180
- const useMipmaps = getWebGlRendererInstance()?.isWebGL2 ?? false;
181
- this._texture = new Texture(
182
- this.canvas,
183
- UVMapping,
184
- ClampToEdgeWrapping,
185
- ClampToEdgeWrapping,
186
- LinearFilter,
187
- useMipmaps ? LinearMipmapLinearFilter : LinearFilter,
188
- );
189
- this._texture.generateMipmaps = useMipmaps;
235
+ const wrapper = getWebGlRendererInstance();
236
+ if (this.mipMaps > 0) {
237
+ this._texture = new Texture(
238
+ this.canvas,
239
+ UVMapping,
240
+ ClampToEdgeWrapping,
241
+ ClampToEdgeWrapping,
242
+ LinearFilter,
243
+ LinearMipmapLinearFilter,
244
+ );
245
+ this._texture.generateMipmaps = false;
246
+ // mipmaps[i] is uploaded as WebGL level i. Level 0 must be the base
247
+ // canvas; levels 1..N are the custom-rendered smaller canvases.
248
+ // Rendering text natively at each mip size is sharper than GPU box-filtering.
249
+ this._texture.mipmaps = [this.canvas, ...this._mipCanvases];
250
+ } else {
251
+ // WebGL1 only supports mipmaps on power-of-two textures, and these
252
+ // canvases are arbitrarily sized. Skip mipmap generation outside WebGL2.
253
+ this._texture = new Texture(
254
+ this.canvas,
255
+ UVMapping,
256
+ ClampToEdgeWrapping,
257
+ ClampToEdgeWrapping,
258
+ LinearFilter,
259
+ LinearMipmapLinearFilter,
260
+ );
261
+ this._texture.generateMipmaps = true;
262
+ }
263
+ this._texture.anisotropy = wrapper?.maxAnisotropy ?? 1;
190
264
  this._texture.needsUpdate = true;
191
265
  this._texture.colorSpace = SRGBColorSpace;
192
266
 
@@ -7,11 +7,14 @@ export class PlayerCanvas extends ParameterizedCanvas {
7
7
  width = 50;
8
8
  height = 50;
9
9
 
10
- constructor(private player: PlayerChit) {
10
+ constructor(
11
+ private player: PlayerChit,
12
+ private colorBlend?: number,
13
+ ) {
11
14
  super();
12
15
  }
13
16
 
14
17
  protected render() {
15
- return <Player player={this.player} />;
18
+ return <Player player={this.player} colorBlend={this.colorBlend} />;
16
19
  }
17
20
  }
@@ -206,8 +206,8 @@ export function Image({
206
206
  * @param args
207
207
  * @returns
208
208
  */
209
- export function Player({ player }: { player: PlayerChit }) {
210
- return <WrappedCanvasOperation operation={new PlayerCanvasOperation(player)} />;
209
+ export function Player({ player, colorBlend }: { player: PlayerChit; colorBlend?: number }) {
210
+ return <WrappedCanvasOperation operation={new PlayerCanvasOperation(player, colorBlend)} />;
211
211
  }
212
212
 
213
213
  /**
@@ -40,6 +40,10 @@ export abstract class ParameterizedCanvas extends ObjectWithProps {
40
40
  .join("___")}`;
41
41
  }
42
42
 
43
+ protected get mipLevels(): number | undefined {
44
+ return undefined;
45
+ }
46
+
43
47
  get(): IUpdatingCanvas {
44
48
  const signature = this.signature();
45
49
  let result = ParameterizedCanvas.lu[signature];
@@ -48,7 +52,7 @@ export abstract class ParameterizedCanvas extends ObjectWithProps {
48
52
  result = ParameterizedCanvas.lu[signature] = (() => {
49
53
  try {
50
54
  const ops = this.render();
51
- return new CanvasStack(this.width, this.height, unwrapCanvasNode(ops));
55
+ return new CanvasStack(this.width, this.height, unwrapCanvasNode(ops), this.mipLevels);
52
56
  } catch (e) {
53
57
  console.error(e);
54
58
  return new CanvasStack(this.width, this.height, new LayeredCanvasOperation([]));