KlippControls
KlippControls is a whole camera in one piece: drag to orbit around a target, right-drag, Shift-drag or drag with two fingers to pan, middle-drag, Ctrl-drag, scroll or pinch to zoom. It's a virtual camera like any other, so it takes part in priority and blending, and takes Extensions and Noise. In React the input is built in. On the core and in three.js, connectControls from @pmndrs/klipp/dom connects it to the canvas in one call.
How it moves
Orbit. horizontal and vertical move the camera around the center of the orbit, in degrees, as in OrbitFollow. The camera always looks at that center.
Zoom. zoom is the natural log of how many times closer than distance the camera is: 0 is distance, ln 2 twice as close, -ln 2 twice as far. minDistance and maxDistance limit it. In an orthographic view the view size scales instead.
Pan. panX and panY take the drag in pixels and move the center of the orbit so the point under the cursor follows it. pan holds how far panning has moved the center from the target, in the frame of orbit.bindingMode. Set it back to [0, 0, 0] to center on the target again. Panning measures the drag against the viewport height from klipp.setSize, in CSS pixels. <Klipp> sets it for you.
Pan lock. panLock stops panning along the axes it holds: { y: true } pans on the ground, { y: true, z: true } only left and right. With frame: 'view', the default, x and z turn with the camera. With frame: 'orbit' they are the axes of orbit.bindingMode: the world's, or the target's with lockToTarget.
Pan bounds. With panBounds, a box [minX, minY, minZ, maxX, maxY, maxZ] or a sphere { center, radius } in world space, the center of the orbit stays inside it, also when the target moves out. Within panSlowingDistance of an edge, panning toward it slows down the closer the center gets. In three.js and React panBounds also takes a Box3 or a Sphere, read every frame.
Target. The camera orbits an Object3D, a ref or a fixed point, and follows it when it moves. Without a target it orbits the world origin, and panning moves it freely. With orbit.damping, the camera glides to a new target instead of jumping.
Starting point. A position in the camera's initialState sets where the camera starts: horizontal and vertical from its direction around the target, and zoom from its distance. With BlendHints.inheritPosition it starts from the shot on screen each time it becomes the active one: at the shot's position, orbiting the point the shot looked at.
Settings
The camera's own settings, name, priority, active, hints, initialState and standbyUpdate, work as on VirtualCamera.
Axes
Every axis starts at its center. Their damping smooths the input: 0, the default, follows the hand right away. With recentering, horizontal, vertical and zoom ease back to their centers after a while without input. With autoMove on horizontal, the camera turns around the target. Input and moves from methods restart the wait on every axis.
<KlippControls target={subject} horizontal={{ autoMove: { enabled: true } }} />
Input
<KlippControls> and connectControls(klipp, controls, element, options) take the same input options. connectControls takes input only while the camera is on screen. It returns the controller, an InputControllerDom for pointer lock or raw input, and stop().
controls.inputConfig(overrides) returns the mapping with the axes themselves, for an InputControllerDom of your own.
Methods
Every method but stop and forward takes damping. 0, the default, jumps there. Above it, the camera eases there with that response time, and input stops it where it is.
In three.js and React, moveTo and setLookAt also take a Vector3, and fit also takes a Box3, a Sphere, or an Object3D, framed by every vertex of it and its children, and forward also writes into a Vector3.
Events
Besides the transition events of every camera, KlippControls tells when the viewer controls it and when it moves:
The wheel moves the camera without a controlStart. The events come once the frame is over, so a listener can start another move. A camera turned off or removed while controlled or moving sends controlEnd and rest.
In React, <KlippControls.Events> takes them as onControlStart, onControlEnd, onWake and onRest, next to the transition callbacks. For example, to lower the resolution while the camera moves:
function RegressWhileMoving() {
const regress = useThree((state) => state.performance.regress);
const moving = useRef(false);
useFrame(() => {
if (moving.current) regress();
});
return <KlippControls.Events onWake={() => (moving.current = true)} onRest={() => (moving.current = false)} />;
}
<KlippControls target={subject}>
<RegressWhileMoving />
</KlippControls>;
In core and three.js, listen with controls.addEventListener('rest', listener).
Extensions and Noise
Extensions and Noise go on KlippControls as on any camera. The Body and the Aim are built in: setting another one only logs a warning. For a different Body or Aim, build a VirtualCamera with OrbitFollow.
Usage
import { KlippControls, targetPose } from '@pmndrs/klipp';
import { connectControls } from '@pmndrs/klipp/dom';
const model = targetPose.create(); // keep model.position up to date
const controls = new KlippControls('controls', { target: model, priority: 10, distance: 8, minDistance: 2 });
klipp.add(controls);
klipp.setSize(canvas.clientWidth, canvas.clientHeight);
connectControls(klipp, controls, canvas);
Several cameras with their own connectControls never react to the same drag: each takes input only while it is on screen.
import { connectControls } from '@pmndrs/klipp/dom';
import { KlippControlsThree } from '@pmndrs/klipp/three';
const controls = new KlippControlsThree('controls', { target: model, priority: 10, distance: 8, minDistance: 2 });
klipp.add(controls);
klipp.setSize(innerWidth, innerHeight);
connectControls(klipp, controls, renderer.domElement);
Several cameras with their own connectControls never react to the same drag: each takes input only while it is on screen.
import { Extension, Klipp, KlippControls } from '@pmndrs/klipp/react';
<Klipp>
<KlippControls name="controls" priority={10} target={modelRef} distance={8} minDistance={2}>
<Extension.Confiner bounds={room} />
</KlippControls>
</Klipp>
Children add Extensions and Noise like in <VirtualCamera>. ref gives you the KlippControlsThree, for the methods, <KlippControls.Events> listens to its transitions like <VirtualCamera.Events>, and <KlippControls.WhileOnScreen> works like <VirtualCamera.WhileOnScreen>.
Framing a model
const controls = useRef<KlippControlsThree>(null);
<KlippControls ref={controls} name="controls" target={modelRef} />
// in a click handler
controls.current?.fit(modelRef.current, { padding: 0.2, damping: 0.4 });
Turning a source off
<KlippControls name="controls" target={modelRef} mouseButtons={{ right: null }} touches={{ two: null, three: null }} />
The same options go to connectControls(klipp, controls, canvas, { mouseButtons: { right: null } }) elsewhere.