Camera Operations
Interface List
| Interface Name | Description |
|---|---|
| Rotate Camera | Rotate the camera |
| Translate Camera | Translate the camera |
| Zoom Camera | Zoom the camera |
| Set Up Direction | Set the up direction |
Get the camera object through the scene, as shown in the following code
let camera = scene.getCurrentViewPort().getCamera()
Rotating the Camera
// Rotating the camera requires creating a rotation quaternion first; the corresponding type is M3D.M3DMath.Quaternion
// The constructor supports several forms:
// 1. M3D.M3DMath.Quaternion: creates a new instance from an existing rotation quaternion
// 2. M3D.M3DMath.Matrix3: creates a rotation quaternion instance from a matrix; the matrix data consists of the three axis orientations, where each column represents one orientation (x, y, and z, respectively)
// 3. angle: creates a rotation quaternion instance from a rotation angle, of type number
// 4. data: creates a rotation quaternion instance from binary data, of type Float32Array; uses the values at indices 0, 1, 2, and 3 of data, corresponding to w, x, y, and z
// 5. angle, axis: creates a rotation quaternion instance from a rotation angle and a rotation axis, of type number and M3D.M3DMath.Vector3, respectively
// 6. x, y, z: creates a quaternion instance from rotation angles around the three directions, of type number; rotation is applied around Z first, then X, then Y
// 7. start, end: creates a quaternion instance from a start vector and an end vector, of type M3D.M3DMath.Vector3
// 8. xAxis, yAxis, zAxis: creates a rotation quaternion from the three axis directions, corresponding to the X-axis, Y-axis, and Z-axis directions, respectively, of type M3D.M3DMath.Vector3
// 9. w, x, y, z: creates a new rotation quaternion instance from quaternion data
let quaternion = new M3D.M3DMath.Quaternion(x, y, z);
// Set the camera's rotation center; the parameter is a 3D coordinate point of type M3D.M3DMath.Vector3
camera.setRotateCenter(center);
// Rotate through the camera interface; the second parameter specifies the rotation mode — generally, rotation in the world coordinate system is M3D.TS_WORLD
camera.rotateAroundCenter(quaternion, M3D.TS_WORLD);
Translating the Camera
// Call the camera interface to set camera translation
// The parameter delta is the translation amount, of type M3D.M3DMath.Vector3; space is the translation mode — generally, translation in the world coordinate system is M3D.TS_WORLD
camera.translate(delta, space);
Zooming the Camera
// Call the camera interface to set camera zoom
// The parameter is the zoom factor, of type number. It is relative to the current zoom factor, and the zoom factor is limited to the range 0.01–1000.
camera.zoomView(scaleFactor);
// After zooming the camera, call the interface to update the viewport
camera.markDirty();
Setting the Up Direction
// To set the up direction, you must first set the configuration and then call the interface to set it. Both interfaces below need to be called; the first parameter is the configuration name, and the second is the up direction.
// 0: X+ (1,0,0); 1: X- (-1,0,0); 2: Y+ (0,1,0); 3: Y- (0,-1,0); 4: Z+ (0,0,1); 5: Z- (0,0,-1)
M3D.Config.Parameters.getInstance().setParameter(M3D.Config.verticalAxis, 2);
M3D.Config.ViewPortParameters.getInstance().setParameter(M3D.Config.verticalAxis, 2);
// Then call the interface to set the up direction
scene.getCurrentViewPort().setUpDirection(new M3D.M3DMath.Vector3(0, 1, 0));