Skip to content

Commit 765259f

Browse files
committed
test(XR): WIP snap rotation
1 parent ce66ef1 commit 765259f

File tree

5 files changed

+275
-64
lines changed

5 files changed

+275
-64
lines changed

Examples/Geometry/VR/index.js

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

39-
const coneSource = vtkConeSource.newInstance({ height: 100.0, radius: 50 });
39+
const coneSource = vtkConeSource.newInstance({ height: 1.0, radius: 0.5 });
4040
const filter = vtkCalculator.newInstance();
4141

4242
filter.setInputConnection(coneSource.getOutputPort());
@@ -66,7 +66,7 @@ mapper.setInputConnection(filter.getOutputPort());
6666

6767
const actor = vtkActor.newInstance();
6868
actor.setMapper(mapper);
69-
actor.setPosition(0.0, 0.0, -20.0);
69+
actor.setPosition(0.0, 0.0, -1.0);
7070

7171
renderer.addActor(actor);
7272
renderer.resetCamera();

Sources/Interaction/Style/InteractorStyleTrackballCamera/index.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import vtkInteractorStyle from 'vtk.js/Sources/Rendering/Core/InteractorStyle';
33
import vtkInteractorStyleConstants from 'vtk.js/Sources/Rendering/Core/InteractorStyle/Constants';
44
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
55
import {
6+
Axis,
67
Device,
78
Input,
89
} from 'vtk.js/Sources/Rendering/Core/RenderWindowInteractor/Constants';
@@ -58,7 +59,7 @@ function vtkInteractorStyleTrackballCamera(publicAPI, model) {
5859
ed &&
5960
ed.pressed &&
6061
ed.device === Device.RightController &&
61-
(ed.input === Input.Trigger || ed.input === Input.TrackPad)
62+
ed.input === Input.Trigger // || ed.input === Input.TrackPad)
6263
) {
6364
publicAPI.startCameraPose();
6465
return;
@@ -67,24 +68,46 @@ function vtkInteractorStyleTrackballCamera(publicAPI, model) {
6768
ed &&
6869
!ed.pressed &&
6970
ed.device === Device.RightController &&
70-
(ed.input === Input.Trigger || ed.input === Input.TrackPad) &&
71+
ed.input === Input.Trigger && // || ed.input === Input.TrackPad) &&
7172
model.state === States.IS_CAMERA_POSE
7273
) {
7374
publicAPI.endCameraPose();
7475
// return;
7576
}
77+
if (
78+
ed &&
79+
ed.device === Device.RightController &&
80+
ed.input &&
81+
(ed.input === Axis.ThumbstickX || Input.TrackPad)
82+
) {
83+
publicAPI.updateCameraOrientation(ed);
84+
}
85+
if (ed && ed.input && (ed.input === Axis.ThumbstickX || Input.TrackPad)) {
86+
publicAPI.updateCameraOrientation(ed);
87+
}
7688
};
7789

7890
publicAPI.handleMove3D = (ed) => {
7991
switch (model.state) {
8092
case States.IS_CAMERA_POSE:
81-
publicAPI.updateCameraPose(ed);
93+
publicAPI.updateCameraPosition(ed);
8294
break;
8395
default:
8496
}
8597
};
8698

87-
publicAPI.updateCameraPose = (ed) => {
99+
publicAPI.updateCameraOrientation = (ed) => {
100+
// rotate the world in the direction
101+
// of the controller
102+
const camera = ed.pokedRenderer.getActiveCamera();
103+
let worldMatrix = new Float64Array(16);
104+
camera.getWorldToPhysicalMatrix(worldMatrix);
105+
106+
const angle = ed.device === Device.LeftController ? -22.5 : 22.5;
107+
camera.applyPhysicalYaw(angle);
108+
};
109+
110+
publicAPI.updateCameraPosition = (ed) => {
88111
// move the world in the direction of the
89112
// controller
90113
const camera = ed.pokedRenderer.getActiveCamera();

Sources/Rendering/Core/Camera/index.js

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,124 @@ function vtkCamera(publicAPI, model) {
397397

398398
mat4.scale(result, result, tmpvec1);
399399
mat4.translate(result, result, model.physicalTranslation);
400+
401+
// let rot = mat4.identity(new Float64Array(16));
402+
//mat4.fromRotation(rot, model.physicalYawAngle / 180 * 3.14, model.physicalViewUp);
403+
//// mat4.invert(rot, rot);
404+
//mat4.mul(result, rot, result);
405+
406+
//let tmpRot = mat4.identity(new Float64Array(16));
407+
//let matInv = mat4.identity(new Float64Array(16));
408+
//mat4.rotateY(tmpRot, tmpRot, model.physicalYawAngle / 180 * 3.14);
409+
//mat4.transpose(tmpRot, tmpRot);
410+
//mat4.invert(matInv, )
411+
// mat4.mul(result, tmpRot, result);
412+
};
413+
414+
publicAPI.applyPhysicalYaw = (angle) => {
415+
model.physicalYawAngle += angle;
416+
if (model.physicalYawAngle > 360) {
417+
model.physicalYawAngle -= 360;
418+
} else if (model.physicalYawAngle < 0) {
419+
model.physicalYawAngle += 360;
420+
}
400421
};
401422

402423
publicAPI.computeViewParametersFromViewMatrix = (vmat) => {
403424
// invert to get view to world
404-
mat4.invert(tmpMatrix, vmat);
425+
let worldToViewPos = new Float64Array(4);
426+
let viewToWorldPos = new Float64Array(4);
427+
let v2wMatrix = new Float64Array(16);
428+
worldToViewPos[0] = vmat[12];
429+
worldToViewPos[1] = vmat[13];
430+
worldToViewPos[2] = vmat[14];
431+
worldToViewPos[3] = 1;
432+
mat4.invert(v2wMatrix, vmat);
433+
viewToWorldPos[0] = v2wMatrix[12];
434+
viewToWorldPos[1] = v2wMatrix[13];
435+
viewToWorldPos[2] = v2wMatrix[14];
436+
viewToWorldPos[3] = 1;
437+
mat4.copy(tmpMatrix, v2wMatrix);
438+
//console.log('View -> World');
439+
//console.log(v2wMatrix);
440+
//mat4.copy(tmpMatrix, vmat);
441+
//v[0] = tmpMatrix[12]; //tmpMatrix[12] = 0;
442+
//tmpMatrix[13] = 0;
443+
//tmpMatrix[14] = 0;
444+
//mat4.invert(tmpMatrix, tmpMatrix);
445+
446+
////
447+
////
448+
//// mat4.invert(tmpMatrix, tmpMatrix);
449+
//// mat4.rotateY(tmpMatrix, tmpMatrix, model.physicalYawAngle / 180 * 3.14);
450+
//// mat4.translate(tmpMatrix, tmpMatrix, [-v[0], -v[1], -v[2]]);
451+
452+
////// Correct center of rotation, incorrect view translation
453+
////mat4.invert(tmpMatrix, tmpMatrix);
454+
////mat4.rotateY(tmpMatrix, tmpMatrix, model.physicalYawAngle / 180 * 3.14);
455+
456+
//// Correct view translation, incorrect center of rotation
457+
//// mat4.invert(tmpMatrix, tmpMatrix););
458+
////let tmp = mat4.identity(new Float64Array(16));
459+
////tmpMatrix[12] = 0;
460+
////tmpMatrix[14] = 0;
461+
//mat4.invert(tmpMatrix, tmpMatrix);
462+
463+
let w2vTranslate = mat4.identity(new Float64Array(16));
464+
let rotate = mat4.identity(new Float64Array(16));
465+
let rotateRev = mat4.identity(new Float64Array(16));
466+
let w2vTranslateReverse = mat4.identity(new Float64Array(16));
467+
let v2wTranslate = mat4.identity(new Float64Array(16));
468+
let v2wTranslateReverse = mat4.identity(new Float64Array(16));
469+
470+
mat4.translate(v2wTranslate, v2wTranslate, [
471+
-viewToWorldPos[0],
472+
-viewToWorldPos[1],
473+
-viewToWorldPos[2],
474+
]);
475+
mat4.rotateY(rotate, rotate, (model.physicalYawAngle / 180) * 3.14);
476+
mat4.rotateY(
477+
rotateRev,
478+
rotateRev,
479+
((-1 * model.physicalYawAngle) / 180) * 3.14
480+
);
481+
mat4.translate(v2wTranslateReverse, v2wTranslateReverse, viewToWorldPos);
482+
mat4.translate(w2vTranslate, w2vTranslate, [
483+
-worldToViewPos[0],
484+
-worldToViewPos[1],
485+
-worldToViewPos[2],
486+
]);
487+
mat4.translate(w2vTranslateReverse, w2vTranslateReverse, worldToViewPos);
488+
489+
//mat4.mul(tmpMatrix, w2vTranslate, vmat);
490+
//mat4.mul(tmpMatrix, rotate, tmpMatrix);
491+
//mat4.mul(tmpMatrix, w2vTranslateReverse, tmpMatrix);
492+
//mat4.invert(tmpMatrix, tmpMatrix);
493+
494+
// FIXME works post-snap? mat4.mul(v2wTranslateReverse, v2wTranslate, rotate);
495+
// mat4.mul(v2wTranslate, v2wTranslate, rotate);
496+
//mat4.mul(v2wTranslateReverse, v2wTranslateReverse, rotate);
497+
mat4.mul(v2wTranslateReverse, v2wTranslate, rotate);
498+
mat4.mul(tmpMatrix, v2wTranslateReverse, v2wMatrix);
499+
mat4.mul(tmpMatrix, tmpMatrix, rotate);
500+
mat4.mul(tmpMatrix, v2wTranslate, tmpMatrix);
501+
502+
//mat4.rotateY(tmpMatrix, tmpMatrix, -1 * model.physicalYawAngle / 180 * 3.14);
503+
//mat4.translate(tmpMatrix, tmpMatrix, [0, 0, 0.01 * model.physicalYawAngle]);
504+
//mat4.translate(tmpMatrix, tmpMatrix, v);
505+
//mat4.invert(tmpMatrix, tmpMatrix);
506+
////
507+
//mat4.invert(tmpMatrix, tmpMatrix);
508+
//tmpMatrix[12] = v[0];
509+
//tmpMatrix[13] = v[1];
510+
//tmpMatrix[14] = v[2];
511+
512+
// FIXME try pushing through the cone position at <0,0,-1>
513+
//v[0] = 0;
514+
//v[1] = 0;
515+
//v[1] = -1;
516+
//vec3.transformMat4(tmpvec1, v, tmpMatrix);
517+
// console.log(v);
405518

406519
// note with glmatrix operations happen in
407520
// the reverse order
@@ -443,6 +556,27 @@ function vtkCamera(publicAPI, model) {
443556
// world -> view
444557
mat4.multiply(tmpMatrix, mat, tmpMatrix);
445558

559+
console.log('orig');
560+
console.log(tmpMatrix);
561+
let rot = mat4.identity(new Float64Array(16));
562+
let v = new Float64Array(3);
563+
v[0] = tmpMatrix[12];
564+
v[1] = tmpMatrix[13];
565+
v[2] = tmpMatrix[14];
566+
//mat4.rotateY(rot, rot, model.physicalYawAngle / 180 * 3.14);
567+
// mat4.transpose(rot, rot);
568+
//mat4.translate(tmpMatrix, tmpMatrix, [-v[0], -v[1], -v[2]]);
569+
mat4.mul(tmpMatrix, tmpMatrix, rot);
570+
tmpMatrix[12] = v[0];
571+
tmpMatrix[13] = v[1];
572+
tmpMatrix[14] = v[2];
573+
//mat4.mul(tmpMatrix, rot, tmpMatrix);
574+
//mat4.translate(tmpMatrix, tmpMatrix, v);
575+
576+
console.log(tmpMatrix);
577+
let tmp = tmpMatrix[12];
578+
//tmpMatrix[12] = tmpMatrix[14];
579+
//tmpMatrix[14] = tmp;
446580
publicAPI.computeViewParametersFromViewMatrix(tmpMatrix);
447581
};
448582

@@ -721,6 +855,8 @@ export const DEFAULT_VALUES = {
721855
physicalScale: 1.0,
722856
physicalViewUp: [0, 1, 0],
723857
physicalViewNorth: [0, 0, -1],
858+
859+
physicalYawAngle: 0,
724860
};
725861

726862
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)