Skip to content

Commit 0522acf

Browse files
committed
feat(XR): Update to WebXR functionality in OpenGL RenderWindow
1 parent 2ea94a2 commit 0522acf

File tree

4 files changed

+154
-153
lines changed

4 files changed

+154
-153
lines changed

Examples/Applications/SkyboxViewer/index.js

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ let autoInit = true;
4141
const cameraFocalPoint = userParams.direction || [0, 0, -1];
4242
const cameraViewUp = userParams.up || [0, 1, 0];
4343
const cameraViewAngle = userParams.viewAngle || 100;
44-
const enableVR = !!userParams.vr;
44+
const enableXR = !!userParams.xr;
4545
const eyeSpacing = userParams.eye || 0.0;
4646
const grid = userParams.debug || false;
4747
const autoIncrementTimer = userParams.timer || 0;
@@ -189,13 +189,7 @@ function createVisualization(container, mapReader) {
189189
updateSkybox(allPositions[nextIdx]);
190190
}
191191

192-
if (enableVR && vtkDeviceOrientationToCamera.isDeviceOrientationSupported()) {
193-
// vtkMobileVR.getVRHeadset().then((headset) => {
194-
// console.log('got headset');
195-
// console.log(headset);
196-
// console.log(vtkMobileVR.hardware);
197-
// });
198-
192+
if (enableXR && navigator.xr.isSessionSupported('immersive-vr')) {
199193
leftRenderer = vtkRenderer.newInstance();
200194
rightRenderer = vtkRenderer.newInstance();
201195

@@ -236,7 +230,7 @@ function createVisualization(container, mapReader) {
236230
distPass.setCameraCenterX1(-eyeSpacing);
237231
distPass.setCameraCenterX2(eyeSpacing);
238232
distPass.setDelegates([vtkForwardPass.newInstance()]);
239-
fullScreenRenderer.getAPISpecificRenderWindow().setRenderPasses([distPass]);
233+
fullScreenRenderer.getApiSpecificRenderWindow().setRenderPasses([distPass]);
240234

241235
// Hide any controller
242236
fullScreenRenderer.setControllerVisibility(false);
@@ -278,29 +272,27 @@ function createVisualization(container, mapReader) {
278272
mainRenderer.addActor(actor);
279273

280274
// add vr option button if supported
281-
fullScreenRenderer.getApiSpecificRenderWindow().onHaveVRDisplay(() => {
282-
if (
283-
fullScreenRenderer.getApiSpecificRenderWindow().getVrDisplay()
284-
.capabilities.canPresent
285-
) {
286-
const button = document.createElement('button');
287-
button.style.position = 'absolute';
288-
button.style.left = '10px';
289-
button.style.bottom = '10px';
290-
button.style.zIndex = 10000;
291-
button.textContent = 'Send To VR';
292-
document.querySelector('body').appendChild(button);
293-
button.addEventListener('click', () => {
294-
if (button.textContent === 'Send To VR') {
295-
fullScreenRenderer.getApiSpecificRenderWindow().startVR();
296-
button.textContent = 'Return From VR';
297-
} else {
298-
fullScreenRenderer.getApiSpecificRenderWindow().stopVR();
299-
button.textContent = 'Send To VR';
300-
}
301-
});
302-
}
303-
});
275+
if (
276+
navigator.xr !== undefined &&
277+
navigator.xr.isSessionSupported('immersive-vr')
278+
) {
279+
const button = document.createElement('button');
280+
button.style.position = 'absolute';
281+
button.style.left = '10px';
282+
button.style.bottom = '10px';
283+
button.style.zIndex = 10000;
284+
button.textContent = 'Send To VR';
285+
document.querySelector('body').appendChild(button);
286+
button.addEventListener('click', () => {
287+
if (button.textContent === 'Send To VR') {
288+
fullScreenRenderer.getApiSpecificRenderWindow().startXR();
289+
button.textContent = 'Return From VR';
290+
} else {
291+
fullScreenRenderer.getApiSpecificRenderWindow().stopXR();
292+
button.textContent = 'Send To VR';
293+
}
294+
});
295+
}
304296
}
305297

306298
renderWindow.render();

Examples/Geometry/VR/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ const renderWindow = fullScreenRenderer.getRenderWindow();
3636
// this
3737
// ----------------------------------------------------------------------------
3838

39-
const coneSource = vtkConeSource.newInstance({ height: 100.0, radius: 50.0 });
40-
// const coneSource = vtkConeSource.newInstance({ height: 1.0, radius: 0.5 });
39+
const coneSource = vtkConeSource.newInstance({ height: 100.0, radius: 50 });
4140
const filter = vtkCalculator.newInstance();
4241

4342
filter.setInputConnection(coneSource.getOutputPort());
@@ -67,7 +66,7 @@ mapper.setInputConnection(filter.getOutputPort());
6766

6867
const actor = vtkActor.newInstance();
6968
actor.setMapper(mapper);
70-
actor.setPosition(20.0, 0.0, 0.0);
69+
actor.setPosition(0.0, 0.0, -20.0);
7170

7271
renderer.addActor(actor);
7372
renderer.resetCamera();
@@ -96,10 +95,10 @@ resolutionChange.addEventListener('input', (e) => {
9695

9796
vrbutton.addEventListener('click', (e) => {
9897
if (vrbutton.textContent === 'Send To VR') {
99-
fullScreenRenderer.getApiSpecificRenderWindow().startVR();
98+
fullScreenRenderer.getApiSpecificRenderWindow().startXR();
10099
vrbutton.textContent = 'Return From VR';
101100
} else {
102-
fullScreenRenderer.getApiSpecificRenderWindow().stopVR();
101+
fullScreenRenderer.getApiSpecificRenderWindow().stopXR();
103102
vrbutton.textContent = 'Send To VR';
104103
}
105104
});

Sources/Rendering/Core/RenderWindowInteractor/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ function vtkRenderWindowInteractor(publicAPI, model) {
366366
return;
367367
}
368368
animationRequesters.add(requestor);
369-
if (animationRequesters.size === 1) {
369+
if (animationRequesters.size === 1 && !model.xrAnimation) {
370370
model.lastFrameTime = 0.1;
371371
model.lastFrameStart = Date.now();
372372
model.animationRequest = requestAnimationFrame(publicAPI.handleAnimation);
@@ -375,7 +375,7 @@ function vtkRenderWindowInteractor(publicAPI, model) {
375375
};
376376

377377
publicAPI.isAnimating = () =>
378-
model.vrAnimation || model.animationRequest !== null;
378+
model.xrAnimation || model.animationRequest !== null;
379379

380380
publicAPI.cancelAnimation = (requestor, skipWarning = false) => {
381381
if (!animationRequesters.has(requestor)) {
@@ -398,17 +398,17 @@ function vtkRenderWindowInteractor(publicAPI, model) {
398398
}
399399
};
400400

401-
publicAPI.switchToVRAnimation = () => {
401+
publicAPI.switchToXRAnimation = () => {
402402
// cancel existing animation if any
403403
if (model.animationRequest) {
404404
cancelAnimationFrame(model.animationRequest);
405405
model.animationRequest = null;
406406
}
407-
model.vrAnimation = true;
407+
model.xrAnimation = true;
408408
};
409409

410-
publicAPI.returnFromVRAnimation = () => {
411-
model.vrAnimation = false;
410+
publicAPI.returnFromXRAnimation = () => {
411+
model.xrAnimation = false;
412412
if (animationRequesters.size !== 0) {
413413
model.FrameTime = -1;
414414
model.animationRequest = requestAnimationFrame(publicAPI.handleAnimation);
@@ -756,7 +756,7 @@ function vtkRenderWindowInteractor(publicAPI, model) {
756756
// do not want extra renders as the make the apparent interaction
757757
// rate slower.
758758
publicAPI.render = () => {
759-
if (model.animationRequest === null && !model.inRender) {
759+
if (!publicAPI.isAnimating() && !model.inRender) {
760760
forceRender();
761761
}
762762
};

0 commit comments

Comments
 (0)