@vitessce/all 4.0.0-test.0 → 4.0.0-test.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.
@@ -1,3845 +0,0 @@
1
- import { jsxs, jsx } from "react/jsx-runtime";
2
- import { useRef, useState, useMemo, useEffect, Suspense } from "react";
3
- import { useThree, useFrame, Canvas } from "@react-three/fiber";
4
- import { OrbitControls } from "@react-three/drei";
5
- import * as THREE from "three";
6
- import { Data3DTexture, RedFormat, UnsignedByteType, LinearFilter, RedIntegerFormat, UnsignedIntType, NearestFilter, Vector3, Vector2, Vector4, UniformsUtils, WebGLMultipleRenderTargets } from "three";
7
- import { l as log, i as isEqual, C as CoordinationType, u as useEventCallback } from "./index-5W6nu-ix.js";
8
- const LogLevel = {
9
- INFO: "info",
10
- WARN: "warn",
11
- ERROR: "error",
12
- DEBUG: "debug",
13
- TRACE: "trace"
14
- // default value
15
- };
16
- const OrderedLogLevels = [
17
- LogLevel.TRACE,
18
- LogLevel.DEBUG,
19
- LogLevel.INFO,
20
- LogLevel.WARN,
21
- LogLevel.ERROR
22
- ];
23
- function getLogLevel() {
24
- return log.getLevel();
25
- }
26
- function atLeastLogLevel(someLevel) {
27
- const currLevel = getLogLevel();
28
- const numericTarget = OrderedLogLevels.indexOf(someLevel);
29
- return currLevel <= numericTarget;
30
- }
31
- const BRICK_SIZE = 32;
32
- const BRICK_CACHE_SIZE_X = 64;
33
- const BRICK_CACHE_SIZE_Y = 64;
34
- const BRICK_CACHE_SIZE_Z = 4;
35
- const TOTAL_NUM_BRICKS = BRICK_CACHE_SIZE_X * BRICK_CACHE_SIZE_Y * BRICK_CACHE_SIZE_Z;
36
- const BRICK_CACHE_SIZE_VOXELS_X = BRICK_CACHE_SIZE_X * BRICK_SIZE;
37
- const BRICK_CACHE_SIZE_VOXELS_Y = BRICK_CACHE_SIZE_Y * BRICK_SIZE;
38
- const BRICK_CACHE_SIZE_VOXELS_Z = BRICK_CACHE_SIZE_Z * BRICK_SIZE;
39
- const INIT_STATUS = {
40
- NOT_STARTED: "not_started",
41
- IN_PROGRESS: "in_progress",
42
- COMPLETE: "complete",
43
- FAILED: "failed"
44
- };
45
- function logWithColor$2(message) {
46
- if (atLeastLogLevel(LogLevel.DEBUG)) {
47
- console.warn(`%cDM: ${message}`, "background: blue; color: white; padding: 2px; border-radius: 3px;");
48
- }
49
- }
50
- function _resolutionStatsToShapes(multiResolutionStats) {
51
- return multiResolutionStats.map((stats) => {
52
- const { dims } = stats;
53
- const shape = [
54
- // Other spatial-accelerated code assumes TCZYX dimension order
55
- dims.t,
56
- // TODO: standardize lowercase/uppercase dim names at store-level
57
- dims.c,
58
- // TODO: handle case when dimension(s) are missing
59
- dims.z,
60
- dims.y,
61
- dims.x
62
- ];
63
- return shape;
64
- });
65
- }
66
- function _resolutionStatsToBrickLayout(multiResolutionStats) {
67
- return multiResolutionStats.map((stats) => {
68
- const shape = [
69
- stats.depth,
70
- // z
71
- stats.height,
72
- // y
73
- stats.width
74
- // x
75
- ];
76
- return [
77
- Math.ceil((shape[0] || 1) / BRICK_SIZE),
78
- Math.ceil((shape[1] || 1) / BRICK_SIZE),
79
- Math.ceil((shape[2] || 1) / BRICK_SIZE)
80
- ];
81
- });
82
- }
83
- function _initMRMCPT(zarrStoreBrickLayout, channelsZarrMappingsLength) {
84
- log.debug("_initMRMCPT", zarrStoreBrickLayout, channelsZarrMappingsLength);
85
- const PT = {
86
- channelOffsets: [
87
- [0, 0, 1],
88
- [0, 1, 0],
89
- [0, 1, 1],
90
- [1, 0, 0],
91
- [1, 0, 1],
92
- [1, 1, 0],
93
- [1, 1, 1]
94
- ],
95
- anchors: [],
96
- offsets: [],
97
- xExtent: 0,
98
- // includes the offset inclusive
99
- yExtent: 0,
100
- // includes the offset inclusive
101
- zExtent: 0,
102
- // includes the offset inclusive
103
- z0Extent: 0,
104
- // l0 z extent
105
- zTotal: 0
106
- // original z extent plus the l0 z extent times the channel count
107
- };
108
- PT.xExtent = 1;
109
- PT.yExtent = 1;
110
- PT.zExtent = 1;
111
- const l0z = zarrStoreBrickLayout[0][0];
112
- PT.z0Extent = l0z;
113
- PT.lowestDataRes = zarrStoreBrickLayout.length - 1;
114
- for (let i = zarrStoreBrickLayout.length - 1; i > 0; i--) {
115
- PT.anchors.push([
116
- PT.xExtent,
117
- PT.yExtent,
118
- PT.zExtent
119
- ]);
120
- PT.xExtent += zarrStoreBrickLayout[i][2];
121
- PT.yExtent += zarrStoreBrickLayout[i][1];
122
- PT.zExtent += zarrStoreBrickLayout[i][0];
123
- }
124
- PT.anchors.push([0, 0, PT.zExtent]);
125
- PT.anchors.reverse();
126
- PT.zTotal = PT.zExtent + channelsZarrMappingsLength * l0z;
127
- const brickCacheData = new Uint8Array(BRICK_CACHE_SIZE_VOXELS_X * BRICK_CACHE_SIZE_VOXELS_Y * BRICK_CACHE_SIZE_VOXELS_Z);
128
- brickCacheData.fill(0);
129
- const pageTableData = new Uint32Array(PT.xExtent * PT.yExtent * PT.zTotal);
130
- pageTableData.fill(0);
131
- const bcTHREE = new Data3DTexture(brickCacheData, BRICK_CACHE_SIZE_VOXELS_X, BRICK_CACHE_SIZE_VOXELS_Y, BRICK_CACHE_SIZE_VOXELS_Z);
132
- bcTHREE.format = RedFormat;
133
- bcTHREE.type = UnsignedByteType;
134
- bcTHREE.internalFormat = "R8";
135
- bcTHREE.minFilter = LinearFilter;
136
- bcTHREE.magFilter = LinearFilter;
137
- bcTHREE.generateMipmaps = false;
138
- bcTHREE.needsUpdate = true;
139
- const ptTHREE = new Data3DTexture(pageTableData, PT.xExtent, PT.yExtent, PT.zTotal);
140
- ptTHREE.format = RedIntegerFormat;
141
- ptTHREE.type = UnsignedIntType;
142
- ptTHREE.internalFormat = "R32UI";
143
- ptTHREE.minFilter = NearestFilter;
144
- ptTHREE.magFilter = NearestFilter;
145
- ptTHREE.generateMipmaps = false;
146
- ptTHREE.needsUpdate = true;
147
- log.debug("_initMRMCPT", PT, ptTHREE, bcTHREE);
148
- return {
149
- PT,
150
- ptTHREE,
151
- bcTHREE
152
- };
153
- }
154
- function _packPT(min, max, bcX, bcY, bcZ) {
155
- const clamp7 = (v) => Math.max(0, Math.min(127, Math.floor(v / 2)));
156
- return (1 << 31 | 1 << 30 | clamp7(min) << 23 | clamp7(max) << 16 | (bcX & 63) << 10 | (bcY & 63) << 4 | bcZ & 15) >>> 0;
157
- }
158
- function _ptToZarr(ptx, pty, ptz, ptInfo) {
159
- const { PT_zExtent, PT_z0Extent, PT_anchors } = ptInfo;
160
- let channel = -1;
161
- let resolution = -1;
162
- let x = -1;
163
- let y = -1;
164
- let z = -1;
165
- if (ptz >= PT_zExtent) {
166
- resolution = 0;
167
- x = ptx;
168
- y = pty;
169
- z = (ptz - PT_zExtent) % PT_z0Extent;
170
- channel = Math.floor((ptz - PT_zExtent) / PT_z0Extent);
171
- } else {
172
- for (let i = 1; i < PT_anchors.length; i++) {
173
- if (ptx < PT_anchors[i][0] && pty < PT_anchors[i][1] && ptz < PT_anchors[i][2]) ;
174
- else {
175
- resolution = i;
176
- const channelMask = [0, 0, 0];
177
- if (ptx >= PT_anchors[i][0]) {
178
- channelMask[0] = 1;
179
- }
180
- if (pty >= PT_anchors[i][1]) {
181
- channelMask[1] = 1;
182
- }
183
- if (ptz >= PT_anchors[i][2]) {
184
- channelMask[2] = 1;
185
- }
186
- const binaryChannel = channelMask[0] << 2 | channelMask[1] << 1 | channelMask[2];
187
- channel = Math.max(1, Math.min(7, binaryChannel)) - 1;
188
- const thisOffset = channelMask.map((v, j) => v * PT_anchors[i][j]);
189
- x = ptx - thisOffset[0];
190
- y = pty - thisOffset[1];
191
- z = ptz - thisOffset[2];
192
- break;
193
- }
194
- }
195
- }
196
- return {
197
- channel,
198
- resolution,
199
- x,
200
- y,
201
- z
202
- };
203
- }
204
- const DEFAULT_SIGMA_NORMALIZED = 0.25;
205
- function _requestBufferToRequestObjects(buffer, k, optsForWeighting) {
206
- const counts = /* @__PURE__ */ new Map();
207
- const { width, height, sigmaNormalized } = optsForWeighting;
208
- const useWeighting = Number.isInteger(width) && Number.isInteger(height) && width > 0 && height > 0 && typeof sigmaNormalized === "number";
209
- if (!useWeighting) {
210
- log.warn("_requestBufferToRequestObjects: proceeding without weighting");
211
- }
212
- const maxPixels = Math.floor(buffer.length / 4);
213
- for (let pix = 0; pix < maxPixels; pix += 1) {
214
- const i = pix * 4;
215
- const r = buffer[i];
216
- const g = buffer[i + 1];
217
- const b = buffer[i + 2];
218
- const a = buffer[i + 3];
219
- if ((r | g | b | a) === 0) {
220
- continue;
221
- }
222
- const packed = (r << 24 | g << 16 | b << 8 | a) >>> 0;
223
- let weight = 1;
224
- if (useWeighting) {
225
- const x = pix % width;
226
- const y = Math.floor(pix / width);
227
- const dx = x - width / 2;
228
- const dy = y - height / 2;
229
- const nd = Math.sqrt(dx * dx / (width * width) + dy * dy / (height * height));
230
- const norm = nd / sigmaNormalized;
231
- weight = Math.exp(-0.5 * norm * norm);
232
- }
233
- counts.set(packed, (counts.get(packed) || 0) + weight);
234
- }
235
- const requests = [...counts.entries()].sort((a, b) => b[1] - a[1]).slice(0, k).map(([packed]) => ({
236
- x: packed >> 22 & 1023,
237
- y: packed >> 12 & 1023,
238
- z: packed & 4095
239
- }));
240
- return { requests, origRequestCount: counts.size };
241
- }
242
- class VolumeDataManager {
243
- constructor(glParam) {
244
- logWithColor$2("CLASS INITIALIZING");
245
- log.debug("VolumeDataManager constructor", { glParam, glParamContext: glParam.getContext?.() });
246
- const gl = glParam.getContext?.() || glParam;
247
- const renderer = glParam;
248
- if (gl.domElement && gl.getContext) {
249
- this.gl = gl.getContext();
250
- } else if (gl.isWebGLRenderer) {
251
- this.gl = gl.getContext();
252
- } else {
253
- this.gl = gl;
254
- }
255
- this.renderer = renderer;
256
- if (!this.gl || typeof this.gl.getParameter !== "function") {
257
- log.debug("Unable to get WebGL context, using mock context");
258
- this.gl = {
259
- getParameter: (param) => {
260
- const defaults = {
261
- MAX_TEXTURE_SIZE: 4096,
262
- MAX_3D_TEXTURE_SIZE: 256,
263
- MAX_RENDERBUFFER_SIZE: 4096,
264
- MAX_UNIFORM_BUFFER_BINDINGS: 16
265
- };
266
- return defaults[param] || 0;
267
- },
268
- isContextLost: () => false,
269
- MAX_TEXTURE_SIZE: "MAX_TEXTURE_SIZE",
270
- MAX_3D_TEXTURE_SIZE: "MAX_3D_TEXTURE_SIZE",
271
- MAX_RENDERBUFFER_SIZE: "MAX_RENDERBUFFER_SIZE",
272
- MAX_UNIFORM_BUFFER_BINDINGS: "MAX_UNIFORM_BUFFER_BINDINGS"
273
- };
274
- }
275
- this._originalGlParam = glParam;
276
- this._isContextLost = false;
277
- this._contextRestoredCallbacks = [];
278
- if (this.gl && this.gl.canvas) {
279
- this.gl.canvas.addEventListener("webglcontextlost", this._handleContextLost.bind(this));
280
- this.gl.canvas.addEventListener("webglcontextrestored", this._handleContextRestored.bind(this));
281
- }
282
- log.debug("GL CONSTANTS");
283
- log.debug(this.gl);
284
- log.debug(this.gl.TEXTURE0);
285
- log.debug(this.gl.textures);
286
- log.debug("RENDERER");
287
- log.debug(this.renderer);
288
- this.deviceLimits = {
289
- maxTextureSize: this.gl.getParameter(this.gl.MAX_TEXTURE_SIZE),
290
- max3DTextureSize: this.gl.getParameter(this.gl.MAX_3D_TEXTURE_SIZE),
291
- maxRenderbufferSize: this.gl.getParameter(this.gl.MAX_RENDERBUFFER_SIZE),
292
- maxUniformBufferBindings: this.gl.getParameter(this.gl.MAX_UNIFORM_BUFFER_BINDINGS)
293
- };
294
- this.zarrStore = {
295
- resolutions: null,
296
- // 6 (the number of resolutions aka. pyramid levels in the file)
297
- chunkSize: [],
298
- // [32, 32, 32]
299
- shapes: [],
300
- // [[795, 1024, 1024], ..., [64, 64, 64], [32, 32, 32]]
301
- arrays: [],
302
- // [array0, array1, array2, array3, array4, array5]
303
- dtype: "",
304
- // 'uint8'
305
- physicalSizeTotal: [],
306
- // [795 x 0.0688, 1024 x 0.03417, 1024 x 0.03417]
307
- physicalSizeVoxel: [],
308
- // [0.0688, 0.03417, 0.03417]
309
- brickLayout: [],
310
- // [[25, 32, 32],[13, 16, 16], ..., [2,2,2],[1,1,1]]
311
- // store: '', // ref to this.store
312
- // group: '', // ref to this.group
313
- channelCount: 1,
314
- // MAX 7 TODO: get from zarr metadata
315
- scales: [],
316
- // downsample ratios, [x,y,z] per resolution level
317
- lowestDataRes: 0
318
- // lowest resolution level with data
319
- };
320
- this.ptTHREE = null;
321
- this.bcTHREE = null;
322
- this.channels = {
323
- maxChannels: 7,
324
- // lower when dataset has fewer, dictates page table size
325
- zarrMappings: [],
326
- // stores the zarr channel index for every one of the up to 7 channels
327
- colorMappings: [],
328
- // stores the PT slot for every color
329
- downsampleMin: [],
330
- // stores the downsample min for every one of the up to 7 channels
331
- downsampleMax: []
332
- // stores the downsample max for every one of the up to 7 channels
333
- };
334
- this.PT = {
335
- channelOffsets: [
336
- [0, 0, 1],
337
- [0, 1, 0],
338
- [0, 1, 1],
339
- [1, 0, 0],
340
- [1, 0, 1],
341
- [1, 1, 0],
342
- [1, 1, 1]
343
- ],
344
- anchors: [],
345
- offsets: [],
346
- xExtent: 0,
347
- // includes the offset inclusive
348
- yExtent: 0,
349
- // includes the offset inclusive
350
- zExtent: 0,
351
- // includes the offset inclusive
352
- z0Extent: 0,
353
- // l0 z extent
354
- zTotal: 0
355
- // original z extent plus the l0 z extent times the channel count
356
- };
357
- this.bricksEverLoaded = /* @__PURE__ */ new Set();
358
- this.isBusy = false;
359
- this.BCTimeStamps = new Array(TOTAL_NUM_BRICKS).fill(0);
360
- this.BCMinMax = new Array(TOTAL_NUM_BRICKS).fill([0, 0]);
361
- this.BCFull = false;
362
- this.BCUnusedIndex = 0;
363
- this.bc2pt = new Array(TOTAL_NUM_BRICKS).fill(null);
364
- this.LRUStack = [];
365
- this.triggerUsage = true;
366
- this.triggerRequest = false;
367
- this.timeStamp = 0;
368
- this.k = 40;
369
- this.noNewRequests = false;
370
- this.manuallyStopped = false;
371
- this.needsBailout = false;
372
- this.initStatus = INIT_STATUS.NOT_STARTED;
373
- this.initError = null;
374
- this._lastChannelConfig = null;
375
- this.currentRequestCount = 0;
376
- this.totalBricksRequested = 0;
377
- logWithColor$2("VolumeDataManager constructor complete");
378
- }
379
- /**
380
- * Get loading progress information
381
- * @returns {Object} Loading progress with bricksLoaded, totalBricks, isLoading, and percentage
382
- */
383
- getLoadingProgress() {
384
- const bricksLoaded = this.bricksEverLoaded.size;
385
- const isLoading = !this.noNewRequests && this.currentRequestCount > 0;
386
- const percentage = this.totalBricksRequested > 0 ? (this.totalBricksRequested - this.currentRequestCount) / this.totalBricksRequested * 100 : 0;
387
- return {
388
- bricksLoaded,
389
- currentRequestCount: this.currentRequestCount,
390
- totalBricksRequested: this.totalBricksRequested,
391
- isLoading,
392
- percentage,
393
- noNewRequests: this.noNewRequests
394
- };
395
- }
396
- /**
397
- * Manually stop loading and render at highest resolution
398
- */
399
- stopLoading() {
400
- log.debug("Manually stopping data loading");
401
- this.noNewRequests = true;
402
- this.manuallyStopped = true;
403
- this.needsBailout = true;
404
- this.currentRequestCount = 0;
405
- this.totalBricksRequested = 0;
406
- }
407
- /**
408
- * Manually restart loading
409
- */
410
- restartLoading() {
411
- log.debug("Manually restarting data loading");
412
- this.noNewRequests = false;
413
- this.manuallyStopped = false;
414
- this.needsBailout = false;
415
- this.triggerUsage = true;
416
- this.currentRequestCount = 0;
417
- this.totalBricksRequested = 0;
418
- }
419
- /**
420
- * Handle WebGL context loss
421
- */
422
- _handleContextLost(event) {
423
- logWithColor$2("CONTEXT LOST");
424
- log.warn("WebGL context lost, preventing default and setting flag");
425
- event.preventDefault();
426
- this._isContextLost = true;
427
- if (this.channels && this.channels.zarrMappings) {
428
- this._lastChannelConfig = {
429
- zarrMappings: [...this.channels.zarrMappings],
430
- colorMappings: [...this.channels.colorMappings],
431
- downsampleMin: [...this.channels.downsampleMin],
432
- downsampleMax: [...this.channels.downsampleMax]
433
- };
434
- }
435
- }
436
- /**
437
- * Handle WebGL context restoration
438
- */
439
- _handleContextRestored(event) {
440
- logWithColor$2("CONTEXT RESTORED");
441
- log.warn("WebGL context restored, reinitializing textures");
442
- this._isContextLost = false;
443
- if (this._originalGlParam && this._originalGlParam.getContext) {
444
- this.gl = this._originalGlParam.getContext();
445
- }
446
- if (this._lastChannelConfig) {
447
- this.channels.zarrMappings = [...this._lastChannelConfig.zarrMappings];
448
- this.channels.colorMappings = [...this._lastChannelConfig.colorMappings];
449
- this.channels.downsampleMin = [...this._lastChannelConfig.downsampleMin];
450
- this.channels.downsampleMax = [...this._lastChannelConfig.downsampleMax];
451
- log.debug("Restored channel configuration after context loss");
452
- }
453
- if (this.PT && this.zarrStore && this.zarrStore.brickLayout) {
454
- try {
455
- this.initMRMCPT();
456
- log.debug("Successfully reinitialized MRMCPT after context restoration");
457
- } catch (error) {
458
- log.error("Failed to reinitialize MRMCPT after context restoration:", error);
459
- }
460
- }
461
- this._contextRestoredCallbacks.forEach((callback) => {
462
- try {
463
- callback();
464
- } catch (error) {
465
- log.error("Error in context restored callback:", error);
466
- }
467
- });
468
- }
469
- /**
470
- * Check if WebGL context is lost
471
- */
472
- isContextLost() {
473
- if (this._isContextLost)
474
- return true;
475
- if (this.gl && typeof this.gl.isContextLost === "function") {
476
- return this.gl.isContextLost();
477
- }
478
- return false;
479
- }
480
- /**
481
- * Register a callback to be called when context is restored
482
- */
483
- onContextRestored(callback) {
484
- if (typeof callback === "function") {
485
- this._contextRestoredCallbacks.push(callback);
486
- }
487
- }
488
- initImages(images, imageLayerScopes) {
489
- logWithColor$2("INIT IMAGES");
490
- this.images = images;
491
- this.imageLayerScopes = imageLayerScopes;
492
- }
493
- /**
494
- * Initialize the VolumeDataManager with Zarr store details and device limits
495
- * This should be called ONCE at website initialization
496
- * TODO(mark): merge this with the constructor?
497
- * @returns {Promise<Object>} Object with Zarr store details and device limits
498
- */
499
- async init(config) {
500
- logWithColor$2("INIT()");
501
- if (this.initStatus !== INIT_STATUS.NOT_STARTED) {
502
- log.debug("VolumeDataManager init() was called more than once!");
503
- if (this.initStatus === INIT_STATUS.COMPLETE) {
504
- return {
505
- success: true,
506
- deviceLimits: this.deviceLimits,
507
- zarrStore: this.zarrStore,
508
- // physicalScale: this.physicalScale,
509
- physicalSizeTotal: this.zarrStore.physicalSizeTotal,
510
- physicalSizeVoxel: this.zarrStore.physicalSizeVoxel,
511
- error: null
512
- };
513
- }
514
- if (this.initStatus === INIT_STATUS.FAILED) {
515
- return {
516
- success: false,
517
- error: this.initError || "Unknown initialization error"
518
- };
519
- }
520
- return {
521
- success: false,
522
- pending: true,
523
- error: "Initialization in progress"
524
- };
525
- }
526
- this.initStatus = INIT_STATUS.IN_PROGRESS;
527
- logWithColor$2("INIT() IN PROGRESS");
528
- try {
529
- const imageWrapper = this.images?.[this.imageLayerScopes?.[0]]?.image?.instance;
530
- this.ngffMetadata = imageWrapper.vivLoader.metadata;
531
- log.debug("ngffMetadata", this.ngffMetadata);
532
- if (!imageWrapper || imageWrapper.getType() !== "ome-zarr") {
533
- throw new Error("Invalid imageWrapper or not an OME-Zarr image");
534
- }
535
- const multiResolutionStats = imageWrapper.getMultiResolutionStats();
536
- const shapes = _resolutionStatsToShapes(multiResolutionStats);
537
- const resolutions = multiResolutionStats.length;
538
- this.zarrStore.resolutions = resolutions;
539
- const vivData = imageWrapper.getData();
540
- if (!Array.isArray(vivData) || vivData.length < 1) {
541
- throw new Error("Not a multiresolution loader");
542
- }
543
- if (!isEqual(vivData[0].labels, ["t", "c", "z", "y", "x"])) {
544
- throw new Error("Expected OME-Zarr data with dimensions [t, c, z, y, x]");
545
- }
546
- log.debug("vivData", vivData);
547
- const arrays = vivData.map((resolutionData) => resolutionData._data);
548
- const scales = new Array(resolutions).fill(null);
549
- if (arrays.length > 0) {
550
- const array0 = arrays[0];
551
- this.zarrStore = {
552
- resolutions,
553
- chunkSize: array0.chunks,
554
- shapes,
555
- arrays,
556
- dtype: array0.dtype,
557
- physicalSizeTotal: [],
558
- // Will be populated if metadata exists
559
- physicalSizeVoxel: [],
560
- // Will be populated if metadata exists
561
- brickLayout: [],
562
- // Calculate from shapes and chunk sizes
563
- // store: this.store,
564
- // group: this.group,
565
- channelCount: shapes[0][1],
566
- scales
567
- };
568
- this.channels.colorMappings = new Array(Math.min(this.zarrStore.channelCount, 7)).fill(-1);
569
- this.channels.zarrMappings = new Array(Math.min(this.zarrStore.channelCount, 7)).fill(void 0);
570
- this.channels.downsampleMin = new Array(Math.min(this.zarrStore.channelCount, 7)).fill(void 0);
571
- this.channels.downsampleMax = new Array(Math.min(this.zarrStore.channelCount, 7)).fill(void 0);
572
- if (array0.meta && array0.meta.physicalSizes) {
573
- const { x, y, z } = array0.meta.physicalSizes;
574
- const zSize = z?.size || 1;
575
- const ySize = y?.size || 1;
576
- const xSize = x?.size || 1;
577
- this.zarrStore.physicalSizeVoxel = [zSize, ySize, xSize];
578
- if (array0.shape && array0.shape.length >= 5) {
579
- this.zarrStore.physicalSizeTotal = [
580
- (array0.shape[2] || 1) * zSize,
581
- (array0.shape[3] || 1) * ySize,
582
- (array0.shape[4] || 1) * xSize
583
- ];
584
- }
585
- } else {
586
- this.zarrStore.physicalSizeVoxel = [1, 1, 1];
587
- this.zarrStore.physicalSizeTotal = [
588
- array0.shape[2] || 1,
589
- array0.shape[3] || 1,
590
- array0.shape[4] || 1
591
- ];
592
- }
593
- const { multiscales } = this.ngffMetadata;
594
- if (!multiscales) {
595
- throw new Error("Expected multiscales metadata in group.attrs");
596
- }
597
- if (multiscales?.[0]?.datasets?.[0]?.coordinateTransformations) {
598
- for (let i = 0; i < resolutions; i++) {
599
- if (multiscales?.[0]?.datasets?.[i]?.coordinateTransformations?.[0]?.scale) {
600
- const { scale } = multiscales[0].datasets[i].coordinateTransformations[0];
601
- scales[i] = [scale[4], scale[3], scale[2]];
602
- }
603
- }
604
- } else {
605
- log.error("no coordinateTransformations available, assuming downsampling ratio of 2 per dimension");
606
- for (let i = 0; i < resolutions; i++) {
607
- const scale = 2 ** i;
608
- scales[i] = [scale, scale, scale];
609
- }
610
- }
611
- this.zarrStore.scales = scales;
612
- const { coordinateTransformations } = this.ngffMetadata;
613
- if (coordinateTransformations?.[0]?.scale) {
614
- const { scale } = coordinateTransformations[0];
615
- const scaleLength = scale.length;
616
- if (scaleLength >= 3) {
617
- const zScale = scale[scaleLength - 3];
618
- const yScale = scale[scaleLength - 2];
619
- const xScale = scale[scaleLength - 1];
620
- this.zarrStore.physicalSizeVoxel = [zScale, yScale, xScale];
621
- if (array0.shape && array0.shape.length >= 5) {
622
- this.zarrStore.physicalSizeTotal = [
623
- (array0.shape[2] || 1) * zScale,
624
- (array0.shape[3] || 1) * yScale,
625
- (array0.shape[4] || 1) * xScale
626
- ];
627
- }
628
- }
629
- }
630
- this.zarrStore.brickLayout = _resolutionStatsToBrickLayout(imageWrapper.getMultiResolutionStats());
631
- log.debug("config", config);
632
- const { omero } = this.ngffMetadata || {};
633
- if (!omero) {
634
- throw new Error("Expected omero metadata in ngffMetadata");
635
- }
636
- log.debug("omero", omero);
637
- Object.keys(config).forEach((key, i) => {
638
- const configChannel = config[key].spatialTargetC;
639
- this.channels.zarrMappings[i] = configChannel;
640
- this.channels.colorMappings[i] = i;
641
- this.channels.downsampleMin[i] = omero?.channels?.[configChannel]?.window?.min || 0;
642
- this.channels.downsampleMax[i] = omero?.channels?.[configChannel]?.window?.max || 65535;
643
- });
644
- log.debug("zarrMappings after init", this.channels.zarrMappings);
645
- log.debug("colorMappings after init", this.channels.colorMappings);
646
- log.debug("downsampleMin after init", this.channels.downsampleMin);
647
- log.debug("downsampleMax after init", this.channels.downsampleMax);
648
- this.initMRMCPT();
649
- }
650
- this.initStatus = INIT_STATUS.COMPLETE;
651
- logWithColor$2("INIT() COMPLETE");
652
- return {
653
- success: true,
654
- deviceLimits: this.deviceLimits,
655
- zarrStore: this.zarrStore,
656
- // physicalScale: this.physicalScale,
657
- physicalSizeTotal: this.zarrStore.physicalSizeTotal,
658
- physicalSizeVoxel: this.zarrStore.physicalSizeVoxel,
659
- error: null
660
- };
661
- } catch (error) {
662
- logWithColor$2("INIT() FAILED");
663
- log.error("Error initializing VolumeDataManager:", error);
664
- this.initStatus = INIT_STATUS.FAILED;
665
- this.initError = error.message || "Unknown error";
666
- return {
667
- success: false,
668
- error: this.initError
669
- };
670
- }
671
- }
672
- /**
673
- * Initialize the BrickCache and PageTable
674
- * MRMCPT: multi-resolution multi-channel page table
675
- *
676
- * Depends on:
677
- * - zarrStore.brickLayout
678
- * - zarrMappings.length (zarrMappings: the zarr channel index for every one of the up to 7 channels)
679
- * -
680
- */
681
- initMRMCPT() {
682
- logWithColor$2("initMRMCPT");
683
- const { PT, ptTHREE, bcTHREE } = _initMRMCPT(this.zarrStore.brickLayout, this.channels.zarrMappings.length);
684
- this.PT = PT;
685
- this.ptTHREE = ptTHREE;
686
- this.bcTHREE = bcTHREE;
687
- logWithColor$2("initMRMCPT() COMPLETE");
688
- }
689
- async initTexture() {
690
- const requests = [
691
- { x: 0, y: 0, z: 1 }
692
- ];
693
- logWithColor$2("initTexture - loading first brick");
694
- await this.handleBrickRequests(requests);
695
- }
696
- updateChannels(channelProps) {
697
- logWithColor$2("updateChannels");
698
- log.debug("channelProps", channelProps);
699
- log.debug("this.channels.zarrMappings", this.channels.zarrMappings);
700
- log.debug("this.channels.colorMappings", this.channels.colorMappings);
701
- log.debug("this.channels.downsampleMin", this.channels.downsampleMin);
702
- log.debug("this.channels.downsampleMax", this.channels.downsampleMax);
703
- if (this.channels.zarrMappings.length === 0) {
704
- log.debug("channels not initialized yet");
705
- return;
706
- }
707
- const requestedZarrChannels = Object.values(channelProps).map((channelData) => channelData.spatialTargetC).filter((targetC) => targetC !== void 0);
708
- const currentZarrChannels = this.channels.zarrMappings.filter((mapping) => mapping !== void 0);
709
- const requestedSorted = [...new Set(requestedZarrChannels)].sort((a, b) => a - b);
710
- const currentSorted = [...new Set(currentZarrChannels)].sort((a, b) => a - b);
711
- if (requestedSorted.length === currentSorted.length && requestedSorted.every((val, index) => val === currentSorted[index])) {
712
- log.debug("Channel mappings unchanged, skipping update");
713
- }
714
- log.debug("Channel mappings changed:", {
715
- current: currentSorted,
716
- requested: requestedSorted
717
- });
718
- Object.entries(channelProps).forEach(([uiChannelKey, channelData]) => {
719
- const targetZarrChannel = channelData.spatialTargetC;
720
- log.debug(`UI channel "${uiChannelKey}" wants zarr channel ${targetZarrChannel}`);
721
- const existingSlotIndex = this.channels.zarrMappings.indexOf(targetZarrChannel);
722
- if (existingSlotIndex === -1) {
723
- const nextFreeSlot = this.channels.zarrMappings.findIndex((slot) => slot === void 0);
724
- if (nextFreeSlot !== -1) {
725
- this.channels.zarrMappings[nextFreeSlot] = targetZarrChannel;
726
- log.debug("channelData", channelData);
727
- log.debug("this.ngffMetadata?.omero?.channels", this.ngffMetadata?.omero?.channels);
728
- log.debug("targetZarrChannel", targetZarrChannel);
729
- this.channels.downsampleMin[nextFreeSlot] = this.ngffMetadata?.omero?.channels?.[targetZarrChannel]?.window?.min || 0;
730
- this.channels.downsampleMax[nextFreeSlot] = this.ngffMetadata?.omero?.channels?.[targetZarrChannel]?.window?.max || 65535;
731
- log.debug(`Mapped zarr channel ${targetZarrChannel} to slot ${nextFreeSlot}`);
732
- log.debug("channels", this.channels);
733
- } else {
734
- log.debug("No free slots found, looking for unused mapped channels");
735
- const currentlyMapped = this.channels.zarrMappings.filter((mapping) => mapping !== void 0);
736
- const stillRequested = requestedZarrChannels;
737
- const unusedMappedChannels = currentlyMapped.filter((mappedChannel) => !stillRequested.includes(mappedChannel));
738
- log.debug("Currently mapped:", currentlyMapped);
739
- log.debug("Still requested:", stillRequested);
740
- log.debug("Unused mapped channels:", unusedMappedChannels);
741
- if (unusedMappedChannels.length > 0) {
742
- const slotToReuse = this.channels.zarrMappings.findIndex((mapping) => unusedMappedChannels.includes(mapping));
743
- if (slotToReuse !== -1) {
744
- const oldZarrChannel = this.channels.zarrMappings[slotToReuse];
745
- this.channels.zarrMappings[slotToReuse] = targetZarrChannel;
746
- this.channels.downsampleMin[slotToReuse] = this.ngffMetadata?.omero?.channels?.[targetZarrChannel]?.window?.min || 0;
747
- this.channels.downsampleMax[slotToReuse] = this.ngffMetadata?.omero?.channels?.[targetZarrChannel]?.window?.max || 65535;
748
- log.debug(`Reused slot ${slotToReuse}: ${oldZarrChannel} -> ${targetZarrChannel}`);
749
- this._purgeChannel(slotToReuse);
750
- } else {
751
- log.error("Could not find slot to reuse - this should not happen");
752
- }
753
- } else {
754
- log.error("All slots are full and all mapped channels are still in use");
755
- }
756
- }
757
- } else {
758
- log.debug(`Zarr channel ${targetZarrChannel} already mapped to slot ${existingSlotIndex}`);
759
- }
760
- });
761
- const newColorMappings = Object.values(channelProps).map((ch) => {
762
- const slot = this.channels.zarrMappings.indexOf(ch.spatialTargetC);
763
- return slot !== -1 ? slot : -1;
764
- });
765
- while (newColorMappings.length < 7) {
766
- newColorMappings.push(-1);
767
- }
768
- log.debug("newColorMappings", newColorMappings);
769
- this.channels.colorMappings = newColorMappings;
770
- log.debug("updatedChannels", this.channels);
771
- this._lastChannelConfig = {
772
- zarrMappings: [...this.channels.zarrMappings],
773
- colorMappings: [...this.channels.colorMappings],
774
- downsampleMin: [...this.channels.downsampleMin],
775
- downsampleMax: [...this.channels.downsampleMax]
776
- };
777
- }
778
- /**
779
- * Try to load a resolution level
780
- * @param {number} resolutionIndex - The resolution level to load
781
- * @param {Array} arrays - Array to store the loaded arrays
782
- * @returns {Promise} Promise resolving when the resolution is loaded or rejected
783
- */
784
- /*
785
- async tryLoadResolution(resolutionIndex, arrays) {
786
- logWithColor('tryLoadResolution');
787
- log.debug(resolutionIndex, arrays);
788
- try {
789
- const array = await zarrita.open(this.group.resolve(String(resolutionIndex)));
790
- // Create new arrays to avoid modifying parameters directly
791
- const newArrays = [...arrays];
792
- newArrays[resolutionIndex] = array;
793
- // Update the original arrays
794
- Object.assign(arrays, newArrays);
795
- logWithColor('tryLoadResolution() COMPLETE');
796
- return { success: true, level: resolutionIndex };
797
- } catch (err) {
798
- log.error(`Failed to load resolution ${resolutionIndex}:`, err);
799
- return { success: false, level: resolutionIndex, error: err.message };
800
- }
801
- }
802
- */
803
- /**
804
- * Get physical dimensions
805
- * @returns {Array} Physical dimensions [X, Y, Z]
806
- */
807
- getPhysicalDimensionsXYZ() {
808
- log.debug("getPhysicalDimensionsXYZ");
809
- log.debug("this.zarrStore.physicalSizeTotal", this.zarrStore.physicalSizeTotal);
810
- const out = [
811
- this.zarrStore.physicalSizeTotal[2],
812
- this.zarrStore.physicalSizeTotal[1],
813
- this.zarrStore.physicalSizeTotal[0]
814
- ];
815
- log.debug("out", out);
816
- return out;
817
- }
818
- /**
819
- * Get the maximum resolution
820
- * @returns {number} Maximum resolution
821
- */
822
- getMaxResolutionXYZ() {
823
- log.debug("getMaxResolutionXYZ");
824
- log.debug("this.zarrStore.shapes", this.zarrStore.shapes);
825
- const out = [
826
- this.zarrStore.shapes[0][4],
827
- this.zarrStore.shapes[0][3],
828
- this.zarrStore.shapes[0][2]
829
- ];
830
- log.debug("out", out);
831
- return out;
832
- }
833
- getOriginalScaleXYZ() {
834
- logWithColor$2("getOriginalScaleXYZ");
835
- log.debug("this.zarrStore.physicalSizeVoxel", this.zarrStore.physicalSizeVoxel);
836
- const out = [
837
- this.zarrStore.physicalSizeVoxel[2],
838
- this.zarrStore.physicalSizeVoxel[1],
839
- this.zarrStore.physicalSizeVoxel[0]
840
- ];
841
- log.debug("out", out);
842
- return out;
843
- }
844
- getNormalizedScaleXYZ() {
845
- log.debug("getNormalizedScaleXYZ");
846
- const out = [
847
- 1,
848
- this.zarrStore.physicalSizeVoxel[1] / this.zarrStore.physicalSizeVoxel[2],
849
- this.zarrStore.physicalSizeVoxel[0] / this.zarrStore.physicalSizeVoxel[0]
850
- ];
851
- log.debug("out", out);
852
- return out;
853
- }
854
- getBoxDimensionsXYZ() {
855
- log.debug("getBoxDimensionsXYZ");
856
- log.debug("this.zarrStore.shapes", this.zarrStore.shapes);
857
- const out = [
858
- 1,
859
- this.zarrStore.shapes[0][3] / this.zarrStore.shapes[0][4],
860
- this.zarrStore.shapes[0][2] / this.zarrStore.shapes[0][4]
861
- ];
862
- log.debug("out", out);
863
- return out;
864
- }
865
- /**
866
- * Load a specific Zarr chunk based on [t,c,z,y,x] coordinates
867
- * @param {number} t - Time point (default 0)
868
- * @param {number} c - Channel (default 0)
869
- * @param {number} z - Z coordinate
870
- * @param {number} y - Y coordinate
871
- * @param {number} x - X coordinate
872
- * @param {number} resolution - Resolution level
873
- * @returns {Promise<Uint8Array>} 32x32x32 chunk data
874
- */
875
- async loadZarrChunk(t = 0, c = 0, z, y, x, resolution) {
876
- if (!this.zarrStore || !this.zarrStore.arrays[resolution]) {
877
- throw new Error("Zarr store or resolution not initialized");
878
- }
879
- log.debug("loadZarrChunk", { t, c, z, y, x, resolution });
880
- const array = this.zarrStore.arrays[resolution];
881
- const chunkEntry = await array.getChunk([t, c, z, y, x]);
882
- if (!chunkEntry) {
883
- throw new Error(`No chunk found at coordinates [${t},${c},${z},${y},${x}]`);
884
- }
885
- if (chunkEntry.data.length !== BRICK_SIZE * BRICK_SIZE * BRICK_SIZE) {
886
- throw new Error(`Unexpected chunk size: ${chunkEntry.data.length}`);
887
- }
888
- return chunkEntry.data;
889
- }
890
- /**
891
- * Process the buffer of brick requests from the shader, turning them into
892
- * actual Promises for Zarr chunks on the JS side.
893
- * @param {Uint8Array} buffer The bufRequest (of length width*height*4)
894
- * containing the brick requests from the shader.
895
- * @param {object} optsForWeighting
896
- * @param {number} optsForWeighting.width The width of the render target.
897
- * @param {number} optsForWeighting.height The height of the render target.
898
- * @param {number} optsForWeighting.sigmaNormalized The normalized sigma value
899
- * to use for weighting the brick requests based on their distance from
900
- * the center of the render target.
901
- */
902
- async processRequestData(buffer, optsForWeighting) {
903
- if (this.isBusy) {
904
- log.debug("processRequestData: already busy, skipping");
905
- return;
906
- }
907
- if (this.noNewRequests) {
908
- log.debug("processRequestData: loading stopped by user, skipping");
909
- return;
910
- }
911
- if (this.isContextLost()) {
912
- log.debug("processRequestData: WebGL context is lost, skipping");
913
- return;
914
- }
915
- this.isBusy = true;
916
- this.triggerRequest = false;
917
- const { requests, origRequestCount } = _requestBufferToRequestObjects(buffer, this.k, optsForWeighting);
918
- if (requests.length === 0) {
919
- this.noNewRequests = true;
920
- }
921
- log.debug(`processRequestData: handling ${requests.length} requests of ${origRequestCount}`);
922
- await this.handleBrickRequests(requests);
923
- this.triggerUsage = true;
924
- this.isBusy = false;
925
- }
926
- async processUsageData(buffer) {
927
- if (this.isBusy) {
928
- log.debug("processUsageData: already busy, skipping");
929
- this.needsBailout = true;
930
- return;
931
- }
932
- if (this.isContextLost()) {
933
- log.debug("processUsageData: WebGL context is lost, skipping");
934
- return;
935
- }
936
- this.isBusy = true;
937
- this.triggerUsage = false;
938
- const now = ++this.timeStamp;
939
- const usedBricks = /* @__PURE__ */ new Set();
940
- for (let i = 0; i < buffer.length; i += 4) {
941
- const x = buffer[i];
942
- const y = buffer[i + 1];
943
- const z = buffer[i + 2];
944
- if ((x | y | z) === 0) {
945
- continue;
946
- }
947
- const bcIndex = z * BRICK_CACHE_SIZE_X * BRICK_CACHE_SIZE_Y + y * BRICK_CACHE_SIZE_X + x;
948
- if (bcIndex < this.BCTimeStamps.length) {
949
- usedBricks.add(bcIndex);
950
- }
951
- }
952
- Array.from(usedBricks).forEach((bcIndex) => {
953
- this.BCTimeStamps[bcIndex] = now;
954
- });
955
- if (this.BCFull)
956
- this._buildLRU();
957
- this.triggerRequest = true;
958
- this.isBusy = false;
959
- }
960
- // Helper method to update PT entries for evicted bricks
961
- _evictBrick(bcIndex) {
962
- const pt = this.bc2pt[bcIndex];
963
- if (!pt)
964
- return;
965
- const [min, max] = this.BCMinMax[bcIndex] || [0, 0];
966
- const ptVal = (0 << 31 | 1 << 30 | Math.min(127, min >> 1) << 23 | Math.min(127, max >> 1) << 16) >>> 0;
967
- this._updatePTEntry(pt.x, pt.y, pt.z, ptVal);
968
- this.bc2pt[bcIndex] = null;
969
- }
970
- _purgeChannel(ptChannelIndex) {
971
- log.debug("purging channel", ptChannelIndex);
972
- log.debug("corresponding zarr channel", this.channels.zarrMappings[ptChannelIndex]);
973
- if (!this.ptTHREE) {
974
- log.error("pagetable texture not initialized");
975
- return;
976
- }
977
- if (this.isContextLost()) {
978
- log.warn("WebGL context is lost, skipping channel purge");
979
- return;
980
- }
981
- this.channels.downsampleMin[ptChannelIndex] = void 0;
982
- this.channels.downsampleMax[ptChannelIndex] = void 0;
983
- this.channels.zarrMappings[ptChannelIndex] = void 0;
984
- const channelMask = this.PT.channelOffsets[ptChannelIndex];
985
- log.debug("channelMask", channelMask);
986
- log.error("TODO: not tested yet");
987
- const { gl } = this;
988
- const texPT = this.renderer.properties.get(this.ptTHREE).__webglTexture;
989
- gl.activeTexture(gl.TEXTURE0);
990
- gl.bindTexture(gl.TEXTURE_3D, texPT);
991
- for (let r = 0; r < this.zarrStore.resolutions; r++) {
992
- const anchor = [
993
- this.PT.anchors[r][2] * channelMask[2],
994
- this.PT.anchors[r][1] * channelMask[1],
995
- this.PT.anchors[r][0] * channelMask[0]
996
- ];
997
- log.debug("anchor", anchor);
998
- const extents = this.zarrStore.brickLayout[r];
999
- const size = extents[0] * extents[1] * extents[2];
1000
- log.debug("extents", extents);
1001
- log.debug("size", size);
1002
- gl.texSubImage3D(gl.TEXTURE_3D, 0, anchor[0], anchor[1], anchor[2], extents[0], extents[1], extents[2], gl.RED_INTEGER, gl.UNSIGNED_INT, new Uint32Array(size));
1003
- }
1004
- gl.bindTexture(gl.TEXTURE_3D, null);
1005
- }
1006
- // Update a PT entry
1007
- _updatePTEntry(ptX, ptY, ptZ, ptVal) {
1008
- if (!this.ptTHREE)
1009
- return;
1010
- if (this.isContextLost()) {
1011
- log.warn("WebGL context is lost, skipping PT entry update");
1012
- return;
1013
- }
1014
- const { gl } = this;
1015
- const texPT = this.renderer.properties.get(this.ptTHREE).__webglTexture;
1016
- gl.activeTexture(gl.TEXTURE0);
1017
- gl.bindTexture(gl.TEXTURE_3D, texPT);
1018
- gl.texSubImage3D(gl.TEXTURE_3D, 0, ptX, ptY, ptZ, 1, 1, 1, gl.RED_INTEGER, gl.UNSIGNED_INT, new Uint32Array([ptVal]));
1019
- gl.bindTexture(gl.TEXTURE_3D, null);
1020
- }
1021
- /* ------------------------------------------------------------- *
1022
- * 2. Allocate the next n free bricks in the brick cache *
1023
- * ------------------------------------------------------------- */
1024
- /**
1025
- *
1026
- * @param {number} n The number of slots to allocate
1027
- * @returns {{ bcIndex, x, y, z }[]} Array of brick cache coordinates for the allocated slots.
1028
- */
1029
- _allocateBCSlots(n) {
1030
- let slots = [];
1031
- const total = BRICK_CACHE_SIZE_X * BRICK_CACHE_SIZE_Y * BRICK_CACHE_SIZE_Z;
1032
- if (!this.BCFull && this.BCUnusedIndex + n > total) {
1033
- this.BCFull = true;
1034
- log.debug("BRICK CACHE FULL");
1035
- }
1036
- if (!this.BCFull) {
1037
- for (let i = 0; i < n; ++i) {
1038
- const bcIndex = (this.BCUnusedIndex + i) % total;
1039
- const z = Math.floor(bcIndex / (BRICK_CACHE_SIZE_X * BRICK_CACHE_SIZE_Y));
1040
- const rem = bcIndex - z * BRICK_CACHE_SIZE_X * BRICK_CACHE_SIZE_Y;
1041
- const y = Math.floor(rem / BRICK_CACHE_SIZE_X);
1042
- const x = rem % BRICK_CACHE_SIZE_X;
1043
- slots.push({ bcIndex, x, y, z });
1044
- }
1045
- this.BCUnusedIndex += n;
1046
- } else {
1047
- if (this.LRUStack.length < n)
1048
- this._buildLRU();
1049
- slots = this.LRUStack.splice(0, n).map((bcIndex) => {
1050
- this._evictBrick(bcIndex);
1051
- const z = Math.floor(bcIndex / (BRICK_CACHE_SIZE_X * BRICK_CACHE_SIZE_Y));
1052
- const rem = bcIndex - z * BRICK_CACHE_SIZE_X * BRICK_CACHE_SIZE_Y;
1053
- const y = Math.floor(rem / BRICK_CACHE_SIZE_X);
1054
- const x = rem % BRICK_CACHE_SIZE_X;
1055
- return { bcIndex, x, y, z };
1056
- });
1057
- }
1058
- return slots;
1059
- }
1060
- /* ------------------------------------------------------------- *
1061
- * 4. Upload one brick + PT entry *
1062
- * ------------------------------------------------------------- */
1063
- async _uploadBrick(ptCoord, bcSlot) {
1064
- log.debug("uploading brick", ptCoord, bcSlot);
1065
- if (this.isContextLost()) {
1066
- log.warn("WebGL context is lost, skipping brick upload");
1067
- return;
1068
- }
1069
- if (ptCoord.x >= this.PT.xExtent || ptCoord.y >= this.PT.yExtent || ptCoord.z >= this.PT.zTotal || ptCoord.x < 0 || ptCoord.y < 0 || ptCoord.z < 0) {
1070
- log.error("this.PT", this.PT);
1071
- log.error("ptCoord out of bounds", ptCoord);
1072
- return;
1073
- }
1074
- const { channel, resolution, x, y, z } = _ptToZarr(ptCoord.x, ptCoord.y, ptCoord.z, { PT_zExtent: this.PT.zExtent, PT_z0Extent: this.PT.z0Extent, PT_anchors: this.PT.anchors });
1075
- if (!this.channels || !this.channels.zarrMappings || this.channels.zarrMappings.length === 0) {
1076
- log.error("Channel mappings not initialized, skipping brick upload");
1077
- return;
1078
- }
1079
- if (channel < 0 || channel >= this.channels.zarrMappings.length) {
1080
- log.error("Channel index out of bounds", { channel, mappingsLength: this.channels.zarrMappings.length });
1081
- return;
1082
- }
1083
- const zarrChannel = this.channels.zarrMappings[channel];
1084
- if (zarrChannel === void 0 || zarrChannel === -1) {
1085
- log.warn("zarrChannel is undefined or -1", {
1086
- zarrChannel,
1087
- channel,
1088
- ptCoord,
1089
- channelMappings: this.channels.zarrMappings,
1090
- contextLost: this.isContextLost()
1091
- });
1092
- if (this._lastChannelConfig && this._lastChannelConfig.zarrMappings[channel] !== void 0) {
1093
- log.warn("Attempting to use last known channel config");
1094
- this.channels.zarrMappings = [...this._lastChannelConfig.zarrMappings];
1095
- this.channels.colorMappings = [...this._lastChannelConfig.colorMappings];
1096
- this.channels.downsampleMin = [...this._lastChannelConfig.downsampleMin];
1097
- this.channels.downsampleMax = [...this._lastChannelConfig.downsampleMax];
1098
- const restoredZarrChannel = this.channels.zarrMappings[channel];
1099
- if (restoredZarrChannel !== void 0 && restoredZarrChannel !== -1) {
1100
- log.debug("Successfully restored channel mapping, continuing with upload");
1101
- } else {
1102
- log.error("Could not restore valid channel mapping, aborting brick upload");
1103
- return;
1104
- }
1105
- } else {
1106
- log.error("No fallback channel config available, aborting brick upload");
1107
- return;
1108
- }
1109
- }
1110
- log.debug("starting to load zarr chunk", { resolution, z, y, x, zarrChannel });
1111
- let chunk = await this.loadZarrChunk(0, zarrChannel, z, y, x, resolution);
1112
- log.debug("chunk", chunk);
1113
- if (chunk instanceof Uint16Array) {
1114
- log.debug("chunk is Uint16Array, converting to Uint8Array");
1115
- if (this.channels.downsampleMin[channel] === void 0) {
1116
- const channelId = this.channels.zarrMappings[channel];
1117
- log.debug("channelId was not found in this.channels.downsampleMin[channel]", channelId);
1118
- this.channels.downsampleMin[channel] = this.ngffMetadata?.omero?.channels?.[channelId]?.window?.min || 0;
1119
- this.channels.downsampleMax[channel] = this.ngffMetadata?.omero?.channels?.[channelId]?.window?.max || 65535;
1120
- log.debug("this.channels.downsampleMin[channel]", this.channels.downsampleMin[channel]);
1121
- log.debug("this.channels.downsampleMax[channel]", this.channels.downsampleMax[channel]);
1122
- }
1123
- const uint16View = new Uint16Array(chunk.buffer);
1124
- const uint8Chunk = new Uint8Array(chunk.length);
1125
- const minVal = this.channels.downsampleMin[channel];
1126
- const maxVal = this.channels.downsampleMax[channel];
1127
- const range = maxVal - minVal;
1128
- for (let i = 0; i < uint16View.length; i++) {
1129
- const val = uint16View[i];
1130
- const clamped = Math.max(minVal, Math.min(maxVal, val));
1131
- uint8Chunk[i] = (clamped - minVal) * 255 / range | 0;
1132
- }
1133
- chunk = uint8Chunk;
1134
- }
1135
- if (!(chunk instanceof Uint8Array)) {
1136
- throw new Error(`Unsupported chunk type: ${chunk.constructor.name}. Expected Uint8Array.`);
1137
- }
1138
- let min = 255;
1139
- let max = 0;
1140
- for (let i = 0; i < chunk.length; ++i) {
1141
- const v = chunk[i];
1142
- if (v < min)
1143
- min = v;
1144
- if (v > max)
1145
- max = v;
1146
- }
1147
- const { gl } = this;
1148
- const texBC = this.renderer.properties.get(this.bcTHREE).__webglTexture;
1149
- gl.activeTexture(gl.TEXTURE2);
1150
- gl.bindTexture(gl.TEXTURE_3D, texBC);
1151
- gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
1152
- gl.texSubImage3D(gl.TEXTURE_3D, 0, bcSlot.x * BRICK_SIZE, bcSlot.y * BRICK_SIZE, bcSlot.z * BRICK_SIZE, BRICK_SIZE, BRICK_SIZE, BRICK_SIZE, gl.RED, gl.UNSIGNED_BYTE, chunk);
1153
- gl.pixelStorei(gl.UNPACK_ALIGNMENT, 4);
1154
- gl.bindTexture(gl.TEXTURE_3D, null);
1155
- const error = gl.getError();
1156
- if (error !== gl.NO_ERROR) {
1157
- log.error("WebGL error during brick upload:", error, chunk);
1158
- }
1159
- if (channel >= this.channels.zarrMappings.length) {
1160
- log.debug("channel is out of bounds", channel);
1161
- min = 255;
1162
- max = 255;
1163
- }
1164
- const ptVal = _packPT(min, max, bcSlot.x, bcSlot.y, bcSlot.z);
1165
- const texPT = this.renderer.properties.get(this.ptTHREE).__webglTexture;
1166
- gl.activeTexture(gl.TEXTURE0);
1167
- gl.bindTexture(gl.TEXTURE_3D, texPT);
1168
- gl.texSubImage3D(gl.TEXTURE_3D, 0, ptCoord.x, ptCoord.y, ptCoord.z, 1, 1, 1, gl.RED_INTEGER, gl.UNSIGNED_INT, new Uint32Array([ptVal]));
1169
- gl.bindTexture(gl.TEXTURE_3D, null);
1170
- const error2 = gl.getError();
1171
- if (error2 !== gl.NO_ERROR) {
1172
- log.error("WebGL error during pagetable upload:", error2, chunk);
1173
- }
1174
- this.BCTimeStamps[bcSlot.bcIndex] = this.timeStamp;
1175
- this.BCMinMax[bcSlot.bcIndex] = [min, max];
1176
- this.bc2pt[bcSlot.bcIndex] = ptCoord;
1177
- }
1178
- /* ------------------------------------------------------------- *
1179
- * 5. Public: handle a batch of PT requests (array of {x,y,z}) *
1180
- * ------------------------------------------------------------- */
1181
- async handleBrickRequests(ptRequests) {
1182
- if (ptRequests.length === 0)
1183
- return;
1184
- this.totalBricksRequested = ptRequests.length;
1185
- this.currentRequestCount = ptRequests.length;
1186
- const slots = this._allocateBCSlots(ptRequests.length);
1187
- log.debug("Handling brick requests:", { requestCount: ptRequests.length, slotCount: slots.length });
1188
- log.debug("handleBrickRequests: starting for loop");
1189
- for (let i = 0; i < ptRequests.length; ++i) {
1190
- await this._uploadBrick(ptRequests[i], slots[i]);
1191
- this.currentRequestCount = ptRequests.length - i - 1;
1192
- const rlength = this.bricksEverLoaded.size;
1193
- this.bricksEverLoaded.add(`${ptRequests[i].x},${ptRequests[i].y},${ptRequests[i].z}`);
1194
- if (rlength === this.bricksEverLoaded.size) {
1195
- log.debug("DUPLICATE BRICK LOADED", ptRequests[i]);
1196
- }
1197
- if (this.needsBailout) {
1198
- log.debug("Bailing out of handleBrickRequests early due to needsBailout flag");
1199
- this.needsBailout = false;
1200
- this.currentRequestCount = 0;
1201
- break;
1202
- }
1203
- }
1204
- this.currentRequestCount = 0;
1205
- log.debug("this.bricksEverLoaded", this.bricksEverLoaded);
1206
- }
1207
- /* --------------------------------------------------------- *
1208
- * Rebuild the LRUStack with the k least-recently-used bricks *
1209
- * --------------------------------------------------------- */
1210
- _buildLRU() {
1211
- const brickIndicesWithTimes = this.BCTimeStamps.map((time, index) => ({ index, time }));
1212
- this.LRUStack = brickIndicesWithTimes.sort((a, b) => a.time - b.time).slice(0, this.k).map((item) => item.index);
1213
- }
1214
- }
1215
- const volumeVertexShader = `//
1216
- // Output: Unnormalized ray direction from camera to each vertex
1217
- // Used by fragment shader for ray marching through the volume
1218
- out vec3 rayDirUnnorm;
1219
-
1220
- // Output: Camera position transformed into volume's local coordinate system
1221
- // Used to calculate ray origins in the fragment shader
1222
- out vec3 cameraCorrected;
1223
-
1224
- // Volume scale uniform (likely for anisotropic voxels)
1225
- uniform vec3 u_vol_scale;
1226
-
1227
- // Volume size uniform
1228
- uniform vec3 u_size;
1229
-
1230
- // Output: Vertex positions normalized to [0,1] range within volume bounds
1231
- // Standard coordinate system for volume sampling
1232
- varying vec3 worldSpaceCoords;
1233
-
1234
- // Output: Texture coordinates for sampling volume data
1235
- varying vec2 vUv;
1236
-
1237
- // Output: Final clip-space position (stored for fragment shader access)
1238
- varying vec4 glPosition;
1239
-
1240
- // Volume bounding box size uniform
1241
- uniform highp vec3 boxSize;
1242
-
1243
- void main()
1244
- {
1245
- // Transform vertex positions from [-0.5, 0.5] range to [0, 1] range
1246
- // This is the standard coordinate system for volume sampling
1247
- //
1248
- // Mathematical transformation:
1249
- // worldSpaceCoords = (position / boxSize) + 0.5
1250
- //
1251
- // Example:
1252
- // position = (-0.5, -0.5, -0.5) → worldSpaceCoords = (0, 0, 0)
1253
- // position = ( 0.0, 0.0, 0.0) → worldSpaceCoords = (0.5, 0.5, 0.5)
1254
- // position = ( 0.5, 0.5, 0.5) → worldSpaceCoords = (1, 1, 1)
1255
- worldSpaceCoords = position / boxSize + vec3(0.5, 0.5, 0.5); //move it from [-0.5;0.5] to [0,1]
1256
-
1257
- // Transform camera position into volume's local coordinate system
1258
- // This gives us the ray origin in volume space
1259
- cameraCorrected = (inverse(modelMatrix) * vec4(cameraPosition, 1.)).xyz;
1260
-
1261
- // Calculate unnormalized ray direction from camera to each vertex
1262
- // Used by fragment shader for ray marching through the volume
1263
- rayDirUnnorm = position - cameraCorrected;
1264
-
1265
- // Apply standard MVP transformation to get clip-space coordinates
1266
- gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
1267
-
1268
- // Store clip-space position for fragment shader access
1269
- glPosition = gl_Position;
1270
-
1271
- // Pass through texture coordinates for volume sampling
1272
- vUv = uv;
1273
- }
1274
- `;
1275
- const volumeFragmentShader = `//
1276
- // #include <packing>
1277
- precision highp float;
1278
- precision highp int;
1279
- precision highp sampler3D;
1280
- precision highp usampler3D;
1281
-
1282
- // ========================================
1283
- // INPUT VARIABLES (from vertex shader)
1284
- // ========================================
1285
- // Unnormalized ray direction from camera
1286
- in vec3 rayDirUnnorm;
1287
- // Camera position in world space
1288
- in vec3 cameraCorrected;
1289
-
1290
- // ========================================
1291
- // TEXTURE SAMPLERS
1292
- // ========================================
1293
- // 3D texture containing cached brick data (2048x2048x128)
1294
- // (2048*2048*128)/(32*32*32) = 16,384 bricks can be stored?
1295
- uniform sampler3D brickCacheTex;
1296
- // 3D texture containing page table entries (brick metadata)
1297
- uniform usampler3D pageTableTex;
1298
-
1299
- // ========================================
1300
- // RENDERING PARAMETERS/CONSTANTS
1301
- // ========================================
1302
- // Rendering style: 0=MIP, 1=MinIP, 2=standard volume rendering, 3=DEBUG
1303
- uniform int u_renderstyle;
1304
- // Global opacity multiplier for volume rendering
1305
- uniform float opacity;
1306
-
1307
- // ========================================
1308
- // CONTRAST LIMITS (per channel)
1309
- // per channel min/max values for value normalization
1310
- // ========================================
1311
- uniform vec2 clim0;
1312
- uniform vec2 clim1;
1313
- uniform vec2 clim2;
1314
- uniform vec2 clim3;
1315
- uniform vec2 clim4;
1316
- uniform vec2 clim5;
1317
- uniform vec2 clim6;
1318
-
1319
- // ========================================
1320
- // CLIPPING PLANES
1321
- // e.g., for X-axis clipping: (min_x, max_x) or (-1, -1) if disabled
1322
- // ========================================
1323
- uniform vec2 xClip;
1324
- uniform vec2 yClip;
1325
- uniform vec2 zClip;
1326
-
1327
- // ========================================
1328
- // CHANNEL COLORS AND OPACITIES
1329
- // rgb -- color values, a -- visibility (boolean)
1330
- // ========================================
1331
- uniform vec4 color0;
1332
- uniform vec4 color1;
1333
- uniform vec4 color2;
1334
- uniform vec4 color3;
1335
- uniform vec4 color4;
1336
- uniform vec4 color5;
1337
- uniform vec4 color6;
1338
-
1339
- // maps colors to physical spaces
1340
- uniform int channelMapping[7];
1341
-
1342
- // ========================================
1343
- // VOLUME AND RESOLUTION PARAMETERS
1344
- // ========================================
1345
- // Volume bounding box size in world space
1346
- uniform highp vec3 boxSize;
1347
- // Rendering resolution level (affects step size)
1348
- // stepsize, correlates with resolution
1349
- uniform int renderRes;
1350
- // Volume dimensions in voxels (x, y, z)
1351
- // resolution 0 voxel extents
1352
- uniform uvec3 voxelExtents;
1353
- // Global resolution range: (min_res, max_res)
1354
- // global range of requested resolutions
1355
- uniform ivec2 resGlobal;
1356
- // Maximum number of active channels
1357
- // max number of channels (relevant for the cache statistics)
1358
- // between 1 and 7
1359
- uniform int maxChannels;
1360
-
1361
- // ========================================
1362
- // PER-CHANNEL RESOLUTION RANGES
1363
- // per color channel resolution range
1364
- // Each channel can have different available resolution levels
1365
- // e.g., for Channel 0: (min_res, max_res)
1366
- // ========================================
1367
- uniform ivec2 res0;
1368
- uniform ivec2 res1;
1369
- uniform ivec2 res2;
1370
- uniform ivec2 res3;
1371
- uniform ivec2 res4;
1372
- uniform ivec2 res5;
1373
- uniform ivec2 res6;
1374
- // Channel 7: unused
1375
- uniform ivec2 res7;
1376
-
1377
- // ========================================
1378
- // LEVEL-OF-DETAIL PARAMETERS
1379
- // controls how fast we decrease the resolution
1380
- // ========================================
1381
- // LOD factor for distance-based resolution selection
1382
- uniform float lodFactor;
1383
-
1384
- // ========================================
1385
- // ANCHOR POINTS (per resolution level)
1386
- // per resolution anchor point for pagetable
1387
- // ========================================
1388
- // Anchor points define the origin of page table for each resolution level
1389
- // Resolution 0 anchor point (highest detail)
1390
- uniform uvec3 anchor0;
1391
- uniform uvec3 anchor1;
1392
- uniform uvec3 anchor2;
1393
- uniform uvec3 anchor3;
1394
- uniform uvec3 anchor4;
1395
- uniform uvec3 anchor5;
1396
- uniform uvec3 anchor6;
1397
- uniform uvec3 anchor7;
1398
- uniform uvec3 anchor8;
1399
- uniform uvec3 anchor9;
1400
- // Resolution 9 anchor point (lowest detail)
1401
-
1402
- // ========================================
1403
- // SCALE FACTORS (per resolution level)
1404
- // per resolution downsample factor
1405
- // ========================================
1406
- // Scale factors determine voxel size at each resolution level
1407
- // Resolution 0 scale factors (should be 1,1,1)
1408
- uniform vec3 scale0;
1409
- uniform vec3 scale1;
1410
- uniform vec3 scale2;
1411
- uniform vec3 scale3;
1412
- uniform vec3 scale4;
1413
- uniform vec3 scale5;
1414
- uniform vec3 scale6;
1415
- uniform vec3 scale7;
1416
- uniform vec3 scale8;
1417
- uniform vec3 scale9;
1418
- // Resolution 9 scale factors
1419
-
1420
- // ========================================
1421
- // VARYING VARIABLES (unused but required)
1422
- // ========================================
1423
- // Fragment position (unused)
1424
- varying vec4 glPosition;
1425
- // World space coordinates (used for depth only)
1426
- varying vec3 worldSpaceCoords;
1427
-
1428
- // ========================================
1429
- // OUTPUT VARIABLES (multiple render targets)
1430
- // output buffers
1431
- // ========================================
1432
- // Final rendered color (sRGB)
1433
- layout(location = 0) out vec4 gColor;
1434
- // Brick loading requests (packed coordinates)
1435
- layout(location = 1) out vec4 gRequest;
1436
- // Brick usage tracking (for cache management)
1437
- layout(location = 2) out vec4 gUsage;
1438
-
1439
- // ========================================
1440
- // CONSTANTS
1441
- // ========================================
1442
- // Size of each brick in voxels (32x32x32)
1443
- const float BRICK_SIZE = 32.0;
1444
- // Brick cache texture width
1445
- const float BRICK_CACHE_SIZE_X = 2048.0;
1446
- // Brick cache texture height
1447
- const float BRICK_CACHE_SIZE_Y = 2048.0;
1448
- // Brick cache texture depth
1449
- const float BRICK_CACHE_SIZE_Z = 128.0;
1450
- // Number of bricks in X (64)
1451
- const float BRICK_CACHE_BRICKS_X = BRICK_CACHE_SIZE_X / BRICK_SIZE;
1452
- // Number of bricks in Y (64)
1453
- const float BRICK_CACHE_BRICKS_Y = BRICK_CACHE_SIZE_Y / BRICK_SIZE;
1454
- // Number of bricks in Z (4)
1455
- const float BRICK_CACHE_BRICKS_Z = BRICK_CACHE_SIZE_Z / BRICK_SIZE;
1456
-
1457
- // ========================================
1458
- // RAY-VOLUME INTERSECTION
1459
- // calculating the intersection of the ray with the bounding box
1460
- // ========================================
1461
- // Calculates the intersection of a ray with the volume's bounding box
1462
- // Returns (entry_time, exit_time) for the ray-box intersection
1463
- // Handles clipping planes by adjusting the bounding box
1464
- //
1465
- // Parameters:
1466
- // orig - vec3: Ray origin point in world space
1467
- // dir - vec3: Ray direction vector (should be normalized)
1468
- //
1469
- // Returns:
1470
- // vec2: (entry_time, exit_time) where:
1471
- // - entry_time: Distance along ray to enter the volume
1472
- // - exit_time: Distance along ray to exit the volume
1473
- // - If no intersection: entry_time > exit_time
1474
- vec2 intersect_hit(vec3 orig, vec3 dir) {
1475
- // Start with full volume bounds
1476
- vec3 boxMin = vec3(-0.5) * boxSize;
1477
- vec3 boxMax = vec3(0.5) * boxSize;
1478
-
1479
- // Apply clipping planes if they're active (xClip.x > -1.0 means active)
1480
- if (xClip.x > -1.0) {
1481
- boxMin.x = xClip.x - (boxSize.x / 2.0);
1482
- if (xClip.y < boxSize.x)
1483
- boxMax.x = xClip.y - (boxSize.x / 2.0);
1484
- }
1485
- if (yClip.x > -1.0) {
1486
- boxMin.y = yClip.x - (boxSize.y / 2.0);
1487
- if (yClip.y < boxSize.y)
1488
- boxMax.y = yClip.y - (boxSize.y / 2.0);
1489
- }
1490
- if (zClip.x > -1.0) {
1491
- boxMin.z = zClip.x - (boxSize.z / 2.0);
1492
- if (zClip.y < boxSize.z)
1493
- boxMax.z = zClip.y - (boxSize.z / 2.0);
1494
- }
1495
-
1496
- // Standard ray-box intersection algorithm
1497
- vec3 invDir = 1.0 / dir;
1498
- vec3 tmin0 = (boxMin - orig) * invDir;
1499
- vec3 tmax0 = (boxMax - orig) * invDir;
1500
- vec3 tmin = min(tmin0, tmax0);
1501
- vec3 tmax = max(tmin0, tmax0);
1502
- float t0 = max(tmin.x, max(tmin.y, tmin.z)); // Entry time
1503
- float t1 = min(tmax.x, min(tmax.y, tmax.z)); // Exit time
1504
- return vec2(t0, t1);
1505
- }
1506
-
1507
- // ========================================
1508
- // UTILITY FUNCTIONS
1509
- // ========================================
1510
-
1511
- // Pseudo-random number generator for jittered sampling
1512
- // random number generator based on the uv coordinate
1513
- // Author @patriciogv - 2015
1514
- // http://patriciogonzalezvivo.com
1515
- //
1516
- // Parameters:
1517
- // None (uses gl_FragCoord.xy as input)
1518
- //
1519
- // Returns:
1520
- // float: Random value between 0.0 and 1.0 based on fragment coordinates
1521
- float random() {
1522
- return fract(sin(dot(gl_FragCoord.xy, vec2(12.9898,78.233)))* 43758.5453123);
1523
- }
1524
-
1525
- // Convert from linear RGB to sRGB color space
1526
- // Implements the standard sRGB transfer function for gamma correction
1527
- //
1528
- // Parameters:
1529
- // x - float: Linear RGB value between 0.0 and 1.0
1530
- //
1531
- // Returns:
1532
- // float: sRGB value between 0.0 and 1.0
1533
- float linear_to_srgb(float x) {
1534
- if (x <= 0.0031308f) {
1535
- return 12.92f * x;
1536
- }
1537
- return 1.055f * pow(x, 1.f / 2.4f) - 0.055f;
1538
- }
1539
-
1540
- // Convert from linear RGB to sRGB color space (vector version)
1541
- // Applies sRGB conversion to each RGB component while preserving alpha
1542
- //
1543
- // Parameters:
1544
- // x - vec4: Linear RGBA color with components between 0.0 and 1.0
1545
- //
1546
- // Returns:
1547
- // vec4: sRGB RGBA color with components between 0.0 and 1.0
1548
- vec4 linear_to_srgb(vec4 x) {
1549
- return vec4(linear_to_srgb(x.r), linear_to_srgb(x.g), linear_to_srgb(x.b), x.a);
1550
- }
1551
-
1552
- // ========================================
1553
- // PAGE TABLE COORDINATE PACKING
1554
- // transform the pagetable coordinate into a RGBA8 value
1555
- // ========================================
1556
- // Packs 3D page table coordinates into RGBA8 texture format
1557
- // Uses 10 bits for X, 10 bits for Y, 12 bits for Z
1558
- //
1559
- // Parameters:
1560
- // coord - uvec3: 3D coordinates to pack (X, Y, Z components)
1561
- // - X coordinate: 10-bit unsigned integer (0-1023)
1562
- // - Y coordinate: 10-bit unsigned integer (0-1023)
1563
- // - Z coordinate: 12-bit unsigned integer (0-4095)
1564
- //
1565
- // Returns:
1566
- // vec4: RGBA8 encoded coordinates with components between 0.0 and 1.0
1567
- // - R: Upper 8 bits of packed 32-bit value
1568
- // - G: Middle-upper 8 bits of packed 32-bit value
1569
- // - B: Middle-lower 8 bits of packed 32-bit value
1570
- // - A: Lower 8 bits of packed 32-bit value
1571
- vec4 packPTCoordToRGBA8(uvec3 coord) {
1572
-
1573
- uint x = coord.x & 0x3FFu; // 10 bits for X coordinate
1574
- uint y = coord.y & 0x3FFu; // 10 bits for Y coordinate
1575
- uint z = coord.z & 0xFFFu; // 12 bits for Z coordinate
1576
-
1577
- // Pack into 32-bit integer
1578
- uint packed =
1579
- (x << 22u) |
1580
- (y << 12u) |
1581
- (z);
1582
-
1583
- // Decompose into RGBA8 format
1584
- return vec4(
1585
- float((packed >> 24u) & 0xFFu) / 255.0,
1586
- float((packed >> 16u) & 0xFFu) / 255.0,
1587
- float((packed >> 8u) & 0xFFu) / 255.0,
1588
- float(packed & 0xFFu) / 255.0
1589
- );
1590
- }
1591
-
1592
- // ========================================
1593
- // RESOLUTION AND ANCHOR POINT ACCESSORS
1594
- // ========================================
1595
-
1596
- // Get anchor point for a specific resolution level
1597
- // Anchor points define the origin of the page table for each resolution
1598
- //
1599
- // Parameters:
1600
- // index - int: Resolution level index (0-9, where 0 is highest resolution)
1601
- //
1602
- // Returns:
1603
- // uvec3: 3D anchor point coordinates in the page table, or (-1, -1, -1) if invalid
1604
- uvec3 getAnchorPoint(int index) {
1605
- if (index == 0) return anchor0;
1606
- if (index == 1) return anchor1;
1607
- if (index == 2) return anchor2;
1608
- if (index == 3) return anchor3;
1609
- if (index == 4) return anchor4;
1610
- if (index == 5) return anchor5;
1611
- if (index == 6) return anchor6;
1612
- if (index == 7) return anchor7;
1613
- if (index == 8) return anchor8;
1614
- if (index == 9) return anchor9;
1615
- return uvec3(-1, -1, -1);
1616
- }
1617
-
1618
- // Find the lowest available resolution level
1619
- // Returns the highest resolution index that has valid data
1620
- //
1621
- // Parameters:
1622
- // None
1623
- //
1624
- // Returns:
1625
- // int: Highest resolution level index (0-9) that has valid anchor point data
1626
- // Returns 9 if no valid resolution levels are found
1627
- int getLowestRes() {
1628
- for (int i = 0; i < 10; i++) {
1629
- if (getAnchorPoint(i) == uvec3(0,0,0)) {
1630
- return i - 1;
1631
- }
1632
- }
1633
- return 9;
1634
- }
1635
-
1636
- // Get the downsample factor for a resolution level
1637
- // Scale factors determine the voxel size at each resolution
1638
- //
1639
- // Parameters:
1640
- // index - int: Resolution level index (0-9, where 0 is highest resolution)
1641
- //
1642
- // Returns:
1643
- // vec3: Scale factors (x, y, z) for the resolution level, or (-1, -1, -1) if invalid
1644
- // Higher scale factors indicate larger voxels (lower resolution)
1645
- vec3 getScale(int index) {
1646
- if (index == 0) return scale0;
1647
- if (index == 1) return scale1;
1648
- if (index == 2) return scale2;
1649
- if (index == 3) return scale3;
1650
- if (index == 4) return scale4;
1651
- if (index == 5) return scale5;
1652
- if (index == 6) return scale6;
1653
- if (index == 7) return scale7;
1654
- if (index == 8) return scale8;
1655
- if (index == 9) return scale9;
1656
- return vec3(-1.0, -1.0, -1.0);
1657
- }
1658
-
1659
- // Get the resolution range for a color channel
1660
- // Returns (min_res, max_res) for the channel
1661
- //
1662
- // Parameters:
1663
- // index - int: Channel index (0-6)
1664
- //
1665
- // Returns:
1666
- // ivec2: Resolution range as (min_resolution_level, max_resolution_level)
1667
- // Returns (-1, -1) if channel index is invalid
1668
- ivec2 getRes(int index) {
1669
- if (index == 0) return res0;
1670
- if (index == 1) return res1;
1671
- if (index == 2) return res2;
1672
- if (index == 3) return res3;
1673
- if (index == 4) return res4;
1674
- if (index == 5) return res5;
1675
- if (index == 6) return res6;
1676
- return ivec2(-1, -1);
1677
- }
1678
-
1679
- // Get the min/max values (contrast limits) for a color channel
1680
- // Returns (min_value, max_value) for normalization
1681
- //
1682
- // Parameters:
1683
- // index - int: Channel index (0-6)
1684
- //
1685
- // Returns:
1686
- // vec2: Contrast limits as (min_value, max_value) for data normalization
1687
- // Returns (-1.0, -1.0) if channel index is invalid
1688
- vec2 getClim(int index) {
1689
- if (index == 0) return clim0;
1690
- if (index == 1) return clim1;
1691
- if (index == 2) return clim2;
1692
- if (index == 3) return clim3;
1693
- if (index == 4) return clim4;
1694
- if (index == 5) return clim5;
1695
- if (index == 6) return clim6;
1696
- return vec2(-1.0, -1.0);
1697
- }
1698
-
1699
- // ========================================
1700
- // COORDINATE TRANSFORMATIONS
1701
- // ========================================
1702
-
1703
- // Convert normalized coordinates (0-1) to voxel coordinates.
1704
- // get the voxel coordinate in the specified resolution from the normalized coordinate
1705
- //
1706
- // Parameters:
1707
- // normalized - vec3: Normalized coordinates in range [0,1] for each axis
1708
- // res - int: Resolution level (0=highest detail, 9=lowest detail)
1709
- //
1710
- // Returns:
1711
- // vec3: Voxel coordinates in the volume space at the specified resolution
1712
- vec3 getVoxelFromNormalized(vec3 normalized, int res) {
1713
- vec3 extents = (vec3(voxelExtents) / getScale(res)); // Voxel extents at this resolution
1714
- vec3 voxel = normalized * extents;
1715
- return voxel;
1716
- }
1717
-
1718
- // Convert voxel coordinates to normalized coordinates (0-1)
1719
- // get the normalized coordinate based on the voxel coordinate in the specified resolution
1720
- //
1721
- // Parameters:
1722
- // voxel - vec3: Voxel coordinates in the volume space
1723
- // res - int: Resolution level (0=highest detail, 9=lowest detail)
1724
- //
1725
- // Returns:
1726
- // vec3: Normalized coordinates in range [0,1] for each axis
1727
- vec3 getNormalizedFromVoxel(vec3 voxel, int res) {
1728
- vec3 extents = (vec3(voxelExtents) / getScale(res)); // Voxel extents at this resolution
1729
- vec3 normalized = voxel / extents;
1730
- return normalized;
1731
- }
1732
-
1733
- // Convert normalized coordinates to brick coordinates.
1734
- // get the brick coordinate in the specified resolution based on the normalized coordinate
1735
- // needed for pagetable calculations
1736
- //
1737
- // Parameters:
1738
- // normalized - vec3: Normalized coordinates in range [0,1] for each axis
1739
- // res - int: Resolution level (0=highest detail, 9=lowest detail)
1740
- //
1741
- // Returns:
1742
- // vec3: Brick coordinates (each brick is 32x32x32 voxels)
1743
- vec3 getBrickFromNormalized(vec3 normalized, int res) {
1744
- vec3 voxel = getVoxelFromNormalized(normalized, res);
1745
- vec3 brick = floor(voxel / 32.0); // Each brick is 32x32x32 voxels
1746
- return brick;
1747
- }
1748
-
1749
- // Convert voxel coordinates to brick coordinates.
1750
- // get the brick coordinate in the specified resolution based on the voxel coordinate
1751
- //
1752
- // Parameters:
1753
- // voxel - vec3: Voxel coordinates in the volume space
1754
- // res - int: Resolution level (0=highest detail, 9=lowest detail)
1755
- //
1756
- // Returns:
1757
- // vec3: Brick coordinates (each brick is 32x32x32 voxels)
1758
- vec3 getBrickFromVoxel(vec3 voxel, int res) {
1759
- vec3 brick = floor(voxel / 32.0); // Each brick is 32x32x32 voxels
1760
- return brick;
1761
- }
1762
-
1763
- // ========================================
1764
- // CHANNEL-SPECIFIC ACCESSORS
1765
- // ========================================
1766
-
1767
- // Get channel offset in page table.
1768
- // get the vector for the specified channel slot in the pagetable
1769
- // Different channels are stored at different Z-offsets in the page table
1770
- //
1771
- // Parameters:
1772
- // index - int: Channel index (0-6)
1773
- //
1774
- // Returns:
1775
- // uvec3: 3D offset coordinates for the channel in the page table
1776
- uvec3 getChannelOffset(int index) {
1777
- if (index == 0) return uvec3(0, 0, 1);
1778
- if (index == 1) return uvec3(0, 1, 0);
1779
- if (index == 2) return uvec3(0, 1, 1);
1780
- if (index == 3) return uvec3(1, 0, 0);
1781
- if (index == 4) return uvec3(1, 0, 1);
1782
- if (index == 5) return uvec3(1, 1, 0);
1783
- if (index == 6) return uvec3(1, 1, 1);
1784
- return uvec3(0, 0, 0);
1785
- }
1786
-
1787
- // Get color for a channel.
1788
- // get the color per color channel
1789
- //
1790
- // Parameters:
1791
- // index - int: Channel index (0-6)
1792
- //
1793
- // Returns:
1794
- // vec3: RGB color values for the specified channel
1795
- vec3 getChannelColor(int index) {
1796
- if (index == 0) return color0.xyz;
1797
- if (index == 1) return color1.xyz;
1798
- if (index == 2) return color2.xyz;
1799
- if (index == 3) return color3.xyz;
1800
- if (index == 4) return color4.xyz;
1801
- if (index == 5) return color5.xyz;
1802
- if (index == 6) return color6.xyz;
1803
- return vec3(0.0, 0.0, 0.0);
1804
- }
1805
-
1806
- // Get opacity for a channel
1807
- // get the opacity (used as visibility) per color channel
1808
- //
1809
- // Parameters:
1810
- // index - int: Channel index (0-6)
1811
- //
1812
- // Returns:
1813
- // float: Opacity value (0.0-1.0) for the specified channel
1814
- float getChannelOpacity(int index) {
1815
- if (index == 0) return color0.w;
1816
- if (index == 1) return color1.w;
1817
- if (index == 2) return color2.w;
1818
- if (index == 3) return color3.w;
1819
- if (index == 4) return color4.w;
1820
- if (index == 5) return color5.w;
1821
- if (index == 6) return color6.w;
1822
- return 0.0;
1823
- }
1824
-
1825
- // ========================================
1826
- // PAGE TABLE DECODING
1827
- // ========================================
1828
-
1829
- /**
1830
- * retrieving the brick based on:
1831
- * location -- normalized coordinate
1832
- * targetRes -- target resolution
1833
- * channel -- physical channel slot
1834
- * rnd -- random number for jittering requests
1835
- * query -- whether to query the brick (we dont query for interblock interpolation)
1836
- * colorIndex -- color index for querying the min max values
1837
- *
1838
- * returns:
1839
- * w >= 0 -- xyz contains brick cache coordinate, w stores resolution
1840
- * w == -1 -- not resident in any resolution, should be treated as empty
1841
- * w == -2 -- empty (with respect to current transfer function)
1842
- * w == -3 -- constant full (with respect to current transfer function)
1843
- * w == -4 -- constant value within range, x stores that value
1844
- *
1845
- * bit layout:
1846
- * [1] 31 | 0 — flag resident
1847
- * [1] 30 | 1 — flag init
1848
- * [7] 23…29 | 2…8 — min → 128
1849
- * [7] 16…22 | 9…15 — max → 128
1850
- * [6] 10…15 | 16…21 — x offset in brick cache → max 64
1851
- * [6] 4…9 | 22…27 — y offset in brick cache → max 64
1852
- * [4] 0…3 | 28…31 — z offset in brick cache → max 16, effectively 4
1853
- */
1854
-
1855
- /*
1856
- Page table entry format (32 bits):
1857
- [31] | 0 — flag resident (1=loaded in cache)
1858
- [30] | 1 — flag init (1=initialized)
1859
- [29:23] | 2…8 — min value (7 bits) → 128 levels
1860
- [22:16] | 9…15 — max value (7 bits) → 128 levels
1861
- [15:10] | 16…21 — x offset in brick cache (6 bits) → 64 bricks
1862
- [9:4] | 22…27 — y offset in brick cache (6 bits) → 64 bricks
1863
- [3:0] | 28…31 — z offset in brick cache (4 bits) → 16 bricks
1864
- */
1865
-
1866
- // Query page table to find brick location and status
1867
- // Searches for a brick at the specified location across multiple resolution levels
1868
- // and returns its cache coordinates and status information
1869
- //
1870
- // Parameters:
1871
- // location - vec3: Normalized coordinates (0-1) within the volume
1872
- // targetRes - int: Target resolution level to start searching from
1873
- // channel - int: Channel index (0-6) to query
1874
- // rnd - float: Random value (0-1) used for brick loading request selection
1875
- // query - bool: Whether to allow brick loading requests (true) or just query (false)
1876
- // colorIndex - int
1877
- //
1878
- // Returns:
1879
- // ivec4: (x_offset, y_offset, z_offset, status) where:
1880
- // - x_offset, y_offset, z_offset: Brick cache coordinates if found
1881
- // - status: Resolution level (>=0) if found, or status code:
1882
- // * -1: Not found at any resolution level
1883
- // * -2: Empty brick (all values below threshold)
1884
- // * -3: Constant full brick (all values above threshold)
1885
- // * -4: Constant value brick (uniform value)
1886
- // add maxres here
1887
- ivec4 getBrickLocation(vec3 location, int targetRes, int channel, float rnd, bool query, int colorIndex) {
1888
-
1889
- // min max for current color
1890
- vec2 clim = getClim(colorIndex);
1891
-
1892
- // resolution ranges, TODO: connect this back to color
1893
- int channelMin = getRes(channel).x;
1894
- int channelMax = getRes(channel).y;
1895
-
1896
- // Clamp resolution to channel's available range
1897
- int currentRes = clamp(targetRes, channelMin, channelMax);
1898
- currentRes = clamp(currentRes, resGlobal.x, resGlobal.y);
1899
- int lowestRes = clamp(resGlobal.y, channelMin, channelMax);
1900
-
1901
- // Determine if this channel should request brick loading.
1902
- // request the current channel based on probability
1903
- bool requestChannel = false;
1904
- if (int(floor(rnd * float(maxChannels))) == colorIndex) {
1905
- requestChannel = true;
1906
- }
1907
-
1908
- // Try progressively lower resolutions until we find data.
1909
- // loop through resolutions
1910
- while (currentRes <= lowestRes) {
1911
-
1912
- // Calculate page table coordinates for this brick
1913
- uvec3 anchorPoint = getAnchorPoint(currentRes);
1914
- vec3 brickLocation = getBrickFromNormalized(location, currentRes);
1915
- uvec3 channelOffset = getChannelOffset(channel);
1916
- vec3 coordinate = floor(vec3(anchorPoint * channelOffset)) + brickLocation;
1917
-
1918
- // Special handling for resolution 0 (highest detail)
1919
- if (currentRes == 0) {
1920
- int zExtent = int(ceil(float(voxelExtents.z) / 32.0));
1921
- coordinate = vec3(anchorPoint) + vec3(0.0, 0.0, zExtent * channel) + brickLocation;
1922
- }
1923
-
1924
- // Query the page table.
1925
- // get PT entry
1926
- uint ptEntry = texelFetch(pageTableTex, ivec3(coordinate), 0).r;
1927
-
1928
- // Check if brick is initialized.
1929
- // check if the PT entry is initialized
1930
- uint isInit = (ptEntry >> 30u) & 1u;
1931
- if (isInit == 0u) {
1932
- currentRes++;
1933
- // Request brick loading if needed
1934
- if (requestChannel == true && (gRequest.a + gRequest.b + gRequest.g + gRequest.r == 0.0) && query == true) {
1935
- gRequest = packPTCoordToRGBA8(uvec3(coordinate));
1936
- }
1937
- continue;
1938
- }
1939
-
1940
- // Extract min/max values from page table entry.
1941
- // get the min max values of the brick
1942
- uint umin = ((ptEntry >> 23u) & 0x7Fu);
1943
- uint umax = ((ptEntry >> 16u) & 0x7Fu);
1944
- float min = float(int(umin)) / 127.0;
1945
- float max = float(int(umax)) / 127.0;
1946
-
1947
- // Check if brick is empty (all values below threshold).
1948
- // exit early if brick is constant
1949
- if (float(max) <= clim.x) {
1950
- return ivec4(0,0,0,-2);
1951
- // EMPTY
1952
- } else if (float(min) >= clim.y) {
1953
- return ivec4(0,0,0,-3); // CONSTANT FULL
1954
- } else if ((umax - umin) < 2u) {
1955
- return ivec4(min,0,0,-4); // CONSTANT OTHER VALUE
1956
- }
1957
-
1958
- // Check if brick is resident in cache.
1959
- // return brick cache location if resident
1960
- // continue to next resolution if not resident
1961
- uint isResident = (ptEntry >> 31u) & 1u;
1962
- if (isResident == 0u) {
1963
- currentRes++;
1964
- // Request brick loading if needed
1965
- if (requestChannel == true && (gRequest.a + gRequest.b + gRequest.g + gRequest.r == 0.0) && query == true) {
1966
- gRequest = packPTCoordToRGBA8(uvec3(coordinate));
1967
- }
1968
- continue;
1969
- } else {
1970
- // Extract brick cache coordinates
1971
- uint xBrickCache = (ptEntry >> 10u) & 0x3Fu;
1972
- uint yBrickCache = (ptEntry >> 4u) & 0x3Fu;
1973
- uint zBrickCache = ptEntry & 0xFu;
1974
- uvec3 brickCacheCoord = uvec3(xBrickCache, yBrickCache, zBrickCache);
1975
-
1976
- return ivec4(brickCacheCoord, currentRes);
1977
- }
1978
- }
1979
-
1980
- // not resident in any resolution, should be treated as empty
1981
- return ivec4(0,0,0,-1); // Not found
1982
- }
1983
-
1984
- // Request brick loading for a specific location and resolution.
1985
- // Initiates a request to load a brick from disk into the brick cache.
1986
- // set the brick request for the specified slot channel
1987
- //
1988
- // Parameters:
1989
- // location - vec3: Normalized world space coordinates (0.0 to 1.0) where the brick is needed
1990
- // targetRes - int: Target resolution level (0-9, where 0 is highest resolution)
1991
- // channel - int: Channel index (0-6) for multi-channel datasets
1992
- // rnd - float: Random value between 0.0 and 1.0 used for load balancing across channels
1993
- //
1994
- // Returns:
1995
- // void: No return value, but sets gRequest output variable if conditions are met
1996
- void setBrickRequest(vec3 location, int targetRes, int channel, float rnd) {
1997
- uvec3 anchorPoint = getAnchorPoint(targetRes);
1998
- vec3 brickLocation = getBrickFromNormalized(location, targetRes);
1999
- uvec3 channelOffset = getChannelOffset(channel);
2000
- vec3 coordinate = floor(vec3(anchorPoint * channelOffset)) + brickLocation;
2001
-
2002
- // Special handling for resolution 0
2003
- if (targetRes == 0) {
2004
- int zExtent = int(ceil(float(voxelExtents.z) / 32.0));
2005
- coordinate = vec3(anchorPoint) + vec3(0.0, 0.0, zExtent * channel) + brickLocation;
2006
- }
2007
-
2008
- // Pack coordinates and set request
2009
- if (int(floor(rnd * float(maxChannels))) == channel) {
2010
- gRequest = packPTCoordToRGBA8(uvec3(coordinate));
2011
- }
2012
- }
2013
-
2014
- // Track brick usage for cache management.
2015
- // Records which brick in the cache is being accessed for LRU (Least Recently Used) eviction.
2016
- // set the usage for the specified brick
2017
- //
2018
- // Parameters:
2019
- // brickCacheOffset - ivec3: 3D coordinates of the brick within the brick cache texture
2020
- // t_hit_min_os - float: Ray entry time in object space (start of ray-volume intersection)
2021
- // t_hit_max_os - float: Ray exit time in object space (end of ray-volume intersection)
2022
- // t_os - float: Current sampling position along the ray in object space
2023
- // rnd - float: Random value between 0.0 and 1.0 used for probabilistic usage tracking
2024
- //
2025
- // Returns:
2026
- // void: No return value, but sets gUsage output variable to track cache access patterns
2027
- void setUsage(ivec3 brickCacheOffset, float t_hit_min_os, float t_hit_max_os, float t_os, float rnd) {
2028
- float normalized_t_os = (t_os - t_hit_min_os) / (t_hit_max_os - t_hit_min_os); // Normalize to 0-1
2029
- if (normalized_t_os <= rnd || gUsage == vec4(0.0, 0.0, 0.0, 0.0)) {
2030
- gUsage = vec4(vec3(brickCacheOffset) / 255.0, 1.0);
2031
- }
2032
- }
2033
-
2034
- // ========================================
2035
- // UTILITY FUNCTIONS
2036
- // ========================================
2037
-
2038
- // Get maximum component of a 3D vector.
2039
- // get the max value of a vec3
2040
- //
2041
- // Parameters:
2042
- // v - vec3: Input 3D vector
2043
- //
2044
- // Returns:
2045
- // float: The maximum value among the x, y, and z components
2046
- float vec3_max(vec3 v) {
2047
- return max(v.x, max(v.y, v.z));
2048
- }
2049
-
2050
- // Get minimum component of a 3D vector.
2051
- // get the min value of a vec3
2052
- //
2053
- // Parameters:
2054
- // v - vec3: Input 3D vector
2055
- //
2056
- // Returns:
2057
- // float: The minimum value among the x, y, and z components
2058
- float vec3_min(vec3 v) {
2059
- return min(v.x, min(v.y, v.z));
2060
- }
2061
-
2062
- // Calculate level-of-detail based on distance.
2063
- // Uses logarithmic scaling to determine appropriate resolution level.
2064
- // get the LOD based on the distance to the camera
2065
- //
2066
- // Parameters:
2067
- // distance - float: Distance from camera to sampling point
2068
- // highestRes - int: Highest available resolution level (typically 0)
2069
- // lowestRes - int: Lowest available resolution level (typically 9)
2070
- // lodFactor - float: Scaling factor that controls LOD sensitivity
2071
- //
2072
- // Returns:
2073
- // int: Resolution level index (0-9) where 0 is highest resolution
2074
- int getLOD(float distance, int highestRes, int lowestRes, float lodFactor) {
2075
- int lod = int(log2(distance * lodFactor));
2076
- return clamp(lod, highestRes, lowestRes);
2077
- }
2078
-
2079
- // Calculate step size for ray marching at a given resolution.
2080
- // Determines optimal sampling step size based on voxel dimensions and ray direction.
2081
- // get the voxel step in object space
2082
- //
2083
- // Parameters:
2084
- // res - int: Resolution level index (0-9, where 0 is highest resolution)
2085
- // osDir - vec3: Ray direction vector in object space (should be normalized)
2086
- //
2087
- // Returns:
2088
- // float: Optimal step size in object space units for stable ray marching
2089
- float voxelStepOS(int res, vec3 osDir) {
2090
- vec3 voxelSize = getScale(res) / vec3(voxelExtents);
2091
- vec3 dt_vec = voxelSize / abs(osDir);
2092
- return min(dt_vec.x, min(dt_vec.y, dt_vec.z));
2093
- }
2094
-
2095
- // ========================================
2096
- // INTERPOLATION FUNCTIONS
2097
- // ========================================
2098
-
2099
- // Linear interpolation between two values.
2100
- // Performs smooth interpolation between two scalar values.
2101
- //
2102
- // Parameters:
2103
- // v0 - float: First value to interpolate from
2104
- // v1 - float: Second value to interpolate to
2105
- // fx - float: Interpolation factor between 0.0 and 1.0
2106
- //
2107
- // Returns:
2108
- // float: Interpolated value between v0 and v1 based on fx
2109
- float lerp(float v0, float v1, float fx) {
2110
- return mix(v0, v1, fx); // (1-fx)·v0 + fx·v1
2111
- }
2112
-
2113
- // Bilinear interpolation between four values
2114
- // Performs 2D interpolation using four corner values arranged in a square
2115
- //
2116
- // Parameters:
2117
- // v00 - float: Bottom-left corner value
2118
- // v10 - float: Bottom-right corner value
2119
- // v01 - float: Top-left corner value
2120
- // v11 - float: Top-right corner value
2121
- // f - vec2: 2D interpolation factors (x, y) between 0.0 and 1.0
2122
- //
2123
- // Returns:
2124
- // float: Interpolated value from the four corner values
2125
- float bilerp(float v00, float v10, float v01, float v11, vec2 f) {
2126
- float c0 = mix(v00, v10, f.x); // Interpolate in X on bottom row
2127
- float c1 = mix(v01, v11, f.x); // Interpolate in X on top row
2128
- return mix(c0, c1, f.y); // Now interpolate those in Y
2129
- }
2130
-
2131
- // Trilinear interpolation between eight values
2132
- // Performs 3D interpolation using eight corner values arranged in a cube
2133
- //
2134
- // Parameters:
2135
- // v000 - float: Bottom-left-back corner value
2136
- // v100 - float: Bottom-right-back corner value
2137
- // v010 - float: Bottom-left-front corner value
2138
- // v110 - float: Bottom-right-front corner value
2139
- // v001 - float: Top-left-back corner value
2140
- // v101 - float: Top-right-back corner value
2141
- // v011 - float: Top-left-front corner value
2142
- // v111 - float: Top-right-front corner value
2143
- // f - vec3: 3D interpolation factors (x, y, z) between 0.0 and 1.0
2144
- //
2145
- // Returns:
2146
- // float: Interpolated value from the eight corner values
2147
- float trilerp(
2148
- float v000, float v100, float v010, float v110,
2149
- float v001, float v101, float v011, float v111,
2150
- vec3 f) { // f = fract(coord)
2151
- // Interpolate along X for each of the four bottom-face voxels
2152
- float c00 = mix(v000, v100, f.x);
2153
- float c10 = mix(v010, v110, f.x);
2154
- float c01 = mix(v001, v101, f.x);
2155
- float c11 = mix(v011, v111, f.x);
2156
-
2157
- // Interpolate those along Y
2158
- float c0 = mix(c00, c10, f.y);
2159
- float c1 = mix(c01, c11, f.y);
2160
-
2161
- // Final interpolation along Z
2162
- return mix(c0, c1, f.z);
2163
- }
2164
-
2165
- // ========================================
2166
- // BRICK CACHE SAMPLING
2167
- // ========================================
2168
-
2169
- // Sample a value from the brick cache texture.
2170
- // Converts brick coordinates and voxel position to texture coordinates for sampling.
2171
- // sample the brick cache based on the brick cache coordinate and the in-brick coordinate
2172
- //
2173
- // Parameters:
2174
- // brickCacheCoord - vec3: 3D coordinates of the brick within the brick cache
2175
- // voxelInBrick - vec3: 3D coordinates of the voxel within the brick (0-31 in each dimension)
2176
- //
2177
- // Returns:
2178
- // float: Sampled voxel value from the brick cache texture (typically normalized 0.0-1.0)
2179
- float sampleBrick(vec3 brickCacheCoord, vec3 voxelInBrick) {
2180
- vec3 brickCacheCoordNormalized = vec3(
2181
- (float(brickCacheCoord.x) * BRICK_SIZE + float(voxelInBrick.x)) / BRICK_CACHE_SIZE_X,
2182
- (float(brickCacheCoord.y) * BRICK_SIZE + float(voxelInBrick.y)) / BRICK_CACHE_SIZE_Y,
2183
- (float(brickCacheCoord.z) * BRICK_SIZE + float(voxelInBrick.z)) / BRICK_CACHE_SIZE_Z
2184
- );
2185
- return texture(brickCacheTex, brickCacheCoordNormalized).r;
2186
- }
2187
-
2188
- /**
2189
- * main renderloop
2190
- */
2191
- void main(void) {
2192
-
2193
- // ========================================
2194
- // INITIALIZATION
2195
- // ========================================
2196
-
2197
- // Initialize all render targets (multiple output textures)
2198
- gRequest = vec4(0,0,0,0); // Brick loading requests
2199
- gUsage = vec4(0,0,0,0); // Brick usage tracking
2200
- gColor = vec4(0.0, 0.0, 0.0, 0.0); // Final color output
2201
-
2202
- // out color sums up our accumulated value before writing it into the gColor buffer
2203
- vec4 outColor = vec4(0.0, 0.0, 0.0, 0.0); // Accumulated color
2204
-
2205
- // Generate random number for jittered sampling (reduces artifacts)
2206
- float rnd = random();
2207
-
2208
- // Get the lowest available resolution level
2209
- int lowestDataRes = getLowestRes();
2210
-
2211
- // ========================================
2212
- // RAY-VOLUME INTERSECTION
2213
- // ========================================
2214
-
2215
- // Normalize the view ray direction
2216
- vec3 ws_rayDir = normalize(rayDirUnnorm);
2217
-
2218
- // Calculate intersection with volume bounding box
2219
- // Returns (entry_time, exit_time) for the ray-box intersection
2220
- vec2 t_hit = intersect_hit(cameraCorrected, ws_rayDir);
2221
- if (t_hit.x >= t_hit.y) { discard; } // Ray misses volume entirely
2222
-
2223
- t_hit.x = max(t_hit.x, 0.0); // Clamp entry to 0 (no negative distances)
2224
- float t = t_hit.x;
2225
-
2226
- // Calculate distance from camera for LOD selection
2227
- float distance = abs((cameraCorrected / boxSize).z + (ws_rayDir / boxSize).z * t );
2228
-
2229
- // ========================================
2230
- // COORDINATE SPACE CONVERSION
2231
- // ========================================
2232
-
2233
- // Convert from world space to object space (normalized 0-1 coordinates)
2234
- float ws2os = length(ws_rayDir / boxSize); // Scale factor for conversion
2235
- float t_hit_min_os = t_hit.x * ws2os; // Entry point in object space
2236
- float t_hit_max_os = t_hit.y * ws2os; // Exit point in object space
2237
- float t_os = t_hit_min_os; // Current position in object space
2238
-
2239
- // Calculate effective LOD factor based on volume size.
2240
- // voxel edge is the max extent of the volume
2241
- float voxelEdge = float(max(voxelExtents.x, max(voxelExtents.y, voxelExtents.z)));
2242
-
2243
- // calculate LOD factor based on the voxel edge
2244
- float lodFactorEffective = lodFactor * voxelEdge / 256.0;
2245
-
2246
- // ========================================
2247
- // RESOLUTION AND SAMPLING SETUP
2248
- // ========================================
2249
-
2250
- // Determine target resolution based on distance (LOD)
2251
- int targetRes = getLOD(t, 0, 9, lodFactorEffective);
2252
-
2253
- // Set adaptive stepping resolution
2254
- int stepResAdaptive = renderRes;
2255
- int stepResEffective = clamp(stepResAdaptive, 0, lowestDataRes);
2256
-
2257
- // Convert ray to object space coordinates
2258
- vec3 os_rayDir = normalize(ws_rayDir / boxSize);
2259
- vec3 os_rayOrigin = cameraCorrected / boxSize + vec3(0.5);
2260
-
2261
- // Calculate step size based on current resolution
2262
- float dt = voxelStepOS(stepResEffective, os_rayDir);
2263
-
2264
- // ========================================
2265
- // SAMPLING POSITION INITIALIZATION
2266
- // ========================================
2267
-
2268
- // Convert to normalized sampling coordinates (0-1 range)
2269
- vec3 p = cameraCorrected + t_hit.x * ws_rayDir;
2270
- p = p / boxSize + vec3(0.5); // Transform to 0-1 range
2271
-
2272
- // Calculate step vector in normalized space
2273
- vec3 dp = (os_rayDir * dt);
2274
-
2275
- // Apply jittered sampling to reduce artifacts
2276
- p += dp * (rnd);
2277
- // Avoid boundary issues
2278
- p = clamp(p, 0.0 + 0.0000028, 1.0 - 0.0000028);
2279
-
2280
- // ========================================
2281
- // RENDERING VARIABLES
2282
- // ========================================
2283
-
2284
- // Color accumulation for front-to-back compositing.
2285
- // color accumulation variables, are calculated per 'slice'
2286
- vec3 rgbCombo = vec3(0.0);
2287
- float total = 0.0;
2288
-
2289
- // For alpha blending.
2290
- // alpha accumulation variable runs globally
2291
- float alphaMultiplicator = 1.0;
2292
-
2293
- // Request tracking (for brick loading).
2294
- // if we have a request for a brick which not visible in lower
2295
- // resolutions, we can overwrite it once
2296
- bool overWrittenRequest = false;
2297
-
2298
- // Current state tracking
2299
- vec3 currentTargetResPTCoord = vec3(0,0,0);
2300
- int currentLOD = targetRes;
2301
-
2302
- // ========================================
2303
- // CHANNEL-SPECIFIC CONSTANTS
2304
- // ========================================
2305
-
2306
- // Pre-compute channel properties for efficiency.
2307
- // constants per color channel
2308
- vec3 [] c_color = vec3[7](getChannelColor(0), getChannelColor(1), getChannelColor(2), getChannelColor(3), getChannelColor(4), getChannelColor(5), getChannelColor(6));
2309
- float [] c_opacity = float[7](getChannelOpacity(0), getChannelOpacity(1), getChannelOpacity(2), getChannelOpacity(3), getChannelOpacity(4), getChannelOpacity(5), getChannelOpacity(6));
2310
- // resolution ranges (currently) per color channel
2311
- // TODO: figure out how to hook it up with frontend
2312
- int [] c_res_min = int[7](getRes(0).x, getRes(1).x, getRes(2).x, getRes(3).x, getRes(4).x, getRes(5).x, getRes(6).x);
2313
- int [] c_res_max = int[7](getRes(0).y, getRes(1).y, getRes(2).y, getRes(3).y, getRes(4).y, getRes(5).y, getRes(6).y);
2314
- vec3 [] res_color = vec3[10](
2315
- vec3(1.0, 0.0, 0.0),
2316
- vec3(0.0, 0.0, 1.0),
2317
- vec3(0.0, 1.0, 0.0),
2318
- vec3(1.0, 0.0, 1.0),
2319
- vec3(0.0, 1.0, 1.0),
2320
- vec3(1.0, 1.0, 0.0),
2321
- vec3(1.0, 0.5, 0.5),
2322
- vec3(0.5, 0.5, 1.0),
2323
- vec3(0.5, 1.0, 0.5),
2324
- vec3(0.5, 0.5, 0.5)
2325
- );
2326
-
2327
- // ========================================
2328
- // PER-CHANNEL STATE ARRAYS
2329
- // ========================================
2330
-
2331
- // Current state for each channel.
2332
- // current state variables per color channel
2333
-
2334
- // current resolution
2335
- int [] c_res_current = int[7](0,0,0,0,0,0,0);
2336
- // current value
2337
- float [] c_val_current = float[7](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
2338
- // current brick cache coordinate
2339
- vec3 [] c_brickCacheCoord_current = vec3[7](vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0));
2340
- // current voxel in current resolution
2341
- vec3 [] c_voxel_current = vec3[7](vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0));
2342
- // current pagetable coordinate
2343
- vec3 [] c_ptCoord_current = vec3[7](vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0));
2344
- // current render mode -- 0: empty (add 0), 1: constant (add current val), 2: voxel (query new voxel)
2345
- // upon change of PT we re-query anyways
2346
- int [] c_renderMode_current = int[7](-1, -1, -1, -1, -1, -1, -1);
2347
-
2348
- // Adjacent brick caching for interpolation
2349
- // current pagetable coordinate of the adjacent bricks in X, Y, Z or diagonal direction
2350
- vec3 [] c_PT_X_adjacent = vec3[7](vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0));
2351
- vec3 [] c_PT_Y_adjacent = vec3[7](vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0));
2352
- vec3 [] c_PT_Z_adjacent = vec3[7](vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0));
2353
- vec3 [] c_PT_XYZ_adjacent = vec3[7](vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0));
2354
- // corresponding brick coordinates of the adjacent bricks in X, Y, Z or diagonal direction
2355
- vec4 [] c_brick_X_adjacent = vec4[7](vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0));
2356
- vec4 [] c_brick_Y_adjacent = vec4[7](vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0));
2357
- vec4 [] c_brick_Z_adjacent = vec4[7](vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0));
2358
- vec4 [] c_brick_XYZ_adjacent = vec4[7](vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0), vec4(-1.0));
2359
-
2360
- // Min/max tracking for MIP/MinIP rendering.
2361
- // min and max values of the current color
2362
- // used for minimum/maximum intensity projection
2363
- float [] c_minVal = float[7](-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0);
2364
- float [] c_maxVal = float[7](0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
2365
-
2366
- // Per-resolution coordinate tracking
2367
- vec3 [] r_ptCoord = vec3[10](vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0));
2368
- vec3 [] r_voxel = vec3[10](vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0));
2369
- vec3 [] r_prevPTCoord = vec3[10](vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0));
2370
- vec3 [] r_prevVoxel = vec3[10](vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0), vec3(-1.0));
2371
-
2372
- // resolution changed flag
2373
- bool resolutionChanged = false;
2374
- // number of repetitions (for debugging purposes)
2375
- int reps = 0;
2376
-
2377
- // ========================================
2378
- // MAIN RAY-MARCHING LOOP
2379
- // ========================================
2380
-
2381
- // while we are 'in' the volume.
2382
- // Continue marching until we exit the volume or reach maximum opacity.
2383
- while (t_os < t_hit_max_os && t_os >= t_hit_min_os
2384
- && vec3_max(p) < 1.0 && vec3_min(p) >= 0.0
2385
- ) {
2386
-
2387
- // Reset per-sample accumulation.
2388
- // initialize slice values
2389
- vec3 rgbCombo = vec3(0.0);
2390
- float total = 0.0;
2391
-
2392
- // Update target resolution based on current distance (with jitter).
2393
- // calculate target resolution based on distance and lod factor
2394
- targetRes = getLOD(t, 0, 9, lodFactorEffective * (0.999 + 0.002 * rnd));
2395
-
2396
- // ========================================
2397
- // RESOLUTION CHANGE HANDLING
2398
- // ========================================
2399
-
2400
- // if target resolution changed, update the current resolution and stepsize
2401
- if (targetRes != currentLOD) {
2402
- currentLOD = targetRes;
2403
- stepResAdaptive++;
2404
- stepResEffective = clamp(stepResAdaptive, 0, lowestDataRes);
2405
-
2406
- // Adjust sampling position for new resolution
2407
- p -= dp * rnd;
2408
- dt = voxelStepOS(stepResEffective, os_rayDir);
2409
- dp = os_rayDir * dt;
2410
- p += dp * rnd;
2411
- resolutionChanged = true;
2412
-
2413
- // Check bounds after resolution change
2414
- if (p.x < 0.0 || p.x >= 1.0 || p.y < 0.0 || p.y >= 1.0 || p.z < 0.0 || p.z >= 1.0) {
2415
- break;
2416
- }
2417
- } else {
2418
- resolutionChanged = false;
2419
- }
2420
-
2421
- // ========================================
2422
- // UPDATE COORDINATES FOR ALL RESOLUTIONS
2423
- // ========================================
2424
-
2425
- // Calculate page table coordinates and voxel positions for all resolution levels
2426
- for (int r = 0; r < 10; r++) {
2427
- r_prevPTCoord[r] = r_ptCoord[r];
2428
- r_prevVoxel[r] = r_voxel[r];
2429
- r_ptCoord[r] = getBrickFromNormalized(p, r);
2430
- r_voxel[r] = getVoxelFromNormalized(p, r);
2431
- }
2432
-
2433
- // ========================================
2434
- // RENDER MODE DEFINITIONS
2435
- // ========================================
2436
- // 0: empty brick (no data)
2437
- // 1: constant brick (uniform value)
2438
- // 2: voxel brick (variable data)
2439
-
2440
- // initialize the per channel slice values
2441
- vec3 sliceColor = vec3(0.0);
2442
- float sliceAlpha = 0.0;
2443
-
2444
- // ========================================
2445
- // MULTI-CHANNEL SAMPLING
2446
- // ========================================
2447
-
2448
- // Process each channel independently.
2449
- // iterate over up to 7 channels by color
2450
- for (int c = 0; c < 7; c++) {
2451
- // Skip channels with zero opacity.
2452
- // skip if opacity is 0 or if color is not mapped to a physical slot
2453
- if (c_opacity[c] <= 0.000001) {
2454
- continue;
2455
- } else if (channelMapping[c] == -1) {
2456
- continue;
2457
- }
2458
-
2459
- // physical slot in pagetable
2460
- int slot = channelMapping[c];
2461
-
2462
- // keep track of status
2463
- bool newBrick = false;
2464
- bool newVoxel = false;
2465
- // best possible resolution
2466
- int bestRes = clamp(targetRes, c_res_min[c], c_res_max[c]);
2467
-
2468
- // Check if we need to load a new brick at a better resolution.
2469
- // check if any new better resolution could be available, if so, we need to re-query the brick
2470
- bool betterResChanged = false;
2471
- for (int r = bestRes; r <= c_res_current[c]; r++ ) {
2472
- if (r_ptCoord[r] != r_prevPTCoord[r]) {
2473
- betterResChanged = true;
2474
- break;
2475
- }
2476
- }
2477
-
2478
- // Determine if we need to load new brick data.
2479
- // check if we need to re-query the brick / voxel or reuse past 'val'
2480
- if (r_ptCoord[bestRes] != r_prevPTCoord[bestRes]
2481
- || c_renderMode_current[c] == -1
2482
- || resolutionChanged == true
2483
- || betterResChanged
2484
- ) {
2485
- newBrick = true;
2486
- newVoxel = true;
2487
- } else if (c_renderMode_current[c] == 2) {
2488
- newVoxel = true;
2489
- } else if (c_renderMode_current[c] == 0) {
2490
- continue; // Skip empty bricks
2491
- }
2492
-
2493
- // ========================================
2494
- // BRICK LOADING AND CACHING
2495
- // ========================================
2496
-
2497
- // check if a new brick is available in the best possible resolution
2498
- if (newBrick) {
2499
- // Query page table for brick location and status
2500
- ivec4 brickCacheInfo = getBrickLocation(p, bestRes, slot, rnd, true, c);
2501
- // check information about the newly queried brick
2502
- // if the res is not at best res, possibly the same as previous brick
2503
- if (brickCacheInfo.w == -1 || brickCacheInfo.w == -2) {
2504
- // Empty brick - no data available.
2505
- // we can skip the rest of the loop
2506
- c_val_current[c] = 0.0;
2507
- c_renderMode_current[c] = 0;
2508
- c_minVal[c] = 0.0;
2509
- continue;
2510
- } else if (brickCacheInfo.w == -3) {
2511
- // Solid brick - constant maximum value.
2512
- // we set the value and do not need to query a voxel
2513
- c_val_current[c] = 1.0;
2514
- c_renderMode_current[c] = 1;
2515
- c_maxVal[c] = 1.0;
2516
- newVoxel = false;
2517
- } else if (brickCacheInfo.w == -4) {
2518
- // Constant brick - uniform value.
2519
- // static value -- we set the value and do not need to query a voxel
2520
- float val = float(brickCacheInfo.x);
2521
- c_val_current[c] = max(0.0, (val - getClim(c).x) / (getClim(c).y - getClim(c).x));
2522
- c_renderMode_current[c] = 1;
2523
- newVoxel = false;
2524
- } else if (brickCacheInfo.w >= 0) {
2525
- // Voxel brick - variable data, load from cache.
2526
- // new brick -- we set the coordinate and resolution and need to query a voxel
2527
- c_res_current[c] = brickCacheInfo.w;
2528
- c_ptCoord_current[c] = r_ptCoord[c_res_current[c]];
2529
- c_brickCacheCoord_current[c] = vec3(brickCacheInfo.xyz);
2530
- c_renderMode_current[c] = 2;
2531
- newVoxel = true;
2532
-
2533
- // Track brick usage for cache management.
2534
- // we set the usage of the brick based on the channel and the relative distance into the cube
2535
- if (int(floor(rnd * float(maxChannels))) == c) {
2536
- setUsage(brickCacheInfo.xyz, t_hit_min_os, t_hit_max_os, t_os, rnd);
2537
- }
2538
- }
2539
- }
2540
-
2541
- // ========================================
2542
- // VOXEL SAMPLING WITH INTERPOLATION
2543
- // ========================================
2544
-
2545
- // we need to query a new voxel e.g. sample the brick cache
2546
- if (newVoxel) {
2547
- c_voxel_current[c] = r_voxel[c_res_current[c]];
2548
-
2549
- // we clamp the coordinate to be inside the brick and sample the volume
2550
- reps++;
2551
-
2552
- // Calculate position within the brick (0-31 range)
2553
- vec3 voxelInBrick = mod(c_voxel_current[c], 32.0);
2554
- vec3 clampedVoxelInBrick = clamp(voxelInBrick, 0.5, 31.5);
2555
- // Sample the brick cache texture
2556
- float val = sampleBrick(c_brickCacheCoord_current[c].xyz, clampedVoxelInBrick);
2557
-
2558
- // ========================================
2559
- // HIGH-QUALITY INTERPOLATION (renderRes == 0)
2560
- // ========================================
2561
-
2562
- // interblock interpolation
2563
- if (renderRes == 0) {
2564
- // calculate what axis we need to interpolate
2565
-
2566
- // Check if we're near brick boundaries (need interpolation)
2567
- bvec3 clampedMin = lessThan(voxelInBrick, clampedVoxelInBrick);
2568
- bvec3 clampedMax = greaterThan(voxelInBrick, clampedVoxelInBrick);
2569
- bvec3 clamped = bvec3(clampedMin.x || clampedMax.x, clampedMin.y || clampedMax.y, clampedMin.z || clampedMax.z);
2570
- vec3 diff = voxelInBrick - clampedVoxelInBrick;
2571
-
2572
- if (any(clampedMin) || any(clampedMax)) {
2573
- int boundaryAxes = int(clamped.x) + int(clamped.y) + int(clamped.z);
2574
- float f = 0.0;
2575
-
2576
- if (boundaryAxes == 1) {
2577
- // Linear interpolation across one boundary
2578
- vec3 otherGlobalVoxelPos = vec3(0,0,0);
2579
- vec3 otherP = vec3(0,0,0);
2580
- float otherVoxelVal = 0.0;
2581
-
2582
- // Determine which axis we're interpolating across
2583
- if (clampedMin.x) {
2584
- otherGlobalVoxelPos = c_voxel_current[c] - vec3(1.0, 0.0, 0.0);
2585
- otherP = getNormalizedFromVoxel(otherGlobalVoxelPos, c_res_current[c]);
2586
- f = abs(diff.x);
2587
- } else if (clampedMax.x) {
2588
- otherGlobalVoxelPos = c_voxel_current[c] + vec3(1.0, 0.0, 0.0);
2589
- otherP = getNormalizedFromVoxel(otherGlobalVoxelPos, c_res_current[c]);
2590
- f = abs(diff.x);
2591
- } else if (clampedMin.y) {
2592
- otherGlobalVoxelPos = c_voxel_current[c] - vec3(0.0, 1.0, 0.0);
2593
- otherP = getNormalizedFromVoxel(otherGlobalVoxelPos, c_res_current[c]);
2594
- f = abs(diff.y);
2595
- } else if (clampedMax.y) {
2596
- otherGlobalVoxelPos = c_voxel_current[c] + vec3(0.0, 1.0, 0.0);
2597
- otherP = getNormalizedFromVoxel(otherGlobalVoxelPos, c_res_current[c]);
2598
- f = abs(diff.y);
2599
- } else if (clampedMin.z) {
2600
- otherGlobalVoxelPos = c_voxel_current[c] - vec3(0.0, 0.0, 1.0);
2601
- otherP = getNormalizedFromVoxel(otherGlobalVoxelPos, c_res_current[c]);
2602
- f = abs(diff.z);
2603
- } else if (clampedMax.z) {
2604
- otherGlobalVoxelPos = c_voxel_current[c] + vec3(0.0, 0.0, 1.0);
2605
- otherP = getNormalizedFromVoxel(otherGlobalVoxelPos, c_res_current[c]);
2606
- f = abs(diff.z);
2607
- }
2608
-
2609
- // Sample the neighboring voxel
2610
- vec3 otherPTcoord = getBrickFromNormalized(otherP, c_res_current[c]);
2611
- otherPTcoord = getBrickFromVoxel(otherGlobalVoxelPos, c_res_current[c]);
2612
- vec3 otherVoxelInBrick = mod(otherGlobalVoxelPos, 32.0);
2613
- otherVoxelInBrick -= diff;
2614
-
2615
- // Check if neighbor is outside volume bounds
2616
- if (otherP.x < 0.0 || otherP.x >= 1.0 || otherP.y < 0.0 || otherP.y >= 1.0 || otherP.z < 0.0 || otherP.z >= 1.0) {
2617
- otherVoxelVal = val;
2618
- } else if (otherPTcoord == c_PT_XYZ_adjacent[c].xyz && c_brick_XYZ_adjacent[c].w >= 0.0) {
2619
- // Use cached adjacent brick
2620
- otherVoxelVal = sampleBrick(c_brick_XYZ_adjacent[c].xyz, otherVoxelInBrick);
2621
- } else {
2622
- // Load new adjacent brick
2623
- ivec4 otherBrickCacheInfo = ivec4(-1);
2624
- if (otherPTcoord == c_PT_XYZ_adjacent[c].xyz) {
2625
- otherBrickCacheInfo = ivec4(c_brick_XYZ_adjacent[c]);
2626
- } else {
2627
- otherBrickCacheInfo = getBrickLocation(otherP, c_res_current[c], slot, rnd, false, c);
2628
- }
2629
- if (otherBrickCacheInfo.w == -1 || otherBrickCacheInfo.w == -2) {
2630
- otherVoxelVal = val;
2631
- } else if (otherBrickCacheInfo.w == -3) {
2632
- otherVoxelVal = 1.0;
2633
- } else if (otherBrickCacheInfo.w == -4) {
2634
- otherVoxelVal = float(otherBrickCacheInfo.x);
2635
- } else {
2636
- // TODO: we do not recalculate the voxelInBrick based on the resolution
2637
- otherVoxelVal = sampleBrick(vec3(otherBrickCacheInfo.xyz), otherVoxelInBrick);
2638
- }
2639
- c_PT_XYZ_adjacent[c] = getBrickFromVoxel(otherGlobalVoxelPos, c_res_current[c]);
2640
- c_brick_XYZ_adjacent[c] = vec4(otherBrickCacheInfo);
2641
- }
2642
-
2643
- // Perform linear interpolation
2644
- float originalVal = val;
2645
- val = lerp(originalVal, otherVoxelVal, f);
2646
- } else if (boundaryAxes == 2) {
2647
- // Bilinear interpolation across two boundaries
2648
- vec3 offA = vec3(0.0);
2649
- vec3 offB = vec3(0.0);
2650
- vec2 f = vec2(0.0);
2651
-
2652
- // Determine which two axes we're interpolating across
2653
- if (clamped.x && clamped.y) {
2654
- offA.x = clampedMin.x ? -1.0 : 1.0;
2655
- offB.y = clampedMin.y ? -1.0 : 1.0;
2656
- f = vec2(abs(diff.x), abs(diff.y));
2657
- } else if (clamped.x && clamped.z) {
2658
- offA.x = clampedMin.x ? -1.0 : 1.0;
2659
- offB.z = clampedMin.z ? -1.0 : 1.0;
2660
- f = vec2(abs(diff.x), abs(diff.z));
2661
- } else if (clamped.y && clamped.z) {
2662
- offA.y = clampedMin.y ? -1.0 : 1.0;
2663
- offB.z = clampedMin.z ? -1.0 : 1.0;
2664
- f = vec2(abs(diff.y), abs(diff.z));
2665
- }
2666
-
2667
- // Macro for sampling at offset positions
2668
- #define SAMPLE_AT_OFFSET(OFF, DEST) { vec3 otherGlobalVoxelPos = c_voxel_current[c] + (OFF); vec3 otherP = getNormalizedFromVoxel( otherGlobalVoxelPos, c_res_current[c]); if ( any(lessThan(otherP, vec3(0.0))) || any(greaterThanEqual(otherP, vec3(1.0))) ) { DEST = val; } else { vec3 otherPTcoord = getBrickFromNormalized( otherP, c_res_current[c]); vec3 otherVoxelInBrick = mod(otherGlobalVoxelPos, 32.0) - diff; bool matched = false; if (otherPTcoord == c_PT_X_adjacent[c].xyz && c_brick_X_adjacent[c].w >= 0.0) { DEST = sampleBrick(c_brick_X_adjacent[c].xyz, otherVoxelInBrick); matched = true; } else if (otherPTcoord == c_PT_Y_adjacent[c].xyz && c_brick_Y_adjacent[c].w >= 0.0) { DEST = sampleBrick(c_brick_Y_adjacent[c].xyz, otherVoxelInBrick); matched = true; } else if (otherPTcoord == c_PT_Z_adjacent[c].xyz && c_brick_Z_adjacent[c].w >= 0.0) { DEST = sampleBrick(c_brick_Z_adjacent[c].xyz, otherVoxelInBrick); matched = true; } else if (otherPTcoord == c_PT_XYZ_adjacent[c].xyz && c_brick_XYZ_adjacent[c].w >= 0.0) { DEST = sampleBrick(c_brick_XYZ_adjacent[c].xyz, otherVoxelInBrick); matched = true; } ivec4 info = ivec4(-1); if (otherPTcoord == c_PT_X_adjacent[c].xyz) { info = ivec4(c_brick_X_adjacent[c]); } else if (otherPTcoord == c_PT_Y_adjacent[c].xyz) { info = ivec4(c_brick_Y_adjacent[c]); } else if (otherPTcoord == c_PT_Z_adjacent[c].xyz) { info = ivec4(c_brick_Z_adjacent[c]); } else if (otherPTcoord == c_PT_XYZ_adjacent[c].xyz) { info = ivec4(c_brick_XYZ_adjacent[c]); } else { info = getBrickLocation(otherP, c_res_current[c], slot, rnd, false, c); } if (!matched) { if (info.w == -1 || info.w == -2) { DEST = val; } else if (info.w == -3) { DEST = 1.0; } else if (info.w == -4) { DEST = float(info.x); } else { DEST = sampleBrick(vec3(info.xyz), otherVoxelInBrick); } if (abs((OFF).x) > 0.5 && abs((OFF).y) < 0.5 && abs((OFF).z) < 0.5) { c_PT_X_adjacent[c] = otherPTcoord; c_brick_X_adjacent[c] = vec4(info); } else if (abs((OFF).y) > 0.5 && abs((OFF).x) < 0.5 && abs((OFF).z) < 0.5) { c_PT_Y_adjacent[c] = otherPTcoord; c_brick_Y_adjacent[c] = vec4(info); } else if (abs((OFF).z) > 0.5 && abs((OFF).x) < 0.5 && abs((OFF).y) < 0.5) { c_PT_Z_adjacent[c] = otherPTcoord; c_brick_Z_adjacent[c] = vec4(info); } else { c_PT_XYZ_adjacent[c] = otherPTcoord; c_brick_XYZ_adjacent[c] = vec4(info); } } } }
2669
-
2670
- // Sample the four corners for bilinear interpolation
2671
- float v00 = val;
2672
- float v10; float v01; float v11;
2673
- SAMPLE_AT_OFFSET(offA, v10);
2674
- SAMPLE_AT_OFFSET(offB, v01);
2675
- SAMPLE_AT_OFFSET(offA + offB, v11);
2676
-
2677
- val = bilerp(v00, v10, v01, v11, f);
2678
-
2679
- #undef SAMPLE_AT_OFFSET
2680
-
2681
- } else if (boundaryAxes == 3) {
2682
- // Trilinear interpolation across all three boundaries
2683
-
2684
- vec3 offA = vec3(0.0);
2685
- vec3 offB = vec3(0.0);
2686
- vec3 offC = vec3(0.0);
2687
- vec3 f = vec3(0.0);
2688
-
2689
- offA.x = clampedMin.x ? -1.0 : 1.0;
2690
- offB.y = clampedMin.y ? -1.0 : 1.0;
2691
- offC.z = clampedMin.z ? -1.0 : 1.0;
2692
-
2693
- f = vec3(abs(diff.x), abs(diff.y), abs(diff.z));
2694
-
2695
- // Macro for sampling at offset positions
2696
- #define SAMPLE_AT_OFFSET(OFF, DEST) { vec3 otherGlobalVoxelPos = c_voxel_current[c] + (OFF); vec3 otherP = getNormalizedFromVoxel(otherGlobalVoxelPos, c_res_current[c]); if (any(lessThan(otherP, vec3(0.0))) || any(greaterThanEqual(otherP, vec3(1.0)))) { DEST = val; } else { vec3 otherPTcoord = getBrickFromNormalized(otherP, c_res_current[c]); vec3 otherVoxelInBrick = mod(otherGlobalVoxelPos, 32.0) - diff; if (otherPTcoord == c_PT_X_adjacent[c] && c_brick_X_adjacent[c].w >= 0.0) { DEST = sampleBrick(c_brick_X_adjacent[c].xyz, otherVoxelInBrick); } else if (otherPTcoord == c_PT_Y_adjacent[c].xyz && c_brick_Y_adjacent[c].w >= 0.0) { DEST = sampleBrick(c_brick_Y_adjacent[c].xyz, otherVoxelInBrick); } else if (otherPTcoord == c_PT_Z_adjacent[c].xyz && c_brick_Z_adjacent[c].w >= 0.0) { DEST = sampleBrick(c_brick_Z_adjacent[c].xyz, otherVoxelInBrick); } else if (otherPTcoord == c_PT_XYZ_adjacent[c].xyz && c_brick_XYZ_adjacent[c].w >= 0.0) { DEST = sampleBrick(c_brick_XYZ_adjacent[c].xyz, otherVoxelInBrick); } else { ivec4 otherBrickCacheInfo = getBrickLocation(otherP, c_res_current[c], slot, rnd, false, c); vec3 otherVoxelInBrick = mod(otherGlobalVoxelPos, 32.0) - diff; if (otherBrickCacheInfo.w == -1 || otherBrickCacheInfo.w == -2) { DEST = val; } else if (otherBrickCacheInfo.w == -3) { DEST = 1.0; } else if (otherBrickCacheInfo.w == -4) { DEST = float(otherBrickCacheInfo.x); } else { DEST = sampleBrick(vec3(otherBrickCacheInfo.xyz), otherVoxelInBrick); } } } }
2697
-
2698
- // Sample all eight corners for trilinear interpolation
2699
- float v000 = val;
2700
- float v100; float v010; float v001; float v110; float v101; float v011; float v111;
2701
- SAMPLE_AT_OFFSET(offA, v100);
2702
- SAMPLE_AT_OFFSET(offB, v010);
2703
- SAMPLE_AT_OFFSET(offC, v001);
2704
- SAMPLE_AT_OFFSET(offA + offB, v110);
2705
- SAMPLE_AT_OFFSET(offA + offC, v101);
2706
- SAMPLE_AT_OFFSET(offB + offC, v011);
2707
- SAMPLE_AT_OFFSET(offA + offB + offC, v111);
2708
-
2709
- val = trilerp(v000, v100, v010, v001, v110, v101, v011, v111, f);
2710
-
2711
- #undef SAMPLE_AT_OFFSET
2712
-
2713
- }
2714
-
2715
- } else {
2716
- // No boundary interpolation needed - clear adjacent brick cache.
2717
- // no adjacent bricks -> reset the adjacent trackers
2718
- c_PT_X_adjacent[c] = c_PT_Y_adjacent[c] = c_PT_Z_adjacent[c] = c_PT_XYZ_adjacent[c] = vec3(-1.0);
2719
- c_brick_X_adjacent[c] = c_brick_Y_adjacent[c] = c_brick_Z_adjacent[c] = c_brick_XYZ_adjacent[c] = vec4(-1.0);
2720
- }
2721
- }
2722
-
2723
- // ========================================
2724
- // VALUE NORMALIZATION AND TRACKING
2725
- // ========================================
2726
-
2727
- // Normalize value to 0-1 range using channel-specific contrast limits.
2728
- // we normalize the (accumulated) value to the range of the color channel
2729
- c_val_current[c] = max(0.0, (val - getClim(c).x) / (getClim(c).y - getClim(c).x));
2730
-
2731
- // Track min/max values for MIP/MinIP rendering.
2732
- // update the min and max values for the min/max projection
2733
- if (c_minVal[c] == -1.0) {
2734
- c_minVal[c] = c_val_current[c];
2735
- } else {
2736
- c_minVal[c] = min(c_minVal[c], c_val_current[c]);
2737
- }
2738
- c_maxVal[c] = max(c_maxVal[c], c_val_current[c]);
2739
-
2740
- }
2741
-
2742
- // ========================================
2743
- // BRICK REQUEST GENERATION
2744
- // ========================================
2745
-
2746
- // Request higher resolution bricks if we're using lower resolution than optimal.
2747
- // potentially overwrite brick request
2748
- if (!overWrittenRequest
2749
- && c_res_current[c] != bestRes
2750
- && c_val_current[c] > 0.0
2751
- && c_renderMode_current[c] == 2
2752
- && int(floor(rnd * float(maxChannels))) == c) {
2753
- setBrickRequest(p, bestRes, slot, rnd);
2754
- overWrittenRequest = true;
2755
- }
2756
-
2757
- // ========================================
2758
- // CHANNEL COMPOSITING
2759
- // ========================================
2760
-
2761
- // Accumulate this channel's contribution.
2762
- // sum up the values onto the slice values
2763
- total += c_val_current[c];
2764
- if (u_renderstyle == 3) {
2765
- rgbCombo += c_val_current[c] * res_color[targetRes];
2766
- } else if (u_renderstyle == 4) {
2767
- rgbCombo += c_val_current[c] * res_color[c_res_current[c]];
2768
- } else {
2769
- rgbCombo += c_val_current[c] * c_color[c];
2770
- }
2771
-
2772
- }
2773
-
2774
- // ========================================
2775
- // FRONT-TO-BACK COMPOSITING
2776
- // ========================================
2777
-
2778
- // Clamp total intensity and calculate alpha.
2779
- // add the calculated slice to the total color
2780
- total = clamp(total, 0.0, 1.0);
2781
- sliceAlpha = total * opacity * dt * 32.0; // Scale by step size and brick size
2782
- sliceColor = rgbCombo;
2783
-
2784
- // Front-to-back alpha blending
2785
- outColor.rgb += sliceAlpha * alphaMultiplicator * sliceColor;
2786
- outColor.a += sliceAlpha * alphaMultiplicator;
2787
- alphaMultiplicator *= (1.0 - sliceAlpha);
2788
-
2789
- // Early termination for opaque regions (standard rendering only).
2790
- // check if we can exit early
2791
- if (outColor.a > 0.99 && u_renderstyle == 0) { break; }
2792
-
2793
- // ========================================
2794
- // ADVANCE RAY POSITION
2795
- // ========================================
2796
-
2797
- // Move to next sample position
2798
- t += dt;
2799
- p += dp;
2800
- t_os += dt;
2801
- }
2802
-
2803
- // ========================================
2804
- // RENDERING STYLE POST-PROCESSING
2805
- // ========================================
2806
-
2807
- if (u_renderstyle == 1) {
2808
- // Minimum Intensity Projection (MinIP)
2809
- // Shows the minimum value encountered along each ray
2810
- outColor = vec4(0.0);
2811
- for (int c = 0; c < 7; c++) {
2812
- if (c_color[c] != vec3(0.0, 0.0, 0.0)) {
2813
- outColor.rgb += c_minVal[c] * c_color[c];
2814
- outColor.a += c_minVal[c];
2815
- }
2816
- }
2817
- } else if (u_renderstyle == 0) {
2818
- // Maximum Intensity Projection (MIP)
2819
- // Shows the maximum value encountered along each ray
2820
- outColor = vec4(0.0);
2821
- for (int c = 0; c < 7; c++) {
2822
- if (c_color[c] != vec3(0.0, 0.0, 0.0)) {
2823
- outColor.rgb += c_maxVal[c] * c_color[c];
2824
- }
2825
- }
2826
- outColor.a = 1.0;
2827
- }
2828
-
2829
- // ========================================
2830
- // FINAL OUTPUT
2831
- // ========================================
2832
-
2833
- // Convert from linear to sRGB color space and set all render targets
2834
- gColor = vec4(linear_to_srgb(outColor.r),
2835
- linear_to_srgb(outColor.g),
2836
- linear_to_srgb(outColor.b),
2837
- outColor.a);
2838
-
2839
- }
2840
- `;
2841
- const VolumeShader = {
2842
- uniforms: {
2843
- u_size: { value: new Vector3(1, 1, 1) },
2844
- clim0: { value: new Vector2(0.2, 0.8) },
2845
- clim1: { value: new Vector2(0.2, 0.8) },
2846
- clim2: { value: new Vector2(0.2, 0.8) },
2847
- clim3: { value: new Vector2(0.2, 0.8) },
2848
- clim4: { value: new Vector2(0.2, 0.8) },
2849
- clim5: { value: new Vector2(0.2, 0.8) },
2850
- clim6: { value: new Vector2(0.2, 0.8) },
2851
- clim7: { value: new Vector2(0.2, 0.8) },
2852
- xClip: { value: new Vector2(0, 1e6) },
2853
- yClip: { value: new Vector2(0, 1e6) },
2854
- zClip: { value: new Vector2(0, 1e6) },
2855
- u_window_size: { value: new Vector2(1, 1) },
2856
- u_vol_scale: { value: new Vector3(1, 1, 1) },
2857
- // Set renderstyle to 3 or 4 for debugging modes
2858
- u_renderstyle: { value: 2 },
2859
- brickCacheTex: { type: "sampler3D", value: null },
2860
- pageTableTex: { type: "usampler3D", value: null },
2861
- color0: { value: new Vector4(0, 0, 0) },
2862
- color1: { value: new Vector4(0, 0, 0) },
2863
- color2: { value: new Vector4(0, 0, 0) },
2864
- color3: { value: new Vector4(0, 0, 0) },
2865
- color4: { value: new Vector4(0, 0, 0) },
2866
- color5: { value: new Vector4(0, 0, 0) },
2867
- color6: { value: new Vector4(0, 0, 0) },
2868
- channelMapping: {
2869
- value: [-1, -1, -1, -1, -1, -1, -1]
2870
- },
2871
- resGlobal: { value: new Vector2(0, 9) },
2872
- res0: { value: new Vector2(0, 9) },
2873
- res1: { value: new Vector2(0, 9) },
2874
- res2: { value: new Vector2(0, 9) },
2875
- res3: { value: new Vector2(0, 9) },
2876
- res4: { value: new Vector2(0, 9) },
2877
- res5: { value: new Vector2(0, 9) },
2878
- res6: { value: new Vector2(0, 9) },
2879
- maxChannels: { value: 0 },
2880
- lodFactor: { value: 1 },
2881
- near: { value: 0.1 },
2882
- far: { value: 1e4 },
2883
- opacity: { value: 1 },
2884
- volumeCount: { value: 0 },
2885
- boxSize: { value: new Vector3(1, 1, 1) },
2886
- renderRes: { value: 1e3 },
2887
- voxelExtents: { value: new Vector3(1, 1, 1) },
2888
- anchor0: { value: new Vector3(0, 0, 0) },
2889
- anchor1: { value: new Vector3(0, 0, 0) },
2890
- anchor2: { value: new Vector3(0, 0, 0) },
2891
- anchor3: { value: new Vector3(0, 0, 0) },
2892
- anchor4: { value: new Vector3(0, 0, 0) },
2893
- anchor5: { value: new Vector3(0, 0, 0) },
2894
- anchor6: { value: new Vector3(0, 0, 0) },
2895
- anchor7: { value: new Vector3(0, 0, 0) },
2896
- anchor8: { value: new Vector3(0, 0, 0) },
2897
- anchor9: { value: new Vector3(0, 0, 0) },
2898
- scale0: { value: new Vector3(1, 1, 1) },
2899
- scale1: { value: new Vector3(2, 2, 2) },
2900
- scale2: { value: new Vector3(4, 4, 4) },
2901
- scale3: { value: new Vector3(8, 8, 8) },
2902
- scale4: { value: new Vector3(16, 16, 16) },
2903
- scale5: { value: new Vector3(32, 32, 32) },
2904
- scale6: { value: new Vector3(64, 64, 64) },
2905
- scale7: { value: new Vector3(128, 128, 128) },
2906
- scale8: { value: new Vector3(256, 256, 256) },
2907
- scale9: { value: new Vector3(512, 512, 512) }
2908
- },
2909
- vertexShader: volumeVertexShader,
2910
- fragmentShader: volumeFragmentShader
2911
- };
2912
- function logWithColor$1(message) {
2913
- if (atLeastLogLevel(LogLevel.DEBUG)) {
2914
- console.warn(`%cRM: ${message}`, "background: orange; color: white; padding: 2px; border-radius: 3px;");
2915
- }
2916
- }
2917
- function normalizeValue(value, minMax) {
2918
- return value / minMax[1];
2919
- }
2920
- class VolumeRenderManager {
2921
- constructor() {
2922
- logWithColor$1("Initializing VolumeRenderManager");
2923
- this.uniforms = null;
2924
- this.shader = null;
2925
- this.meshScale = [1, 1, 1];
2926
- this.geometrySize = [1, 1, 1];
2927
- this.boxSize = [1, 1, 1];
2928
- this.zarrInit = false;
2929
- this.channelsVisible = [];
2930
- this.channelTargetC = [];
2931
- this.zarrStoreNumResolutions = null;
2932
- this.channelMaxResolutionIndex = [];
2933
- this.colors = [];
2934
- this.contrastLimits = [];
2935
- this.layerTransparency = 1;
2936
- this.xSlice = new Vector2(-1, 1e5);
2937
- this.ySlice = new Vector2(-1, 1e5);
2938
- this.zSlice = new Vector2(-1, 1e5);
2939
- this.originalScale = [1, 1, 1];
2940
- this.physicalDimensions = [1, 1, 1];
2941
- this.maxResolution = [1, 1, 1];
2942
- this.maxRange = 255;
2943
- this.maxRangeSet = false;
2944
- this.initializeShader();
2945
- }
2946
- /**
2947
- * Initialize shader and uniform objects
2948
- */
2949
- initializeShader() {
2950
- logWithColor$1("Initializing shader");
2951
- this.shader = VolumeShader;
2952
- this.uniforms = UniformsUtils.clone(this.shader.uniforms);
2953
- }
2954
- /**
2955
- * Extract rendering settings from coordination props
2956
- * @param {Object} props - Component props
2957
- * @returns {Object} Extracted rendering settings
2958
- */
2959
- extractRenderingSettingsFromProps(props) {
2960
- const { images = {}, imageLayerScopes = [], imageLayerCoordination = [{}], imageChannelScopesByLayer = {}, imageChannelCoordination = [{}], spatialRenderingMode } = props;
2961
- const layerScope = imageLayerScopes[0];
2962
- if (!layerScope) {
2963
- logWithColor$1("Extracting rendering settings from props - no layer scope");
2964
- return {
2965
- valid: false
2966
- };
2967
- }
2968
- const channelScopes = imageChannelScopesByLayer[layerScope];
2969
- const layerCoordination = imageLayerCoordination[0][layerScope];
2970
- const channelCoordination = imageChannelCoordination[0][layerScope];
2971
- const data = images[layerScope]?.image?.instance?.getData();
2972
- if (!data) {
2973
- logWithColor$1("Extracting rendering settings from props - no image data");
2974
- return {
2975
- valid: false
2976
- };
2977
- }
2978
- if (!channelCoordination[channelScopes?.[0]][CoordinationType.SPATIAL_CHANNEL_WINDOW]) {
2979
- logWithColor$1("Extracting rendering settings from props - no channel window set");
2980
- return {
2981
- valid: false
2982
- };
2983
- }
2984
- const imageWrapperInstance = images[layerScope].image.instance;
2985
- const is3dMode = spatialRenderingMode === "3D";
2986
- const isRgb = layerCoordination[CoordinationType.PHOTOMETRIC_INTERPRETATION] === "RGB";
2987
- const visible = layerCoordination[CoordinationType.SPATIAL_LAYER_VISIBLE];
2988
- const layerTransparency = layerCoordination[CoordinationType.SPATIAL_LAYER_OPACITY];
2989
- const colors = isRgb ? [
2990
- [255, 0, 0],
2991
- [0, 255, 0],
2992
- [0, 0, 255]
2993
- ] : channelScopes.map((cScope) => channelCoordination[cScope][CoordinationType.SPATIAL_CHANNEL_COLOR]);
2994
- const contrastLimits = isRgb ? [
2995
- [0, 255],
2996
- [0, 255],
2997
- [0, 255]
2998
- ] : channelScopes.map((cScope) => channelCoordination[cScope][CoordinationType.SPATIAL_CHANNEL_WINDOW] || [0, 255]);
2999
- if (!this.maxRangeSet) {
3000
- this.maxRange = Math.max(...contrastLimits.map((limit) => limit[1]));
3001
- this.maxRangeSet = true;
3002
- }
3003
- const channelsVisible = isRgb ? [
3004
- visible && true,
3005
- visible && true,
3006
- visible && true
3007
- ] : channelScopes.map((cScope) => visible && channelCoordination[cScope][CoordinationType.SPATIAL_CHANNEL_VISIBLE]);
3008
- const channelTargetC = isRgb ? [
3009
- visible && true,
3010
- visible && true,
3011
- visible && true
3012
- ] : channelScopes.map((cScope) => visible && imageWrapperInstance.getChannelIndex(channelCoordination[cScope][CoordinationType.SPATIAL_TARGET_C]));
3013
- const channelMaxResolutionIndex = isRgb ? [
3014
- visible && null,
3015
- visible && null,
3016
- visible && null
3017
- ] : channelScopes.map((cScope) => channelCoordination[cScope][CoordinationType.SPATIAL_MAX_RESOLUTION]);
3018
- let xSlice = layerCoordination[CoordinationType.SPATIAL_SLICE_X];
3019
- let ySlice = layerCoordination[CoordinationType.SPATIAL_SLICE_Y];
3020
- let zSlice = layerCoordination[CoordinationType.SPATIAL_SLICE_Z];
3021
- const lodFactor = layerCoordination[CoordinationType.SPATIAL_LOD_FACTOR] ?? 1;
3022
- xSlice = xSlice !== null ? xSlice : new Vector2(-1, 1e5);
3023
- ySlice = ySlice !== null ? ySlice : new Vector2(-1, 1e5);
3024
- zSlice = zSlice !== null ? zSlice : new Vector2(-1, 1e5);
3025
- const allChannels = images[layerScope].image.loaders[0].channels;
3026
- logWithColor$1("Extracting rendering settings from props - success");
3027
- return {
3028
- valid: true,
3029
- channelsVisible,
3030
- allChannels,
3031
- channelTargetC,
3032
- channelMaxResolutionIndex,
3033
- data,
3034
- colors,
3035
- contrastLimits,
3036
- is3dMode,
3037
- layerTransparency,
3038
- xSlice,
3039
- ySlice,
3040
- zSlice,
3041
- lodFactor
3042
- };
3043
- }
3044
- /**
3045
- * Update the render settings from the given props
3046
- * @param {Object} props - Component props
3047
- * @returns {boolean} True if settings were successfully updated
3048
- */
3049
- updateFromProps(props) {
3050
- const settings = this.extractRenderingSettingsFromProps(props);
3051
- if (!settings.valid) {
3052
- logWithColor$1("Updating from props - invalid settings");
3053
- return false;
3054
- }
3055
- this.channelsVisible = settings.channelsVisible;
3056
- this.channelTargetC = settings.channelTargetC;
3057
- this.channelMaxResolutionIndex = settings.channelMaxResolutionIndex;
3058
- this.colors = settings.colors;
3059
- this.contrastLimits = settings.contrastLimits;
3060
- this.renderingMode = settings.renderingMode;
3061
- this.layerTransparency = settings.layerTransparency;
3062
- this.xSlice = settings.xSlice;
3063
- this.ySlice = settings.ySlice;
3064
- this.zSlice = settings.zSlice;
3065
- this.uniforms.lodFactor.value = settings.lodFactor;
3066
- logWithColor$1(`lodFactor ${settings.lodFactor}`);
3067
- this.shader.uniforms.lodFactor.value = settings.lodFactor;
3068
- logWithColor$1("Updating from props - success");
3069
- return true;
3070
- }
3071
- /**
3072
- * Update the rendering based on the current volume data from the data manager
3073
- * @param {object} volumeDataManagerProps - Params derived from the volume data manager
3074
- * @returns {Object|null} Updated rendering settings or null if rendering is not possible
3075
- */
3076
- updateRendering({ zarrStoreShapes, originalScaleXYZ, physicalDimensionsXYZ, maxResolutionXYZ, boxDimensionsXYZ, normalizedScaleXYZ, bcTHREE, ptTHREE }) {
3077
- logWithColor$1("Updating rendering");
3078
- this.channelTargetC.findIndex((channel, idx) => this.channelsVisible[idx]);
3079
- if (!Array.isArray(zarrStoreShapes) || zarrStoreShapes.length === 0) {
3080
- return null;
3081
- }
3082
- const shape = zarrStoreShapes[0];
3083
- const dimensions = {
3084
- xLength: shape[4] || 1,
3085
- yLength: shape[3] || 1,
3086
- zLength: shape[2] || 1
3087
- };
3088
- const texturesList = [];
3089
- const colorsSave = [];
3090
- const contrastLimitsList = [];
3091
- this.channelTargetC.forEach((channel, id) => {
3092
- if (this.channelsVisible[id] || true) {
3093
- const max = this.maxRange ? this.maxRange : 255;
3094
- const minMax = [0, max];
3095
- colorsSave.push([
3096
- this.colors[id][0] / 255,
3097
- this.colors[id][1] / 255,
3098
- this.colors[id][2] / 255,
3099
- this.channelsVisible[id] ? 1 : 0
3100
- ]);
3101
- log.debug("colorsSave", colorsSave);
3102
- if (this.contrastLimits[id][0] === 0 && this.contrastLimits[id][1] === 255) {
3103
- contrastLimitsList.push([
3104
- normalizeValue(minMax[0], minMax),
3105
- normalizeValue(minMax[1], minMax)
3106
- ]);
3107
- } else {
3108
- contrastLimitsList.push([
3109
- normalizeValue(this.contrastLimits[id][0], minMax),
3110
- normalizeValue(this.contrastLimits[id][1], minMax)
3111
- ]);
3112
- }
3113
- }
3114
- });
3115
- if (!this.zarrInit) {
3116
- this.originalScale = originalScaleXYZ;
3117
- this.physicalDimensions = physicalDimensionsXYZ;
3118
- this.maxResolution = maxResolutionXYZ;
3119
- const scaledResolution = boxDimensionsXYZ;
3120
- this.normalizedScale = normalizedScaleXYZ;
3121
- this.meshScale = [
3122
- this.originalScale[0] / this.originalScale[0],
3123
- this.originalScale[1] / this.originalScale[0],
3124
- this.originalScale[2] / this.originalScale[0]
3125
- ];
3126
- this.geometrySize = scaledResolution;
3127
- this.boxSize = scaledResolution;
3128
- log.debug("this.boxSize", this.boxSize);
3129
- log.debug("this.geometrySize", this.geometrySize);
3130
- log.debug("this.meshScale", this.meshScale);
3131
- log.debug("this.originalScale", this.originalScale);
3132
- log.debug("this.physicalDimensions", this.physicalDimensions);
3133
- log.debug("this.maxResolution", this.maxResolution);
3134
- log.debug("scaledResolution", scaledResolution);
3135
- this.zarrInit = true;
3136
- }
3137
- this.updateUniforms(
3138
- texturesList,
3139
- dimensions,
3140
- // Pass dimensions object instead of volume
3141
- this.renderingMode,
3142
- contrastLimitsList,
3143
- colorsSave,
3144
- this.layerTransparency,
3145
- this.xSlice,
3146
- this.ySlice,
3147
- this.zSlice,
3148
- bcTHREE,
3149
- ptTHREE
3150
- );
3151
- return {
3152
- uniforms: this.uniforms,
3153
- shader: this.shader,
3154
- meshScale: this.meshScale,
3155
- geometrySize: this.geometrySize,
3156
- boxSize: this.boxSize
3157
- };
3158
- }
3159
- /**
3160
- * Update shader uniforms with current rendering values
3161
- * @param {Array} textures - List of 3D textures
3162
- * @param {Object} dimensions - Dimensions object
3163
- * @param {number} renderstyle - Rendering mode value
3164
- * @param {Array} contrastLimits - List of contrast limits for each channel
3165
- * @param {Array} colors - List of colors for each channel
3166
- * @param {number} layerTransparency - Overall transparency value
3167
- * @param {Vector2} xSlice - X clipping plane
3168
- * @param {Vector2} ySlice - Y clipping plane
3169
- * @param {Vector2} zSlice - Z clipping plane
3170
- */
3171
- updateUniforms(textures, dimensions, renderstyle, contrastLimits, colors, layerTransparency, xSlice, ySlice, zSlice, brickCacheTexture, pageTableTexture) {
3172
- logWithColor$1("Updating uniforms");
3173
- this.uniforms.boxSize.value.set(this.boxSize[0], this.boxSize[1], this.boxSize[2]);
3174
- this.uniforms.brickCacheTex.value = brickCacheTexture;
3175
- this.uniforms.pageTableTex.value = pageTableTexture;
3176
- this.uniforms.near.value = 0.1;
3177
- this.uniforms.far.value = 3e3;
3178
- this.uniforms.opacity.value = layerTransparency;
3179
- this.uniforms.volumeCount.value = textures.length;
3180
- this.uniforms.u_size.value.set(dimensions.xLength, dimensions.yLength, dimensions.zLength);
3181
- this.uniforms.u_window_size.value.set(0, 0);
3182
- this.uniforms.u_vol_scale.value.set(1 / dimensions.xLength, 1 / dimensions.yLength, 1 / dimensions.zLength * 2);
3183
- this.uniforms.clim0.value.set(contrastLimits.length > 0 ? contrastLimits[0][0] : null, contrastLimits.length > 0 ? contrastLimits[0][1] : null);
3184
- this.uniforms.clim1.value.set(contrastLimits.length > 1 ? contrastLimits[1][0] : null, contrastLimits.length > 1 ? contrastLimits[1][1] : null);
3185
- this.uniforms.clim2.value.set(contrastLimits.length > 2 ? contrastLimits[2][0] : null, contrastLimits.length > 2 ? contrastLimits[2][1] : null);
3186
- this.uniforms.clim3.value.set(contrastLimits.length > 3 ? contrastLimits[3][0] : null, contrastLimits.length > 3 ? contrastLimits[3][1] : null);
3187
- this.uniforms.clim4.value.set(contrastLimits.length > 4 ? contrastLimits[4][0] : null, contrastLimits.length > 4 ? contrastLimits[4][1] : null);
3188
- this.uniforms.clim5.value.set(contrastLimits.length > 5 ? contrastLimits[5][0] : null, contrastLimits.length > 5 ? contrastLimits[5][1] : null);
3189
- this.uniforms.clim6.value.set(contrastLimits.length > 6 ? contrastLimits[6][0] : null, contrastLimits.length > 6 ? contrastLimits[6][1] : null);
3190
- this.uniforms.xClip.value.set(xSlice[0] * (1 / this.maxResolution[0]) * this.boxSize[0], xSlice[1] * (1 / this.maxResolution[0]) * this.boxSize[0]);
3191
- this.uniforms.yClip.value.set(ySlice[0] * (1 / this.maxResolution[1]) * this.boxSize[1], ySlice[1] * (1 / this.maxResolution[1]) * this.boxSize[1]);
3192
- this.uniforms.zClip.value.set(zSlice[0] * (1 / this.maxResolution[2]) * this.boxSize[2], zSlice[1] * (1 / this.maxResolution[2]) * this.boxSize[2]);
3193
- this.uniforms.color0.value.set(colors.length > 0 ? colors[0][0] : null, colors.length > 0 ? colors[0][1] : null, colors.length > 0 ? colors[0][2] : null, colors.length > 0 ? colors[0][3] : null);
3194
- this.uniforms.color1.value.set(colors.length > 1 ? colors[1][0] : null, colors.length > 1 ? colors[1][1] : null, colors.length > 1 ? colors[1][2] : null, colors.length > 1 ? colors[1][3] : null);
3195
- this.uniforms.color2.value.set(colors.length > 2 ? colors[2][0] : null, colors.length > 2 ? colors[2][1] : null, colors.length > 2 ? colors[2][2] : null, colors.length > 2 ? colors[2][3] : null);
3196
- this.uniforms.color3.value.set(colors.length > 3 ? colors[3][0] : null, colors.length > 3 ? colors[3][1] : null, colors.length > 3 ? colors[3][2] : null, colors.length > 3 ? colors[3][3] : null);
3197
- this.uniforms.color4.value.set(colors.length > 4 ? colors[4][0] : null, colors.length > 4 ? colors[4][1] : null, colors.length > 4 ? colors[4][2] : null, colors.length > 4 ? colors[4][3] : null);
3198
- this.uniforms.color5.value.set(colors.length > 5 ? colors[5][0] : null, colors.length > 5 ? colors[5][1] : null, colors.length > 5 ? colors[5][2] : null, colors.length > 5 ? colors[5][3] : null);
3199
- this.uniforms.color6.value.set(colors.length > 6 ? colors[6][0] : null, colors.length > 6 ? colors[6][1] : null, colors.length > 6 ? colors[6][2] : null, colors.length > 6 ? colors[6][3] : null);
3200
- for (let i = 0; i < 7; i++) {
3201
- if (typeof this.channelMaxResolutionIndex[i] === "number") {
3202
- this.uniforms[`res${i}`].value.set(Math.max(1, this.channelMaxResolutionIndex[i]), this.zarrStoreNumResolutions - 1);
3203
- }
3204
- }
3205
- }
3206
- /**
3207
- * Sets the processing render target
3208
- * @param {WebGLMultipleRenderTargets} mrt - Multiple render targets object with 3 attachments
3209
- */
3210
- /*
3211
- setProcessingTargets(mrt) {
3212
- logWithColor('setting processing targets');
3213
- this.mrt = mrt;
3214
- }
3215
- */
3216
- setChannelMapping(channelMapping) {
3217
- logWithColor$1("setting channel mapping");
3218
- log.debug("channelMapping", channelMapping);
3219
- this.uniforms.channelMapping.value = channelMapping;
3220
- }
3221
- // Only called on initialization of the
3222
- // VolumeDataManager and VolumeRenderManager for a particular image.
3223
- setZarrUniforms(zarrStore, PT) {
3224
- logWithColor$1("setting zarr uniforms");
3225
- log.debug("zarrStore", zarrStore);
3226
- log.debug("PT", PT);
3227
- for (let i = 0; i <= 9; i++) {
3228
- if (PT.anchors && PT.anchors[i]) {
3229
- this.uniforms[`anchor${i}`].value.set(PT.anchors[i][0] || 0, PT.anchors[i][1] || 0, PT.anchors[i][2] || 0);
3230
- } else {
3231
- log.debug("anchor", i, "does not exist");
3232
- this.uniforms[`anchor${i}`].value.set(0, 0, 0);
3233
- }
3234
- if (zarrStore.scales && zarrStore.scales[i]) {
3235
- this.uniforms[`scale${i}`].value.set(zarrStore.scales[i][0] || 1, zarrStore.scales[i][1] || 1, zarrStore.scales[i][2] || 1);
3236
- } else {
3237
- log.debug("scale", i, "does not exist");
3238
- }
3239
- }
3240
- log.debug("zarrStore.brickLayout", zarrStore.brickLayout);
3241
- this.zarrStoreNumResolutions = zarrStore.brickLayout.length;
3242
- for (let i = 0; i < 7; i++) {
3243
- this.uniforms[`res${i}`].value.set(
3244
- // TODO(mark) make this dependent on the per-channel maxResolution slider value.
3245
- 1,
3246
- zarrStore.brickLayout.length - 1
3247
- );
3248
- }
3249
- this.uniforms.resGlobal.value.set(1, zarrStore.brickLayout.length - 1);
3250
- this.uniforms.voxelExtents.value.set(zarrStore.shapes[0][4], zarrStore.shapes[0][3], zarrStore.shapes[0][2]);
3251
- this.uniforms.maxChannels.value = Math.min(zarrStore.channelCount, 7);
3252
- log.debug("this.channelsVisible", this.channelsVisible);
3253
- log.debug("zarrStore.shapes[0]", zarrStore.shapes[0]);
3254
- log.debug("PT", PT);
3255
- log.debug("uniforms", this.uniforms);
3256
- }
3257
- }
3258
- const gaussianVertexShader = `//
3259
- varying vec2 vUv;
3260
- void main() {
3261
- vUv = uv;
3262
- gl_Position = vec4(position, 1.0);
3263
- }
3264
- `;
3265
- const gaussianFragmentShader = `//
3266
- // Input texture to blur
3267
- uniform sampler2D tDiffuse;
3268
- // Resolution of the texture (width, height)
3269
- uniform vec2 resolution;
3270
- // Blur strength: 1=no blur, 2-3=3x3 kernel, 4-5=5x5 kernel, 6+=7x7 kernel
3271
- uniform int gaussian;
3272
- // Texture coordinates for current pixel
3273
- varying vec2 vUv;
3274
-
3275
- /**
3276
- * No blur - returns the original pixel color
3277
- */
3278
- vec4 noGaussian() {
3279
- vec4 color = texture2D(tDiffuse, vUv);
3280
- return color;
3281
- }
3282
-
3283
- /**
3284
- * Applies 3x3 Gaussian blur kernel
3285
- * Samples 9 pixels in a 3x3 grid around the current pixel
3286
- * Weights are based on 2D Gaussian distribution
3287
- */
3288
- vec4 gaussian3(vec2 texel) {
3289
- vec4 color = vec4(0.0);
3290
-
3291
- // Top row: weights [0.0625, 0.125, 0.0625]
3292
- color += texture2D(tDiffuse, vUv + texel * vec2(-1.0, -1.0)) * 0.0625;
3293
- color += texture2D(tDiffuse, vUv + texel * vec2( 0.0, -1.0)) * 0.125;
3294
- color += texture2D(tDiffuse, vUv + texel * vec2( 1.0, -1.0)) * 0.0625;
3295
-
3296
- // Middle row: weights [0.125, 0.25, 0.125] (center pixel gets highest weight)
3297
- color += texture2D(tDiffuse, vUv + texel * vec2(-1.0, 0.0)) * 0.125;
3298
- color += texture2D(tDiffuse, vUv + texel * vec2( 0.0, 0.0)) * 0.25;
3299
- color += texture2D(tDiffuse, vUv + texel * vec2( 1.0, 0.0)) * 0.125;
3300
-
3301
- // Bottom row: weights [0.0625, 0.125, 0.0625]
3302
- color += texture2D(tDiffuse, vUv + texel * vec2(-1.0, 1.0)) * 0.0625;
3303
- color += texture2D(tDiffuse, vUv + texel * vec2( 0.0, 1.0)) * 0.125;
3304
- color += texture2D(tDiffuse, vUv + texel * vec2( 1.0, 1.0)) * 0.0625;
3305
-
3306
- return color;
3307
- }
3308
-
3309
- /**
3310
- * Applies 5x5 Gaussian blur kernel
3311
- * Samples 25 pixels in a 5x5 grid around the current pixel
3312
- * Weights sum to 1.0 (273/273) for proper normalization
3313
- */
3314
- vec4 gaussian5(vec2 texel) {
3315
- vec4 color = vec4(0.0);
3316
-
3317
- // Row 1: weights [1/273, 4/273, 7/273, 4/273, 1/273]
3318
- color += texture2D(tDiffuse, vUv + texel * vec2(-2.0, -2.0)) * 1.0/273.0;
3319
- color += texture2D(tDiffuse, vUv + texel * vec2(-1.0, -2.0)) * 4.0/273.0;
3320
- color += texture2D(tDiffuse, vUv + texel * vec2( 0.0, -2.0)) * 7.0/273.0;
3321
- color += texture2D(tDiffuse, vUv + texel * vec2( 1.0, -2.0)) * 4.0/273.0;
3322
- color += texture2D(tDiffuse, vUv + texel * vec2( 2.0, -2.0)) * 1.0/273.0;
3323
-
3324
- // Row 2: weights [4/273, 16/273, 26/273, 16/273, 4/273]
3325
- color += texture2D(tDiffuse, vUv + texel * vec2(-2.0, -1.0)) * 4.0/273.0;
3326
- color += texture2D(tDiffuse, vUv + texel * vec2(-1.0, -1.0)) * 16.0/273.0;
3327
- color += texture2D(tDiffuse, vUv + texel * vec2( 0.0, -1.0)) * 26.0/273.0;
3328
- color += texture2D(tDiffuse, vUv + texel * vec2( 1.0, -1.0)) * 16.0/273.0;
3329
- color += texture2D(tDiffuse, vUv + texel * vec2( 2.0, -1.0)) * 4.0/273.0;
3330
-
3331
- // Row 3 (center): weights [7/273, 26/273, 41/273, 26/273, 7/273]
3332
- color += texture2D(tDiffuse, vUv + texel * vec2(-2.0, 0.0)) * 7.0/273.0;
3333
- color += texture2D(tDiffuse, vUv + texel * vec2(-1.0, 0.0)) * 26.0/273.0;
3334
- color += texture2D(tDiffuse, vUv + texel * vec2( 0.0, 0.0)) * 41.0/273.0; // Center pixel
3335
- color += texture2D(tDiffuse, vUv + texel * vec2( 1.0, 0.0)) * 26.0/273.0;
3336
- color += texture2D(tDiffuse, vUv + texel * vec2( 2.0, 0.0)) * 7.0/273.0;
3337
-
3338
- // Row 4: weights [4/273, 16/273, 26/273, 16/273, 4/273]
3339
- color += texture2D(tDiffuse, vUv + texel * vec2(-2.0, 1.0)) * 4.0/273.0;
3340
- color += texture2D(tDiffuse, vUv + texel * vec2(-1.0, 1.0)) * 16.0/273.0;
3341
- color += texture2D(tDiffuse, vUv + texel * vec2( 0.0, 1.0)) * 26.0/273.0;
3342
- color += texture2D(tDiffuse, vUv + texel * vec2( 1.0, 1.0)) * 16.0/273.0;
3343
- color += texture2D(tDiffuse, vUv + texel * vec2( 2.0, 1.0)) * 4.0/273.0;
3344
-
3345
- // Row 5: weights [1/273, 4/273, 7/273, 4/273, 1/273]
3346
- color += texture2D(tDiffuse, vUv + texel * vec2(-2.0, 2.0)) * 1.0/273.0;
3347
- color += texture2D(tDiffuse, vUv + texel * vec2(-1.0, 2.0)) * 4.0/273.0;
3348
- color += texture2D(tDiffuse, vUv + texel * vec2( 0.0, 2.0)) * 7.0/273.0;
3349
- color += texture2D(tDiffuse, vUv + texel * vec2( 1.0, 2.0)) * 4.0/273.0;
3350
- color += texture2D(tDiffuse, vUv + texel * vec2( 2.0, 2.0)) * 1.0/273.0;
3351
-
3352
- return color;
3353
- }
3354
-
3355
- /**
3356
- * Applies 7x7 Gaussian blur kernel
3357
- * Samples 49 pixels in a 7x7 grid around the current pixel
3358
- * Weights sum to 1.0 (1003/1003) for proper normalization
3359
- * Creates the strongest blur effect
3360
- */
3361
- vec4 gaussian7(vec2 texel) {
3362
- vec4 color = vec4(0.0);
3363
-
3364
- // Row 1: weights [0, 0, 1/1003, 2/1003, 1/1003, 0, 0]
3365
- color += texture2D(tDiffuse, vUv + texel * vec2(-1.0, -3.0)) * 1.0/1003.0;
3366
- color += texture2D(tDiffuse, vUv + texel * vec2( 0.0, -3.0)) * 2.0/1003.0;
3367
- color += texture2D(tDiffuse, vUv + texel * vec2( 1.0, -3.0)) * 1.0/1003.0;
3368
-
3369
- // Row 2: weights [0, 3/1003, 13/1003, 22/1003, 13/1003, 3/1003, 0]
3370
- color += texture2D(tDiffuse, vUv + texel * vec2(-2.0, -2.0)) * 3.0/1003.0;
3371
- color += texture2D(tDiffuse, vUv + texel * vec2(-1.0, -2.0)) * 13.0/1003.0;
3372
- color += texture2D(tDiffuse, vUv + texel * vec2( 0.0, -2.0)) * 22.0/1003.0;
3373
- color += texture2D(tDiffuse, vUv + texel * vec2( 1.0, -2.0)) * 13.0/1003.0;
3374
- color += texture2D(tDiffuse, vUv + texel * vec2( 2.0, -2.0)) * 3.0/1003.0;
3375
-
3376
- // Row 3: weights [1/1003, 13/1003, 59/1003, 97/1003, 59/1003, 13/1003, 1/1003]
3377
- color += texture2D(tDiffuse, vUv + texel * vec2(-3.0, -1.0)) * 1.0/1003.0;
3378
- color += texture2D(tDiffuse, vUv + texel * vec2(-2.0, -1.0)) * 13.0/1003.0;
3379
- color += texture2D(tDiffuse, vUv + texel * vec2(-1.0, -1.0)) * 59.0/1003.0;
3380
- color += texture2D(tDiffuse, vUv + texel * vec2( 0.0, -1.0)) * 97.0/1003.0;
3381
- color += texture2D(tDiffuse, vUv + texel * vec2( 1.0, -1.0)) * 59.0/1003.0;
3382
- color += texture2D(tDiffuse, vUv + texel * vec2( 2.0, -1.0)) * 13.0/1003.0;
3383
- color += texture2D(tDiffuse, vUv + texel * vec2( 3.0, -1.0)) * 1.0/1003.0;
3384
-
3385
- // Row 4 (center): weights [2/1003, 22/1003, 97/1003, 159/1003, 97/1003, 22/1003, 2/1003]
3386
- color += texture2D(tDiffuse, vUv + texel * vec2(-3.0, 0.0)) * 2.0/1003.0;
3387
- color += texture2D(tDiffuse, vUv + texel * vec2(-2.0, 0.0)) * 22.0/1003.0;
3388
- color += texture2D(tDiffuse, vUv + texel * vec2(-1.0, 0.0)) * 97.0/1003.0;
3389
- color += texture2D(tDiffuse, vUv + texel * vec2( 0.0, 0.0)) * 159.0/1003.0; // Center pixel
3390
- color += texture2D(tDiffuse, vUv + texel * vec2( 1.0, 0.0)) * 97.0/1003.0;
3391
- color += texture2D(tDiffuse, vUv + texel * vec2( 2.0, 0.0)) * 22.0/1003.0;
3392
- color += texture2D(tDiffuse, vUv + texel * vec2( 3.0, 0.0)) * 2.0/1003.0;
3393
-
3394
- // Row 5: weights [1/1003, 13/1003, 59/1003, 97/1003, 59/1003, 13/1003, 1/1003]
3395
- color += texture2D(tDiffuse, vUv + texel * vec2(-3.0, 1.0)) * 1.0/1003.0;
3396
- color += texture2D(tDiffuse, vUv + texel * vec2(-2.0, 1.0)) * 13.0/1003.0;
3397
- color += texture2D(tDiffuse, vUv + texel * vec2(-1.0, 1.0)) * 59.0/1003.0;
3398
- color += texture2D(tDiffuse, vUv + texel * vec2( 0.0, 1.0)) * 97.0/1003.0;
3399
- color += texture2D(tDiffuse, vUv + texel * vec2( 1.0, 1.0)) * 59.0/1003.0;
3400
- color += texture2D(tDiffuse, vUv + texel * vec2( 2.0, 1.0)) * 13.0/1003.0;
3401
- color += texture2D(tDiffuse, vUv + texel * vec2( 3.0, 1.0)) * 1.0/1003.0;
3402
-
3403
- // Row 6: weights [0, 3/1003, 13/1003, 22/1003, 13/1003, 3/1003, 0]
3404
- color += texture2D(tDiffuse, vUv + texel * vec2(-2.0, 2.0)) * 3.0/1003.0;
3405
- color += texture2D(tDiffuse, vUv + texel * vec2(-1.0, 2.0)) * 13.0/1003.0;
3406
- color += texture2D(tDiffuse, vUv + texel * vec2( 0.0, 2.0)) * 22.0/1003.0;
3407
- color += texture2D(tDiffuse, vUv + texel * vec2( 1.0, 2.0)) * 13.0/1003.0;
3408
- color += texture2D(tDiffuse, vUv + texel * vec2( 2.0, 2.0)) * 3.0/1003.0;
3409
-
3410
- // Row 7: weights [0, 0, 1/1003, 2/1003, 1/1003, 0, 0]
3411
- color += texture2D(tDiffuse, vUv + texel * vec2(-1.0, 3.0)) * 1.0/1003.0;
3412
- color += texture2D(tDiffuse, vUv + texel * vec2( 0.0, 3.0)) * 2.0/1003.0;
3413
- color += texture2D(tDiffuse, vUv + texel * vec2( 1.0, 3.0)) * 1.0/1003.0;
3414
-
3415
- return color;
3416
- }
3417
-
3418
- /**
3419
- * Main fragment shader function
3420
- * Determines which blur kernel to apply based on the 'gaussian' uniform
3421
- */
3422
- void main() {
3423
- // Calculate the size of one texel (pixel) in texture coordinates
3424
- vec2 texel = 1.0 / resolution;
3425
-
3426
- // Initialize output color (this line appears to be unused/debug code)
3427
- vec4 color = vec4(1.0, 0.0, 0.0, 0.0);
3428
-
3429
- // TODO: remove these variables since not used
3430
- bool left = vUv.x < 0.5;
3431
- bool top = vUv.y < 0.5;
3432
-
3433
- if (gaussian > 1 && gaussian <= 3) {
3434
- // Apply 3x3 Gaussian blur for values 2-3
3435
- color = gaussian3(texel);
3436
- } else if (gaussian > 3 && gaussian <= 5) {
3437
- // Apply 5x5 Gaussian blur for values 4-5
3438
- color = gaussian5(texel);
3439
- } else if (gaussian > 5) {
3440
- // Apply 7x7 Gaussian blur for values 6 and above
3441
- color = gaussian7(texel);
3442
- } else {
3443
- // No blur for value 1 or less
3444
- color = noGaussian();
3445
- }
3446
-
3447
- // Output the final blurred color
3448
- gl_FragColor = color;
3449
- }
3450
- `;
3451
- function framebufferFor(renderer, rt) {
3452
- const p = renderer.properties?.get(rt);
3453
- return p?.framebuffer || p?.__webglFramebuffer || rt.__webglFramebuffer;
3454
- }
3455
- function logWithColor(msg) {
3456
- if (atLeastLogLevel(LogLevel.DEBUG)) {
3457
- console.warn(`%cV: ${msg}`, "background: deeppink; color: white; padding: 2px; border-radius: 3px;");
3458
- }
3459
- }
3460
- function performGeometryPass(_gl, _camera, _scene, { mrtRef }) {
3461
- _gl.setRenderTarget(mrtRef.current);
3462
- _gl.clear(true, true, true);
3463
- _gl.render(_scene, _camera);
3464
- }
3465
- function performBlitPass(_gl, { screenSceneRef, screenCameraRef }) {
3466
- _gl.setRenderTarget(null);
3467
- _gl.clear(true, true, true);
3468
- _gl.render(screenSceneRef.current, screenCameraRef.current);
3469
- }
3470
- function handleRequests(_gl, { frameRef, dataManager, mrtRef, bufRequest, bufUsage }) {
3471
- const ctx = _gl.getContext();
3472
- const f = frameRef.current;
3473
- if (dataManager.noNewRequests === true && !dataManager.manuallyStopped && f % 100 === 0 && f < 500) {
3474
- dataManager.noNewRequests = false;
3475
- }
3476
- if (dataManager.triggerRequest === true && dataManager.noNewRequests === false) {
3477
- ctx.bindFramebuffer(ctx.READ_FRAMEBUFFER, framebufferFor(_gl, mrtRef.current));
3478
- ctx.readBuffer(ctx.COLOR_ATTACHMENT1);
3479
- ctx.readPixels(0, 0, mrtRef.current.width, mrtRef.current.height, ctx.RGBA, ctx.UNSIGNED_BYTE, bufRequest.current);
3480
- dataManager.processRequestData(bufRequest.current, {
3481
- // If width/height are not yet available,
3482
- // processRequestData will proceed without weighting.
3483
- width: mrtRef?.current?.width,
3484
- height: mrtRef?.current?.height,
3485
- // Default sigmaNormalized = 0.25; can be tuned
3486
- sigmaNormalized: DEFAULT_SIGMA_NORMALIZED
3487
- });
3488
- } else if (dataManager.triggerUsage === true && dataManager.noNewRequests === false) {
3489
- ctx.bindFramebuffer(ctx.READ_FRAMEBUFFER, framebufferFor(_gl, mrtRef.current));
3490
- ctx.readBuffer(ctx.COLOR_ATTACHMENT2);
3491
- ctx.readPixels(0, 0, mrtRef.current.width, mrtRef.current.height, ctx.RGBA, ctx.UNSIGNED_BYTE, bufUsage.current);
3492
- dataManager.processUsageData(bufUsage.current);
3493
- }
3494
- }
3495
- function handleAdaptiveQuality(clock, params) {
3496
- const { invalidate, isInteracting, dataManager, spatialRenderingModeChanging, meshRef, stillRef, screenQuadRef, lastSampleRef, frameRef, lastFrameCountRef } = params;
3497
- if (screenQuadRef.current) {
3498
- if (!stillRef.current) {
3499
- screenQuadRef.current.material.uniforms.gaussian.value = 7;
3500
- } else {
3501
- screenQuadRef.current.material.uniforms.gaussian.value = 0;
3502
- }
3503
- }
3504
- if (isInteracting) {
3505
- if (!dataManager.manuallyStopped) {
3506
- dataManager.noNewRequests = false;
3507
- dataManager.triggerUsage = true;
3508
- }
3509
- return;
3510
- }
3511
- if (spatialRenderingModeChanging)
3512
- return;
3513
- const meshRefUniforms = meshRef.current?.material?.uniforms;
3514
- const renderSpeed = meshRefUniforms?.renderRes?.value ?? dataManager?.PT?.lowestDataRes;
3515
- if (dataManager.noNewRequests) {
3516
- if (renderSpeed !== 0) {
3517
- if (meshRefUniforms) {
3518
- meshRefUniforms.renderRes.value = 0;
3519
- }
3520
- log.debug("Adaptive Quality: No new requests. Setting renderSpeed to 0 (best quality).");
3521
- stillRef.current = false;
3522
- screenQuadRef.current.material.uniforms.gaussian.value = 7;
3523
- invalidate();
3524
- } else if (!stillRef.current) {
3525
- log.debug("Adaptive Quality: No new requests and already at best quality. Setting stillRef to true.");
3526
- stillRef.current = true;
3527
- screenQuadRef.current.material.uniforms.gaussian.value = 0;
3528
- }
3529
- return;
3530
- }
3531
- if (stillRef.current) {
3532
- stillRef.current = false;
3533
- invalidate();
3534
- }
3535
- const t = clock.getElapsedTime();
3536
- if (t - lastSampleRef.current < 1) {
3537
- return;
3538
- }
3539
- const timeElapsedDuringSample = t - lastSampleRef.current;
3540
- const framesRenderedDuringSample = frameRef.current - lastFrameCountRef.current;
3541
- let fps = 0;
3542
- if (timeElapsedDuringSample > 0) {
3543
- fps = framesRenderedDuringSample / timeElapsedDuringSample;
3544
- }
3545
- lastSampleRef.current = t;
3546
- lastFrameCountRef.current = frameRef.current;
3547
- const upscale = fps > 100 && renderSpeed > 0;
3548
- const downscale = fps < 30 && renderSpeed < dataManager.PT.lowestDataRes;
3549
- if (upscale || downscale) {
3550
- const newSpeed = renderSpeed + (downscale ? 1 : -1);
3551
- if (meshRefUniforms) {
3552
- meshRefUniforms.renderRes.value = newSpeed;
3553
- }
3554
- invalidate();
3555
- }
3556
- }
3557
- function VolumeView(props) {
3558
- const {
3559
- images,
3560
- imageLayerScopes,
3561
- imageLayerCoordination,
3562
- imageChannelScopesByLayer,
3563
- imageChannelCoordination,
3564
- // onInitComplete,
3565
- spatialRenderingMode,
3566
- spatialRenderingModeChanging,
3567
- onVolumeLoadingUpdate
3568
- } = props;
3569
- const {
3570
- gl
3571
- // scene,
3572
- // camera
3573
- } = useThree();
3574
- const invalidate = useThree((state) => state.invalidate);
3575
- const orbitRef = useRef(null);
3576
- const meshRef = useRef(null);
3577
- const bufRequest = useRef(null);
3578
- const bufUsage = useRef(null);
3579
- const mrtRef = useRef(null);
3580
- const [renderState, setRenderState] = useState({
3581
- uniforms: null,
3582
- shader: null,
3583
- meshScale: [1, 1, 1],
3584
- geometrySize: [1, 1, 1]
3585
- });
3586
- const is3D = spatialRenderingMode === "3D";
3587
- const screenSceneRef = useRef(null);
3588
- const screenCameraRef = useRef(null);
3589
- const screenQuadRef = useRef(null);
3590
- const [isInteracting, _setIsInteracting] = useState(false);
3591
- const interactionTimeoutRef = useRef(null);
3592
- const stillRef = useRef(false);
3593
- const frameRef = useRef(0);
3594
- const lastSampleRef = useRef(0);
3595
- const lastFrameCountRef = useRef(0);
3596
- const dataManager = useMemo(() => new VolumeDataManager(gl), [gl]);
3597
- const renderManager = useMemo(() => new VolumeRenderManager(), []);
3598
- useEffect(() => {
3599
- logWithColor("useEffect MRT target matching canvas");
3600
- const { width, height } = gl.domElement;
3601
- const mrt = new WebGLMultipleRenderTargets(width, height, 3);
3602
- mrt.texture.forEach((tex) => {
3603
- tex.format = THREE.RGBAFormat;
3604
- tex.type = THREE.UnsignedByteType;
3605
- tex.minFilter = THREE.NearestFilter;
3606
- tex.magFilter = THREE.NearestFilter;
3607
- tex.generateMipmaps = false;
3608
- });
3609
- const screenScene = new THREE.Scene();
3610
- const screenCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 10);
3611
- screenCamera.position.z = 1;
3612
- const screenMaterial = new THREE.ShaderMaterial({
3613
- uniforms: {
3614
- // Bind the first render target texture as the input of the gaussian blur shader.
3615
- tDiffuse: { value: mrt.texture[0] },
3616
- resolution: { value: new THREE.Vector2(width, height) },
3617
- gaussian: { value: 7 }
3618
- },
3619
- vertexShader: gaussianVertexShader,
3620
- fragmentShader: gaussianFragmentShader,
3621
- transparent: true
3622
- });
3623
- const screenQuad = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), screenMaterial);
3624
- screenScene.add(screenQuad);
3625
- screenSceneRef.current = screenScene;
3626
- screenCameraRef.current = screenCamera;
3627
- screenQuadRef.current = screenQuad;
3628
- bufRequest.current = new Uint8Array(width * height * 4);
3629
- bufUsage.current = new Uint8Array(width * height * 4);
3630
- mrtRef.current = mrt;
3631
- return () => {
3632
- mrt.dispose();
3633
- screenMaterial.dispose();
3634
- screenQuad.geometry.dispose();
3635
- };
3636
- }, [gl]);
3637
- const firstImageLayerScope = imageLayerScopes?.[0];
3638
- const firstImage = images?.[firstImageLayerScope];
3639
- imageChannelScopesByLayer?.[firstImageLayerScope];
3640
- const firstImageLayerChannelCoordination = imageChannelCoordination?.[0]?.[firstImageLayerScope];
3641
- useEffect(() => {
3642
- logWithColor("useEffect INIT");
3643
- let hasRerun = false;
3644
- if (!dataManager || !renderManager) {
3645
- log.debug("dataManager or renderManager not initialized yet");
3646
- return;
3647
- }
3648
- if (!firstImage) {
3649
- log.debug("no first image layer yet");
3650
- return;
3651
- }
3652
- if (!firstImageLayerChannelCoordination) {
3653
- log.debug("no firstImageLayerChannelCoordination yet");
3654
- return;
3655
- }
3656
- const initializeDataManager = async () => {
3657
- if (dataManager.initStatus === INIT_STATUS.COMPLETE) {
3658
- log.debug("dataManager already initialized, skipping");
3659
- return;
3660
- }
3661
- dataManager.initImages(images, imageLayerScopes);
3662
- await dataManager.init(firstImageLayerChannelCoordination);
3663
- if (hasRerun) {
3664
- log.debug("Initialization useEffect has rerun, aborting remaining initialization");
3665
- return;
3666
- }
3667
- log.debug("dataManager initialized");
3668
- renderManager.setZarrUniforms(dataManager.zarrStore, dataManager.PT);
3669
- renderManager.setChannelMapping(dataManager.channels.colorMappings);
3670
- log.debug("rm.uniforms", renderManager.uniforms);
3671
- invalidate();
3672
- };
3673
- initializeDataManager();
3674
- return () => {
3675
- hasRerun = true;
3676
- };
3677
- }, [dataManager, renderManager, images, imageLayerScopes]);
3678
- useEffect(() => {
3679
- const on3D = spatialRenderingMode === "3D";
3680
- if (on3D && dataManager && renderManager) {
3681
- logWithColor("useEffect spatialRenderingMode");
3682
- const propsForRenderManager = {
3683
- images,
3684
- imageLayerScopes,
3685
- imageLayerCoordination,
3686
- imageChannelScopesByLayer,
3687
- imageChannelCoordination,
3688
- spatialRenderingMode
3689
- };
3690
- if (renderManager.updateFromProps(propsForRenderManager)) {
3691
- const { zarrInit } = renderManager;
3692
- if (!zarrInit) {
3693
- dataManager.ptTHREE.needsUpdate = false;
3694
- dataManager.bcTHREE.needsUpdate = false;
3695
- dataManager.renderer.initTexture(dataManager.bcTHREE);
3696
- dataManager.renderer.initTexture(dataManager.ptTHREE);
3697
- dataManager.initTexture();
3698
- }
3699
- const nextRenderState = renderManager.updateRendering({
3700
- zarrStoreShapes: dataManager.zarrStore.shapes,
3701
- originalScaleXYZ: dataManager.getOriginalScaleXYZ(),
3702
- physicalDimensionsXYZ: dataManager.getPhysicalDimensionsXYZ(),
3703
- maxResolutionXYZ: dataManager.getMaxResolutionXYZ(),
3704
- boxDimensionsXYZ: dataManager.getBoxDimensionsXYZ(),
3705
- normalizedScaleXYZ: dataManager.getNormalizedScaleXYZ(),
3706
- bcTHREE: dataManager.bcTHREE,
3707
- ptTHREE: dataManager.ptTHREE
3708
- });
3709
- if (nextRenderState) {
3710
- setRenderState(nextRenderState);
3711
- }
3712
- }
3713
- }
3714
- }, [dataManager, renderManager, images, imageLayerScopes, imageLayerCoordination, imageChannelScopesByLayer, imageChannelCoordination, spatialRenderingMode]);
3715
- const RENDER_PRIORITY = 1;
3716
- useFrame((state, delta, xrFrame) => {
3717
- if (!mrtRef.current || !dataManager || !renderManager) {
3718
- return;
3719
- }
3720
- if (!renderState.shader) {
3721
- return;
3722
- }
3723
- const { gl: frameGl, camera: frameCamera, scene: frameScene, clock } = state;
3724
- if (!stillRef.current) {
3725
- performGeometryPass(frameGl, frameCamera, frameScene, { mrtRef });
3726
- }
3727
- performBlitPass(frameGl, { screenSceneRef, screenCameraRef });
3728
- handleRequests(frameGl, { frameRef, dataManager, mrtRef, bufRequest, bufUsage });
3729
- handleAdaptiveQuality(clock, {
3730
- invalidate,
3731
- isInteracting,
3732
- dataManager,
3733
- spatialRenderingModeChanging,
3734
- meshRef,
3735
- stillRef,
3736
- screenQuadRef,
3737
- lastSampleRef,
3738
- frameRef,
3739
- lastFrameCountRef
3740
- });
3741
- frameRef.current += 1;
3742
- }, RENDER_PRIORITY);
3743
- const setIsInteracting = useEventCallback((interacting) => {
3744
- logWithColor("invalidateOnInteraction callback");
3745
- _setIsInteracting(interacting);
3746
- if (interacting) {
3747
- const meshRefUniforms = meshRef.current?.material?.uniforms;
3748
- if (meshRefUniforms) {
3749
- meshRefUniforms.renderRes.value = dataManager.PT.lowestDataRes;
3750
- }
3751
- stillRef.current = false;
3752
- invalidate();
3753
- }
3754
- });
3755
- const onOrbitControlsStart = useEventCallback((e) => {
3756
- setIsInteracting(true);
3757
- });
3758
- const onOrbitControlsEnd = useEventCallback((e) => {
3759
- clearTimeout(interactionTimeoutRef.current);
3760
- interactionTimeoutRef.current = setTimeout(() => {
3761
- setIsInteracting(false);
3762
- }, 300);
3763
- });
3764
- useEffect(() => {
3765
- logWithColor("useEffect firstImageLayerChannelCoordination");
3766
- setIsInteracting(true);
3767
- log.debug("something about channels changed");
3768
- dataManager.updateChannels(firstImageLayerChannelCoordination);
3769
- renderManager.setChannelMapping(dataManager.channels.colorMappings);
3770
- clearTimeout(interactionTimeoutRef.current);
3771
- interactionTimeoutRef.current = setTimeout(() => {
3772
- setIsInteracting(false);
3773
- }, 300);
3774
- }, [dataManager, firstImageLayerChannelCoordination, renderManager, setIsInteracting]);
3775
- const stopLoading = useEventCallback(() => {
3776
- if (dataManager) {
3777
- dataManager.stopLoading();
3778
- }
3779
- });
3780
- const restartLoading = useEventCallback(() => {
3781
- if (dataManager) {
3782
- dataManager.restartLoading();
3783
- }
3784
- });
3785
- const getLoadingProgress = useEventCallback(() => {
3786
- if (dataManager) {
3787
- return dataManager.getLoadingProgress();
3788
- }
3789
- return null;
3790
- });
3791
- useEffect(() => {
3792
- if (!onVolumeLoadingUpdate)
3793
- return void 0;
3794
- const intervalId = setInterval(() => {
3795
- const loadingProgress = getLoadingProgress();
3796
- onVolumeLoadingUpdate({
3797
- loadingProgress,
3798
- stillRef,
3799
- onStopLoading: stopLoading,
3800
- onRestartLoading: restartLoading
3801
- });
3802
- }, 1e3);
3803
- return () => clearInterval(intervalId);
3804
- }, [onVolumeLoadingUpdate, stillRef]);
3805
- if (!is3D || !dataManager || !renderManager)
3806
- return null;
3807
- if (!renderState.shader) {
3808
- return jsxs("group", { children: [jsxs("mesh", { children: [jsx("boxGeometry", { args: [1, 1, 1] }), jsx("meshBasicMaterial", { color: "#444", wireframe: true })] }), jsx(OrbitControls, { ref: orbitRef })] });
3809
- }
3810
- return jsxs("group", { children: [jsx(
3811
- OrbitControls,
3812
- {
3813
- // ref={mainOrbitControlsRef}
3814
- enableDamping: false,
3815
- onStart: onOrbitControlsStart,
3816
- onEnd: onOrbitControlsEnd
3817
- }
3818
- ), jsxs("mesh", { ref: meshRef, scale: renderState.meshScale, children: [jsx("boxGeometry", { args: renderState.geometrySize }), jsx("shaderMaterial", { uniforms: renderState.uniforms, vertexShader: renderState.shader.vertexShader, fragmentShader: renderState.shader.fragmentShader, side: THREE.BackSide, transparent: false, glslVersion: THREE.GLSL3 })] })] });
3819
- }
3820
- function SpatialWrapper(props) {
3821
- return jsx(Canvas, { frameloop: "always", style: {
3822
- position: "absolute",
3823
- top: 0,
3824
- left: 0,
3825
- width: "100%",
3826
- height: "100%",
3827
- padding: 0,
3828
- margin: 0
3829
- // backgroundColor: 'white',
3830
- }, camera: {
3831
- fov: 50,
3832
- up: [0, 1, 0],
3833
- position: [0, 0, 4],
3834
- near: 0.01,
3835
- far: 15
3836
- }, gl: {
3837
- antialias: true,
3838
- logarithmicDepthBuffer: false,
3839
- preserveDrawingBuffer: false,
3840
- autoClear: false
3841
- }, children: jsx(Suspense, { fallback: "Initializing Volume View...", children: jsx(VolumeView, { ...props }) }) });
3842
- }
3843
- export {
3844
- SpatialWrapper
3845
- };