copper3d 3.10.4 → 3.11.1
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/README.md +128 -0
- package/ai-index/guides/4D-MRI-LV-rendering.md +580 -0
- package/ai-index/guides/ai-assist-api.md +382 -0
- package/ai-index/guides/camera-framing.md +238 -0
- package/ai-index/guides/control-axes.md +124 -0
- package/ai-index/guides/copper3d-4D-API.md +219 -0
- package/ai-index/guides/copper3d-rendering-optimization.md +155 -0
- package/ai-index/guides/gltf-decoders.md +105 -0
- package/ai-index/guides/heart-sensor.md +137 -0
- package/ai-index/guides/load-callbacks.md +244 -0
- package/ai-index/guides/mcp-server.md +139 -0
- package/ai-index/guides/nrrd-tools.md +1888 -0
- package/ai-index/guides/on-demand-controls.md +154 -0
- package/ai-index/guides/rotation-pivot.md +197 -0
- package/ai-index/guides/scene-resources.md +227 -0
- package/ai-index/guides/segmentation-module.md +1539 -0
- package/ai-index/guides/surface-annotation-api.md +388 -0
- package/ai-index/guides/volume-display.md +194 -0
- package/ai-index/manifest.json +144 -0
- package/ai-index/symbols.json +1 -0
- package/dist/Utils/segmentation/DrawToolCore.d.ts +19 -0
- package/dist/Utils/segmentation/DrawToolCore.js +62 -36
- package/dist/Utils/segmentation/DrawToolCore.js.map +1 -1
- package/dist/Utils/segmentation/tools/DrawingTool.js +14 -2
- package/dist/Utils/segmentation/tools/DrawingTool.js.map +1 -1
- package/dist/Utils/segmentation/tools/ToolHost.d.ts +2 -1
- package/dist/bundle.esm.js +79 -41
- package/dist/bundle.umd.js +79 -41
- package/dist/types/Utils/segmentation/DrawToolCore.d.ts +19 -0
- package/dist/types/Utils/segmentation/tools/ToolHost.d.ts +2 -1
- package/package.json +7 -4
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# On-Demand Rendering & Trackball Controls
|
|
2
|
+
|
|
3
|
+
`copperSceneOnDemond` draws a frame only when something asks for one, instead
|
|
4
|
+
of running a `requestAnimationFrame` loop forever. That saves a laptop's battery
|
|
5
|
+
and lets several viewers share a page — but it changes what a controls class has
|
|
6
|
+
to do, and `Copper3dTrackballControls` did not do it until **3.9.0**.
|
|
7
|
+
|
|
8
|
+
## The deadlock
|
|
9
|
+
|
|
10
|
+
`Copper3dTrackballControls` splits a gesture in two. Its pointer handlers only
|
|
11
|
+
*record* positions:
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
function onMouseMove(event) {
|
|
15
|
+
if (state === STATE.ROTATE && !scope.noRotate) {
|
|
16
|
+
_movePrev.copy(_moveCurr);
|
|
17
|
+
_moveCurr.copy(getMouseOnCircle(event.pageX, event.pageY));
|
|
18
|
+
}
|
|
19
|
+
// ...nothing has moved yet
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The camera only actually moves inside `update()`, which is also the only place
|
|
24
|
+
`change` is dispatched.
|
|
25
|
+
|
|
26
|
+
Under a continuous render loop that is fine — `render()` calls `update()` sixty
|
|
27
|
+
times a second, so recorded input is applied on the next frame. Under on-demand
|
|
28
|
+
rendering it closes a cycle with no way in:
|
|
29
|
+
|
|
30
|
+
> no render → no `update()` → the camera never moves → no `change` →
|
|
31
|
+
> nothing requests a render
|
|
32
|
+
|
|
33
|
+
The viewer ignores the mouse completely. Nothing throws, nothing warns, and
|
|
34
|
+
every unit test stays green, because the controls object's internal state *is*
|
|
35
|
+
being updated correctly — it just never reaches the camera.
|
|
36
|
+
|
|
37
|
+
`OrbitControls` has no such problem: it moves the camera inside the pointer
|
|
38
|
+
handler and dispatches `change` there, which is why swapping one for the other
|
|
39
|
+
appears to break a viewer that was working.
|
|
40
|
+
|
|
41
|
+
## The fix: `updateOnInput`
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
const controls = new Copper.Copper3dTrackballControls(scene.camera, canvas);
|
|
45
|
+
controls.updateOnInput = true;
|
|
46
|
+
controls.addEventListener("change", scene.requestRenderIfNotRequested);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
With the flag on, `onMouseMove`, `onTouchMove` and the wheel handler each call
|
|
50
|
+
`update()` themselves. The camera moves during the gesture, `change` fires with
|
|
51
|
+
the camera already in its new position, and your listener schedules the frame.
|
|
52
|
+
|
|
53
|
+
**It is off by default**, and that is deliberate — see the next section.
|
|
54
|
+
|
|
55
|
+
### Use `staticMoving` with it
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
controls.updateOnInput = true;
|
|
59
|
+
controls.staticMoving = true; // strongly recommended
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
`staticMoving = false` (the default) gives the camera inertia: it keeps gliding
|
|
63
|
+
after the pointer stops, and each `update()` decays that glide by one step. Two
|
|
64
|
+
consequences:
|
|
65
|
+
|
|
66
|
+
- Input-driven updates make the glide **shorter**, because input events add
|
|
67
|
+
extra decay steps on top of the per-frame ones. This is why the flag cannot
|
|
68
|
+
simply default to on: it would change the feel of every existing
|
|
69
|
+
continuous-render viewer.
|
|
70
|
+
- Inertia needs frames *after* the input stops, and on-demand rendering has
|
|
71
|
+
nothing left to trigger them — the glide stalls partway. If you want inertia
|
|
72
|
+
under on-demand rendering, hold a continuous render loop open for the
|
|
73
|
+
duration of the gesture instead.
|
|
74
|
+
|
|
75
|
+
With `staticMoving = true` the camera stops exactly where the pointer left it,
|
|
76
|
+
which is what an on-demand viewer wants anyway, and a redundant `update()` is a
|
|
77
|
+
no-op.
|
|
78
|
+
|
|
79
|
+
### What it does not change
|
|
80
|
+
|
|
81
|
+
`updateOnInput` is about *when* the camera is updated, not *whether* it may be.
|
|
82
|
+
`enabled`, `noRotate`, `noZoom` and `noPan` are all still honoured — a locked
|
|
83
|
+
axis stays locked.
|
|
84
|
+
|
|
85
|
+
## Choosing the controls up front <Badge type="tip" text="3.9.0" />
|
|
86
|
+
|
|
87
|
+
`copperSceneOnDemond` hardcoded `new OrbitControls(...)`, so the swap below was
|
|
88
|
+
the only way to get a trackball. `createScene` now takes a per-scene option:
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
const scene = renderer.createScene("case-1", { controls: "copper3d" });
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
| value | class | notes |
|
|
95
|
+
|---|---|---|
|
|
96
|
+
| *omitted* | `OrbitControls` | The default, unchanged. |
|
|
97
|
+
| `"copper3d"` | `Copper3dTrackballControls` | `updateOnInput` is turned on for you. |
|
|
98
|
+
| `"trackball"` | three's `TrackballControls` | Same deadlock, no equivalent flag — you must pump frames yourself. Prefer `"copper3d"`. |
|
|
99
|
+
|
|
100
|
+
The default stays `OrbitControls` even though `copperScene`'s default is the
|
|
101
|
+
trackball: changing it would silently swap the controls under every existing
|
|
102
|
+
on-demand viewer.
|
|
103
|
+
|
|
104
|
+
The renderer's own `options.controls` is deliberately **not** consulted. It has
|
|
105
|
+
never had any effect on on-demand scenes — it is read only by `copperScene` —
|
|
106
|
+
so honouring it now would change behaviour for anyone who set it and never
|
|
107
|
+
noticed. Pass it to `createScene` instead.
|
|
108
|
+
|
|
109
|
+
## Full example
|
|
110
|
+
|
|
111
|
+
Only needed when you are replacing the controls on a scene that already exists
|
|
112
|
+
— for a new scene, pass `{ controls: "copper3d" }` to `createScene` instead.
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
const renderer = new Copper.copperRendererOnDemond(container);
|
|
116
|
+
const scene = renderer.getSceneByName("case-1")
|
|
117
|
+
?? renderer.createScene("case-1");
|
|
118
|
+
|
|
119
|
+
// Swap in the trackball, keeping on-demand rendering working.
|
|
120
|
+
scene.controls.removeEventListener("change", scene.requestRenderIfNotRequested);
|
|
121
|
+
scene.controls.enabled = false;
|
|
122
|
+
scene.controls.dispose?.();
|
|
123
|
+
|
|
124
|
+
const controls = new Copper.Copper3dTrackballControls(
|
|
125
|
+
scene.camera,
|
|
126
|
+
renderer.renderer.domElement
|
|
127
|
+
);
|
|
128
|
+
controls.staticMoving = true;
|
|
129
|
+
controls.updateOnInput = true;
|
|
130
|
+
controls.addEventListener("change", scene.requestRenderIfNotRequested);
|
|
131
|
+
|
|
132
|
+
scene.controls = controls;
|
|
133
|
+
controls.handleResize(); // it caches the canvas box; call after any resize
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Two things worth knowing about that swap:
|
|
137
|
+
|
|
138
|
+
- Drop the old `change` listener **before** disposing the old controls, so a
|
|
139
|
+
disposed instance cannot request one last frame on its way out.
|
|
140
|
+
- `handleResize()` is not optional. Unlike `OrbitControls`, the trackball caches
|
|
141
|
+
the canvas's page-relative box in `screen` and only recomputes it here, so a
|
|
142
|
+
container resize leaves every pointer position wrong until you call it.
|
|
143
|
+
|
|
144
|
+
## Upgrading from 3.8.x
|
|
145
|
+
|
|
146
|
+
Nothing to do. `updateOnInput` defaults to `false`, which is exactly the
|
|
147
|
+
behaviour you have today. Set it to `true` only on viewers that render on
|
|
148
|
+
demand.
|
|
149
|
+
|
|
150
|
+
If you worked around this by listening for pointer and wheel events yourself and
|
|
151
|
+
calling `requestRenderIfNotRequested()`, you can delete that once the flag is
|
|
152
|
+
on. Keep a browser-level test that drags the viewer and asserts the picture
|
|
153
|
+
changed — this failure mode is invisible to unit tests, and to any test that
|
|
154
|
+
only checks that listeners were registered.
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# Rotating Around the Content, Not the Pan
|
|
2
|
+
|
|
3
|
+
`Copper3dTrackballControls.rotationPivot` decides what a rotate gesture does after
|
|
4
|
+
the reader has panned. It is the answer to "why does my model swing off into space
|
|
5
|
+
once I've moved it?"
|
|
6
|
+
|
|
7
|
+
::: tip New in 3.10.0
|
|
8
|
+
Additive and opt-in. `rotationPivot` defaults to `null`, which is exactly the
|
|
9
|
+
behaviour every earlier version had — leave it alone and nothing changes.
|
|
10
|
+
:::
|
|
11
|
+
|
|
12
|
+
For a drop-in helper that switches between all three behaviours at runtime, and how
|
|
13
|
+
to tell them apart on screen, see
|
|
14
|
+
[Switching Rotation Behaviour After a Pan](/examples/rotation-pivot-switch).
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 1. The problem
|
|
19
|
+
|
|
20
|
+
`panCamera()` translates the **camera and `target` together**:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
scope.object.position.add(pan);
|
|
24
|
+
scope.target.add(pan); // <- the pivot moves with the camera
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
That is what makes a pan a pan: the view slides sideways and the framing stays put.
|
|
28
|
+
But `target` is also the point rotation orbits, because `rotateCamera()` works on
|
|
29
|
+
`_eye = position - target` and `update()` puts the camera back at
|
|
30
|
+
`target + eye`.
|
|
31
|
+
|
|
32
|
+
So the moment someone pans, the orbit pivot is no longer on the content. The next
|
|
33
|
+
rotate swings the volume through a wide arc around an empty point in space instead
|
|
34
|
+
of turning it on the spot. The further they panned, the worse it gets — and nothing
|
|
35
|
+
throws, nothing warns, it just feels broken.
|
|
36
|
+
|
|
37
|
+
## 2. The obvious fix, and what it costs
|
|
38
|
+
|
|
39
|
+
The usual repair is to snap `target` back onto the content's centre when a rotate
|
|
40
|
+
starts. `Copper3dOrbitControls` does exactly this, on the controls' `start` event:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
this._onStart = () => {
|
|
44
|
+
if (isRotateGesture(this.state)) this.target.copy(this._pivot);
|
|
45
|
+
};
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
It fixes the axis. It also **throws the pan away**: assigning `target` does not move
|
|
49
|
+
the camera — `update()` re-derives the offset from the camera's current position —
|
|
50
|
+
but `object.lookAt(target)` re-aims it, so the content slides back to the middle of
|
|
51
|
+
the viewport as the rotation begins.
|
|
52
|
+
|
|
53
|
+
For many viewers that is fine, even desirable. For a reader who panned deliberately
|
|
54
|
+
— to put a lesion against the edge of a panel, next to a measurement — it is not.
|
|
55
|
+
|
|
56
|
+
## 3. `rotationPivot`
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
controls.rotationPivot = new THREE.Vector3(cx, cy, cz); // world space
|
|
60
|
+
controls.rotationPivot = null; // back to orbiting target
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
| | type | default |
|
|
64
|
+
|---|---|---|
|
|
65
|
+
| `rotationPivot` | `Vector3 \| null` | `null` |
|
|
66
|
+
|
|
67
|
+
**Nothing here is on by default.** The default is `null`, which is the stock
|
|
68
|
+
trackball behaviour described in section 1 — a pan drags the orbit pivot with it.
|
|
69
|
+
That is true in 3.10.0 as much as in every earlier version; you have to set the
|
|
70
|
+
pivot for anything to change.
|
|
71
|
+
|
|
72
|
+
With a pivot set, the frame's rotation `q` is applied to `target - pivot` as well as
|
|
73
|
+
to `_eye` and `object.up`. Writing `C` for the pivot:
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
target' = C + q(target − C)
|
|
77
|
+
eye' = q(eye)
|
|
78
|
+
position' = target' + eye'
|
|
79
|
+
= C + q(target − C) + q(position − target)
|
|
80
|
+
= C + q(position − C)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The whole camera rig — camera *and* target — rotates rigidly about `C`. Two things
|
|
84
|
+
follow:
|
|
85
|
+
|
|
86
|
+
- **Every distance to `C` is preserved.** The content turns about its own centre.
|
|
87
|
+
- **`C`'s projection on screen does not move.** The pan offset survives: the content
|
|
88
|
+
stays exactly where it was dragged to while it rotates.
|
|
89
|
+
|
|
90
|
+
When nothing has been panned, `target` *is* the pivot, so the offset being rotated
|
|
91
|
+
is the zero vector and the whole thing is a no-op. A viewer that never pans cannot
|
|
92
|
+
tell the difference.
|
|
93
|
+
|
|
94
|
+
## 4. Using it
|
|
95
|
+
|
|
96
|
+
Set the pivot once per piece of content, in world space:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
const box = new THREE.Box3();
|
|
100
|
+
box.expandByObject(nrrdMesh.x);
|
|
101
|
+
box.expandByObject(nrrdMesh.y);
|
|
102
|
+
box.expandByObject(nrrdMesh.z);
|
|
103
|
+
|
|
104
|
+
controls.rotationPivot = box.getCenter(new THREE.Vector3());
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
::: warning It is world space, and it does not follow anything
|
|
108
|
+
Measure it from the objects themselves rather than assuming the origin. Slice planes
|
|
109
|
+
carry the volume's own offset and a GLB's bounding box is rarely centred, so the
|
|
110
|
+
world origin is usually the wrong point — the same mistake `fitView` avoids with
|
|
111
|
+
`bounds.center`.
|
|
112
|
+
|
|
113
|
+
Recompute it whenever the content changes (a new case, a new volume). Nothing
|
|
114
|
+
invalidates it for you.
|
|
115
|
+
:::
|
|
116
|
+
|
|
117
|
+
Clearing it is a normal thing to do, not a teardown step — a scene where the reader
|
|
118
|
+
should be able to orbit whatever they panned to still wants `null`.
|
|
119
|
+
|
|
120
|
+
### With `fitView` / `setCameraPose`
|
|
121
|
+
|
|
122
|
+
No interaction to manage. Both write `controls.target`, and both aim at the
|
|
123
|
+
content's centre, so after a re-frame or a "reset view" the target and the pivot
|
|
124
|
+
coincide again and the pan offset is genuinely zero. Set the pivot from the same
|
|
125
|
+
box you pass to `fitView` as `bounds.center` and the two agree by construction.
|
|
126
|
+
|
|
127
|
+
### Feature detection
|
|
128
|
+
|
|
129
|
+
The property is a plain field initialised in the constructor, so `in` is enough to
|
|
130
|
+
tell an older build apart and fall back to the snap-on-rotate pattern:
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
if ("rotationPivot" in controls) {
|
|
134
|
+
controls.rotationPivot = centre;
|
|
135
|
+
} else {
|
|
136
|
+
// 3.9.x and earlier: snap the target on rotate start instead, accepting that
|
|
137
|
+
// the pan is discarded. Register in the CAPTURE phase — the trackball latches
|
|
138
|
+
// its gesture state in its own bubble-phase pointerdown handler on the canvas.
|
|
139
|
+
container.addEventListener("pointerdown", (event) => {
|
|
140
|
+
if (isRotateButton(event)) controls.target.copy(centre);
|
|
141
|
+
}, true);
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## 5. What it does not change
|
|
146
|
+
|
|
147
|
+
- **Zoom still moves toward `target`,** not toward the pivot — `zoomCamera()` scales
|
|
148
|
+
`_eye`. That is what keeps the framing steady while zooming a panned view; zooming
|
|
149
|
+
toward the pivot would drag the content back toward the centre.
|
|
150
|
+
- **`minDistance` / `maxDistance` still clamp `|position − target|`,** not the
|
|
151
|
+
distance to the pivot. With a large pan those differ slightly; the limits are
|
|
152
|
+
soft-feel bounds, so it does not matter in practice.
|
|
153
|
+
- **Damping is handled.** The glide after the pointer is released rotates the target
|
|
154
|
+
too, so the rig cannot come apart over those frames.
|
|
155
|
+
- **`noRotate`, `enabled` and `updateOnInput` are untouched.** A locked axis stays
|
|
156
|
+
locked; this only changes *what* a permitted rotation turns about.
|
|
157
|
+
|
|
158
|
+
## 6. Which one should you use?
|
|
159
|
+
|
|
160
|
+
There are **three** states, not two — doing nothing is one of them, and it is where
|
|
161
|
+
you start:
|
|
162
|
+
|
|
163
|
+
| | how you get it | rotation axis | a pan the reader made | needs |
|
|
164
|
+
|---|---|---|---|---|
|
|
165
|
+
| **orbit-target** | do nothing — the default | the panned `target` | kept, but the axis is wrong | — |
|
|
166
|
+
| **recentre** | snap `target` on rotate start | content's centre | discarded when rotation starts | any version |
|
|
167
|
+
| **keep-pan** | `rotationPivot = centre` | content's centre | kept | 3.10.0+ |
|
|
168
|
+
|
|
169
|
+
The first row is the behaviour this page exists to fix, so the real choice is
|
|
170
|
+
between the last two. Both make "the centre of rotation is the centre of the image"
|
|
171
|
+
true; they differ only in what happens to the pan, so pick on that.
|
|
172
|
+
|
|
173
|
+
**Never run the last two at once** — the snap would discard the very offset the
|
|
174
|
+
pivot is preserving, and you would get `recentre` while believing you configured
|
|
175
|
+
`keep-pan`.
|
|
176
|
+
|
|
177
|
+
To see which one a viewer is currently in: pan the content into a corner of the
|
|
178
|
+
viewport, then drag to rotate. It swings away on a wide arc (`orbit-target`), snaps
|
|
179
|
+
back to the middle and then turns (`recentre`), or stays in the corner and turns
|
|
180
|
+
(`keep-pan`).
|
|
181
|
+
|
|
182
|
+
## 7. Upgrading from 3.9.x
|
|
183
|
+
|
|
184
|
+
Nothing to do. The default is `null` and the rotation path is byte-identical to
|
|
185
|
+
before when it is unset or equal to `target`.
|
|
186
|
+
|
|
187
|
+
If you are currently snapping `target` on rotate start, **remove that** when you set
|
|
188
|
+
a pivot — the snap would throw away the very offset the pivot is preserving.
|
|
189
|
+
|
|
190
|
+
## 8. Tests
|
|
191
|
+
|
|
192
|
+
`src/ts/__tests__/trackballRotationPivot.test.ts` pins the rigid-rotation
|
|
193
|
+
invariants: distance to the pivot, the pan offset's length, and the camera-to-target
|
|
194
|
+
distance all survive a rotate, the camera actually moves, and an un-panned viewer
|
|
195
|
+
produces identical output with and without a pivot. These are all silent failures —
|
|
196
|
+
each wrong version still renders a plausible picture — so they are worth asserting
|
|
197
|
+
rather than eyeballing.
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
# copper3d — Scene Resources
|
|
2
|
+
|
|
3
|
+
Freeing what a scene holds, bounding how much stays resident, and crossfading one model
|
|
4
|
+
into another. All exported from the package entry (`copper3d`).
|
|
5
|
+
|
|
6
|
+
::: tip New in 3.8.0
|
|
7
|
+
Additive only. The residency budget is **off by default** — nothing is evicted unless you
|
|
8
|
+
build one and act on it.
|
|
9
|
+
:::
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## 1. `disposeObject3D()` and `disposeMaterial()`
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
disposeObject3D(root: DisposableObject3D): void
|
|
17
|
+
disposeMaterial(material: DisposableMaterial | DisposableMaterial[] | null | undefined): void
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`scene.remove()` only unlinks. three.js keeps the geometry's buffers and the material's
|
|
21
|
+
textures uploaded until they are disposed explicitly, and nothing in copper3d disposes
|
|
22
|
+
what it creates — so anything that swaps models or evicts scenes needs this, or its
|
|
23
|
+
footprint is unbounded.
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
scene.scene.remove(oldModel);
|
|
27
|
+
disposeObject3D(oldModel);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Two things worth knowing:
|
|
31
|
+
|
|
32
|
+
- **`Material.dispose()` does not cascade into its textures.** A canvas-backed slice
|
|
33
|
+
texture or a glTF baseColor map stays on the GPU without an explicit call.
|
|
34
|
+
`disposeMaterial` walks the texture slots three's built-in materials use (`map`,
|
|
35
|
+
`normalMap`, `roughnessMap`, `metalnessMap`, `aoMap`, `emissiveMap`, `alphaMap`,
|
|
36
|
+
`bumpMap`, `displacementMap`, `envMap`, `lightMap`, `specularMap`).
|
|
37
|
+
- **It handles the array form.** A multi-material mesh would otherwise reach
|
|
38
|
+
`Array.prototype.dispose`, which does not exist.
|
|
39
|
+
|
|
40
|
+
`disposeObject3D` works on a lone mesh as well as on a whole subtree — `traverse` visits
|
|
41
|
+
the root itself — so a slice plane that was never added to a scene is freed by the same
|
|
42
|
+
call. That matters for `loadNrrd`, which builds a full `VolumeSlice` (its own geometry,
|
|
43
|
+
material and canvas-backed texture) for **x, y and z**, whether or not you display all
|
|
44
|
+
three:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
scene.loadNrrd(url, bar, true, (volume, meshes, slices) => {
|
|
48
|
+
scene.addObject(meshes.z);
|
|
49
|
+
// An axial-only viewer never shows these, and they are retained in
|
|
50
|
+
// `volume.sliceList` for the volume's whole lifetime otherwise.
|
|
51
|
+
disposeObject3D(meshes.x);
|
|
52
|
+
disposeObject3D(meshes.y);
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## 2. `disposeScene()` and `removeSceneFromMap()`
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
disposeScene(host: SceneDisposalHost, name: string): DisposableScene | undefined
|
|
62
|
+
removeSceneFromMap(host: SceneDisposalHost, name: string): void
|
|
63
|
+
|
|
64
|
+
// or as a method
|
|
65
|
+
copperRendererOnDemond.disposeScene(name)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Until 3.8.0 there was **no way out of `sceneMap` at all** — nothing removed an entry, so a
|
|
69
|
+
long-lived renderer accumulated every scene it had ever built, decoded volumes included.
|
|
70
|
+
|
|
71
|
+
`disposeScene` unregisters the scene, disables its controls, drops the `change` listener,
|
|
72
|
+
and frees every child's geometry, material and textures. It returns the scene it disposed,
|
|
73
|
+
or `undefined` if there was none under that name.
|
|
74
|
+
|
|
75
|
+
`removeSceneFromMap` unregisters **without freeing anything**, for callers re-registering
|
|
76
|
+
a scene under a different name.
|
|
77
|
+
|
|
78
|
+
### It deliberately does not call `controls.dispose()`
|
|
79
|
+
|
|
80
|
+
That looks like the obvious way to break the last reference, and it is a trap. Every scene
|
|
81
|
+
built by one renderer shares **one canvas**, and `OrbitControls.dispose()` calls
|
|
82
|
+
`disconnect()`, which ends with `domElement.style.touchAction = ''`. Only `connect()` ever
|
|
83
|
+
sets it back to `'none'`. So disposing one evicted scene's controls re-enables browser
|
|
84
|
+
touch scrolling over the canvas for whichever scene is actually on screen — one eviction
|
|
85
|
+
breaks touch orbiting for the rest of the session.
|
|
86
|
+
|
|
87
|
+
Removing just the `change` listener breaks the same reference chain and touches nothing
|
|
88
|
+
shared.
|
|
89
|
+
|
|
90
|
+
### It fails loudly if removal did not take
|
|
91
|
+
|
|
92
|
+
`delete` on a missing property is a silent no-op, so restructuring `sceneMap` into a real
|
|
93
|
+
`Map` would stop eviction working with no crash and no type error — the map would grow
|
|
94
|
+
without bound again, invisibly. Removal is confirmed through the host's own accessor and
|
|
95
|
+
throws if the entry survived.
|
|
96
|
+
|
|
97
|
+
### It sweeps children, not names
|
|
98
|
+
|
|
99
|
+
Iterating `scene.children` rather than removing objects by a hardcoded name list: a name
|
|
100
|
+
list silently misses anything added under a name nobody thought to list.
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
## 2a. `copperSceneOnDemond.dispose()` <Badge type="tip" text="3.9.0" />
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
scene.dispose(): void
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Releases what the scene attached **outside itself** — specifically the `resize` listener
|
|
111
|
+
its constructor puts on `window`.
|
|
112
|
+
|
|
113
|
+
Before 3.9.0 that listener had no counterpart. Every scene ever created stayed subscribed
|
|
114
|
+
for the life of the page, kept alive by the closure, and went on calling
|
|
115
|
+
`requestRenderIfNotRequested` — and so `onWindowResize`, which resizes the **shared**
|
|
116
|
+
renderer — long after it stopped being displayed. An app that builds a scene per case
|
|
117
|
+
leaks one per case, and they all fire on every window resize.
|
|
118
|
+
|
|
119
|
+
`disposeScene()` now calls it for you, so eviction is complete on its own. Call it
|
|
120
|
+
directly only when you are dropping a scene without going through the renderer's map:
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
scene.dispose();
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
It does not touch the scene graph or the renderer. What a scene's contents are worth
|
|
127
|
+
keeping is the caller's decision, and the renderer is shared with every other scene —
|
|
128
|
+
`disposeScene` is the one that tears contents down. Safe to call twice, and safe on a
|
|
129
|
+
scene class that has none (`disposeScene` calls it optionally).
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## 3. Residency budget
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
createSceneBudget(limitBytes: number): SceneBudget
|
|
137
|
+
defaultBudgetBytes(deviceMemoryGiB?: number): number
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
An LRU budget **in bytes**, with pinning. It decides *which* scenes to evict; you call
|
|
141
|
+
`disposeScene` on the names it hands back.
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
const budget = createSceneBudget(defaultBudgetBytes(navigator.deviceMemory));
|
|
145
|
+
|
|
146
|
+
// after a scene finishes loading
|
|
147
|
+
budget.register(name, volume.data.byteLength);
|
|
148
|
+
budget.pin(name); // it is on screen
|
|
149
|
+
for (const victim of budget.overflow()) {
|
|
150
|
+
renderer.disposeScene(victim);
|
|
151
|
+
budget.release(victim);
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
| method | meaning |
|
|
156
|
+
|---|---|
|
|
157
|
+
| `register(key, bytes)` | Records (or re-records) a resident scene's decoded size and marks it most-recently-used. |
|
|
158
|
+
| `touch(key)` | Marks an already-registered scene most-recently-used. No-op if absent. |
|
|
159
|
+
| `pin(key)` / `unpin(key)` | Protects a scene from eviction while it is on screen. |
|
|
160
|
+
| `release(key)` | Forgets a scene entirely. Call after its GPU resources are freed. |
|
|
161
|
+
| `rename(from, to)` | Carries bytes, queue position and pin state across, for callers that swap a scene's content and re-key it. |
|
|
162
|
+
| `overflow()` | Keys to evict, least-recently-used first, never pinned. Empty when within budget or nothing evictable is left. |
|
|
163
|
+
| `bytes()` | Current total. |
|
|
164
|
+
|
|
165
|
+
### Why bytes and not a scene count
|
|
166
|
+
|
|
167
|
+
A count is not the thing at risk. An NRRD volume can be 51 KB or 53 MB on disk, and NRRD
|
|
168
|
+
decodes to a raw typed array larger still than its compressed size — so "three scenes" is
|
|
169
|
+
anywhere from 150 KB to 250 MB.
|
|
170
|
+
|
|
171
|
+
### Why pinning
|
|
172
|
+
|
|
173
|
+
With several viewports sharing one renderer, more than one scene is visible at a time.
|
|
174
|
+
Whatever is on screen should be pinned and is never a victim, **however far over the limit
|
|
175
|
+
that puts the total** — blanking a view the reader is looking at is strictly worse than
|
|
176
|
+
the memory it saves. `overflow()` returns an empty array rather than evicting a pinned
|
|
177
|
+
scene.
|
|
178
|
+
|
|
179
|
+
### `defaultBudgetBytes`
|
|
180
|
+
|
|
181
|
+
Returns 500 MB at or above 8 GiB of device memory, 250 MB otherwise. `navigator.deviceMemory`
|
|
182
|
+
is a Chromium-only hint, absent in Safari and every iOS browser — and **absent must mean
|
|
183
|
+
the small budget**: an iPad is both the device that omits the API and the one most likely
|
|
184
|
+
to have its tab killed for using too much.
|
|
185
|
+
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
## 4. Model crossfade
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
collectFadeTargets(root: DisposableObject3D): FadeTarget[]
|
|
192
|
+
setFade(targets: FadeTarget[], factor: number): void
|
|
193
|
+
restoreFade(targets: FadeTarget[]): void
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
Split into capture / apply / restore so the caller owns the timing — the fade is driven
|
|
197
|
+
frame by frame from whatever animation loop you already have, rather than this starting a
|
|
198
|
+
second one.
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
const outgoing = collectFadeTargets(previous);
|
|
202
|
+
const incoming = collectFadeTargets(next);
|
|
203
|
+
setFade(incoming, 0);
|
|
204
|
+
|
|
205
|
+
// per frame, t from 0 to 1
|
|
206
|
+
setFade(incoming, t);
|
|
207
|
+
setFade(outgoing, 1 - t);
|
|
208
|
+
|
|
209
|
+
// at the end
|
|
210
|
+
restoreFade(incoming);
|
|
211
|
+
scene.scene.remove(previous);
|
|
212
|
+
disposeObject3D(previous);
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Two non-obvious rules, both encoded in `setFade`:
|
|
216
|
+
|
|
217
|
+
- **Opacity is scaled, not assigned.** `opacity = factor` would end every crossfade with
|
|
218
|
+
an already-translucent material fully opaque — a 40% translucent shell would finish at
|
|
219
|
+
1.0 and hide whatever it was meant to reveal.
|
|
220
|
+
- **`depthWrite` is suppressed for the whole fade** and only restored at the fully-opaque
|
|
221
|
+
end. A partially transparent mesh that still writes depth occludes everything drawn
|
|
222
|
+
behind it, so a fade with depth writing left on shows the outgoing model punching holes
|
|
223
|
+
in the incoming one. A material that never had `depthWrite` does not get it back.
|
|
224
|
+
|
|
225
|
+
`restoreFade` puts every captured property back exactly as `collectFadeTargets` found it.
|
|
226
|
+
Call it at the end even if the fade was interrupted — snapping to the end state beats
|
|
227
|
+
freezing two half-transparent models on screen.
|