Camera controls module
Onirix Camera Controls Module is an addon to Onirix Embed SDK that allows manipulating the camera in a more intuitive and direct way than using the low-level camera actions provided by it. It makes many camera-related tasks easy:
- Animating the camera when entering a scene.
- Moving the camera to different points of interest around the scene.
- Customizing camera controls in an advanced way, e.g., using interface elements.
- Restricting camera movement in some ways.
Onirix Embed SDK camera actions should not be used directly when using the Camera Controls Module.
Initialization
Before accessing any of its functions, the camera module must be initialized by providing an instance of the Onirix Embed SDK as shown below:
import OnirixEmbedSDK from 'https://www.unpkg.com/@onirix/embed-sdk@1.10.0/dist/ox-embed-sdk.esm.js';
import OnirixCameraModule from 'https://www.unpkg.com/@onirix/camera-controls-module@1.1.0/dist/ox-camera-controls-module.esm.js';
const embedSDK = new OnirixEmbedSDK();
embedSDK.connect();
const camera = new OnirixCameraModule(embedSDK);
The constructor of OnirixCameraModule may include an additional parameter "enabledPlatforms", which allows to specify from with environments camera controls are enabled. Allowed options are:
- PREVIEW (default): Applies to scene visualization from Onirix's integrated 3D viewer.
- AR: Applies to scene visualization in Augmented Reality.
Options must be especified in array form:
// Default: Only on PREVIEW mode
const camera = new OnirixCameraModule(embedSDK);
// Only on AR mode
const camera = new OnirixCameraModule(embedSDK, ['AR']);
// Both modes: AR and PREVIEW
const camera = new OnirixCameraModule(embedSDK, ['AR', 'PREVIEW']);
Camera manipulation and animations
The camera can be moved using the camera.animateTo(...) function. This function receives the following parameters:
-
position_x, position_y, and position_z: The coordinates the camera should move to. If any of them is undefined, the camera will stay in place.
-
look_at_x, look_at_y, and look_at_z: The coordinates the camera should look at. If any of them is undefined, the camera will keep looking at the point it is currently looking at.
-
mode: How to animate the transition to these positions. It is specified by a string with two components: an interpolator (a component that describes the path the camera should take) and an easing function (a component that describes the speed of the camera along said path). This string must be formatted as
<interpolator> <ease function>
. Alternatively, any of the components can be omitted. If the mode is optional (can be undefined).There are two interpolators available:
- linear: The camera moves in a straight line.
- spherical: The camera moves following an arc around a center. The default center is the target of the controls or, if they are not defined, the x=0, y=0, z=0 point.
There are seven easing functions available:
- linear-in-out: The camera moves always at the same speed.
- ease-in: The camera starts slower.
- ease-out: The camera reduces its speed as it reaches its destination.
- ease-in-out: The camera accelerates at the beginning and decelerates as it reaches the end.
- bounce-in: The camera anticipates the movement by going in the opposite direction for a short period of time.
- bounce-out: The camera overshoots the destination for a short period of time.
- bounce-in-out: The camera anticipates the movement by going in the opposite direction and overshoots the destination for a short period of time.
Some examples of valid modes are 'linear', 'ease-in-out', 'spherical bounce-out'.
-
time: Length of the animation in seconds. If not provided it is assumed to be 0 .
-
center_x, center_y, and center_z: When using a spherical interpolator, the center of the arc. Otherwise it is ignored. Its default value is the target of the controls or, if they are not defined, the x=0, y=0, z=0 point.
Controls
The camera controls are the components that describe how user interactions (touches, mouse clicks, key presses...) are translated to camera movements around the scene. Camera controls are optional and you may use none if the camera should not be controlled by the user.
Currently, the camera controls module supports the following camera controls:
- Orbit controls: The camera moves around a point (the target). The user can zoom using the mouse wheel or by doing pinch gestures, pan by dragging while pressing right mouse button or sliding with two fingers, and cycle around by dragging while pressing left mouse button or sliding with a single finger. They can be enabled using camera.enableOrbitControls().
The function camera.disableControls() can be used to disable the current camera controls.
Orbit controls
Some functions allow setting limits to the camera transformations that the user can perform:
- camera.setOrbitControlsPanRange(min_x, max_x, min_y, max_y, min_z, max_z): allows setting a minimum and maximum pan value for each of the coordinate axes.
- camera.setOrbitControlsRotateRange(min_x, max_x, min_y, max_y, min_z, max_z): allows setting a minimum and maximum rotation value for each of the coordiante axes. This rotation is measured as the angle from the camera to the axis.
- camera.setOrbitControlsZoomRange(min, max): allows setting a minimum and maximum zoom value.
Also, there is a function that allows setting the orbit target: camera.setOrbitControlsTarget(x, y, z).
If you want to copy any camera position in your scene editor take into account that you can use the button "copy camera position" within the main scene controls
Examples of camera poses in 3D mode
You can try these following examples in this bluetooth amplifier experience with camera poses.
Moving to a position on scene load
With this you can create loading effects in 3D scenes like this, for example to showcase a new product:
import OnirixEmbedSDK from 'https://www.unpkg.com/@onirix/embed-sdk@1.10.0/dist/ox-embed-sdk.esm.js';
import OnirixCameraModule from 'https://www.unpkg.com/@onirix/camera-controls-module@1.1.0/dist/ox-camera-controls-module.esm.js';
/**
* Name of the collection containing the elements to be displayed at the start of the scene.
*/
const MAIN_CONTENT = 'main-collection';
const embedSDK = new OnirixEmbedSDK();
embedSDK.connect();
const camera = new OnirixCameraModule(embedSDK, ['PREVIEW']);
camera.enableOrbitControls();
embedSDK.subscribe(OnirixEmbedSDK.Events.SCENE_LOAD_END, () => {
camera.animateTo(
-0.6662117640795381,
0.8871396686378923,
1.4374782248840745,
0.0753237898528869,
0.2700653804410498,
-0.15988023261712958,
'spherical ease-in-out',
3
);
// We use requestAnimationFrame to ensure that content
// is enabled after motion is initiated and to prevent unwanted running conditions.
window.requestAnimationFrame( () => embedSDK.enable(MAIN_CONTENT));
});
If you use camera movements when loading the scene you should keep in mind that initially the scene will be displayed in a fixed position and then the movement will be applied. To avoid unwanted failures you can disable the initial content of your scene and enable it right after starting the camera movement.
Change camera positions on element clicks
You can also include some annotations, and use the "click" event in each element to launch a new camera orbit to zoom in the product, this way:
In order to do this you can subscribe the ELEMENT_CLICK event, and for each Label, include the new camera position:
embedSDK.subscribe(OnirixEmbedSDK.Events.ELEMENT_CLICK, (params) => {
console.log(`Element ${params.oid} clicked.`, params);
if(params.name === 'Controllers'){
camera.animateTo(-0.07941945411482829, 1.0063628746921263, 0.1966578407826555, -0.06569062491463451, 0.20609320326495678, -0.24894345145422433, 'spherical ease-in-out', 3);
}
else if(params.name ==='Back'){
camera.animateTo(-0.015122002120656501, 0.4221901717125174, -1.3529244316196265, -0.06569062491463451, 0.20609320326495678, -0.24894345145422433, 'spherical ease-in-out', 3);
}
});
Orbit and zoom animation on scene load
As the start of an experience, and by combining the camera movements in several phases, effects like this can also be achieved:
import OnirixEmbedSDK from 'https://www.unpkg.com/@onirix/embed-sdk@1.10.0/dist/ox-embed-sdk.esm.js';
import OnirixCameraModule from 'https://www.unpkg.com/@onirix/camera-controls-module@1.1.0/dist/ox-camera-controls-module.esm.js';
const embedSDK = new OnirixEmbedSDK();
embedSDK.connect();
const camera = new OnirixCameraModule(embedSDK);
camera.enableOrbitControls();
embedSDK.subscribe(OnirixEmbedSDK.Events.SCENE_LOAD_END, async () => {
await camera.animateTo(
0, 0.5, -5,
0, 0.5, 0
);
await camera.animateTo(
0, 0.5, 5,
0, 0.5, 0,
'spherical ease-in-out',
2,
0, 0.5, 0
);
await camera.animateTo(
0, 0.5, 2,
0, 0.5, 0,
'linear ease-in-out',
2,
0, 0.5, 0
);
});