Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sphere mapper picking support #2729

Merged
merged 1 commit into from Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions Sources/Rendering/Core/SphereMapper/example/controlPanel.html
Expand Up @@ -17,4 +17,10 @@
<input class='scaleFactor' type="range" min="0.1" max="5" step="0.1" value="1.0" />
</td>
</tr>
<tr>
<td>Picking Attribute ID</td>
<td>
<div id='attributeID'></div>
</td>
</tr>
</table>
46 changes: 45 additions & 1 deletion Sources/Rendering/Core/SphereMapper/example/index.js
Expand Up @@ -11,7 +11,12 @@ import vtkPlaneSource from '@kitware/vtk.js/Filters/Sources/PlaneSource';
import vtkSphereMapper from '@kitware/vtk.js/Rendering/Core/SphereMapper';

import { AttributeTypes } from '@kitware/vtk.js/Common/DataModel/DataSetAttributes/Constants';
import { FieldDataTypes } from '@kitware/vtk.js/Common/DataModel/DataSet/Constants';
import {
FieldDataTypes,
FieldAssociations,
} from '@kitware/vtk.js/Common/DataModel/DataSet/Constants';

import { throttle } from '@kitware/vtk.js/macros';

import controlPanel from './controlPanel.html';

Expand Down Expand Up @@ -87,11 +92,50 @@ renderer.addActor(actor);
renderer.resetCamera();
renderWindow.render();

const interactor = renderWindow.getInteractor();
const apiSpecificRenderWindow = interactor.getView();
const hwSelector = apiSpecificRenderWindow.getSelector();
hwSelector.setCaptureZValues(true);
hwSelector.setFieldAssociation(FieldAssociations.FIELD_ASSOCIATION_CELLS);

function eventToWindowXY(event) {
// We know we are full screen => window.innerXXX
// Otherwise we can use pixel device ratio or else...
const { clientX, clientY } = event;
const [width, height] = apiSpecificRenderWindow.getSize();
const x = Math.round((width * clientX) / window.innerWidth);
const y = Math.round(height * (1 - clientY / window.innerHeight)); // Need to flip Y
return [x, y];
finetjul marked this conversation as resolved.
Show resolved Hide resolved
}

// -----------------------------------------------------------
// UI control handling
// -----------------------------------------------------------

fullScreenRenderer.addController(controlPanel);
const attributeIdDiv = document.querySelector('#attributeID');

function pickOnMouseEvent(event) {
if (interactor.isAnimating()) {
// We should not do picking when interacting with the scene
return;
}
const [x, y] = eventToWindowXY(event);

hwSelector.getSourceDataAsync(renderer, x, y, x, y).then((result) => {
const selections = result?.generateSelection(x, y, x, y);
if (selections && selections.length >= 1) {
const selection = selections[0];
const props = selection.get().properties;
attributeIdDiv.textContent = props.attributeID;
} else {
attributeIdDiv.textContent = '';
}
});
}

document.addEventListener('mousemove', throttle(pickOnMouseEvent, 20));

['xResolution', 'yResolution'].forEach((propertyName) => {
document.querySelector(`.${propertyName}`).addEventListener('input', (e) => {
const value = Number(e.target.value);
Expand Down
4 changes: 4 additions & 0 deletions Sources/Rendering/OpenGL/glsl/vtkSphereMapperVS.glsl
Expand Up @@ -22,6 +22,8 @@ attribute vec2 offsetMC;
// optional normal declaration
//VTK::Normal::Dec

//VTK::Picking::Dec

// Texture coordinates
//VTK::TCoord::Dec

Expand All @@ -45,6 +47,8 @@ uniform float scaleFactor;

void main()
{
//VTK::Picking::Impl

//VTK::Color::Impl

//VTK::Normal::Impl
Expand Down