1.0.5 • Published 6 years ago

regl-inertia-camera v1.0.5

Weekly downloads
5
License
MIT
Repository
github
Last release
6 years ago

regl-inertia-camera

A 3D spherical coordinate camera for regl, for desktop and mobile

Introduction

A 3D spherical coordinate camera with rotation, panning, zooming, and pivoting (i.e. yaw and pitch). Designed to function on desktop and mobile.

Example

const camera = require('regl-inertia-camera')(regl, {
  phi: 0.5,
  theta: 1,
  distance: 20,
});

camera(() => {
  reglDrawCmd();
});

See demo.js for fully worked example.

Usage

camera = require('regl-inertia-camera')(regl[, opts])

Given existing regl instance and options, returns a camera instance which sets camera projection and view matrix context and uniforms. For convenience, view, projection and eye are exposed through regl context variables as well as uniforms. Configuration options are:

OptionTypeDefaultMeaning
elementHTML elementwindowelement to which to attach
viewUniformNameStringviewname of view matrix uniform
projectionUniformNameStringprojectionname of projection matrix uniform
eyeUniformNameStringeyename of eye vector uniform

The camera may be invoked as:

camera(state, callback)

where state is an object containing changes to the camera state variables, defined below, and callback is a function invoked within the regl context of the camera. Callback receives camera.state as its parameter.

The returned camera contains the following computed properties which will be overwritten on every draw frame and so cannot (meaningfully) be modified:

Camera variableTypeMeaning
aspectRatioNumbercurrent aspect ratio
eyevec3location of camera
heightNumbercurrent height of view
projectionmat4projection matrix
projectionInvmat4inverse projection matrix
viewmat4view matrix
viewInvmat4inverese view matrix
widthNumbercurrent width of view

The returned camera contains a .state property which contains the following state values, all of which may be written directly. On each invocation of draw these parameters will be checked for differences and will trigger a dirty camera where applicable so that the view is redrawn automatically.

State variableTypeDefault/InitialMeaning
centervec3[0, 0, 0]point at the center of the view
distanceNumber10distance of eye from center
dirtyBooleantruetrue when camera view has changed
dPhiNumber0current phi inertia of camera
dThetaNumber0current theta inertia of camera
enablePanBooleantrueallow panning
enablePivotBooleantrueallow pivoting (yaw and pitch) of view
enableRotationBooleantrueallow rotation view
enableZoomBooleantrueallow zooming of view
farNumber100far clipping plane
fovYNumberπ / 4field of view in the vertical direction, in radians
nearNumber0.1near clipping plane
panDecayTimeNumber100half life of panning inertia in ms
panXNumber0current horizontal amount to pan at next draw
panYNumber0current vertical amount to pan at next draw
panZNumber0current in/out of plane amount to pan at next draw
phiNumber0azimuthal angle of camera
pitchNumber0current amount to pitch at next draw, in radians
rotationCentervec3[0, 0, 0]point about which the view rotates
rotationSpeedNumber1.0Speed of rotation interaction
rotationDecayTimeNumber100half life of rotation inertia in ms
rotateAboutCenterBooleanfalseIf false, rotate about rotationCenter, otherwise rotates about the current view center.
thetaNumber0horizontal rotation of camera
upvec3[0, 1, 0]vertical direction
wheelSpeedNumber1.0Speed of mouse wheel interaction
x0Numbernullcurrent horizontal location of interaction, in pixels
y0Numbernullcurrent vertical location of interaction, in pixels
yawNumber0current amount to yaw at next draw, in radians
zoomNumber0current amount to zoom at next draw (0 = no change)
zoomDecayTimeNumber100half life of zooming inertia in ms

Finally, the returned camera contains the following methods:

MethodMeaning
.taint()Mark the view "dirty" to trigger drawing on the next frame.
.resize()Recompute the viewport size and aspect ratio and mark the view dirty

See also

Credits

This module is heavily based on the work of mikolalysenko and mattdesl. Development supported by Standard Cyborg.

License

© 2018 Ricky Reusser. MIT License.