Skip to content

Commit 132be4b

Browse files
committed
fix: Fix update of color and opacity textures
BREAKING CHANGE: getTransferFunctionHash becomes getTransferFunctionsHash It takes a list of functions as argument instead of a single function Also ImageResliceMapper doesn't wieght color using mix.
1 parent 14649a2 commit 132be4b

File tree

6 files changed

+151
-93
lines changed

6 files changed

+151
-93
lines changed

Sources/Rendering/OpenGL/ImageCPRMapper/index.js

+34-20
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import vtkShaderProgram from 'vtk.js/Sources/Rendering/OpenGL/ShaderProgram';
1313
import vtkViewNode from 'vtk.js/Sources/Rendering/SceneGraph/ViewNode';
1414

1515
import {
16-
getTransferFunctionHash,
16+
getTransferFunctionsHash,
1717
getImageDataHash,
1818
} from 'vtk.js/Sources/Rendering/OpenGL/RenderWindow/resourceSharingHelper';
1919

@@ -243,15 +243,21 @@ function vtkOpenGLImageCPRMapper(publicAPI, model) {
243243
const numIComps = iComps ? numComp : 1;
244244
const textureHeight = iComps ? 2 * numIComps : 1;
245245

246-
const colorTransferFunc = ppty.getRGBTransferFunction();
247-
const colorTextureHash = getTransferFunctionHash(
248-
colorTransferFunc,
246+
const colorTransferFunctions = [];
247+
for (let component = 0; component < numIComps; ++component) {
248+
colorTransferFunctions.push(ppty.getRGBTransferFunction(component));
249+
}
250+
const colorTextureHash = getTransferFunctionsHash(
251+
colorTransferFunctions,
249252
iComps,
250253
numIComps
251254
);
252255

256+
const firstColorTransferFunc = ppty.getRGBTransferFunction();
253257
const cachedColorEntry =
254-
model._openGLRenderWindow.getGraphicsResourceForObject(colorTransferFunc);
258+
model._openGLRenderWindow.getGraphicsResourceForObject(
259+
firstColorTransferFunc
260+
);
255261
const reBuildColorTexture =
256262
!cachedColorEntry?.oglObject?.getHandle() ||
257263
cachedColorEntry?.hash !== colorTextureHash;
@@ -261,7 +267,7 @@ function vtkOpenGLImageCPRMapper(publicAPI, model) {
261267
const cTable = new Uint8ClampedArray(cSize);
262268
model.colorTexture = vtkOpenGLTexture.newInstance();
263269
model.colorTexture.setOpenGLRenderWindow(model._openGLRenderWindow);
264-
if (colorTransferFunc) {
270+
if (firstColorTransferFunc) {
265271
const tmpTable = new Float32Array(cWidth * 3);
266272

267273
for (let c = 0; c < numIComps; c++) {
@@ -303,23 +309,23 @@ function vtkOpenGLImageCPRMapper(publicAPI, model) {
303309
);
304310
}
305311

306-
if (colorTransferFunc) {
312+
if (firstColorTransferFunc) {
307313
model._openGLRenderWindow.setGraphicsResourceForObject(
308-
colorTransferFunc,
314+
firstColorTransferFunc,
309315
model.colorTexture,
310316
colorTextureHash
311317
);
312-
if (colorTransferFunc !== model._colorTransferFunc) {
318+
if (firstColorTransferFunc !== model._colorTransferFunc) {
313319
model._openGLRenderWindow.registerGraphicsResourceUser(
314-
colorTransferFunc,
320+
firstColorTransferFunc,
315321
publicAPI
316322
);
317323
model._openGLRenderWindow.unregisterGraphicsResourceUser(
318324
model._colorTransferFunc,
319325
publicAPI
320326
);
321327
}
322-
model._colorTransferFunc = colorTransferFunc;
328+
model._colorTransferFunc = firstColorTransferFunc;
323329
}
324330
} else {
325331
model.colorTexture = cachedColorEntry.oglObject;
@@ -328,10 +334,18 @@ function vtkOpenGLImageCPRMapper(publicAPI, model) {
328334
// Build piecewise function buffer. This buffer is used either
329335
// for component weighting or opacity, depending on whether we're
330336
// rendering components independently or not.
331-
const pwFunc = ppty.getPiecewiseFunction();
332-
const pwfTextureHash = getTransferFunctionHash(pwFunc, iComps, numIComps);
337+
const opacityFunctions = [];
338+
for (let component = 0; component < numIComps; ++component) {
339+
opacityFunctions.push(ppty.getPiecewiseFunction(component));
340+
}
341+
const pwfTextureHash = getTransferFunctionsHash(
342+
opacityFunctions,
343+
iComps,
344+
numIComps
345+
);
346+
const firstPwFunc = ppty.getPiecewiseFunction();
333347
const cachedPwfEntry =
334-
model._openGLRenderWindow.getGraphicsResourceForObject(pwFunc);
348+
model._openGLRenderWindow.getGraphicsResourceForObject(firstPwFunc);
335349
const reBuildPwf =
336350
!cachedPwfEntry?.oglObject?.getHandle() ||
337351
cachedPwfEntry?.hash !== pwfTextureHash;
@@ -341,7 +355,7 @@ function vtkOpenGLImageCPRMapper(publicAPI, model) {
341355
const pwfTable = new Uint8ClampedArray(pwfSize);
342356
model.pwfTexture = vtkOpenGLTexture.newInstance();
343357
model.pwfTexture.setOpenGLRenderWindow(model._openGLRenderWindow);
344-
if (pwFunc) {
358+
if (firstPwFunc) {
345359
const pwfFloatTable = new Float32Array(pwfSize);
346360
const tmpTable = new Float32Array(pwfWidth);
347361

@@ -386,23 +400,23 @@ function vtkOpenGLImageCPRMapper(publicAPI, model) {
386400
pwfTable
387401
);
388402
}
389-
if (pwFunc) {
403+
if (firstPwFunc) {
390404
model._openGLRenderWindow.setGraphicsResourceForObject(
391-
pwFunc,
405+
firstPwFunc,
392406
model.pwfTexture,
393407
pwfTextureHash
394408
);
395-
if (pwFunc !== model._pwFunc) {
409+
if (firstPwFunc !== model._pwFunc) {
396410
model._openGLRenderWindow.registerGraphicsResourceUser(
397-
pwFunc,
411+
firstPwFunc,
398412
publicAPI
399413
);
400414
model._openGLRenderWindow.unregisterGraphicsResourceUser(
401415
model._pwFunc,
402416
publicAPI
403417
);
404418
}
405-
model._pwFunc = pwFunc;
419+
model._pwFunc = firstPwFunc;
406420
}
407421
} else {
408422
model.pwfTexture = cachedPwfEntry.oglObject;

Sources/Rendering/OpenGL/ImageMapper/index.js

+36-21
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from 'vtk.js/Sources/Rendering/OpenGL/Texture/Constants';
1515
import { InterpolationType } from 'vtk.js/Sources/Rendering/Core/ImageProperty/Constants';
1616

17-
import { getTransferFunctionHash } from 'vtk.js/Sources/Rendering/OpenGL/RenderWindow/resourceSharingHelper';
17+
import { getTransferFunctionsHash } from 'vtk.js/Sources/Rendering/OpenGL/RenderWindow/resourceSharingHelper';
1818

1919
import vtkPolyDataVS from 'vtk.js/Sources/Rendering/OpenGL/glsl/vtkPolyDataVS.glsl';
2020
import vtkPolyDataFS from 'vtk.js/Sources/Rendering/OpenGL/glsl/vtkPolyDataFS.glsl';
@@ -931,14 +931,21 @@ function vtkOpenGLImageMapper(publicAPI, model) {
931931
const numIComps = iComps ? numComp : 1;
932932
const textureHeight = iComps ? 2 * numIComps : 1;
933933

934-
const colorTransferFunc = actorProperty.getRGBTransferFunction();
935-
const cfunToString = getTransferFunctionHash(
936-
colorTransferFunc,
934+
const colorTransferFunctions = [];
935+
for (let component = 0; component < numIComps; ++component) {
936+
colorTransferFunctions.push(
937+
actorProperty.getRGBTransferFunction(component)
938+
);
939+
}
940+
const cfunToString = getTransferFunctionsHash(
941+
colorTransferFunctions,
937942
iComps,
938943
numIComps
939944
);
940-
const cTex =
941-
model._openGLRenderWindow.getGraphicsResourceForObject(colorTransferFunc);
945+
const firstColorTransferFunc = actorProperty.getRGBTransferFunction();
946+
const cTex = model._openGLRenderWindow.getGraphicsResourceForObject(
947+
firstColorTransferFunc
948+
);
942949

943950
const reBuildC =
944951
!cTex?.oglObject?.getHandle() || cTex?.hash !== cfunToString;
@@ -959,7 +966,7 @@ function vtkOpenGLImageMapper(publicAPI, model) {
959966
model.colorTexture.setMagnificationFilter(Filter.LINEAR);
960967
}
961968

962-
if (colorTransferFunc) {
969+
if (firstColorTransferFunc) {
963970
const tmpTable = new Float32Array(cWidth * 3);
964971

965972
for (let c = 0; c < numIComps; c++) {
@@ -1000,23 +1007,23 @@ function vtkOpenGLImageMapper(publicAPI, model) {
10001007
);
10011008
}
10021009

1003-
if (colorTransferFunc) {
1010+
if (firstColorTransferFunc) {
10041011
model._openGLRenderWindow.setGraphicsResourceForObject(
1005-
colorTransferFunc,
1012+
firstColorTransferFunc,
10061013
model.colorTexture,
10071014
cfunToString
10081015
);
1009-
if (colorTransferFunc !== model._colorTransferFunc) {
1016+
if (firstColorTransferFunc !== model._colorTransferFunc) {
10101017
model._openGLRenderWindow.registerGraphicsResourceUser(
1011-
colorTransferFunc,
1018+
firstColorTransferFunc,
10121019
publicAPI
10131020
);
10141021
model._openGLRenderWindow.unregisterGraphicsResourceUser(
10151022
model._colorTransferFunc,
10161023
publicAPI
10171024
);
10181025
}
1019-
model._colorTransferFunc = colorTransferFunc;
1026+
model._colorTransferFunc = firstColorTransferFunc;
10201027
}
10211028
} else {
10221029
model.colorTexture = cTex.oglObject;
@@ -1025,10 +1032,18 @@ function vtkOpenGLImageMapper(publicAPI, model) {
10251032
// Build piecewise function buffer. This buffer is used either
10261033
// for component weighting or opacity, depending on whether we're
10271034
// rendering components independently or not.
1028-
const pwFunc = actorProperty.getPiecewiseFunction();
1029-
const pwfunToString = getTransferFunctionHash(pwFunc, iComps, numIComps);
1035+
const opacityFunctions = [];
1036+
for (let component = 0; component < numIComps; ++component) {
1037+
opacityFunctions.push(actorProperty.getPiecewiseFunction(component));
1038+
}
1039+
const pwfunToString = getTransferFunctionsHash(
1040+
opacityFunctions,
1041+
iComps,
1042+
numIComps
1043+
);
1044+
const firstPwFunc = actorProperty.getPiecewiseFunction();
10301045
const pwfTex =
1031-
model._openGLRenderWindow.getGraphicsResourceForObject(pwFunc);
1046+
model._openGLRenderWindow.getGraphicsResourceForObject(firstPwFunc);
10321047
// rebuild opacity tfun?
10331048
const reBuildPwf =
10341049
!pwfTex?.oglObject?.getHandle() || pwfTex?.hash !== pwfunToString;
@@ -1049,7 +1064,7 @@ function vtkOpenGLImageMapper(publicAPI, model) {
10491064
model.pwfTexture.setMagnificationFilter(Filter.LINEAR);
10501065
}
10511066

1052-
if (pwFunc) {
1067+
if (firstPwFunc) {
10531068
const pwfFloatTable = new Float32Array(pwfSize);
10541069
const tmpTable = new Float32Array(pwfWidth);
10551070

@@ -1094,23 +1109,23 @@ function vtkOpenGLImageMapper(publicAPI, model) {
10941109
);
10951110
}
10961111

1097-
if (pwFunc) {
1112+
if (firstPwFunc) {
10981113
model._openGLRenderWindow.setGraphicsResourceForObject(
1099-
pwFunc,
1114+
firstPwFunc,
11001115
model.pwfTexture,
11011116
pwfunToString
11021117
);
1103-
if (pwFunc !== model._pwFunc) {
1118+
if (firstPwFunc !== model._pwFunc) {
11041119
model._openGLRenderWindow.registerGraphicsResourceUser(
1105-
pwFunc,
1120+
firstPwFunc,
11061121
publicAPI
11071122
);
11081123
model._openGLRenderWindow.unregisterGraphicsResourceUser(
11091124
model._pwFunc,
11101125
publicAPI
11111126
);
11121127
}
1113-
model._pwFunc = pwFunc;
1128+
model._pwFunc = firstPwFunc;
11141129
}
11151130
} else {
11161131
model.pwfTexture = pwfTex.oglObject;

Sources/Rendering/OpenGL/ImageResliceMapper/index.js

+35-20
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import vtkTransform from 'vtk.js/Sources/Common/Transform/Transform';
1717
import vtkViewNode from 'vtk.js/Sources/Rendering/SceneGraph/ViewNode';
1818

1919
import {
20-
getTransferFunctionHash,
20+
getTransferFunctionsHash,
2121
getImageDataHash,
2222
} from 'vtk.js/Sources/Rendering/OpenGL/RenderWindow/resourceSharingHelper';
2323

@@ -341,14 +341,21 @@ function vtkOpenGLImageResliceMapper(publicAPI, model) {
341341
const numIComps = iComps ? model.numberOfComponents : 1;
342342
const textureHeight = iComps ? 2 * numIComps : 1;
343343

344-
const colorTransferFunc = firstActorProperty.getRGBTransferFunction();
345-
const colorFuncHash = getTransferFunctionHash(
346-
colorTransferFunc,
344+
const colorTransferFunctions = [];
345+
for (let component = 0; component < numIComps; ++component) {
346+
colorTransferFunctions.push(
347+
firstActorProperty.getRGBTransferFunction(component)
348+
);
349+
}
350+
const colorFuncHash = getTransferFunctionsHash(
351+
colorTransferFunctions,
347352
iComps,
348353
numIComps
349354
);
350-
const cTex =
351-
model._openGLRenderWindow.getGraphicsResourceForObject(colorTransferFunc);
355+
const firstColorTransferFunc = firstActorProperty.getRGBTransferFunction();
356+
const cTex = model._openGLRenderWindow.getGraphicsResourceForObject(
357+
firstColorTransferFunc
358+
);
352359
const reBuildC =
353360
!cTex?.oglObject?.getHandle() || cTex?.hash !== colorFuncHash;
354361
if (reBuildC) {
@@ -357,7 +364,7 @@ function vtkOpenGLImageResliceMapper(publicAPI, model) {
357364
const cTable = new Uint8ClampedArray(cSize);
358365
const newColorTexture = vtkOpenGLTexture.newInstance();
359366
newColorTexture.setOpenGLRenderWindow(model._openGLRenderWindow);
360-
if (colorTransferFunc) {
367+
if (firstColorTransferFunc) {
361368
const tmpTable = new Float32Array(cWidth * 3);
362369

363370
for (let c = 0; c < numIComps; c++) {
@@ -403,9 +410,9 @@ function vtkOpenGLImageResliceMapper(publicAPI, model) {
403410
);
404411
}
405412

406-
if (colorTransferFunc) {
413+
if (firstColorTransferFunc) {
407414
model._openGLRenderWindow.setGraphicsResourceForObject(
408-
colorTransferFunc,
415+
firstColorTransferFunc,
409416
newColorTexture,
410417
colorFuncHash
411418
);
@@ -417,17 +424,25 @@ function vtkOpenGLImageResliceMapper(publicAPI, model) {
417424
replaceGraphicsResource(
418425
model._openGLRenderWindow,
419426
model._colorTextureCore,
420-
colorTransferFunc
427+
firstColorTransferFunc
421428
);
422-
model._colorTextureCore = colorTransferFunc;
429+
model._colorTextureCore = firstColorTransferFunc;
423430

424431
// Build piecewise function buffer. This buffer is used either
425432
// for component weighting or opacity, depending on whether we're
426433
// rendering components independently or not.
427-
const pwFunc = firstActorProperty.getPiecewiseFunction();
428-
const opacityFuncHash = getTransferFunctionHash(pwFunc, iComps, numIComps);
434+
const opacityFunctions = [];
435+
for (let component = 0; component < numIComps; ++component) {
436+
opacityFunctions.push(firstActorProperty.getPiecewiseFunction(component));
437+
}
438+
const opacityFuncHash = getTransferFunctionsHash(
439+
opacityFunctions,
440+
iComps,
441+
numIComps
442+
);
443+
const firstPwFunc = firstActorProperty.getPiecewiseFunction();
429444
const pwfTex =
430-
model._openGLRenderWindow.getGraphicsResourceForObject(pwFunc);
445+
model._openGLRenderWindow.getGraphicsResourceForObject(firstPwFunc);
431446
const reBuildPwf =
432447
!pwfTex?.oglObject?.getHandle() || pwfTex?.hash !== opacityFuncHash;
433448
if (reBuildPwf) {
@@ -436,7 +451,7 @@ function vtkOpenGLImageResliceMapper(publicAPI, model) {
436451
const pwfTable = new Uint8ClampedArray(pwfSize);
437452
const newOpacityTexture = vtkOpenGLTexture.newInstance();
438453
newOpacityTexture.setOpenGLRenderWindow(model._openGLRenderWindow);
439-
if (pwFunc) {
454+
if (firstPwFunc) {
440455
const pwfFloatTable = new Float32Array(pwfSize);
441456
const tmpTable = new Float32Array(pwfWidth);
442457

@@ -481,9 +496,9 @@ function vtkOpenGLImageResliceMapper(publicAPI, model) {
481496
pwfTable
482497
);
483498
}
484-
if (pwFunc) {
499+
if (firstPwFunc) {
485500
model._openGLRenderWindow.setGraphicsResourceForObject(
486-
pwFunc,
501+
firstPwFunc,
487502
newOpacityTexture,
488503
opacityFuncHash
489504
);
@@ -495,9 +510,9 @@ function vtkOpenGLImageResliceMapper(publicAPI, model) {
495510
replaceGraphicsResource(
496511
model._openGLRenderWindow,
497512
model._pwfTextureCore,
498-
pwFunc
513+
firstPwFunc
499514
);
500-
model._pwfTextureCore = pwFunc;
515+
model._pwfTextureCore = firstPwFunc;
501516

502517
const vboString = `${model.resliceGeom.getMTime()}A${model.renderable.getSlabThickness()}`;
503518
if (
@@ -1098,7 +1113,7 @@ function vtkOpenGLImageResliceMapper(publicAPI, model) {
10981113
const rgba = ['r', 'g', 'b', 'a'];
10991114
for (let comp = 0; comp < tNumComp; ++comp) {
11001115
tcoordFSImpl = tcoordFSImpl.concat([
1101-
`vec3 tcolor${comp} = mix${comp} * texture2D(colorTexture1, vec2(tvalue.${rgba[comp]} * cscale${comp} + cshift${comp}, height${comp})).rgb;`,
1116+
`vec3 tcolor${comp} = texture2D(colorTexture1, vec2(tvalue.${rgba[comp]} * cscale${comp} + cshift${comp}, height${comp})).rgb;`,
11021117
`float compWeight${comp} = mix${comp} * texture2D(pwfTexture1, vec2(tvalue.${rgba[comp]} * pwfscale${comp} + pwfshift${comp}, height${comp})).r;`,
11031118
]);
11041119
}

0 commit comments

Comments
 (0)