@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.
- package/dist/components/PanelSpark.js +1 -1
- package/dist/components/PlayerImage.d.ts.map +1 -1
- package/dist/components/PlayerImage.js +21 -1
- package/dist/components/PlayerImage.js.map +1 -1
- package/dist/game/PlayerChit.d.ts +1 -0
- package/dist/game/PlayerChit.d.ts.map +1 -1
- package/dist/game/PlayerChit.js +5 -2
- package/dist/game/PlayerChit.js.map +1 -1
- package/dist/game/Turn.d.ts +6 -2
- package/dist/game/Turn.d.ts.map +1 -1
- package/dist/game/Turn.js +100 -61
- package/dist/game/Turn.js.map +1 -1
- package/dist/hooks/useWebGlRenderer.d.ts +1 -0
- package/dist/hooks/useWebGlRenderer.d.ts.map +1 -1
- package/dist/hooks/useWebGlRenderer.js +33 -21
- package/dist/hooks/useWebGlRenderer.js.map +1 -1
- package/dist/utilities/CanvasStack/CanvasOperations.d.ts +15 -1
- package/dist/utilities/CanvasStack/CanvasOperations.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/CanvasOperations.js +65 -6
- package/dist/utilities/CanvasStack/CanvasOperations.js.map +1 -1
- package/dist/utilities/CanvasStack/CanvasStack.d.ts +7 -1
- package/dist/utilities/CanvasStack/CanvasStack.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/CanvasStack.js +72 -6
- package/dist/utilities/CanvasStack/CanvasStack.js.map +1 -1
- package/dist/utilities/CanvasStack/PlayerCanvas.d.ts +2 -1
- package/dist/utilities/CanvasStack/PlayerCanvas.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/PlayerCanvas.js +3 -2
- package/dist/utilities/CanvasStack/PlayerCanvas.js.map +1 -1
- package/dist/utilities/CanvasStack/ReactCanvas.d.ts +2 -1
- package/dist/utilities/CanvasStack/ReactCanvas.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/ReactCanvas.js +2 -2
- package/dist/utilities/CanvasStack/ReactCanvas.js.map +1 -1
- package/dist/utilities/ParameterizedCanvas.d.ts +1 -0
- package/dist/utilities/ParameterizedCanvas.d.ts.map +1 -1
- package/dist/utilities/ParameterizedCanvas.js +4 -1
- package/dist/utilities/ParameterizedCanvas.js.map +1 -1
- package/package.json +1 -1
- package/src/library/components/PanelSpark.tsx +1 -1
- package/src/library/components/PlayerImage.tsx +26 -2
- package/src/library/game/PlayerChit.ts +6 -2
- package/src/library/game/Turn.ts +112 -69
- package/src/library/hooks/useWebGlRenderer.tsx +44 -24
- package/src/library/utilities/CanvasStack/CanvasOperations.tsx +87 -17
- package/src/library/utilities/CanvasStack/CanvasStack.ts +86 -12
- package/src/library/utilities/CanvasStack/PlayerCanvas.tsx +5 -2
- package/src/library/utilities/CanvasStack/ReactCanvas.tsx +2 -2
- package/src/library/utilities/ParameterizedCanvas.ts +5 -1
package/src/library/game/Turn.ts
CHANGED
|
@@ -94,6 +94,16 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
94
94
|
private dirtyChitIds = new Set<string>();
|
|
95
95
|
private pendingNewChits = new Set<Chit>();
|
|
96
96
|
private _statePool?: Map<string, string>;
|
|
97
|
+
private _serializeCache?: SerializeCache;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Drop any cached serialize() reconstruction on this turn and every ancestor,
|
|
101
|
+
* since their snapshots include our contribution.
|
|
102
|
+
*/
|
|
103
|
+
private invalidateSerializeCache() {
|
|
104
|
+
this._serializeCache = undefined;
|
|
105
|
+
this.parent?.invalidateSerializeCache();
|
|
106
|
+
}
|
|
97
107
|
|
|
98
108
|
private get statePool(): Map<string, string> {
|
|
99
109
|
if (this.parent) return this.parent.statePool;
|
|
@@ -305,8 +315,12 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
305
315
|
}
|
|
306
316
|
|
|
307
317
|
if (hasChange) {
|
|
318
|
+
// Pops and in-place merges can leave any cached walk pointing into invalid territory.
|
|
319
|
+
this.invalidateSerializeCache();
|
|
308
320
|
this.pass++;
|
|
309
|
-
|
|
321
|
+
// No trailing force-flush: it would only push an empty FlushClockStep (no chit
|
|
322
|
+
// diff, no log) that the client has to walk through for no visual change. The
|
|
323
|
+
// bumped `pass` is read live from `clockDetails` on the next notify.
|
|
310
324
|
}
|
|
311
325
|
}
|
|
312
326
|
|
|
@@ -360,7 +374,8 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
360
374
|
}
|
|
361
375
|
}
|
|
362
376
|
|
|
363
|
-
// 2. Serialize only dirty chits
|
|
377
|
+
// 2. Serialize only dirty chits. `newStates` now stores only the diff against the
|
|
378
|
+
// previous flush — unchanged-after-dirty-check chits are dropped from the step.
|
|
364
379
|
for (const id of this.dirtyChitIds) {
|
|
365
380
|
const chit = this.chitLookup[id];
|
|
366
381
|
if (!chit) continue;
|
|
@@ -369,20 +384,11 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
369
384
|
if (serialized !== lastState) {
|
|
370
385
|
this.lastChitStates[id] = newStates[id] = serialized;
|
|
371
386
|
sawChange = true;
|
|
372
|
-
} else {
|
|
373
|
-
newStates[id] = serialized;
|
|
374
387
|
}
|
|
375
388
|
}
|
|
376
389
|
this.dirtyChitIds.clear();
|
|
377
390
|
|
|
378
|
-
//
|
|
379
|
-
for (const id of Object.keys(this.lastChitStates)) {
|
|
380
|
-
if (newStates[id] === undefined) {
|
|
381
|
-
newStates[id] = this.lastChitStates[id];
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
// 5. Detect deleted chits — orphans in lastChitStates that weren't serialized above
|
|
391
|
+
// 3. Detect deleted chits — orphans in lastChitStates that weren't serialized above
|
|
386
392
|
const deletedRoots: Chit[] = [];
|
|
387
393
|
for (const id of Object.keys(this.lastChitStates)) {
|
|
388
394
|
if (this.lastChitStates[id] === Chit.deletedIfSerialized()) continue;
|
|
@@ -416,6 +422,8 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
416
422
|
}
|
|
417
423
|
|
|
418
424
|
this.clockSteps.push(new FlushClockStep(this.clock, newStates));
|
|
425
|
+
// Ancestors' caches included our prior contribution; the new step makes them stale.
|
|
426
|
+
this.parent?.invalidateSerializeCache();
|
|
419
427
|
}
|
|
420
428
|
}
|
|
421
429
|
|
|
@@ -479,6 +487,8 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
479
487
|
}
|
|
480
488
|
lastStep.turns.push(turn);
|
|
481
489
|
}
|
|
490
|
+
// Either case mutates a step our own cache (or an ancestor's) may have already walked past.
|
|
491
|
+
this.invalidateSerializeCache();
|
|
482
492
|
|
|
483
493
|
this.activeSubTurns.push(turn);
|
|
484
494
|
|
|
@@ -501,10 +511,27 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
501
511
|
});
|
|
502
512
|
|
|
503
513
|
this.lastChitStates = { ...this.lastChitStates, ...turn.lastChitStates };
|
|
514
|
+
turn.releasePostAbsorptionRefs();
|
|
504
515
|
|
|
505
516
|
return result;
|
|
506
517
|
}
|
|
507
518
|
|
|
519
|
+
/**
|
|
520
|
+
* Called by the parent right after it has absorbed our `chitLookup` and `lastChitStates`.
|
|
521
|
+
* After this we will only ever be walked via `serialize` / `chitsHistory` / `gameLog`,
|
|
522
|
+
* which only need `clockSteps` and `lockedChitStates` — so drop the heavy per-chit
|
|
523
|
+
* bookkeeping to keep finished sub-turns from pinning hundreds of chit references.
|
|
524
|
+
* @internal
|
|
525
|
+
*/
|
|
526
|
+
private releasePostAbsorptionRefs() {
|
|
527
|
+
this.chitLookup = {};
|
|
528
|
+
this.chitsToLock = [];
|
|
529
|
+
this.lastChitStates = {};
|
|
530
|
+
this.pendingNewChits.clear();
|
|
531
|
+
this.dirtyChitIds.clear();
|
|
532
|
+
this._serializeCache = undefined;
|
|
533
|
+
}
|
|
534
|
+
|
|
508
535
|
public async runParallelTurns<A>(
|
|
509
536
|
players: P[],
|
|
510
537
|
chits: (p: P) => Chit[],
|
|
@@ -761,31 +788,6 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
761
788
|
return result;
|
|
762
789
|
}
|
|
763
790
|
|
|
764
|
-
private findIndexOfLastFlushStepBefore(clock: number): number {
|
|
765
|
-
for (let j = this.clockSteps.length - 1; j >= 0; j--) {
|
|
766
|
-
if (this.clockSteps[j] instanceof FlushClockStep && this.clockSteps[j].startClock < clock) {
|
|
767
|
-
return j;
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
return 0;
|
|
771
|
-
}
|
|
772
|
-
private findIndexOfLastFlushStepWithLogMessageBefore(clock: number): number {
|
|
773
|
-
for (let j = this.clockSteps.length - 1; j >= 0; j--) {
|
|
774
|
-
if (
|
|
775
|
-
this.clockSteps[j] instanceof FlushClockStep &&
|
|
776
|
-
this.clockSteps[j].startClock < clock &&
|
|
777
|
-
(this.clockSteps[j] as FlushClockStep).log
|
|
778
|
-
) {
|
|
779
|
-
return j;
|
|
780
|
-
}
|
|
781
|
-
if (this.clockSteps[j].startClock < clock && this.clockSteps[j] instanceof SubTurnsClockStep) {
|
|
782
|
-
// logs cannot be inside subturns
|
|
783
|
-
break;
|
|
784
|
-
}
|
|
785
|
-
}
|
|
786
|
-
return -1;
|
|
787
|
-
}
|
|
788
|
-
|
|
789
791
|
/** @internal */
|
|
790
792
|
chitsHistory(playerId: string, ids: string[]): ChitHistoryResponse {
|
|
791
793
|
const result: ChitHistoryResponse = {};
|
|
@@ -803,6 +805,15 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
803
805
|
}
|
|
804
806
|
};
|
|
805
807
|
|
|
808
|
+
// Seed with the initial locked state so chits that never diff in this turn still
|
|
809
|
+
// surface their starting form (flush steps now hold diffs only).
|
|
810
|
+
ids.forEach((id) => {
|
|
811
|
+
const initial = this.lockedChitStates[id];
|
|
812
|
+
if (initial !== undefined) {
|
|
813
|
+
appendLog(id, 0, initial);
|
|
814
|
+
}
|
|
815
|
+
});
|
|
816
|
+
|
|
806
817
|
this.clockSteps.forEach((step) => {
|
|
807
818
|
if (step instanceof FlushClockStep) {
|
|
808
819
|
ids.forEach((id) => appendLog(id, step.startClock, step.state[id]));
|
|
@@ -824,51 +835,51 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
824
835
|
serialize(playerId: string, clock: number): ChitSerializationResponse {
|
|
825
836
|
clock = Math.max(0, Math.min(clock, this.playerVisibleClockTime(playerId)));
|
|
826
837
|
|
|
827
|
-
let chits
|
|
828
|
-
let
|
|
829
|
-
|
|
830
|
-
// going forwards
|
|
831
|
-
let index = this.findIndexOfLastFlushStepBefore(clock);
|
|
832
|
-
const lastLogIndex = this.findIndexOfLastFlushStepWithLogMessageBefore(clock);
|
|
833
|
-
|
|
834
|
-
// set up logs
|
|
838
|
+
let chits: ChitStateLookup;
|
|
839
|
+
let subTurns: { [turnId: string]: ClockDetails } | undefined;
|
|
835
840
|
let log: LogMessageSerializationResponse | undefined;
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
};
|
|
841
|
+
let resultingClock: number;
|
|
842
|
+
let index: number;
|
|
843
|
+
|
|
844
|
+
const cache = this._serializeCache;
|
|
845
|
+
if (cache && cache.playerId === playerId && clock >= cache.resultingClock) {
|
|
846
|
+
chits = { ...cache.chits };
|
|
847
|
+
subTurns = cache.subTurns ? { ...cache.subTurns } : undefined;
|
|
848
|
+
log = cache.log ? { ...cache.log } : undefined;
|
|
849
|
+
resultingClock = cache.resultingClock;
|
|
850
|
+
index = cache.nextIndex;
|
|
851
|
+
} else {
|
|
852
|
+
// Diff-replay: seed with the initial locked snapshot and merge every flush diff
|
|
853
|
+
// up to (but not including) the target clock.
|
|
854
|
+
chits = { ...this.lockedChitStates };
|
|
855
|
+
subTurns = undefined;
|
|
856
|
+
log = undefined;
|
|
857
|
+
resultingClock = -1;
|
|
858
|
+
index = 0;
|
|
842
859
|
}
|
|
843
|
-
let subTurns: { [turnId: string]: ClockDetails } | undefined = undefined;
|
|
844
860
|
|
|
845
|
-
|
|
846
|
-
while (
|
|
861
|
+
let cacheable = true;
|
|
862
|
+
while (index < this.clockSteps.length) {
|
|
847
863
|
const clockStep = this.clockSteps[index];
|
|
848
|
-
|
|
849
|
-
if (!clockStep) {
|
|
850
|
-
break;
|
|
851
|
-
}
|
|
852
|
-
|
|
853
|
-
if (clockStep.startClock >= clock) {
|
|
854
|
-
break;
|
|
855
|
-
}
|
|
864
|
+
if (clockStep.startClock >= clock) break;
|
|
856
865
|
|
|
857
866
|
if (clockStep instanceof FlushClockStep) {
|
|
858
867
|
subTurns = undefined;
|
|
859
|
-
chits
|
|
868
|
+
Object.assign(chits, clockStep.state);
|
|
860
869
|
resultingClock = clockStep.endClock();
|
|
870
|
+
if (clockStep.log) {
|
|
871
|
+
log = { message: clockStep.log, clock: clockStep.startClock };
|
|
872
|
+
}
|
|
873
|
+
index++;
|
|
861
874
|
} else if (clockStep instanceof SubTurnsClockStep) {
|
|
862
875
|
subTurns = {};
|
|
863
876
|
resultingClock = clockStep.startClock;
|
|
864
877
|
let remainingClocksToSpend = clock - clockStep.startClock;
|
|
865
878
|
for (const turn of clockStep.visibleTurns(playerId)) {
|
|
866
879
|
const time = Math.max(remainingClocksToSpend, 0);
|
|
867
|
-
if (time <= 0)
|
|
868
|
-
break;
|
|
869
|
-
}
|
|
880
|
+
if (time <= 0) break;
|
|
870
881
|
|
|
871
|
-
// we are going forward so we never want to have a turn go backwards.
|
|
882
|
+
// we are going forward so we never want to have a turn go backwards. ever.
|
|
872
883
|
const serialized = turn.serialize(playerId, time);
|
|
873
884
|
Object.assign(chits, serialized.chits);
|
|
874
885
|
|
|
@@ -876,13 +887,35 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
876
887
|
remainingClocksToSpend -= serialized.clockDetails.clock;
|
|
877
888
|
subTurns[turn.id] = serialized.clockDetails;
|
|
878
889
|
if (serialized.log) {
|
|
879
|
-
log = serialized.log;
|
|
880
|
-
log.clock += clockStep.startClock;
|
|
890
|
+
log = { message: serialized.log.message, clock: serialized.log.clock + clockStep.startClock };
|
|
881
891
|
}
|
|
882
892
|
}
|
|
893
|
+
// We can only advance `index` past this step (and cache from here) if the
|
|
894
|
+
// requested `clock` consumed this step's *entire* extent — i.e. every visible
|
|
895
|
+
// sub-turn was asked for at least its full clock time. Otherwise a future
|
|
896
|
+
// request at a higher `clock` needs to re-ask the sub-turn for more time,
|
|
897
|
+
// which means re-processing this same step.
|
|
898
|
+
if (clock < clockStep.endClock(playerId)) {
|
|
899
|
+
cacheable = false;
|
|
900
|
+
break;
|
|
901
|
+
}
|
|
902
|
+
index++;
|
|
903
|
+
} else {
|
|
904
|
+
index++;
|
|
883
905
|
}
|
|
884
906
|
}
|
|
885
907
|
|
|
908
|
+
if (cacheable) {
|
|
909
|
+
this._serializeCache = {
|
|
910
|
+
playerId,
|
|
911
|
+
nextIndex: index,
|
|
912
|
+
chits: { ...chits },
|
|
913
|
+
subTurns: subTurns ? { ...subTurns } : undefined,
|
|
914
|
+
log: log ? { ...log } : undefined,
|
|
915
|
+
resultingClock: resultingClock === -1 ? clock : resultingClock,
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
|
|
886
919
|
return {
|
|
887
920
|
log,
|
|
888
921
|
chits,
|
|
@@ -1202,6 +1235,7 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
1202
1235
|
this.unresolvedPrompt = undefined;
|
|
1203
1236
|
this.activeSubTurns = [];
|
|
1204
1237
|
this.pass++;
|
|
1238
|
+
this.invalidateSerializeCache();
|
|
1205
1239
|
}
|
|
1206
1240
|
}
|
|
1207
1241
|
|
|
@@ -1212,6 +1246,15 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
1212
1246
|
type ChitLookup = { [id: string]: Chit };
|
|
1213
1247
|
type ChitStateLookup = { [id: string]: string };
|
|
1214
1248
|
|
|
1249
|
+
type SerializeCache = {
|
|
1250
|
+
playerId: string;
|
|
1251
|
+
nextIndex: number;
|
|
1252
|
+
chits: ChitStateLookup;
|
|
1253
|
+
subTurns?: { [turnId: string]: ClockDetails };
|
|
1254
|
+
log?: { message: string; clock: number };
|
|
1255
|
+
resultingClock: number;
|
|
1256
|
+
};
|
|
1257
|
+
|
|
1215
1258
|
abstract class ClockStep {
|
|
1216
1259
|
abstract get startClock(): number;
|
|
1217
1260
|
abstract endClock(playerId?: string): number;
|
|
@@ -51,11 +51,8 @@ class WebGLRendererWrapper {
|
|
|
51
51
|
const gl = this.renderer.getContext();
|
|
52
52
|
this.memoryExtension = gl.getExtension("WEBGL_debug_renderer_info");
|
|
53
53
|
this.loseContextExt = gl.getExtension("WEBGL_lose_context");
|
|
54
|
-
this.isWebGL2 =
|
|
55
|
-
typeof WebGL2RenderingContext !== "undefined" &&
|
|
56
|
-
gl instanceof WebGL2RenderingContext;
|
|
54
|
+
this.isWebGL2 = typeof WebGL2RenderingContext !== "undefined" && gl instanceof WebGL2RenderingContext;
|
|
57
55
|
|
|
58
|
-
this.renderer.setPixelRatio(this.pixelRatio);
|
|
59
56
|
this.renderer.shadowMap.enabled = true;
|
|
60
57
|
this.renderer.setClearColor(0xffffff, 1.0);
|
|
61
58
|
|
|
@@ -115,7 +112,6 @@ class WebGLRendererWrapper {
|
|
|
115
112
|
this._restoreScheduled = false;
|
|
116
113
|
|
|
117
114
|
// Re-apply renderer state
|
|
118
|
-
this.renderer.setPixelRatio(this.pixelRatio);
|
|
119
115
|
this.renderer.shadowMap.enabled = true;
|
|
120
116
|
this.renderer.setClearColor(0xffffff, 1.0);
|
|
121
117
|
this.renderer.setScissorTest(true);
|
|
@@ -186,6 +182,10 @@ class WebGLRendererWrapper {
|
|
|
186
182
|
return this._contextLost;
|
|
187
183
|
}
|
|
188
184
|
|
|
185
|
+
get maxAnisotropy(): number {
|
|
186
|
+
return this.renderer.capabilities.getMaxAnisotropy();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
189
|
onDirty(cb: () => void): () => void {
|
|
190
190
|
this._dirtyCallbacks.add(cb);
|
|
191
191
|
return () => this._dirtyCallbacks.delete(cb);
|
|
@@ -222,7 +222,9 @@ class WebGLRendererWrapper {
|
|
|
222
222
|
|
|
223
223
|
const composer = new EffectComposer(this.renderer, width, height);
|
|
224
224
|
const renderPass = new RenderPass();
|
|
225
|
-
|
|
225
|
+
// width/height are already physical pixels; pixelRatio=1 so the pass
|
|
226
|
+
// doesn't scale them again when creating its internal render targets.
|
|
227
|
+
const outlinePass = new IDBasedOutlinePass(new Vector2(width, height), 1);
|
|
226
228
|
const outputPass = new OutputPass();
|
|
227
229
|
|
|
228
230
|
// Configure transparency
|
|
@@ -319,12 +321,27 @@ class WebGLRendererWrapper {
|
|
|
319
321
|
}
|
|
320
322
|
}
|
|
321
323
|
|
|
322
|
-
render(
|
|
324
|
+
render(
|
|
325
|
+
sceneWrapper: SceneWrapper,
|
|
326
|
+
camera: Camera,
|
|
327
|
+
context2d: CanvasRenderingContext2D,
|
|
328
|
+
theme: GameTheme,
|
|
329
|
+
viewerId?: string,
|
|
330
|
+
): boolean {
|
|
323
331
|
if (this._contextLost) {
|
|
324
|
-
//
|
|
325
|
-
//
|
|
326
|
-
this.
|
|
327
|
-
|
|
332
|
+
// On some mobile browsers (Android Chrome, iOS Safari) the context may be
|
|
333
|
+
// silently restored after a screen lock without firing `webglcontextrestored`.
|
|
334
|
+
// Detect this by checking the actual GL state instead of relying on the event.
|
|
335
|
+
const glCheck = this.renderer.getContext();
|
|
336
|
+
if (glCheck && !glCheck.isContextLost()) {
|
|
337
|
+
this.handleContextRestored();
|
|
338
|
+
// fall through to render
|
|
339
|
+
} else {
|
|
340
|
+
// Still lost — keep trying to restore. The previous attempt may have used
|
|
341
|
+
// the setSize fallback that doesn't always trigger `webglcontextrestored`.
|
|
342
|
+
this.maybeScheduleRestore();
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
328
345
|
}
|
|
329
346
|
|
|
330
347
|
// Detect silent context loss — on some Android WebViews the `webglcontextlost`
|
|
@@ -337,11 +354,11 @@ class WebGLRendererWrapper {
|
|
|
337
354
|
return false;
|
|
338
355
|
}
|
|
339
356
|
|
|
357
|
+
// Use physical pixel dimensions directly — Viewer.tsx is responsible for
|
|
358
|
+
// passing a canvas sized at the correct physical resolution.
|
|
340
359
|
const canvas = context2d.canvas;
|
|
341
|
-
const width =
|
|
342
|
-
const height =
|
|
343
|
-
const targetWidth = Math.ceil(width * this.pixelRatio);
|
|
344
|
-
const targetHeight = Math.ceil(height * this.pixelRatio);
|
|
360
|
+
const width = canvas.width;
|
|
361
|
+
const height = canvas.height;
|
|
345
362
|
|
|
346
363
|
// Ensure renderer can accommodate this size
|
|
347
364
|
this.ensureRendererSize(width, height, viewerId);
|
|
@@ -353,8 +370,8 @@ class WebGLRendererWrapper {
|
|
|
353
370
|
entry.referenceCount++;
|
|
354
371
|
|
|
355
372
|
try {
|
|
356
|
-
// Set scissor
|
|
357
|
-
this.renderer.setScissor(0, 0,
|
|
373
|
+
// Set scissor and viewport in physical pixels (pixelRatio=1 on the renderer).
|
|
374
|
+
this.renderer.setScissor(0, 0, width, height);
|
|
358
375
|
this.renderer.setViewport(0, 0, width, height);
|
|
359
376
|
|
|
360
377
|
// Clear *depth only* in this panel's scissor region before composer.render().
|
|
@@ -383,18 +400,21 @@ class WebGLRendererWrapper {
|
|
|
383
400
|
return false;
|
|
384
401
|
}
|
|
385
402
|
|
|
386
|
-
// Copy the rendered result to the 2D canvas
|
|
403
|
+
// Copy the rendered result to the 2D canvas.
|
|
404
|
+
// WebGL origin is bottom-left; canvas/DOM is top-left. When the renderer
|
|
405
|
+
// is larger than this viewer (multi-viewer sharing), the rendered region
|
|
406
|
+
// sits at the bottom of the WebGL canvas, so offset by the difference.
|
|
387
407
|
const webglCanvas = this.renderer.domElement;
|
|
388
408
|
context2d.drawImage(
|
|
389
409
|
webglCanvas,
|
|
390
410
|
0,
|
|
391
|
-
webglCanvas.height -
|
|
392
|
-
|
|
393
|
-
|
|
411
|
+
webglCanvas.height - height, // Y-flip for bottom-left WebGL origin
|
|
412
|
+
width,
|
|
413
|
+
height,
|
|
414
|
+
0,
|
|
394
415
|
0,
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
targetHeight, // Dest width, height
|
|
416
|
+
width,
|
|
417
|
+
height,
|
|
398
418
|
);
|
|
399
419
|
return true;
|
|
400
420
|
} finally {
|
|
@@ -16,6 +16,8 @@ export abstract class CanvasOperation {
|
|
|
16
16
|
getImage: GetImage,
|
|
17
17
|
reportOutlet: ReportOutlet,
|
|
18
18
|
): void;
|
|
19
|
+
|
|
20
|
+
abstract benefitsFromMipMap(): boolean;
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
export type RenderCallback = (context: CanvasRenderingContext2D, x: number, y: number, w: number, h: number) => void;
|
|
@@ -25,6 +27,10 @@ export class LayeredCanvasOperation extends CanvasOperation {
|
|
|
25
27
|
super();
|
|
26
28
|
}
|
|
27
29
|
|
|
30
|
+
override benefitsFromMipMap() {
|
|
31
|
+
return this.layers.some((l) => l.benefitsFromMipMap());
|
|
32
|
+
}
|
|
33
|
+
|
|
28
34
|
override render(
|
|
29
35
|
context: CanvasRenderingContext2D,
|
|
30
36
|
bounds: RenderBounds,
|
|
@@ -43,6 +49,10 @@ export class ColorCanvasOperation extends CanvasOperation {
|
|
|
43
49
|
super();
|
|
44
50
|
}
|
|
45
51
|
|
|
52
|
+
override benefitsFromMipMap() {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
|
|
46
56
|
override render(
|
|
47
57
|
context: CanvasRenderingContext2D,
|
|
48
58
|
bounds: RenderBounds,
|
|
@@ -67,6 +77,10 @@ export class VerticalStackCanvasOperation extends CanvasOperation {
|
|
|
67
77
|
super();
|
|
68
78
|
}
|
|
69
79
|
|
|
80
|
+
override benefitsFromMipMap() {
|
|
81
|
+
return this.items.some((i) => i.layer?.benefitsFromMipMap() ?? false);
|
|
82
|
+
}
|
|
83
|
+
|
|
70
84
|
override render(
|
|
71
85
|
context: CanvasRenderingContext2D,
|
|
72
86
|
bounds: RenderBounds,
|
|
@@ -90,6 +104,10 @@ export class HorizontalStackCanvasOperation extends CanvasOperation {
|
|
|
90
104
|
super();
|
|
91
105
|
}
|
|
92
106
|
|
|
107
|
+
override benefitsFromMipMap() {
|
|
108
|
+
return this.items.some((i) => i.layer?.benefitsFromMipMap() ?? false);
|
|
109
|
+
}
|
|
110
|
+
|
|
93
111
|
override render(
|
|
94
112
|
context: CanvasRenderingContext2D,
|
|
95
113
|
bounds: RenderBounds,
|
|
@@ -116,6 +134,10 @@ export class OutletCanvasOperation extends CanvasOperation {
|
|
|
116
134
|
super();
|
|
117
135
|
}
|
|
118
136
|
|
|
137
|
+
override benefitsFromMipMap() {
|
|
138
|
+
return this.child.benefitsFromMipMap();
|
|
139
|
+
}
|
|
140
|
+
|
|
119
141
|
override render(
|
|
120
142
|
context: CanvasRenderingContext2D,
|
|
121
143
|
bounds: RenderBounds,
|
|
@@ -142,6 +164,10 @@ export class PadCanvasOperation extends CanvasOperation {
|
|
|
142
164
|
super();
|
|
143
165
|
}
|
|
144
166
|
|
|
167
|
+
override benefitsFromMipMap() {
|
|
168
|
+
return this.item.benefitsFromMipMap();
|
|
169
|
+
}
|
|
170
|
+
|
|
145
171
|
override render(
|
|
146
172
|
context: CanvasRenderingContext2D,
|
|
147
173
|
bounds: RenderBounds,
|
|
@@ -190,6 +216,10 @@ export class MarkdownCanvasOperation extends CanvasOperation {
|
|
|
190
216
|
super();
|
|
191
217
|
}
|
|
192
218
|
|
|
219
|
+
override benefitsFromMipMap() {
|
|
220
|
+
return true;
|
|
221
|
+
}
|
|
222
|
+
|
|
193
223
|
public height = 0;
|
|
194
224
|
override render(context: CanvasRenderingContext2D, bounds: RenderBounds, getImage: GetImage) {
|
|
195
225
|
const iconMap: {
|
|
@@ -252,6 +282,10 @@ export class TextCanvasOperation extends CanvasOperation {
|
|
|
252
282
|
super();
|
|
253
283
|
}
|
|
254
284
|
|
|
285
|
+
override benefitsFromMipMap() {
|
|
286
|
+
return true;
|
|
287
|
+
}
|
|
288
|
+
|
|
255
289
|
private makeFont(): string {
|
|
256
290
|
const fontSize = this.options.fontSize ?? 16;
|
|
257
291
|
const fontFamily = this.options.fontFamily ?? GameTheme.defaultFontFamily;
|
|
@@ -381,6 +415,10 @@ export class ImageCanvasOperation extends CanvasOperation {
|
|
|
381
415
|
super();
|
|
382
416
|
}
|
|
383
417
|
|
|
418
|
+
override benefitsFromMipMap() {
|
|
419
|
+
return false;
|
|
420
|
+
}
|
|
421
|
+
|
|
384
422
|
draw(
|
|
385
423
|
context: CanvasRenderingContext2D,
|
|
386
424
|
bounds: RenderBounds,
|
|
@@ -554,17 +592,7 @@ export class ImageCanvasOperation extends CanvasOperation {
|
|
|
554
592
|
if (swiF > inset * 2 && shiF > inset * 2) {
|
|
555
593
|
const prevSmoothing = context.imageSmoothingEnabled;
|
|
556
594
|
context.imageSmoothingEnabled = false;
|
|
557
|
-
context.drawImage(
|
|
558
|
-
source as any,
|
|
559
|
-
sxiF + inset,
|
|
560
|
-
syiF + inset,
|
|
561
|
-
swiF - inset * 2,
|
|
562
|
-
shiF - inset * 2,
|
|
563
|
-
x,
|
|
564
|
-
y,
|
|
565
|
-
w,
|
|
566
|
-
h,
|
|
567
|
-
);
|
|
595
|
+
context.drawImage(source as any, sxiF + inset, syiF + inset, swiF - inset * 2, shiF - inset * 2, x, y, w, h);
|
|
568
596
|
context.imageSmoothingEnabled = prevSmoothing;
|
|
569
597
|
}
|
|
570
598
|
}
|
|
@@ -592,10 +620,17 @@ export class ImageCanvasOperation extends CanvasOperation {
|
|
|
592
620
|
}
|
|
593
621
|
|
|
594
622
|
export class PlayerCanvasOperation extends CanvasOperation {
|
|
595
|
-
constructor(
|
|
623
|
+
constructor(
|
|
624
|
+
private player: PlayerChit,
|
|
625
|
+
private colorBlend: number = 0,
|
|
626
|
+
) {
|
|
596
627
|
super();
|
|
597
628
|
}
|
|
598
629
|
|
|
630
|
+
override benefitsFromMipMap() {
|
|
631
|
+
return false;
|
|
632
|
+
}
|
|
633
|
+
|
|
599
634
|
drawFill(context: CanvasRenderingContext2D, bounds: RenderBounds, sourceImage: HTMLImageElement) {
|
|
600
635
|
const x = bounds.x,
|
|
601
636
|
y = bounds.y,
|
|
@@ -611,17 +646,44 @@ export class PlayerCanvasOperation extends CanvasOperation {
|
|
|
611
646
|
sh = sourceImage.height;
|
|
612
647
|
|
|
613
648
|
if (targetAspect > sourceAspect) {
|
|
614
|
-
// Target is wider than source; adjust source height and y to maintain aspect ratio
|
|
615
649
|
sh = sw / targetAspect;
|
|
616
|
-
sy += (sourceImage.height - sh) / 2;
|
|
650
|
+
sy += (sourceImage.height - sh) / 2;
|
|
617
651
|
} else {
|
|
618
|
-
// Target is taller than source; adjust source width and x to maintain aspect ratio
|
|
619
652
|
sw = sh * targetAspect;
|
|
620
|
-
sx += (sourceImage.width - sw) / 2;
|
|
653
|
+
sx += (sourceImage.width - sw) / 2;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
if (this.colorBlend < 1) {
|
|
657
|
+
context.globalAlpha = 1 - this.colorBlend;
|
|
658
|
+
context.drawImage(sourceImage, sx, sy, sw, sh, x, y, w, h);
|
|
621
659
|
}
|
|
622
660
|
|
|
661
|
+
if (this.colorBlend <= 0) return;
|
|
662
|
+
|
|
663
|
+
// Build tinted version on an offscreen canvas using composite blend modes
|
|
664
|
+
const pw = Math.ceil(w),
|
|
665
|
+
ph = Math.ceil(h);
|
|
666
|
+
const offscreen = document.createElement("canvas");
|
|
667
|
+
offscreen.width = pw;
|
|
668
|
+
offscreen.height = ph;
|
|
669
|
+
const offCtx = offscreen.getContext("2d")!;
|
|
670
|
+
|
|
671
|
+
// Grayscale + slight contrast boost via CSS filter
|
|
672
|
+
offCtx.filter = "grayscale(1) contrast(2)";
|
|
673
|
+
offCtx.drawImage(sourceImage, sx, sy, sw, sh, 0, 0, pw, ph);
|
|
674
|
+
offCtx.filter = "none";
|
|
675
|
+
|
|
676
|
+
// For dark player colors, screen blend makes dark areas take the color while whites stay white.
|
|
677
|
+
// For light player colors, multiply blend makes white areas take the color while blacks stay black.
|
|
678
|
+
offCtx.globalCompositeOperation = Colors.default(this.player.color).lightness() < 40 ? "screen" : "multiply";
|
|
679
|
+
offCtx.fillStyle = this.player.color;
|
|
680
|
+
offCtx.fillRect(0, 0, pw, ph);
|
|
681
|
+
offCtx.globalCompositeOperation = "source-over";
|
|
682
|
+
|
|
683
|
+
// Overlay the tinted version at colorBlend strength
|
|
684
|
+
context.globalAlpha = this.colorBlend;
|
|
685
|
+
context.drawImage(offscreen, x, y, w, h);
|
|
623
686
|
context.globalAlpha = 1;
|
|
624
|
-
context.drawImage(sourceImage, sx, sy, sw, sh, x, y, w, h);
|
|
625
687
|
}
|
|
626
688
|
|
|
627
689
|
override render(
|
|
@@ -648,6 +710,10 @@ export class CallbackCanvasOperation extends CanvasOperation {
|
|
|
648
710
|
super();
|
|
649
711
|
}
|
|
650
712
|
|
|
713
|
+
override benefitsFromMipMap() {
|
|
714
|
+
return false;
|
|
715
|
+
}
|
|
716
|
+
|
|
651
717
|
override render(
|
|
652
718
|
context: CanvasRenderingContext2D,
|
|
653
719
|
bounds: RenderBounds,
|
|
@@ -670,6 +736,10 @@ export class RoundedRectCanvasOperation extends CanvasOperation {
|
|
|
670
736
|
super();
|
|
671
737
|
}
|
|
672
738
|
|
|
739
|
+
override benefitsFromMipMap() {
|
|
740
|
+
return this.children.some((c) => c.benefitsFromMipMap());
|
|
741
|
+
}
|
|
742
|
+
|
|
673
743
|
override render(
|
|
674
744
|
context: CanvasRenderingContext2D,
|
|
675
745
|
bounds: RenderBounds,
|