Skip to content

Commit d611a58

Browse files
authored
Merge pull request #2203 from tbirdso/xr-init-pos
feat(XR): Adjust world-to-physical factors for XR modalities
2 parents bec9cb3 + 8f7d812 commit d611a58

File tree

1 file changed

+53
-0
lines changed
  • Sources/Rendering/OpenGL/RenderWindow

1 file changed

+53
-0
lines changed

Sources/Rendering/OpenGL/RenderWindow/index.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ const SCREENSHOT_PLACEHOLDER = {
1919
height: '100%',
2020
};
2121

22+
const DEFAULT_RESET_FACTORS = {
23+
vr: {
24+
rescaleFactor: 1.0,
25+
translateZ: -0.7, // 0.7 m forward from the camera
26+
},
27+
ar: {
28+
rescaleFactor: 0.25, // scale down AR for viewing comfort by default
29+
translateZ: -0.5, // 0.5 m forward from the camera
30+
},
31+
};
32+
2233
function checkRenderTargetSupport(gl, format, type) {
2334
// create temporary frame buffer and texture
2435
const framebuffer = gl.createFramebuffer();
@@ -315,6 +326,8 @@ function vtkOpenGLRenderWindow(publicAPI, model) {
315326
model.xrReferenceSpace = refSpace;
316327
});
317328

329+
publicAPI.resetXRScene();
330+
318331
model.renderable.getInteractor().switchToXRAnimation();
319332
model.xrSceneFrame = model.xrSession.requestAnimationFrame(
320333
publicAPI.xrRender
@@ -324,6 +337,46 @@ function vtkOpenGLRenderWindow(publicAPI, model) {
324337
}
325338
};
326339

340+
publicAPI.resetXRScene = (
341+
inputRescaleFactor = DEFAULT_RESET_FACTORS.vr.rescaleFactor,
342+
inputTranslateZ = DEFAULT_RESET_FACTORS.vr.translateZ
343+
) => {
344+
// Adjust world-to-physical parameters for different modalities
345+
// Default parameter values are for VR (model.xrSessionIsAR == false)
346+
let rescaleFactor = inputRescaleFactor;
347+
let translateZ = inputTranslateZ;
348+
349+
if (
350+
model.xrSessionIsAR &&
351+
rescaleFactor === DEFAULT_RESET_FACTORS.vr.rescaleFactor
352+
) {
353+
// Scale down by default in AR
354+
rescaleFactor = DEFAULT_RESET_FACTORS.ar.rescaleFactor;
355+
}
356+
357+
if (
358+
model.xrSessionIsAR &&
359+
translateZ === DEFAULT_RESET_FACTORS.vr.translateZ
360+
) {
361+
// Default closer to the camera in AR
362+
translateZ = DEFAULT_RESET_FACTORS.ar.translateZ;
363+
}
364+
365+
const ren = model.renderable.getRenderers()[0];
366+
ren.resetCamera();
367+
368+
const camera = ren.getActiveCamera();
369+
let physicalScale = camera.getPhysicalScale();
370+
const physicalTranslation = camera.getPhysicalTranslation();
371+
372+
physicalScale /= rescaleFactor;
373+
translateZ *= physicalScale;
374+
physicalTranslation[2] += translateZ;
375+
376+
camera.setPhysicalScale(physicalScale);
377+
camera.setPhysicalTranslation(physicalTranslation);
378+
};
379+
327380
publicAPI.stopXR = async () => {
328381
if (navigator.xr === undefined) {
329382
// WebXR polyfill not available so nothing to do

0 commit comments

Comments
 (0)