Skip to content

Commit

Permalink
feat(rw): wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bruyeret committed Apr 2, 2024
1 parent cd85bd8 commit cd81c23
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 120 deletions.
118 changes: 26 additions & 92 deletions Examples/Rendering/ManyRenderers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,45 +95,6 @@ rootContainer.style.top = 0;
rootContainer.style.pointerEvents = 'none';
document.body.appendChild(rootContainer);

renderWindowView.setContainer(rootContainer);

const interactor = vtkRenderWindowInteractor.newInstance();
interactor.setView(renderWindowView);
interactor.initialize();
interactor.setInteractorStyle(vtkInteractorStyleTrackballCamera.newInstance());

function updateViewPort(element, renderer) {
const { innerHeight, innerWidth } = window;
const { x, y, width, height } = element.getBoundingClientRect();
const viewport = [
x / innerWidth,
1 - (y + height) / innerHeight,
(x + width) / innerWidth,
1 - y / innerHeight,
];
renderer.setViewport(...viewport);
}

function recomputeViewports() {
const rendererElems = document.querySelectorAll('.renderer');
for (let i = 0; i < rendererElems.length; i++) {
const elem = rendererElems[i];
const { id } = elem;
const renderer = RENDERERS[id];
updateViewPort(elem, renderer);
}
renderWindow.render();
}

function resize() {
rootContainer.style.width = `${window.innerWidth}px`;
renderWindowView.setSize(window.innerWidth, window.innerHeight);
recomputeViewports();
}

new ResizeObserver(resize).observe(document.body);
document.addEventListener('scroll', recomputeViewports);

// ----------------------------------------------------------------------------
// Renderers
// ----------------------------------------------------------------------------
Expand All @@ -149,41 +110,14 @@ function applyStyle(element) {
element.style.height = '200px';
element.style.margin = '20px';
element.style.border = 'solid 1px #333';
element.style.display = 'inline-block';
element.style.display = 'inline-flex';
element.style['align-items'] = 'center';
element.style['flex-flow'] = 'column';
element.style.boxSizing = 'border';
element.style.textAlign = 'center';
element.style.color = 'white';
return element;
}

let captureCurrentRenderer = false;

function setCaptureCurrentRenderer(yn) {
captureCurrentRenderer = yn;
if (yn && interactor.getCurrentRenderer()) {
// fix the current renderer to, well, the current renderer
interactor.setCurrentRenderer(interactor.getCurrentRenderer());
} else {
// remove the fixed current renderer
interactor.setCurrentRenderer(null);
}
}

function bindInteractor(renderer, el) {
// only change the interactor's container if needed
if (interactor.getContainer() !== el) {
if (interactor.getContainer()) {
interactor.unbindEvents();
}
if (captureCurrentRenderer) {
interactor.setCurrentRenderer(renderer);
}
if (el) {
interactor.bindEvents(el);
}
}
}

function addRenderer() {
const mesh = meshes[meshIndex];
const prop = properties[propertyIndex];
Expand All @@ -196,6 +130,11 @@ function addRenderer() {
container.id = rendererId++;
document.body.appendChild(container);

const nameDiv = document.createElement('div');
nameDiv.innerHTML = `${mesh.name} ${prop.name}`;
nameDiv.style.position = 'absolute';
container.appendChild(nameDiv);

const actor = vtkActor.newInstance();
actor.setMapper(mesh.mapper);
actor.getProperty().set(prop.properties);
Expand All @@ -204,16 +143,27 @@ function addRenderer() {
actor.getProperty().setSpecularPower(30);
actor.getProperty().setSpecularColor(1.0, 1.0, 1.0);
const renderer = vtkRenderer.newInstance({ background });
container.innerHTML = `${mesh.name} ${prop.name}`;

container.addEventListener('pointerenter', () =>
bindInteractor(renderer, container)
renderer.addActor(actor);
// Create a new renderwindow
const childRenderWindow = vtkRenderWindow.newInstance();
renderWindow.addChildRenderWindow(childRenderWindow); // add the child RW to its parent
renderWindowView.addMissingNode(childRenderWindow); // create the OpenGLRenderWindow for the child
const childrenViews = renderWindowView.getChildren();
const childRenderWindowView = childrenViews[childrenViews.length - 1]; // get the child's OpenGLRenderWindow

childRenderWindow.addRenderer(renderer);
childRenderWindowView.setContainer(container);
childRenderWindowView.setSize(200, 200);

const interactor = vtkRenderWindowInteractor.newInstance();
interactor.setView(childRenderWindowView);
interactor.initialize();
interactor.bindEvents(childRenderWindowView.getCanvas());
interactor.setInteractorStyle(
vtkInteractorStyleTrackballCamera.newInstance()
);
container.addEventListener('pointerleave', () => bindInteractor(null, null));

renderer.addActor(actor);
renderWindow.addRenderer(renderer);
updateViewPort(container, renderer);
renderer.resetCamera();

// Keep track of renderer
Expand All @@ -224,25 +174,9 @@ function addRenderer() {
// Fill up page
// ----------------------------------------------------------------------------

const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.name = 'singleRendererCapture';
const label = document.createElement('label');
label.for = checkbox.name;
label.innerText = 'Enable single renderer capture';

checkbox.addEventListener('input', (ev) => {
setCaptureCurrentRenderer(ev.target.checked);
});

document.body.appendChild(checkbox);
document.body.appendChild(label);
document.body.appendChild(document.createElement('br'));

for (let i = 0; i < 64; i++) {
addRenderer();
}
resize();

function updateCamera(renderer) {
const camera = renderer.getActiveCamera();
Expand Down
2 changes: 2 additions & 0 deletions Sources/Rendering/Core/RenderWindow/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export interface vtkRenderWindow extends vtkObject {
*/
getNeverRendered(): boolean;

// TODO: Add new functions

/**
*
*/
Expand Down
38 changes: 36 additions & 2 deletions Sources/Rendering/Core/RenderWindow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,38 @@ function vtkRenderWindow(publicAPI, model) {
)
.filter((i) => !!i);
};

publicAPI.setParentRenderWindow = (parent) => {
if (parent === model.parentRenderWindow) {
return false;
}
model.parentRenderWindow = parent;
publicAPI.modified();
if (parent) {
parent.addChildRenderWindow(publicAPI);
}
return true;
};

publicAPI.addChildRenderWindow = (child) => {
if (model.childrenRenderWindow.includes(child)) {
return false;
}
model.childrenRenderWindow.push(child);
publicAPI.modified();
child.setParentRenderWindow(publicAPI);
return true;
};

publicAPI.getRootParentRenderWindow = () => {
let rootRenderWindow;
let rootRenderWindowParent = publicAPI;
while (rootRenderWindowParent) {
rootRenderWindow = rootRenderWindowParent;
rootRenderWindowParent = rootRenderWindowParent.getParentRenderWindow();
}
return rootRenderWindow;
};
}

// ----------------------------------------------------------------------------
Expand All @@ -156,6 +188,8 @@ const DEFAULT_VALUES = {
interactor: null,
neverRendered: true,
numberOfLayers: 1,
parentRenderWindow: null,
childrenRenderWindow: [],
};

// ----------------------------------------------------------------------------
Expand All @@ -172,8 +206,8 @@ export function extend(publicAPI, model, initialValues = {}) {
'_views',
'defaultViewAPI',
]);
macro.get(publicAPI, model, ['neverRendered']);
macro.getArray(publicAPI, model, ['renderers']);
macro.get(publicAPI, model, ['neverRendered', 'parentRenderWindow']);
macro.getArray(publicAPI, model, ['renderers', 'childrenRenderWindow']);
macro.moveToProtected(publicAPI, model, ['views']);
macro.event(publicAPI, model, 'completion');

Expand Down
1 change: 0 additions & 1 deletion Sources/Rendering/Core/RenderWindowInteractor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ function vtkRenderWindowInteractor(publicAPI, model) {
publicAPI.initialize = () => {
model.initialized = true;
publicAPI.enable();
publicAPI.render();
};

publicAPI.enable = () => publicAPI.setEnabled(true);
Expand Down
6 changes: 3 additions & 3 deletions Sources/Rendering/OpenGL/ForwardPass/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ function vtkForwardPass(publicAPI, model) {
const numlayers = viewNode.getRenderable().getNumberOfLayers();

// iterate over renderers
const renderers = viewNode.getChildren();
const renderers = viewNode.getRenderable().getRenderersByReference();
for (let i = 0; i < numlayers; i++) {
for (let index = 0; index < renderers.length; index++) {
const renNode = renderers[index];
const ren = viewNode.getRenderable().getRenderers()[index];
const ren = renderers[index];
const renNode = viewNode.getViewNodeFor(ren);

if (ren.getDraw() && ren.getLayer() === i) {
// check for both opaque and volume actors
Expand Down
Loading

0 comments on commit cd81c23

Please sign in to comment.