Skip to content

Commit

Permalink
Merge branch 'master' into webgpu_encoder_checks
Browse files Browse the repository at this point in the history
  • Loading branch information
martinken authored May 24, 2021
2 parents 32434b8 + 6ec5fbc commit f31ab00
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 29 deletions.
8 changes: 8 additions & 0 deletions Examples/Volume/VolumeMapperBlendModes/controller.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@
</select>
</td>
</tr>
<tr class="averageIPScalar" style="display:none;">
<td>Average IP Scalar Min</td>
<td><input class="scalarMin" type="range" min="0" max="1" value="0" step="0.01"></td>
</tr>
<tr class="averageIPScalar" style="display:none;">
<td><p>Average IP Scalar Max</p></td>
<td><input class="scalarMax" type="range" min="0" max="1" value="1" step="0.01"></td>
</tr>
</table>
38 changes: 38 additions & 0 deletions Examples/Volume/VolumeMapperBlendModes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,45 @@ mapper.setInputConnection(reader.getOutputPort());

function updateBlendMode(event) {
const blendMode = parseInt(event.target.value, 10);
const averageIPScalarEls = document.querySelectorAll('.averageIPScalar');

mapper.setBlendMode(blendMode);
mapper.setAverageIPScalarRange(0.0, 1.0);

// if average blend mode
if (blendMode === 3) {
// Show average scalar ui
for (let i = 0; i < averageIPScalarEls.length; i += 1) {
const el = averageIPScalarEls[i];
el.style.display = 'table-row';
}
} else {
// Hide average scalar ui
for (let i = 0; i < averageIPScalarEls.length; i += 1) {
const el = averageIPScalarEls[i];
el.style.display = 'none';
}
}

renderWindow.render();
}

function updateScalarMin(event) {
mapper.setAverageIPScalarRange(
event.target.value,
parseFloat(mapper.getAverageIPScalarRange()[1])
);
renderWindow.render();
}

function updateScalarMax(event) {
mapper.setAverageIPScalarRange(
mapper.getAverageIPScalarRange()[0],
parseFloat(event.target.value)
);
renderWindow.render();
}

reader.setUrl(`${__BASE_PATH__}/data/volume/headsq.vti`).then(() => {
reader.loadData().then(() => {
renderer.addVolume(actor);
Expand All @@ -85,6 +119,10 @@ reader.setUrl(`${__BASE_PATH__}/data/volume/headsq.vti`).then(() => {

const el = document.querySelector('.blendMode');
el.addEventListener('change', updateBlendMode);
const scalarMinEl = document.querySelector('.scalarMin');
scalarMinEl.addEventListener('input', updateScalarMin);
const scalarMaxEl = document.querySelector('.scalarMax');
scalarMaxEl.addEventListener('input', updateScalarMax);
});
});

Expand Down
12 changes: 9 additions & 3 deletions Sources/Rendering/OpenGL/VolumeMapper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,18 +232,24 @@ function vtkOpenGLVolumeMapper(publicAPI, model) {
).result;

const averageIPScalarRange = model.renderable.getAverageIPScalarRange();
let min = averageIPScalarRange[0];
let max = averageIPScalarRange[1];

// If min or max is not already a float.
// make them into floats for glsl
min = Number.isInteger(min) ? min.toFixed(1).toString() : min.toString();
max = Number.isInteger(max) ? max.toFixed(1).toString() : max.toString();

// TODO: Adding the .0 at the end feels hacky
FSSource = vtkShaderProgram.substitute(
FSSource,
'//VTK::AverageIPScalarRangeMin',
`${averageIPScalarRange[0]}.0`
min
).result;

FSSource = vtkShaderProgram.substitute(
FSSource,
'//VTK::AverageIPScalarRangeMax',
`${averageIPScalarRange[1]}.0`
max
).result;

shaders.Fragment = FSSource;
Expand Down
54 changes: 28 additions & 26 deletions Sources/Rendering/OpenGL/glsl/vtkVolumeFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,27 @@ vec4 getColorForValue(vec4 tValue, vec3 posIS, vec3 tstep)
return tColor;
}


bool valueWithinScalarRange(vec4 val, vec4 min, vec4 max) {
bool withinRange = false;
#if vtkNumComponents == 1
if (val.r >= min.r && val.r <= max.r) {
withinRange = true;
}
#endif
#if defined(vtkIndependentComponentsOn) && vtkNumComponents == 2
if (val.r >= min.r && val.r <= max.r &&
val.g >= min.g && val.g <= max.g) {
withinRange = true;
}
#endif
#if defined(vtkIndependentComponentsOn) && vtkNumComponents >= 3
if (all(greaterThanEqual(val, averageIPScalarRangeMin)) &&
all(lessThanEqual(val, averageIPScalarRangeMax))) {
withinRange = true;
}
#endif
return withinRange;
}

//=======================================================================
// Apply the specified blend mode operation along the ray's path.
Expand Down Expand Up @@ -779,20 +799,16 @@ void applyBlend(vec3 posIS, vec3 endIS, float sampleDistanceIS, vec3 tdims)
//VTK::AverageIPScalarRangeMin,
//VTK::AverageIPScalarRangeMin,
//VTK::AverageIPScalarRangeMin,
1.0);
//VTK::AverageIPScalarRangeMax);
vec4 averageIPScalarRangeMax = vec4(
//VTK::AverageIPScalarRangeMax,
//VTK::AverageIPScalarRangeMax,
//VTK::AverageIPScalarRangeMax,
1.0);
//VTK::AverageIPScalarRangeMax);

vec4 sum = vec4(0.);

averageIPScalarRangeMin.a = tValue.a;
averageIPScalarRangeMax.a = tValue.a;

if (all(greaterThanEqual(tValue, averageIPScalarRangeMin)) &&
all(lessThanEqual(tValue, averageIPScalarRangeMax))) {
if (valueWithinScalarRange(tValue, averageIPScalarRangeMin, averageIPScalarRangeMax)) {
sum += tValue;
}

Expand All @@ -818,23 +834,10 @@ void applyBlend(vec3 posIS, vec3 endIS, float sampleDistanceIS, vec3 tdims)
// - We are comparing all values in the texture to see if any of them
// are outside of the scalar range. In the future we might want to allow
// scalar ranges for each component.
// - We are setting the alpha channel for averageIPScalarRangeMin and
// averageIPScalarRangeMax so that we do not trigger this 'continue'
// based on the alpha channel comparison.
// - There might be a better way to do this. I'm not sure if there is an
// equivalent of 'any' which only operates on RGB, though I suppose
// we could write an 'anyRGB' function and see if that is faster.
averageIPScalarRangeMin.a = tValue.a;
averageIPScalarRangeMax.a = tValue.a;
if (any(lessThan(tValue, averageIPScalarRangeMin)) ||
any(greaterThan(tValue, averageIPScalarRangeMax))) {
continue;
if (valueWithinScalarRange(tValue, averageIPScalarRangeMin, averageIPScalarRangeMax)) {
// Sum the values across each step in the path
sum += tValue;
}

// Sum the values across each step in the path
sum += tValue;

// Otherwise, continue along the ray
stepsTraveled++;
posIS += stepIS;
}
Expand All @@ -847,8 +850,7 @@ void applyBlend(vec3 posIS, vec3 endIS, float sampleDistanceIS, vec3 tdims)
tValue = getTextureValue(posIS);

// One can control the scalar range by setting the AverageIPScalarRange to disregard scalar values, not in the range of interest, from the average computation
if (all(greaterThanEqual(tValue, averageIPScalarRangeMin)) &&
all(lessThanEqual(tValue, averageIPScalarRangeMax))) {
if (valueWithinScalarRange(tValue, averageIPScalarRangeMin, averageIPScalarRangeMax)) {
sum += tValue;

stepsTraveled++;
Expand Down

0 comments on commit f31ab00

Please sign in to comment.