Repository navigation
Expand file tree
/
Copy pathMiniTerrainController.js
More file actions
615 lines (576 loc) · 23.1 KB
/
Copy pathMiniTerrainController.js
File metadata and controls
615 lines (576 loc) · 23.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
import * as THREE from 'three';
import {
MINI_CUT_STYLES,
MINI_RECIPES,
createMiniTerrainState,
miniCompositionUniformValues,
miniLodSegments,
miniParamsPatch,
normalizeMiniTerrainState,
normalizeMiniView,
resolveMiniCloudLayout,
resolveMiniFraming,
resolveMiniLayout,
} from './MiniTerrainSettings.js';
import { miniCutColor } from './miniCutGLSL.js';
import { MiniStudioStage } from './MiniStudioStage.js';
import { MiniWaterCut } from './MiniWaterCut.js';
import { MiniLightMap } from './MiniLightMap.js';
import { applyMiniWaterLook, createMiniWaterSurfaceMaterial } from './MiniWaterMaterial.js';
import { miniStylePatch, miniWaterColors } from './MiniLook.js';
// ============================================================================
// MiniTerrainController — the engine-side owner of the Mini Terrain
// presentation. The engine stays the orchestrator: it calls a handful of
// hooks (uniforms, LOD segments, wall/base sizes, plinth sync, underwater
// volume, per-frame water-cut sync) and this class answers them from the
// normalized Mini document. Presentation objects (studio stage, water cut)
// are created on first activation and disposed with the engine.
//
// Mini is a specialisation of the finite studio world, so `active` also
// requires worldMode === 'studio': no other mode ever sees Mini behaviour.
// ============================================================================
// Which Mini fields map to which params scope (see miniParamsPatch).
const FIELD_SCOPES = Object.freeze({
waterLevel: 'water',
vegetation: 'props',
propScale: 'propScale',
clouds: 'clouds',
quality: 'quality',
});
export class MiniTerrainController {
constructor(engine) {
this.engine = engine;
this.state = null;
this.stage = null;
this.waterCut = null;
this.waterSurface = null;
this.lightMap = null;
this._waterEntry = null;
this._surfaceEntry = null;
this._registeredWaterSystem = null;
this._presentationKey = '';
this._homeKey = '';
this._fieldKey = '';
this._fieldStructure = '';
}
get active() {
return !!this.state && this.engine.worldMode === 'studio';
}
get layout() {
return resolveMiniLayout(this.state);
}
serialize() {
return this.state ? structuredClone(this.state) : null;
}
/** Install a (possibly null) Mini document. Never touches params. */
install(state) {
const next = state ? normalizeMiniTerrainState(state) : null;
const wasActive = !!this.state;
this.state = next;
if (next) this._ensureObjects();
if (wasActive && !next) this._deactivate();
this._presentationKey = '';
this._homeKey = '';
return this.state;
}
/** Create a fresh document for a new project. */
createState(spec = {}) {
return createMiniTerrainState({ recipe: spec.recipe, seed: spec.seed, preset: spec.preset });
}
/** Params that realise the current document (scope: see miniParamsPatch). */
paramsPatch(scope = 'all') {
return this.state ? miniParamsPatch(this.state, scope) : {};
}
recipe() {
return this.state ? MINI_RECIPES[this.state.recipe] : null;
}
/**
* Compute the params patch for a document change. Returns
* { state, params, palette, composition } describing what must be
* invalidated — relief composition, palette and water/cloud/props params
* are separate categories so a vegetation change never rebuilds terrain.
*/
planChange(patch = {}) {
if (!this.state) return null;
const prev = this.state;
if ((patch.bevelSync ?? prev.bevelSync) && (patch.waterBevel !== undefined || patch.terrainBevel !== undefined)) {
const bevel = patch.waterBevel ?? patch.terrainBevel;
patch = { ...patch, waterBevel: bevel, terrainBevel: bevel };
}
const next = normalizeMiniTerrainState({ ...prev, ...patch });
const changed = Object.keys(next).filter((key) => JSON.stringify(next[key]) !== JSON.stringify(prev[key]));
let params = {};
let palette = null;
let composition = false;
if (changed.includes('recipe') || changed.includes('preset')) {
// A new recipe is a new look: relief, water look, palette, clouds, props.
const r = MINI_RECIPES[next.recipe];
next.vegetation = patch.vegetation ?? r.vegetation;
next.clouds = patch.clouds ?? r.clouds;
next.cutStyle = patch.cutStyle ?? r.cutStyle;
params = miniParamsPatch(next, 'all');
palette = miniStylePatch(next.recipe, next.preset);
composition = true;
} else {
if (changed.includes('seed')) {
// A seed replaces the generated composition, detail noise and the
// scattered props; amounts, quality and presentation are kept.
const all = miniParamsPatch(next, 'all');
params.seed = all.seed;
params.propsSeed = all.propsSeed;
composition = true;
}
if (changed.includes('relief')) {
// Relief scales the whole height range: the composition heights, and
// for a procedural diorama the stack itself (heightScale) with the
// formation and water levels that are expressed in the same units.
const all = miniParamsPatch(next, 'all');
const structure = miniParamsPatch(next, 'structure');
params.heightScale = structure.heightScale;
params.terrainFormationSeaLevel = structure.terrainFormationSeaLevel;
Object.assign(params, miniParamsPatch(next, 'water'));
params.cloudAltitude = all.cloudAltitude;
composition = true;
}
if (changed.includes('width')) {
params = { ...params, ...miniParamsPatch(next, 'all') };
composition = true;
}
for (const [field, scope] of Object.entries(FIELD_SCOPES)) {
if (changed.includes(field)) Object.assign(params, miniParamsPatch(next, scope));
}
}
return { state: next, changed, params, palette, composition };
}
// ------------------------------------------------------------------ hooks
/** Composition + cut uniforms (shape 0 / cut 0 when not active). */
applyUniforms(u) {
const active = this.active;
const c = miniCompositionUniformValues(active ? this.state : null);
if (u.uMiniShape) u.uMiniShape.value = c.shape;
u.uMiniFrame?.value.set(...c.frame);
u.uMiniGeometry?.value.set(...c.geometry);
u.uMiniHeights?.value.set(...c.heights);
u.uMiniWobble?.value.set(...c.wobble);
u.uMiniDetail?.value.set(...c.detail);
if (u.uMiniCut) u.uMiniCut.value = active ? 1 : 0;
if (u.uMiniTerrainBevel) u.uMiniTerrainBevel.value = active ? this.layout.terrainBevel : 0;
if (u.uMiniBevelRounded) u.uMiniBevelRounded.value = active && this.layout.rounded ? 1 : 0;
if (u.uMiniHexagon) u.uMiniHexagon.value = active && this.layout.hexagon ? 1 : 0;
if (!active) return;
const style = MINI_CUT_STYLES[this.state.cutStyle] ?? MINI_CUT_STYLES.strata;
const L = this.layout;
u.uMiniCutTopsoil?.value.set(...miniCutColor(style.topsoil));
u.uMiniCutBandA?.value.set(...miniCutColor(style.bandA));
u.uMiniCutBandB?.value.set(...miniCutColor(style.bandB));
u.uMiniCutDeep?.value.set(...miniCutColor(style.deep));
u.uMiniCutSediment?.value.set(...miniCutColor(style.sediment));
u.uMiniCutParams?.value.set(style.bands, -L.baseDepth, Math.max(4, L.width * 0.012), L.reliefHeight);
}
lodSegments() {
return miniLodSegments(this.state);
}
/** Cloud slab placement override (null = the classic board layout). */
cloudLayout() {
return this.active ? resolveMiniCloudLayout(this.state) : null;
}
/**
* Plinth replacement: hide the classic box, lay out the studio stage and
* the water cut, install the camera home. Returns true when Mini owns the
* base (the caller skips its own plinth build).
*/
syncPresentation() {
const eng = this.engine;
if (!this.active) {
this._deactivate();
return false;
}
this._ensureObjects();
const L = this.layout;
const p = eng.params;
if (eng.plinth) eng.plinth.visible = false;
if (eng.diskWall) eng.diskWall.visible = false;
this._ownsPlinth = true;
const maxHeight = L.formationSea + L.heightScale * 0.8;
this.stage.setLayout({
half: L.half,
baseY: -L.baseDepth,
socleHeight: L.socleHeight,
socleMargin: L.socleMargin,
maxHeight,
socle: this.state.socle,
circle: L.shape === 'circle',
hexagon: L.hexagon,
});
this.stage.setBackdrop(this.state.backdrop);
this.stage.setSocleColor(MINI_CUT_STYLES[this.state.cutStyle]?.sediment ?? [0.7, 0.62, 0.5]);
this.stage.setVisible(eng._plinthWanted !== false);
this.waterCut.setBounds({
half: L.half,
bottom: -L.baseDepth,
top: p.seaLevel,
inset: Math.max(0.35, L.width * 0.0005),
bevel: L.waterBevel,
rounded: L.rounded,
hexagon: L.hexagon,
});
this.lightMap.setLayout({ half: L.half, segments: L.segments });
this._applyWaterLook();
this.syncLighting();
eng.waterSystem?.setStudioOverride(this.waterSurface);
this._syncHome();
return true;
}
_applyWaterLook() {
if (!this.state) return;
const L = this.layout;
const look = {
colors: miniWaterColors(this.state.recipe),
params: this.engine.params,
half: L.half,
depthRange: L.reliefHeight * 0.32,
edges: this.state.waterEdges,
bevel: L.waterBevel,
};
applyMiniWaterLook(this.waterSurface, look);
applyMiniWaterLook(this.waterCut?.material, look);
}
syncLighting(state = null) {
if (!this.active) return;
const eng = this.engine;
const skyActive = eng._skyActive?.() ?? false;
this.stage?.setLighting(state ?? eng._resolveCloudLighting?.(), skyActive);
const u = this.waterSurface?.uniforms;
if (u) {
const sky = eng.proceduralSky?.uniforms;
u.uMiniEnvSky.value = skyActive && sky ? 1 : 0;
if (sky) {
u.uSkyZenith.value.copy(sky.uSkyZenith.value);
u.uSkyHorizon.value.copy(sky.uSkyHorizon.value);
}
}
}
/**
* Per-frame: re-bake the light map when the terrain or sun changed, drive
* the water animation clock, and keep the cut faces on the water surface.
*/
beforeRender() {
if (!this.waterCut) return;
const eng = this.engine;
if (this.active && this.lightMap) {
const sun = eng.uniforms.uSunDir.value;
const L = this.layout;
// Structural identity of the diorama: a different recipe/seed/footprint
// must never be shown with data derived from the previous terrain.
const structure = [this.state.recipe, this.state.seed, this.state.preset ?? '', L.half].join(':');
// The light map is baked from the terrain's vertex heights rendered by
// the compiled height program (one small grid, same frame as an edit),
// so it follows every slider live. It needs the program that draws the
// terrain right now: during a structural recompile (or before the first
// program is ready) the previous map is never shown over a different
// relief (white foam plains, floating dark "mountains").
const baker = eng.terrainHeightBaker;
const program = eng._activeHeightProgram?.('studio');
const programKey = program ? `${Math.round(eng.params.octaves)}:${program.heightSig || program.sig}` : null;
const programReady = !!baker?.material && baker.programKey === programKey
&& eng._terrainSourcePendingToken == null;
let lightValid = false;
if (programReady) {
const u = eng.uniforms;
const key = [
eng._terrainGen, programKey, u.uHeightScale.value,
sun.x.toFixed(3), sun.y.toFixed(3), sun.z.toFixed(3), L.half, L.segments, L.terrainBevel, L.rounded, L.hexagon,
].join(':');
const renderHeights = (grid) => baker.renderHeightGrid(grid, this._gridTransform(L));
if (this.lightMap.update(eng.renderer, key, sun, renderHeights)) eng._needsRender = true;
lightValid = this.lightMap.isCurrent(key);
if (lightValid) this._lightStructure = structure;
}
lightValid = lightValid && this._lightStructure === structure;
const lightTex = lightValid ? this.lightMap.texture : null;
// Props are placed from a CPU copy of the same height grid: request it
// whenever the heights changed (not on a sun-only re-bake).
if (lightValid && eng._miniReadbackEnabled !== false) {
const u = eng.uniforms;
const fieldKey = [eng._terrainGen, programKey, u.uHeightScale.value, L.half, L.segments].join(':');
if (fieldKey !== this._fieldKey) {
this._fieldKey = fieldKey;
this.lightMap.requestHeightField(eng.renderer, eng._terrainGen, u.uHeightScale.value, () => {
this._fieldStructure = structure;
eng._needsRender = true;
});
}
}
const p = eng.params;
const anim = p.waterAnim ? (eng.uniforms.uTime?.value ?? 0) * (p.waterAnimSpeed ?? 1) * (p.waterWaveSpeed ?? 1) : 0;
for (const material of [this.waterSurface, this.waterCut.material]) {
applyMiniWaterLook(material, { lightTex, animTime: anim });
}
this.lightMap.decal.visible = !!lightTex && this.stage?.group.visible !== false;
// the decal draws on the terrain chunks as they are this frame (LOD, culling)
if (this.lightMap.decal.visible) this.lightMap.syncChunks(eng.board?.chunks);
// Vegetation and rocks are regular props: they receive the diorama's
// cliff shadows / AO and stand on the live relief (light map B) on the
// GPU, so they follow a slider drag in the very frame it is drawn. While
// no valid map exists (structural recompile) they are hidden instead of
// floating over a relief they were not placed on.
const pm = eng.propsManager;
if (pm) {
pm.setMiniLightMap(lightTex, L.half, true);
pm.suspended = !lightValid || (!!this.lightMap.field && this._fieldStructure !== structure);
}
}
const visible = this.active && !!eng.water?.visible && eng.params.seaLevel > 0.5;
this.waterCut.mesh.visible = visible;
if (visible) {
const L = this.layout;
this.waterCut.setBounds({
half: L.half,
bottom: -L.baseDepth,
top: eng.params.seaLevel,
inset: Math.max(0.35, L.width * 0.0005),
bevel: L.waterBevel,
rounded: L.rounded,
hexagon: L.hexagon,
});
}
}
/**
* Vertex-height grid read back for prop placement (see
* MiniLightMap.requestHeightField), or null when there is none for the
* current diorama yet (the classic analytic sampler is used meanwhile).
*/
propField() {
if (!this.active || !this.lightMap?.field) return null;
const structure = [this.state.recipe, this.state.seed, this.state.preset ?? '', this.layout.half].join(':');
return this._fieldStructure === structure ? this.lightMap.field : null;
}
/**
* uv transform for TerrainHeightBaker.renderHeightGrid so texel i of the
* (segments + 1)^2 grid lands exactly on surface vertex i
* (x = -half + i * width / segments), whatever the shared bake rectangle.
*/
_gridTransform(L) {
const u = this.engine.uniforms;
const o = u.uBakeOrigin.value;
const span = u.uBakeSpan.value;
const sx = Math.max(span.x, 1);
const sz = Math.max(span.y, 1);
const step = (2 * L.half) / L.segments;
const n = L.segments + 1;
this._gridUv ||= new THREE.Vector4();
return this._gridUv.set(
(-L.half - o.x - 0.5 * step) / sx,
(-L.half - o.y - 0.5 * step) / sz,
(step * n) / sx,
(step * n) / sz,
);
}
setStageVisible(visible) {
this.stage?.setVisible(visible && this.active);
if (this.waterCut && !visible) this.waterCut.mesh.visible = false;
}
/** Bounded water volume for the underwater detector (null = unbounded). */
underwaterVolume() {
if (!this.active) return null;
const L = this.layout;
return { minX: -L.half, maxX: L.half, minZ: -L.half, maxZ: L.half, floorY: -L.baseDepth, bevel: L.waterBevel, rounded: L.rounded, hexagon: L.hexagon };
}
/** Walkable rectangle: the footprint, a step inside the cut faces. */
walkBounds() {
if (!this.active) return null;
const L = this.layout;
const m = Math.max(1.5, L.width * 0.004);
const bounds = { minX: -L.half + m, maxX: L.half - m, minZ: -L.half + m, maxZ: L.half - m };
if (L.shape === 'circle') bounds.radius = L.half - m;
if (L.hexagon) bounds.hexagonHalf = L.half - m * 2 / Math.sqrt(3);
return bounds;
}
framing() {
const cam = this.engine.camera;
return resolveMiniFraming(this.state, { fov: cam?.fov ?? 45, aspect: cam?.aspect || 16 / 9 });
}
/** Recentre on the composition (blend = ease, else cut). */
frame({ blend = true } = {}) {
if (!this.active) return;
this._syncHome(true);
this.engine.controls.goToView(this.engine.controls.home, { blend });
this.engine._needsRender = true;
}
saveView(name) {
if (!this.state) return null;
const view = normalizeMiniView({ name: name || `View ${(this.state.camera?.views?.length ?? 0) + 1}`, ...this.engine.controls.getView() });
const views = [...(this.state.camera?.views ?? []), view].slice(-8);
this.state = normalizeMiniTerrainState({ ...this.state, camera: { views } });
return this.state;
}
removeView(index) {
if (!this.state?.camera?.views) return this.state;
const views = this.state.camera.views.filter((_, i) => i !== index);
this.state = normalizeMiniTerrainState({ ...this.state, camera: { views } });
return this.state;
}
goToView(index) {
const view = this.state?.camera?.views?.[index];
if (!view) return false;
this.engine.controls.goToView(view, { blend: true });
this.engine._needsRender = true;
return true;
}
diagnostics() {
if (!this.state) return null;
const L = this.layout;
return {
active: this.active,
recipe: this.state.recipe,
seed: this.state.seed,
width: L.width,
segments: L.segments,
surfaceTriangles: 2 * L.segments * L.segments,
wallTriangles: 8 * L.segments,
lightDecalTriangles: 2 * L.segments * L.segments,
lightMapReady: !!this.lightMap?.ready,
props: this.engine.propsManager?.getDiagnostics?.()?.instances ?? null,
waterCutVisible: !!this.waterCut?.mesh.visible,
};
}
/**
* What the exporter needs to rebuild the diorama outside the app: section
* depth and strata, water colours (linear) and the socle. Null when off.
*/
exportSpec() {
if (!this.active) return null;
const L = this.layout;
const style = MINI_CUT_STYLES[this.state.cutStyle] ?? MINI_CUT_STYLES.strata;
const water = miniWaterColors(this.state.recipe);
return {
width: L.width,
half: L.half,
baseDepth: L.baseDepth,
terrainBevel: L.terrainBevel,
waterBevel: L.waterBevel,
rounded: L.rounded,
hexagon: L.hexagon,
cut: {
topsoil: miniCutColor(style.topsoil),
bandA: miniCutColor(style.bandA),
bandB: miniCutColor(style.bandB),
deep: miniCutColor(style.deep),
sediment: miniCutColor(style.sediment),
bands: style.bands,
topsoilDepth: Math.max(4, L.width * 0.012),
reliefHeight: L.reliefHeight,
},
water: { ...water, depthRange: L.reliefHeight * 0.32 },
socle: this.state.socle
? { height: L.socleHeight, margin: L.socleMargin, color: miniCutColor(style.sediment), circle: L.shape === 'circle', hexagon: L.hexagon }
: null,
};
}
preparationPasses(renderTarget = null) {
if (!this.active) return [];
const eng = this.engine;
return [
...this.lightMap.preparationPasses(),
...[this.stage.backdrop, this.stage.shadow, this.stage.socle, this.lightMap.decalProbe,
this.waterCut.mesh, eng.water && { geometry: eng.water.geometry, material: this.waterSurface }]
.filter(Boolean).map(source => ({ material: source.material, source,
camera: eng.camera, targetScene: eng.scene, renderTarget })),
];
}
/** Materials the engine should warm with the Mini scene. */
compileMaterials() {
return [
this.waterSurface, this.waterCut?.material, ...(this.lightMap?.materials ?? []),
...(this.stage?.materials ?? []),
].filter(Boolean);
}
dispose() {
this._deactivate();
this._bindWaterSystem(null);
this._waterEntry = null;
this._surfaceEntry = null;
this.lightMap?.dispose();
this.waterSurface?.dispose();
this.stage?.dispose();
this.waterCut?.dispose();
this.lightMap = null;
this.waterSurface = null;
this.stage = null;
this.waterCut = null;
}
// ---------------------------------------------------------------- internal
_ensureObjects() {
const eng = this.engine;
if (!eng.scene || !eng.uniforms) return;
if (!this.stage) {
this.stage = new MiniStudioStage(eng.uniforms);
eng.scene.add(this.stage.group);
}
if (!this.waterCut) {
this.waterCut = new MiniWaterCut(eng.uniforms);
eng.scene.add(this.waterCut.mesh);
this._waterEntry = {
mesh: this.waterCut.mesh,
material: this.waterCut.material,
applySettings: (params) => applyMiniWaterLook(this.waterCut?.material, { params }),
};
}
if (!this.waterSurface) {
// Rendered on the studio water mesh through the WaterSystem override;
// registered as auxiliary so it receives the water-free scene capture.
this.waterSurface = createMiniWaterSurfaceMaterial(eng.uniforms);
this._surfaceEntry = {
material: this.waterSurface,
applySettings: (params) => applyMiniWaterLook(this.waterSurface, { params }),
};
}
if (!this.lightMap) {
this.lightMap = new MiniLightMap(eng.uniforms);
this.stage.group.add(this.lightMap.decal);
}
this._bindWaterSystem(eng.waterSystem ?? null);
}
_bindWaterSystem(system) {
const previous = this._registeredWaterSystem;
if (previous === system) return;
for (const entry of [this._waterEntry, this._surfaceEntry]) {
if (entry) previous?.removeAuxiliaryWater(entry);
}
if (previous?.studioOverride === this.waterSurface) previous.setStudioOverride(null);
this._registeredWaterSystem = system;
for (const entry of [this._waterEntry, this._surfaceEntry]) {
if (entry) system?.addAuxiliaryWater(entry);
}
}
_deactivate() {
const eng = this.engine;
if (this.stage) this.stage.setVisible(false);
if (this.waterCut) this.waterCut.mesh.visible = false;
eng.propsManager?.setMiniLightMap(null);
if (eng.propsManager) eng.propsManager.suspended = false;
this._fieldKey = '';
if (eng.waterSystem?.studioOverride && eng.waterSystem.studioOverride === this.waterSurface) {
eng.waterSystem.setStudioOverride(null);
}
if (this._ownsPlinth) {
this._ownsPlinth = false;
if (eng.plinth) eng.plinth.visible = eng._plinthWanted !== false;
eng._plinthGeometryKey = null;
}
if (eng.controls?.home) {
eng.controls.setHome(null);
eng._plinthGeometryKey = null;
}
this._homeKey = '';
}
_syncHome(force = false) {
const controls = this.engine.controls;
if (!controls) return;
const f = this.framing();
const key = JSON.stringify([f.theta, f.radius, f.target, f.minRadius, f.maxRadius]);
if (!force && key === this._homeKey && controls.home) return;
this._homeKey = key;
controls.setHome(f);
}
}