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,124 @@
|
|
|
1
|
+
# Control Axes & Gesture Gating
|
|
2
|
+
|
|
3
|
+
Turning rotation off — for a 2D view, or just while the user drags something
|
|
4
|
+
else — is two lines of code that are wrong more often than they are right. New
|
|
5
|
+
in **3.9.0**: a set of small helpers that make both mistakes impossible.
|
|
6
|
+
|
|
7
|
+
## The naming trap
|
|
8
|
+
|
|
9
|
+
Two spellings, and they are opposites:
|
|
10
|
+
|
|
11
|
+
| Class | Rotate | Pan | Zoom |
|
|
12
|
+
|---|---|---|---|
|
|
13
|
+
| `OrbitControls`, `Copper3dOrbitControls` | `enableRotate` | `enablePan` | `enableZoom` |
|
|
14
|
+
| `TrackballControls`, `Copper3dTrackballControls` | `noRotate` | `noPan` | `noZoom` |
|
|
15
|
+
|
|
16
|
+
Neither class validates the other's property name. Writing `enableRotate` on a
|
|
17
|
+
trackball does not throw, does not warn, and does not do anything — it lands as
|
|
18
|
+
an extra field nobody reads. The view stays rotatable, and you find out months
|
|
19
|
+
later when someone reports that a 2D image spins.
|
|
20
|
+
|
|
21
|
+
`setRotateEnabled` writes whichever spelling the instance actually has:
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import Copper from "copper3d";
|
|
25
|
+
|
|
26
|
+
Copper.setRotateEnabled(scene.controls, false); // trackball or orbit, correct either way
|
|
27
|
+
Copper.setPanEnabled(scene.controls, false);
|
|
28
|
+
Copper.setZoomEnabled(scene.controls, true);
|
|
29
|
+
|
|
30
|
+
if (Copper.isRotateEnabled(scene.controls)) { /* ... */ }
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Also available: `isPanEnabled`, `isZoomEnabled`, `setPanEnabled`,
|
|
34
|
+
`setZoomEnabled`.
|
|
35
|
+
|
|
36
|
+
Three rules they follow:
|
|
37
|
+
|
|
38
|
+
- An axis with **neither** spelling present reads as enabled and is left alone
|
|
39
|
+
on write. Nothing gates it, and inventing a property the class never reads
|
|
40
|
+
would just be a lie that looks like a fix.
|
|
41
|
+
- An instance carrying **both** spellings (a wrapper, or a codebase that once
|
|
42
|
+
wrote the wrong one) gets both written, so it cannot end up disagreeing with
|
|
43
|
+
itself.
|
|
44
|
+
- They take any object with those fields, so a controls instance from the
|
|
45
|
+
published bundle works.
|
|
46
|
+
|
|
47
|
+
## Gesture gating
|
|
48
|
+
|
|
49
|
+
The other half. When a pointer drag means something other than "move the
|
|
50
|
+
camera" — scrubbing a slice plane, dragging an annotation anchor, painting on a
|
|
51
|
+
mesh — rotation has to be suppressed for exactly the length of that gesture:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import Copper from "copper3d";
|
|
55
|
+
|
|
56
|
+
let release: (() => void) | null = null;
|
|
57
|
+
|
|
58
|
+
function onPointerDown(event) {
|
|
59
|
+
if (!hitsMyDraggableThing(event)) return;
|
|
60
|
+
release = Copper.beginGesture(scene.controls);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function onPointerUp() {
|
|
64
|
+
release?.();
|
|
65
|
+
release = null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// The `true` is load-bearing -- see below.
|
|
69
|
+
el.addEventListener("pointerdown", onPointerDown, true);
|
|
70
|
+
el.addEventListener("pointerup", onPointerUp);
|
|
71
|
+
el.addEventListener("pointercancel", onPointerUp);
|
|
72
|
+
el.addEventListener("pointerleave", onPointerUp);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
By default it suppresses rotation only. Ask for more when the gesture needs it:
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
Copper.beginGesture(scene.controls, { rotate: true, pan: true, zoom: true });
|
|
79
|
+
Copper.beginGesture(scene.controls, { rotate: false, pan: true }); // pan only
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Why not just set it back to enabled
|
|
83
|
+
|
|
84
|
+
Because the view may have been locked before your gesture started. A 2D
|
|
85
|
+
modality that ships with rotation off gets silently unlocked by the first drag,
|
|
86
|
+
and nothing ever locks it again — the bug is invisible in the gesture that
|
|
87
|
+
causes it and only shows up later, on a view that has no business rotating.
|
|
88
|
+
|
|
89
|
+
`beginGesture` captures the value **before** it suppresses anything and puts
|
|
90
|
+
that same value back. Restoring a locked view leaves it locked.
|
|
91
|
+
|
|
92
|
+
### Attach in the capture phase
|
|
93
|
+
|
|
94
|
+
This part the helper cannot do for you, and getting it wrong costs a whole
|
|
95
|
+
gesture.
|
|
96
|
+
|
|
97
|
+
`Copper3dTrackballControls` listens on the canvas in the bubble phase, and its
|
|
98
|
+
`pointerdown` handler latches the rotate state right there — it reads
|
|
99
|
+
`noRotate` and records the pointer position for the drag. A listener on an
|
|
100
|
+
ancestor element in the bubble phase runs *after* that, so the suppression you
|
|
101
|
+
just applied arrives too late and the first drag both scrubs and orbits.
|
|
102
|
+
|
|
103
|
+
Registering with `capture: true` puts your handler ahead of it:
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
el.addEventListener("pointerdown", onPointerDown, true);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Releasing
|
|
110
|
+
|
|
111
|
+
- The returned function is **idempotent**. Wiring it to `pointerup`,
|
|
112
|
+
`pointercancel` and `pointerleave` at once is safe and is what you want:
|
|
113
|
+
`pointerleave` is the only one that fires when the pointer crosses out of the
|
|
114
|
+
element mid-drag, and without it the view stays un-rotatable for good.
|
|
115
|
+
- Gates on the same controls object **refcount**. Two overlapping gestures both
|
|
116
|
+
suppress; the axis comes back when the last one releases.
|
|
117
|
+
- `isGestureActive(controls)` reports whether anything currently holds it.
|
|
118
|
+
|
|
119
|
+
## Upgrading from 3.8.x
|
|
120
|
+
|
|
121
|
+
Purely additive — new exports, no existing behaviour changed. If you already
|
|
122
|
+
write `noRotate` directly and it works, it keeps working; the helpers are worth
|
|
123
|
+
adopting where the controls class is not known statically, or where a gesture
|
|
124
|
+
has to restore what it found.
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
# copper3d — Aligned 4D MRI + Surface API
|
|
2
|
+
|
|
3
|
+
Reference for the changes added to copper3d that let you render a cine MRI (one slice,
|
|
4
|
+
N cardiac phases) **spatially and temporally aligned** with deforming surface meshes
|
|
5
|
+
(e.g. LV endo/epi), all from raw DICOM + VTK, with one shared clock and proper disposal.
|
|
6
|
+
|
|
7
|
+
Everything is exported from the package entry (`copper3d_visualisation` / `../ts/index`).
|
|
8
|
+
|
|
9
|
+
> 📖 **Want the "why" behind the API?** See the companion deep dive
|
|
10
|
+
> [4D Heart (MRI + LV Surfaces) Aligned Rendering](./4D-MRI-LV-rendering.md), which walks
|
|
11
|
+
> through how the 32 MRI frames + 64 VTK models are loaded, aligned, and animated on a single
|
|
12
|
+
> cardiac clock.
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## 1. High-level entry: `copperScene.loadAligned4D()`
|
|
17
|
+
|
|
18
|
+
The one call you normally use. Loads the DICOM cine and the surface sequences, places the
|
|
19
|
+
MRI plane at its true patient-space pose, keeps the surfaces in their own world
|
|
20
|
+
coordinates, drives both off a single frame clock, and hands you a controller.
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
loadAligned4D(
|
|
24
|
+
opts: aligned4DOptsType,
|
|
25
|
+
callback?: (ctrl: Aligned4DController) => void
|
|
26
|
+
): void
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Options — `aligned4DOptsType`
|
|
30
|
+
|
|
31
|
+
| field | type | required | meaning |
|
|
32
|
+
|---|---|---|---|
|
|
33
|
+
| `dicomUrls` | `string[]` | yes | The N DICOM frames (one slice, N cardiac phases). Order does not matter — they are sorted internally by TriggerTime/SliceLocation. |
|
|
34
|
+
| `surfaces` | `aligned4DSurfaceType[]` | no | Zero or more surface sequences (e.g. endo, epi). |
|
|
35
|
+
| `cycleMs` | `number` | no | Playback period for one full loop in ms. Default `1012` (≈ one cardiac cycle). |
|
|
36
|
+
| `window` | `{ center: number; width: number }` | no | Override the DICOM window center/width. Omit to use the value baked in the DICOM. |
|
|
37
|
+
|
|
38
|
+
### Surface sequence — `aligned4DSurfaceType`
|
|
39
|
+
|
|
40
|
+
| field | type | required | meaning |
|
|
41
|
+
|---|---|---|---|
|
|
42
|
+
| `name` | `string` | yes | Key used later in the controller (e.g. `"endo"`). |
|
|
43
|
+
| `urls` | `string[]` | yes | The N VTK frames for this surface, in cardiac-phase order (index 0..N-1). |
|
|
44
|
+
| `opts` | `IOptVTKLoader` | no | Material options: `{ wireframe?, color?, transparent?, opacity? }`. |
|
|
45
|
+
| `offset` | `number` | no | Phase offset (in frames) of this surface relative to the MRI. Default `0`. |
|
|
46
|
+
|
|
47
|
+
> Surfaces keep their original VTK coordinates (patient space, mm). They are **not**
|
|
48
|
+
> centered or scaled — that is what makes them line up with the MRI plane.
|
|
49
|
+
|
|
50
|
+
### Example
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
const dicomUrls = Array.from({ length: 32 }, (_, i) => `/data/mri_4ch/${i + 1}.dcm`);
|
|
54
|
+
const pad3 = (n: number) => String(n).padStart(3, "0");
|
|
55
|
+
const endo = Array.from({ length: 32 }, (_, i) => `/data/lv/endo_${pad3(i)}.vtk`);
|
|
56
|
+
const epi = Array.from({ length: 32 }, (_, i) => `/data/lv/epi_${pad3(i)}.vtk`);
|
|
57
|
+
|
|
58
|
+
scene.loadAligned4D(
|
|
59
|
+
{
|
|
60
|
+
dicomUrls,
|
|
61
|
+
surfaces: [
|
|
62
|
+
{ name: "endo", urls: endo, opts: { color: 0xff5a6e, transparent: true, opacity: 0.85 } },
|
|
63
|
+
{ name: "epi", urls: epi, opts: { color: 0x4ea1ff, transparent: true, opacity: 0.25 } },
|
|
64
|
+
],
|
|
65
|
+
// cycleMs: 900,
|
|
66
|
+
// window: { center: 226, width: 537 },
|
|
67
|
+
},
|
|
68
|
+
(ctrl) => {
|
|
69
|
+
// runs once everything is loaded and on screen
|
|
70
|
+
ctrl.setPlaneOpacity(0.9);
|
|
71
|
+
scene.loadViewUrl("/data/heart4d_view.json"); // set the camera the usual copper3d way
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## 2. The controller: `Aligned4DController`
|
|
79
|
+
|
|
80
|
+
The object passed to your `callback`. Use it to drive playback and appearance at runtime.
|
|
81
|
+
|
|
82
|
+
| method / field | signature | what it does |
|
|
83
|
+
|---|---|---|
|
|
84
|
+
| `plane` | `THREE.Mesh` | The MRI plane mesh (ShaderMaterial). |
|
|
85
|
+
| `surfaceMeshes` | `Record<string, THREE.Mesh>` | Each surface mesh by `name`. |
|
|
86
|
+
| `frameCount` | `number` | Number of cardiac phases (= `dicomUrls.length`). |
|
|
87
|
+
| `play()` | `() => void` | Resume playback. |
|
|
88
|
+
| `pause()` | `() => void` | Pause playback. |
|
|
89
|
+
| `toggle()` | `() => void` | Toggle play/pause. |
|
|
90
|
+
| `setSpeed(x)` | `(x: number) => void` | Playback speed multiplier (e.g. `0.5`, `2`). |
|
|
91
|
+
| `setFrame(i)` | `(i: number) => void` | Jump to an absolute frame `i` (wraps; good for a scrub slider while paused). |
|
|
92
|
+
| `setFrameOffset(name, n)` | `(name: string, n: number) => void` | Phase-shift one surface by `n` frames vs the MRI (live). |
|
|
93
|
+
| `setWindow(center, width)` | `(center: number, width: number) => void` | Re-window the MRI (16-bit, recomputed). |
|
|
94
|
+
| `setPlaneOpacity(v)` | `(v: number) => void` | MRI plane opacity, `0..1`. Auto-enables transparency when `v < 1`. |
|
|
95
|
+
| `setSurfaceOpacity(name, v)` | `(name: string, v: number) => void` | Opacity of one surface, `0..1`. Auto-enables transparency when `v < 1`. |
|
|
96
|
+
| `setSurfaceVisible(name, visible)` | `(name: string, visible: boolean) => void` | Show/hide one surface. |
|
|
97
|
+
| `dispose()` | `() => void` | Stops the clock and disposes all geometries / textures / materials, and removes meshes from the scene. **Call this on teardown.** |
|
|
98
|
+
|
|
99
|
+
### Example (wiring to UI)
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
let ctrl: Aligned4DController | undefined;
|
|
103
|
+
// ... inside loadAligned4D callback: ctrl = controller;
|
|
104
|
+
|
|
105
|
+
playBtn.onclick = () => ctrl?.toggle();
|
|
106
|
+
speedSlider.oninput = (e) => ctrl?.setSpeed(+e.target.value);
|
|
107
|
+
endoAlpha.oninput = (e) => ctrl?.setSurfaceOpacity("endo", +e.target.value);
|
|
108
|
+
epiToggle.onchange = (e) => ctrl?.setSurfaceVisible("epi", e.target.checked);
|
|
109
|
+
mriAlpha.oninput = (e) => ctrl?.setPlaneOpacity(+e.target.value);
|
|
110
|
+
windowSlider.oninput = () => ctrl?.setWindow(center, width);
|
|
111
|
+
|
|
112
|
+
// teardown (e.g. Vue onBeforeUnmount):
|
|
113
|
+
appRenderer.stop();
|
|
114
|
+
ctrl?.dispose();
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## 3. Renderer teardown: `copperRenderer.stop()`
|
|
120
|
+
|
|
121
|
+
Stops the render loop (cancels the requestAnimationFrame chain) so the canvas/GPU context
|
|
122
|
+
can be reclaimed. Call it together with `ctrl.dispose()` when leaving the page to avoid the
|
|
123
|
+
"laggy after a while" leak.
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
stop(): void
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
onBeforeUnmount(() => {
|
|
131
|
+
appRenderer.stop();
|
|
132
|
+
ctrl?.dispose();
|
|
133
|
+
});
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## 4. Low-level building blocks (only if you bypass `loadAligned4D`)
|
|
139
|
+
|
|
140
|
+
### 4.1 `computeImagePlaneCorners()` — DICOM tags → world corners
|
|
141
|
+
|
|
142
|
+
Exported from the DICOM loader. Computes the 4 world-space corners of a DICOM image plane.
|
|
143
|
+
IPP is the **center** of pixel (0,0), so corners are half a pixel out. `PixelSpacing` is
|
|
144
|
+
`[rowSpacing, colSpacing]`; the first IOP vector is the +column-index direction.
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
import { computeImagePlaneCorners } from "copper3d_visualisation";
|
|
148
|
+
|
|
149
|
+
function computeImagePlaneCorners(
|
|
150
|
+
ipp: number[], // ImagePositionPatient [x,y,z]
|
|
151
|
+
iop: number[], // ImageOrientationPatient [rx,ry,rz, cx,cy,cz]
|
|
152
|
+
spacing: number[], // PixelSpacing [rowSpacing, colSpacing]
|
|
153
|
+
cols: number, // Columns
|
|
154
|
+
rows: number // Rows
|
|
155
|
+
): planeCorners; // { tl, tr, bl, br } each [x,y,z]
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### 4.2 `copperVolumeType` — new fields
|
|
159
|
+
|
|
160
|
+
The DICOM loader now also fills these (all optional; present when the tags exist):
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
interface copperVolumeType {
|
|
164
|
+
/* ...existing... */
|
|
165
|
+
instanceNumber?: number;
|
|
166
|
+
imagePositionPatient?: number[]; // IPP [x,y,z]
|
|
167
|
+
imageOrientationPatient?: number[]; // IOP [rx,ry,rz, cx,cy,cz]
|
|
168
|
+
pixelSpacing?: number[]; // [rowSpacing, colSpacing]
|
|
169
|
+
corners?: planeCorners; // pre-computed world-space plane corners
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### 4.3 `createTexture2D_Array()` — aligned plane + frame/window control
|
|
174
|
+
|
|
175
|
+
The 2D texture builder gained an `aligned` flag and two control helpers. With
|
|
176
|
+
`aligned = true` and a volume that has `corners`, the plane is built at its real
|
|
177
|
+
world pose (instead of a centered `PlaneGeometry`).
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
function createTexture2D_Array(
|
|
181
|
+
copperVolume: copperVolumeType,
|
|
182
|
+
depth: number, // number of stacked frames (cine length)
|
|
183
|
+
scene: THREE.Scene,
|
|
184
|
+
gui?: GUI,
|
|
185
|
+
aligned?: boolean // default false; true → world-placed corner quad
|
|
186
|
+
): {
|
|
187
|
+
mesh: THREE.Mesh;
|
|
188
|
+
copperVolume: copperVolumeType;
|
|
189
|
+
updateTexture: (v: copperVolumeType) => void;
|
|
190
|
+
setFrame: (i: number) => void; // set the visible cine frame (depth uniform)
|
|
191
|
+
setWindow: (center: number, width: number) => void; // re-window (16-bit)
|
|
192
|
+
};
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
> Default (`aligned = false`) keeps the legacy behaviour used by `loadDicom`, so the
|
|
196
|
+
> existing volume-scroll examples are unchanged.
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
## 5. How alignment works (one paragraph)
|
|
201
|
+
|
|
202
|
+
The DICOM and the surfaces are already in the **same patient coordinate frame** (LPS, mm).
|
|
203
|
+
The MRI plane is built from the 4 world corners derived from `ImagePositionPatient` +
|
|
204
|
+
`ImageOrientationPatient` + `PixelSpacing`; the surfaces are loaded with their original VTK
|
|
205
|
+
coordinates (no center/scale). Both therefore coincide automatically — no manual transform.
|
|
206
|
+
Temporal alignment comes from a single shared `frameIndex` that drives the MRI texture
|
|
207
|
+
layer (`depth` uniform) and the surface geometry swap simultaneously. The texture UV in the
|
|
208
|
+
plane is a fixed structural mapping (corner `tl` ↔ pixel (0,0)), so it is correct for any
|
|
209
|
+
dataset, not just one case.
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## 6. Quick checklist for consumers
|
|
214
|
+
|
|
215
|
+
- [ ] Pass all DICOM frames in `dicomUrls` (order doesn't matter, sorted internally).
|
|
216
|
+
- [ ] Pass each surface's VTK frames in cardiac-phase order (index 0..N-1).
|
|
217
|
+
- [ ] Drive the camera with `scene.loadViewUrl(...)` (camera is NOT baked into the package).
|
|
218
|
+
- [ ] Use `setFrameOffset(name, n)` if a surface is phase-shifted vs the MRI.
|
|
219
|
+
- [ ] Always call `appRenderer.stop()` + `ctrl.dispose()` on teardown.
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# Copper3D GLB Rendering Optimization
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
This document describes the rendering optimizations applied to the Copper3D library's Three.js pipeline to improve GLB model visual quality in the medical image annotator. The original rendering produced dull, dark, and detail-less models. The following changes address lighting, tone mapping, material enhancement, and environment mapping.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 1. Renderer Settings (`baseRenderer.ts`)
|
|
10
|
+
|
|
11
|
+
### Tone Mapping & Exposure
|
|
12
|
+
|
|
13
|
+
| Setting | Before | After |
|
|
14
|
+
|---------|--------|-------|
|
|
15
|
+
| Tone Mapping | Default (Linear) | `THREE.ACESFilmicToneMapping` |
|
|
16
|
+
| Tone Mapping Exposure | 1.0 | **1.8** |
|
|
17
|
+
| Output Color Space | Default | `THREE.SRGBColorSpace` |
|
|
18
|
+
| Pixel Ratio | `window.devicePixelRatio` | `Math.min(window.devicePixelRatio, 2)` |
|
|
19
|
+
|
|
20
|
+
- **ACES Filmic Tone Mapping**: Industry-standard cinematic tone mapping that provides better contrast and color reproduction compared to linear mapping. It compresses highlights naturally while preserving shadow detail.
|
|
21
|
+
- **Exposure 1.8**: Significantly brighter than default (1.0), compensating for the overall dark appearance.
|
|
22
|
+
- **SRGBColorSpace**: Ensures correct gamma-aware color output for displays.
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## 2. Multi-Light System (`baseScene.ts`)
|
|
27
|
+
|
|
28
|
+
The original single-light or minimal-light setup left large areas of the model in darkness (especially the top). A 5-light system was implemented:
|
|
29
|
+
|
|
30
|
+
### Light Configuration
|
|
31
|
+
|
|
32
|
+
| Light | Type | Color | Intensity | Position | Attached To |
|
|
33
|
+
|-------|------|-------|-----------|----------|-------------|
|
|
34
|
+
| Hemisphere | HemisphereLight | Sky: `#ddeeff`, Ground: `#0f0e0d` | 0.6 | Scene root | Scene |
|
|
35
|
+
| Ambient | AmbientLight | `#606060` | 0.8 | - | Camera |
|
|
36
|
+
| Key (Main) | DirectionalLight | `#ffffff` | 1.0 | `(0.5, 0.5, 0.866)` | Camera |
|
|
37
|
+
| Fill | DirectionalLight | `#ffffff` | 0.4 | `(-0.5, -0.2, -0.866)` | Camera |
|
|
38
|
+
| Top | DirectionalLight | `#ffffff` | 0.5 | `(0, 1, 0.2)` | Camera |
|
|
39
|
+
|
|
40
|
+
### Design Rationale
|
|
41
|
+
|
|
42
|
+
- **Camera-attached lights**: All directional lights and the ambient light are children of the camera, so lighting remains consistent regardless of orbit angle. The model always appears well-lit from the viewer's perspective.
|
|
43
|
+
- **Hemisphere Light**: Provides soft, omnidirectional ambient fill simulating sky/ground bounce light.
|
|
44
|
+
- **Key Light** (front-right): Primary modeling light that defines shape and surface detail.
|
|
45
|
+
- **Fill Light** (back-left): Reduces harsh shadows on the opposite side of the key light.
|
|
46
|
+
- **Top Light** (directly above): Specifically added to solve the dark-top problem where the crown of anatomical models was not receiving illumination.
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## 3. HDR Environment Mapping (`baseRenderer.ts`)
|
|
51
|
+
|
|
52
|
+
### Setup
|
|
53
|
+
|
|
54
|
+
- **HDR Source**: `venice_sunset_1k.hdr` loaded via `RGBELoader`
|
|
55
|
+
- **Processing**: `PMREMGenerator` creates a pre-filtered, mipmapped radiance environment map
|
|
56
|
+
- **Application**:
|
|
57
|
+
- `scene.environment` — used by PBR materials for realistic reflections
|
|
58
|
+
- `scene.background` — provides a natural backdrop (overridden by vignette)
|
|
59
|
+
|
|
60
|
+
### Why HDR Environment Matters
|
|
61
|
+
|
|
62
|
+
PBR (Physically Based Rendering) materials in Three.js rely on environment maps for realistic reflections and ambient lighting. Without an environment map, metallic and reflective surfaces appear flat and black. The HDR environment provides:
|
|
63
|
+
|
|
64
|
+
- Realistic specular reflections on glossy surfaces
|
|
65
|
+
- Image-based lighting (IBL) that adds ambient detail
|
|
66
|
+
- Natural color variation in reflections
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## 4. PBR Material Enhancement (`copperScene.ts`)
|
|
71
|
+
|
|
72
|
+
When GLB models are loaded via `loadPureGLB()`, materials are automatically optimized:
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
if (child.material instanceof THREE.MeshStandardMaterial) {
|
|
76
|
+
child.material.roughness = Math.min(child.material.roughness, 0.35);
|
|
77
|
+
child.material.metalness = Math.max(child.material.metalness, 0.05);
|
|
78
|
+
child.material.envMapIntensity = 1.2;
|
|
79
|
+
child.material.side = THREE.DoubleSide;
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
| Property | Constraint | Purpose |
|
|
84
|
+
|----------|-----------|---------|
|
|
85
|
+
| `roughness` | Capped at **0.35** | Ensures surfaces appear polished and reflective, not matte |
|
|
86
|
+
| `metalness` | Minimum **0.05** | Adds subtle metallic reflection even to non-metal surfaces |
|
|
87
|
+
| `envMapIntensity` | Set to **1.2** | Boosts HDR environment reflections for more visible detail |
|
|
88
|
+
| `side` | `DoubleSide` | Prevents missing faces on thin anatomical structures |
|
|
89
|
+
|
|
90
|
+
### Why These Values
|
|
91
|
+
|
|
92
|
+
Medical GLB models exported from segmentation pipelines often have default roughness of 1.0 (completely matte) and metalness of 0.0 (no reflection). This makes them look like clay. By clamping roughness to 0.35 and adding minimal metalness, the models gain visible surface detail and a sense of material quality without appearing unrealistically shiny.
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## 5. Background Vignette
|
|
97
|
+
|
|
98
|
+
A gradient vignette background replaces the plain color:
|
|
99
|
+
|
|
100
|
+
- **Color 1**: `#5454ad` (purple-blue)
|
|
101
|
+
- **Color 2**: `#18e5a7` (cyan-green)
|
|
102
|
+
- **Grain Scale**: `0.001` (subtle noise to prevent banding)
|
|
103
|
+
- **Render Order**: `-1` (behind all scene objects)
|
|
104
|
+
|
|
105
|
+
This provides a professional, non-distracting backdrop that contrasts well with anatomical models.
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## 6. GUI Controls
|
|
110
|
+
|
|
111
|
+
All lighting and rendering parameters are exposed through a dat.GUI panel for real-time adjustment:
|
|
112
|
+
|
|
113
|
+
- **Lights**: Toggle on/off, adjust ambient/directional intensity (0-4) and color
|
|
114
|
+
- **Exposure**: Tone mapping exposure slider
|
|
115
|
+
- **Background**: Gradient color pickers
|
|
116
|
+
- **Wireframe**: Toggle mode
|
|
117
|
+
- **Model Visibility**: Per-mesh toggle
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## Architecture Diagram
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
Camera
|
|
125
|
+
├── AmbientLight (intensity: 0.8)
|
|
126
|
+
├── DirectionalLight [Key] (intensity: 1.0, pos: front-right)
|
|
127
|
+
├── DirectionalLight [Fill] (intensity: 0.4, pos: back-left)
|
|
128
|
+
└── DirectionalLight [Top] (intensity: 0.5, pos: above)
|
|
129
|
+
|
|
130
|
+
Scene
|
|
131
|
+
├── HemisphereLight (intensity: 0.6)
|
|
132
|
+
├── HDR Environment (venice_sunset_1k.hdr → PMREMGenerator)
|
|
133
|
+
├── Vignette Background (render order: -1)
|
|
134
|
+
└── GLB Model
|
|
135
|
+
└── MeshStandardMaterial (roughness ≤ 0.35, metalness ≥ 0.05, envMapIntensity: 1.2)
|
|
136
|
+
|
|
137
|
+
Renderer
|
|
138
|
+
├── ACESFilmicToneMapping
|
|
139
|
+
├── Exposure: 1.8
|
|
140
|
+
├── SRGBColorSpace
|
|
141
|
+
└── Pixel Ratio: min(devicePixelRatio, 2)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
## Summary of Impact
|
|
147
|
+
|
|
148
|
+
| Problem | Solution | Files Modified |
|
|
149
|
+
|---------|----------|----------------|
|
|
150
|
+
| Model looks dull/flat | PBR material clamping + HDR environment | `copperScene.ts` |
|
|
151
|
+
| Scene too dark | ACES tone mapping + exposure 1.8 | `baseRenderer.ts` |
|
|
152
|
+
| Top of model is black | Added dedicated top light | `baseScene.ts` |
|
|
153
|
+
| No surface reflections | HDR environment map + envMapIntensity 1.2 | `baseRenderer.ts`, `copperScene.ts` |
|
|
154
|
+
| Harsh shadows | Multi-light system with fill light | `baseScene.ts` |
|
|
155
|
+
| Lighting changes with rotation | Camera-attached lights | `baseScene.ts` |
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Loading GLB: Draco & KTX2 Decoders
|
|
2
|
+
|
|
3
|
+
A Draco-compressed GLB is typically a tenth the size of the same model
|
|
4
|
+
uncompressed — one of this project's anatomy models goes from 10.18MB to 598KB.
|
|
5
|
+
The catch is that the browser cannot decode it alone: three fetches a separate
|
|
6
|
+
WebAssembly decoder at load time, from a path you have to be able to set.
|
|
7
|
+
|
|
8
|
+
**Before 3.9.0 you could not set it, and the default 404'd.** Both are fixed.
|
|
9
|
+
|
|
10
|
+
## What was wrong
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
// pre-3.9.0, module-level consts, not exported
|
|
14
|
+
new DRACOLoader(MANAGER).setDecoderPath(`${THREE_PATH}/examples/js/libs/draco/gltf/`)
|
|
15
|
+
new KTX2Loader(MANAGER).setTranscoderPath(`${THREE_PATH}/examples/js/libs/basis`)
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Three separate problems:
|
|
19
|
+
|
|
20
|
+
- **`examples/js/` does not exist.** three removed that directory long ago; the
|
|
21
|
+
decoder lives under `examples/jsm/`. Every Draco-compressed GLB failed, and
|
|
22
|
+
what surfaced in the console was a CORS error on the 404 rather than anything
|
|
23
|
+
naming the real cause.
|
|
24
|
+
- **The transcoder path had no trailing slash.** three concatenates the file
|
|
25
|
+
name onto the string, so it asked for `libs/basisbasis_transcoder.js`.
|
|
26
|
+
- **Neither was reachable.** They were module-level `const`s, and neither they
|
|
27
|
+
nor `copperGltfLoader` were exported, so nothing downstream could correct
|
|
28
|
+
them.
|
|
29
|
+
|
|
30
|
+
## Setting your own path
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import Copper from "copper3d";
|
|
34
|
+
|
|
35
|
+
Copper.setDracoDecoderPath("/draco/");
|
|
36
|
+
Copper.setKTX2TranscoderPath("/basis/");
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Both expect a **trailing slash** — three appends `draco_wasm_wrapper.js`,
|
|
40
|
+
`draco_decoder.wasm` and `basis_transcoder.js` directly to what you pass.
|
|
41
|
+
|
|
42
|
+
On a subpath deploy the base URL has to be included. A decoder served from
|
|
43
|
+
`/te-uma/draco/` is not found at `/draco/`:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
// e.g. Nuxt
|
|
47
|
+
Copper.setDracoDecoderPath(`${useRuntimeConfig().app.baseURL.replace(/\/$/, "")}/draco/`);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Call once at startup, before the first GLB loads. Both paths feed shared
|
|
51
|
+
loaders, so a call reaches loaders that already exist — but the decoder module
|
|
52
|
+
itself is fetched on first use and cached inside `DRACOLoader`, so changing the
|
|
53
|
+
path after something has decoded does not re-fetch it.
|
|
54
|
+
|
|
55
|
+
## Why you probably want to self-host
|
|
56
|
+
|
|
57
|
+
The default reaches unpkg for the same three revision copper3d was built
|
|
58
|
+
against. That works, and it is a hard runtime dependency on a third-party CDN —
|
|
59
|
+
which an offline, air-gapped, or firewalled deployment cannot satisfy, and which
|
|
60
|
+
a teaching resource used in a hospital very likely cannot either.
|
|
61
|
+
|
|
62
|
+
Copy the files into your own static directory at build time:
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
public/
|
|
66
|
+
draco/
|
|
67
|
+
draco_wasm_wrapper.js
|
|
68
|
+
draco_decoder.wasm
|
|
69
|
+
basis/
|
|
70
|
+
basis_transcoder.js
|
|
71
|
+
basis_transcoder.wasm
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
They come from `three/examples/jsm/libs/draco/gltf/` and
|
|
75
|
+
`three/examples/jsm/libs/basis/` in your `node_modules`. Copy them from the
|
|
76
|
+
three version you actually build with — a decoder and a `GLTFLoader` from
|
|
77
|
+
different revisions are not guaranteed to agree.
|
|
78
|
+
|
|
79
|
+
## `copperGltfLoader()` <Badge type="tip" text="3.9.0" />
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
copperGltfLoader(renderer: THREE.WebGLRenderer): GLTFLoader
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Also newly exported: the `GLTFLoader` copper3d builds internally, already wired
|
|
86
|
+
to the shared Draco and KTX2 loaders. Use it when you need your own loader — your
|
|
87
|
+
own `LoadingManager`, an `onError` handler, or a load that does not go through a
|
|
88
|
+
copper3d scene — without rebuilding the decoder wiring and getting the paths
|
|
89
|
+
right a second time.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
const loader = Copper.copperGltfLoader(renderer.renderer);
|
|
93
|
+
loader.load(url, onLoad, onProgress, onError);
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Note the fourth argument. `scene.loadGltf()` does not accept one, which is why a
|
|
97
|
+
caller that has to report a failed GLB needs its own loader today.
|
|
98
|
+
|
|
99
|
+
## Upgrading from 3.8.x
|
|
100
|
+
|
|
101
|
+
Nothing to change. The corrected default paths can only work where the old ones
|
|
102
|
+
404'd, and the setters are new exports. If you worked around this by building
|
|
103
|
+
your own `GLTFLoader` and `DRACOLoader`, that keeps working — switching to
|
|
104
|
+
`setDracoDecoderPath` plus `scene.loadGltf` is optional, and worth it only if
|
|
105
|
+
you do not also need the error handling that motivated the workaround.
|