Skip to content

Commit

Permalink
'Turntable' option for camera_mode? #446
Browse files Browse the repository at this point in the history
  • Loading branch information
artur-trzesiok committed Jun 28, 2024
1 parent 1e50d0b commit ecfc809
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 12 deletions.
22 changes: 21 additions & 1 deletion js/src/core/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const msgpack = require('msgpack-lite');

const LilGUI = require('lil-gui').GUI;
const {viewModes} = require('./lib/viewMode');
const {cameraUpAxisModes} = require('./lib/cameraUpAxis');
const _ = require('../lodash');
const {cameraModes} = require('./lib/cameraMode');
const loader = require('./lib/Loader');
Expand All @@ -14,6 +15,7 @@ const detachWindowGUI = require('./lib/detachWindow');
const fullscreen = require('./lib/fullscreen');
const {viewModeGUI} = require('./lib/viewMode');
const {cameraModeGUI} = require('./lib/cameraMode');
const {cameraUpAxisGUI} = require('./lib/cameraUpAxis');
const manipulate = require('./lib/manipulate');
const {getColorLegend} = require('./lib/colorMapLegend');
const objectsGUIProvider = require('./lib/objectsGUIprovider');
Expand Down Expand Up @@ -132,6 +134,7 @@ function K3D(provider, targetDOMNode, parameters) {

viewModeGUI(GUI.controls, self);
cameraModeGUI(GUI.controls, self);
cameraUpAxisGUI(GUI.controls, self);
manipulate.manipulateGUI(GUI.controls, self, changeParameters);

GUI.controls.add(self.parameters, 'cameraFov').step(0.1).min(1.0).max(179)
Expand Down Expand Up @@ -299,6 +302,7 @@ function K3D(provider, targetDOMNode, parameters) {
cameraDampingFactor: 0.0,
name: null,
cameraFov: 60.0,
cameraUpAxis: cameraUpAxisModes.none,
cameraAnimation: {},
autoRendering: true,
axesHelper: 1.0,
Expand Down Expand Up @@ -722,14 +726,29 @@ function K3D(provider, targetDOMNode, parameters) {
self.parameters.cameraDampingFactor = factor;

self.getWorld().changeControls(true);
};

/**
* Set camera up axis
* @memberof K3D.Core
* @param {String} axis
*/
this.setCameraUpAxis = function (axis) {
self.parameters.cameraUpAxis = axis;

self.getWorld().changeControls(true);

if (GUI.controls) {
GUI.controls.controllers.forEach((controller) => {
if (controller.property === 'damping_factor') {
if (controller.property === 'cameraUpAxis') {
controller.updateDisplay();
}
});
}

self.rebuildSceneData(false).then(() => {
self.render();
});
};

/**
Expand Down Expand Up @@ -1268,6 +1287,7 @@ function K3D(provider, targetDOMNode, parameters) {
self.setGrid(self.parameters.grid);
self.setCameraAutoFit(self.parameters.cameraAutoFit);
self.setCameraDampingFactor(self.parameters.cameraDampingFactor);
self.setCameraUpAxis(self.parameters.cameraUpAxis);
self.setClippingPlanes(self.parameters.clippingPlanes);
self.setDirectionalLightingIntensity(self.parameters.lighting);
self.setColorMapLegend(self.parameters.colorbarObjectId);
Expand Down
40 changes: 40 additions & 0 deletions js/src/core/lib/cameraUpAxis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const cameraUpAxisModes = {
x: 'x',
y: 'y',
z: 'z',
none: 'none'
};

function cameraUpAxisGUI(gui, K3D) {
gui.add(K3D.parameters, 'cameraUpAxis', {
x: cameraUpAxisModes.x,
y: cameraUpAxisModes.y,
z: cameraUpAxisModes.z,
none: cameraUpAxisModes.none
}).name('CameraUpAxis').onChange(
(axis) => {
K3D.setCameraUpAxis(axis);

K3D.dispatch(K3D.events.PARAMETERS_CHANGE, {
key: 'camera_up_axis',
value: axis
});
},
);
}

function setupUpVector(camera, cameraUpAxis) {
if (cameraUpAxis === cameraUpAxisModes.x) {
camera.up.set(1, 0, 0);
} else if (cameraUpAxis === cameraUpAxisModes.y) {
camera.up.set(0, 1, 0);
} else if (cameraUpAxis === cameraUpAxisModes.z) {
camera.up.set(0, 0, 1);
}
}

module.exports = {
cameraUpAxisGUI,
cameraUpAxisModes,
setupUpVector
};
6 changes: 6 additions & 0 deletions js/src/k3d.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ class PlotView extends widgets.DOMWidgetView {
this.model.on('change:camera_pan_speed', this._setCameraSpeeds, this);
this.model.on('change:camera_fov', this._setCameraFOV, this);
this.model.on('change:camera_damping_factor', this._setCameraDampingFactor, this);
this.model.on('change:camera_up_axis', this._setCameraUpAxis, this);
this.model.on('change:axes_helper', this._setAxesHelper, this);
this.model.on('change:axes_helper_colors', this._setAxesHelperColors, this);
this.model.on('change:snapshot_type', this._setSnapshotType, this);
Expand Down Expand Up @@ -338,6 +339,7 @@ class PlotView extends widgets.DOMWidgetView {
cameraPanSpeed: this.model.get('camera_pan_speed'),
cameraDampingFactor: this.model.get('camera_damping_factor'),
cameraFov: this.model.get('camera_fov'),
cameraUpAxis: this.model.get('camera_up_axis'),
colorbarObjectId: this.model.get('colorbar_object_id'),
cameraAnimation: this.model.get('camera_animation'),
name: this.model.get('name'),
Expand Down Expand Up @@ -563,6 +565,10 @@ class PlotView extends widgets.DOMWidgetView {
this.K3DInstance.setCameraDampingFactor(this.model.get('camera_damping_factor'));
};

_setCameraUpAxis() {
this.K3DInstance.setCameraUpAxis(this.model.get('camera_up_axis'));
};

_setClippingPlanes() {
this.K3DInstance.setClippingPlanes(this.model.get('clipping_planes'));
};
Expand Down
7 changes: 5 additions & 2 deletions js/src/providers/threejs/helpers/THREE.OrbitControls.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { setupUpVector } = require('./../../../core/lib/cameraUpAxis');

/* eslint-disable */
module.exports = function (THREE) {
THREE.OrbitControls = function (object, domElement) {
THREE.OrbitControls = function (object, domElement, K3D) {

if (domElement !== undefined) {
this.domElement = domElement;
Expand Down Expand Up @@ -144,10 +146,11 @@ module.exports = function (THREE) {

// this method is exposed, but perhaps it would be better if we can make it private...
this.update = function (silent) {

const offset = new THREE.Vector3();

// so camera.up is the orbit axis
setupUpVector(object, K3D.parameters.cameraUpAxis);

const quat = new THREE.Quaternion().setFromUnitVectors(object.up, new THREE.Vector3(0, 1, 0));
const quatInverse = quat.clone().invert();

Expand Down
15 changes: 9 additions & 6 deletions js/src/providers/threejs/helpers/THREE.TrackballControls.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable */
const { setupUpVector } = require('./../../../core/lib/cameraUpAxis');

module.exports = function (THREE) {
THREE.TrackballControls = function (object, domElement) {
THREE.TrackballControls = function (object, domElement, K3D) {
const scope = this;
const STATE = {
NONE: -1, ROTATE: 0, ZOOM: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_ZOOM_PAN: 4,
Expand Down Expand Up @@ -46,7 +47,7 @@ module.exports = function (THREE) {
this.minDistance = 0;
this.maxDistance = Infinity;

this.mouseButtons = {LEFT: THREE.MOUSE.ROTATE, MIDDLE: THREE.MOUSE.DOLLY, RIGHT: THREE.MOUSE.PAN};
this.mouseButtons = { LEFT: THREE.MOUSE.ROTATE, MIDDLE: THREE.MOUSE.DOLLY, RIGHT: THREE.MOUSE.PAN };

// internals

Expand Down Expand Up @@ -91,9 +92,9 @@ module.exports = function (THREE) {

// events

const _changeEvent = {type: 'change'};
const _startEvent = {type: 'start'};
const _endEvent = {type: 'end'};
const _changeEvent = { type: 'change' };
const _startEvent = { type: 'start' };
const _endEvent = { type: 'end' };

// methods

Expand Down Expand Up @@ -292,6 +293,8 @@ module.exports = function (THREE) {
};

this.update = function (silent) {
setupUpVector(scope.object, K3D.parameters.cameraUpAxis);

_eye.subVectors(scope.object.position, scope.target);

if (!scope.noRotate) {
Expand Down Expand Up @@ -648,7 +651,7 @@ module.exports = function (THREE) {

this.domElement.addEventListener('pointerdown', onPointerDown);
this.domElement.addEventListener('pointercancel', onPointerCancel);
this.domElement.addEventListener('wheel', onMouseWheel, {passive: false});
this.domElement.addEventListener('wheel', onMouseWheel, { passive: false });
this.domElement.addEventListener('contextmenu', contextmenu);

currentDocument.addEventListener('keydown', keydown);
Expand Down
1 change: 1 addition & 0 deletions js/src/providers/threejs/initializers/Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module.exports = function (K3D) {

if (array.length === 9) {
this.controls.object.up.fromArray(array, 6);
this.axesHelper.camera.up.copy(this.controls.object.up);
}

this.controls.target.fromArray(array, 3);
Expand Down
4 changes: 2 additions & 2 deletions js/src/providers/threejs/initializers/Canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function addEvents(self, K3D, controls) {
}

function createTrackballControls(self, K3D) {
const controls = new THREE.TrackballControls(self.camera, self.renderer.domElement);
const controls = new THREE.TrackballControls(self.camera, self.renderer.domElement, K3D);

controls.type = cameraModes.trackball;
controls.rotateSpeed = K3D.parameters.cameraRotateSpeed;
Expand All @@ -54,7 +54,7 @@ function createTrackballControls(self, K3D) {
}

function createOrbitControls(self, K3D) {
const controls = new THREE.OrbitControls(self.camera, self.renderer.domElement);
const controls = new THREE.OrbitControls(self.camera, self.renderer.domElement, K3D);

controls.type = cameraModes.orbit;
controls.rotateSpeed = K3D.parameters.cameraRotateSpeed;
Expand Down
14 changes: 14 additions & 0 deletions k3d/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,7 @@ def plot(
camera_zoom_speed=1.2,
camera_pan_speed=0.3,
camera_damping_factor=0.0,
camera_up_axis='none',
fps=25.0,
minimum_fps=-1,
fps_meter=False,
Expand Down Expand Up @@ -2130,6 +2131,18 @@ def plot(
Camera pan speed, by default 0.3.
camera_damping_factor : float, optional
Camera intensity of damping, by default 0.0.
camera_up_axis: `str`.
Fixed up axis for camera.
Legal values are:
:`x`: x axis,
:`y`: y axis,
:`z`: z axis,
:`none`: Handling click_callback and hover_callback on some type of objects.
fps : float, optional
Animations FPS, by default 25.0.
minimum_fps: `Float`.
Expand Down Expand Up @@ -2173,6 +2186,7 @@ def plot(
camera_zoom_speed=camera_zoom_speed,
camera_damping_factor=camera_damping_factor,
camera_pan_speed=camera_pan_speed,
camera_up_axis=camera_up_axis,
auto_rendering=auto_rendering,
fps=fps,
minimum_fps=minimum_fps,
Expand Down
16 changes: 16 additions & 0 deletions k3d/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ class Plot(widgets.DOMWidget):
Camera Field of View.
camera_damping_factor: `Float`.
Defines the intensity of damping. Default is 0 (disabled).
camera_up_axis: `str`.
Fixed up axis for camera.
Legal values are:
:`x`: x axis,
:`y`: y axis,
:`z`: z axis,
:`none`: Handling click_callback and hover_callback on some type of objects.
snapshot_type: `string`.
Can be 'full', 'online' or 'inline'.
axes: `list`.
Expand Down Expand Up @@ -159,6 +171,7 @@ class Plot(widgets.DOMWidget):
camera_zoom_speed = Float().tag(sync=True)
camera_pan_speed = Float().tag(sync=True)
camera_damping_factor = Float().tag(sync=True)
camera_up_axis = Unicode().tag(sync=True)
clipping_planes = ListOrArray(empty_ok=True).tag(sync=True)
colorbar_object_id = Int(-1).tag(sync=True)
colorbar_scientific = Bool(False).tag(sync=True)
Expand Down Expand Up @@ -203,6 +216,7 @@ def __init__(
camera_rotate_speed=1.0,
camera_zoom_speed=1.2,
camera_pan_speed=0.3,
camera_up_axis='none',
snapshot_type='full',
camera_no_pan=False,
camera_fov=45.0,
Expand Down Expand Up @@ -252,6 +266,7 @@ def __init__(
self.camera_pan_speed = camera_pan_speed
self.camera_damping_factor = camera_damping_factor
self.camera_fov = camera_fov
self.camera_up_axis = camera_up_axis
self.axes = axes
self.axes_helper = axes_helper
self.axes_helper_colors = axes_helper_colors
Expand Down Expand Up @@ -498,6 +513,7 @@ def get_plot_params(self):
"cameraZoomSpeed": self.camera_zoom_speed,
"cameraPanSpeed": self.camera_pan_speed,
"cameraDampingFactor": self.camera_damping_factor,
"cameraUpAxis": self.camera_up_axis,
"name": self.name,
"height": self.height,
"cameraFov": self.camera_fov,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "k3d",
"version": "2.16.0",
"version": "2.17.0",
"description": "3D visualization library",
"keywords": [
"jupyter",
Expand Down

0 comments on commit ecfc809

Please sign in to comment.