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