Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 23 additions & 121 deletions Examples/Applications/SkyboxViewer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import 'vtk.js/Sources/Rendering/Profiles/Geometry';
import HttpDataAccessHelper from 'vtk.js/Sources/IO/Core/DataAccessHelper/HttpDataAccessHelper';
import macro from 'vtk.js/Sources/macros';
import vtkDeviceOrientationToCamera from 'vtk.js/Sources/Interaction/Misc/DeviceOrientationToCamera';
import vtkForwardPass from 'vtk.js/Sources/Rendering/OpenGL/ForwardPass';
import vtkFullScreenRenderWindow from 'vtk.js/Sources/Rendering/Misc/FullScreenRenderWindow';
import vtkRadialDistortionPass from 'vtk.js/Sources/Rendering/OpenGL/RadialDistortionPass';
import vtkRenderer from 'vtk.js/Sources/Rendering/Core/Renderer';
import vtkSkybox from 'vtk.js/Sources/Rendering/Core/Skybox';
import vtkSkyboxReader from 'vtk.js/Sources/IO/Misc/SkyboxReader';
import vtkURLExtract from 'vtk.js/Sources/Common/Core/URLExtract';
Expand Down Expand Up @@ -41,14 +38,9 @@ let autoInit = true;
const cameraFocalPoint = userParams.direction || [0, 0, -1];
const cameraViewUp = userParams.up || [0, 1, 0];
const cameraViewAngle = userParams.viewAngle || 100;
const enableVR = !!userParams.vr;
const eyeSpacing = userParams.eye || 0.0;
const grid = userParams.debug || false;
const autoIncrementTimer = userParams.timer || 0;
const disableTouchNext = userParams.disableTouch || false;
const distk1 = userParams.k1 || 0.2;
const distk2 = userParams.k2 || 0.0;
const cameraCenterY = userParams.centerY || 0.0;

const body = document.querySelector('body');
let fullScreenMetod = null;
Expand Down Expand Up @@ -151,10 +143,8 @@ function createVisualization(container, mapReader) {
const mainRenderer = fullScreenRenderer.getRenderer();
const interactor = fullScreenRenderer.getInteractor();
const actor = vtkSkybox.newInstance();
let camera = mainRenderer.getActiveCamera();
let leftRenderer = null;
let rightRenderer = null;
let updateCameraCallBack = mainRenderer.resetCameraClippingRange;
const camera = mainRenderer.getActiveCamera();
const updateCameraCallBack = mainRenderer.resetCameraClippingRange;

// Connect viz pipeline
actor.addTexture(mapReader.getOutputData());
Expand Down Expand Up @@ -189,116 +179,28 @@ function createVisualization(container, mapReader) {
updateSkybox(allPositions[nextIdx]);
}

if (enableVR && vtkDeviceOrientationToCamera.isDeviceOrientationSupported()) {
// vtkMobileVR.getVRHeadset().then((headset) => {
// console.log('got headset');
// console.log(headset);
// console.log(vtkMobileVR.hardware);
// });

leftRenderer = vtkRenderer.newInstance();
rightRenderer = vtkRenderer.newInstance();

// Configure left/right renderers
leftRenderer.setViewport(0, 0, 0.5, 1);
leftRenderer.addActor(actor);
const leftCamera = leftRenderer.getActiveCamera();
leftCamera.set(cameraConfiguration);
leftCamera.setWindowCenter(-eyeSpacing, -cameraCenterY);

rightRenderer.setViewport(0.5, 0, 1, 1);
rightRenderer.addActor(actor);
const rightCamera = rightRenderer.getActiveCamera();
rightCamera.set(cameraConfiguration);
rightCamera.setWindowCenter(eyeSpacing, -cameraCenterY);

// Provide custom update callback + fake camera
updateCameraCallBack = () => {
leftRenderer.resetCameraClippingRange();
rightRenderer.resetCameraClippingRange();
};
camera = {
setDeviceAngles(alpha, beta, gamma, screen) {
leftCamera.setDeviceAngles(alpha, beta, gamma, screen);
rightCamera.setDeviceAngles(alpha, beta, gamma, screen);
},
};

// Reconfigure render window
renderWindow.addRenderer(leftRenderer);
renderWindow.addRenderer(rightRenderer);
renderWindow.removeRenderer(mainRenderer);

const distPass = vtkRadialDistortionPass.newInstance();
distPass.setK1(distk1);
distPass.setK2(distk2);
distPass.setCameraCenterY(cameraCenterY);
distPass.setCameraCenterX1(-eyeSpacing);
distPass.setCameraCenterX2(eyeSpacing);
distPass.setDelegates([vtkForwardPass.newInstance()]);
fullScreenRenderer.getAPISpecificRenderWindow().setRenderPasses([distPass]);

// Hide any controller
fullScreenRenderer.setControllerVisibility(false);

// Remove window interactions
interactor.unbindEvents();

// Attach touch control
if (!disableTouchNext) {
fullScreenRenderer
.getRootContainer()
.addEventListener('touchstart', nextPosition, true);
if (fullScreenMetod) {
fullScreenRenderer.getRootContainer().addEventListener(
'touchend',
(e) => {
body[fullScreenMetod]();
},
true
);
}
}

// Warning if browser does not support fullscreen
/* eslint-disable */
if (navigator.userAgent.match('CriOS')) {
alert(
'Chrome on iOS does not support fullscreen. Please use Safari instead.'
);
}
if (navigator.userAgent.match('FxiOS')) {
alert(
'Firefox on iOS does not support fullscreen. Please use Safari instead.'
);
}
/* eslint-enable */
} else {
camera.set(cameraConfiguration);
mainRenderer.addActor(actor);

// add vr option button if supported
fullScreenRenderer.getApiSpecificRenderWindow().onHaveVRDisplay(() => {
if (
fullScreenRenderer.getApiSpecificRenderWindow().getVrDisplay()
.capabilities.canPresent
) {
const button = document.createElement('button');
button.style.position = 'absolute';
button.style.left = '10px';
button.style.bottom = '10px';
button.style.zIndex = 10000;
camera.set(cameraConfiguration);
mainRenderer.addActor(actor);

// add vr option button if supported
if (
navigator.xr !== undefined &&
navigator.xr.isSessionSupported('immersive-vr')
) {
const button = document.createElement('button');
button.style.position = 'absolute';
button.style.left = '10px';
button.style.bottom = '10px';
button.style.zIndex = 10000;
button.textContent = 'Send To VR';
document.querySelector('body').appendChild(button);
button.addEventListener('click', () => {
if (button.textContent === 'Send To VR') {
fullScreenRenderer.getApiSpecificRenderWindow().startXR();
button.textContent = 'Return From VR';
} else {
fullScreenRenderer.getApiSpecificRenderWindow().stopXR();
button.textContent = 'Send To VR';
document.querySelector('body').appendChild(button);
button.addEventListener('click', () => {
if (button.textContent === 'Send To VR') {
fullScreenRenderer.getApiSpecificRenderWindow().startVR();
button.textContent = 'Return From VR';
} else {
fullScreenRenderer.getApiSpecificRenderWindow().stopVR();
button.textContent = 'Send To VR';
}
});
}
});
}
Expand Down
9 changes: 4 additions & 5 deletions Examples/Geometry/VR/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ const renderWindow = fullScreenRenderer.getRenderWindow();
// this
// ----------------------------------------------------------------------------

const coneSource = vtkConeSource.newInstance({ height: 100.0, radius: 50.0 });
// const coneSource = vtkConeSource.newInstance({ height: 1.0, radius: 0.5 });
const coneSource = vtkConeSource.newInstance({ height: 100.0, radius: 50 });
const filter = vtkCalculator.newInstance();

filter.setInputConnection(coneSource.getOutputPort());
Expand Down Expand Up @@ -67,7 +66,7 @@ mapper.setInputConnection(filter.getOutputPort());

const actor = vtkActor.newInstance();
actor.setMapper(mapper);
actor.setPosition(20.0, 0.0, 0.0);
actor.setPosition(0.0, 0.0, -20.0);

renderer.addActor(actor);
renderer.resetCamera();
Expand Down Expand Up @@ -96,10 +95,10 @@ resolutionChange.addEventListener('input', (e) => {

vrbutton.addEventListener('click', (e) => {
if (vrbutton.textContent === 'Send To VR') {
fullScreenRenderer.getApiSpecificRenderWindow().startVR();
fullScreenRenderer.getApiSpecificRenderWindow().startXR();
vrbutton.textContent = 'Return From VR';
} else {
fullScreenRenderer.getApiSpecificRenderWindow().stopVR();
fullScreenRenderer.getApiSpecificRenderWindow().stopXR();
vrbutton.textContent = 'Send To VR';
}
});
Expand Down
14 changes: 7 additions & 7 deletions Sources/Rendering/Core/RenderWindowInteractor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ function vtkRenderWindowInteractor(publicAPI, model) {
return;
}
animationRequesters.add(requestor);
if (animationRequesters.size === 1) {
if (animationRequesters.size === 1 && !model.xrAnimation) {
model.lastFrameTime = 0.1;
model.lastFrameStart = Date.now();
model.animationRequest = requestAnimationFrame(publicAPI.handleAnimation);
Expand All @@ -375,7 +375,7 @@ function vtkRenderWindowInteractor(publicAPI, model) {
};

publicAPI.isAnimating = () =>
model.vrAnimation || model.animationRequest !== null;
model.xrAnimation || model.animationRequest !== null;

publicAPI.cancelAnimation = (requestor, skipWarning = false) => {
if (!animationRequesters.has(requestor)) {
Expand All @@ -398,17 +398,17 @@ function vtkRenderWindowInteractor(publicAPI, model) {
}
};

publicAPI.switchToVRAnimation = () => {
publicAPI.switchToXRAnimation = () => {
// cancel existing animation if any
if (model.animationRequest) {
cancelAnimationFrame(model.animationRequest);
model.animationRequest = null;
}
model.vrAnimation = true;
model.xrAnimation = true;
};

publicAPI.returnFromVRAnimation = () => {
model.vrAnimation = false;
publicAPI.returnFromXRAnimation = () => {
model.xrAnimation = false;
if (animationRequesters.size !== 0) {
model.FrameTime = -1;
model.animationRequest = requestAnimationFrame(publicAPI.handleAnimation);
Expand Down Expand Up @@ -756,7 +756,7 @@ function vtkRenderWindowInteractor(publicAPI, model) {
// do not want extra renders as the make the apparent interaction
// rate slower.
publicAPI.render = () => {
if (model.animationRequest === null && !model.inRender) {
if (!publicAPI.isAnimating() && !model.inRender) {
forceRender();
}
};
Expand Down
Loading