GroupFraming

Moves the camera closer or farther to keep several targets in view.

Keeps every member on screen. It places the camera in line with the group's center, along the camera's current view direction, at the distance where every member fits. By default it never moves the camera closer than the Body placed it; with fitMode: 'rigid' it always sits at exactly that distance, so it also moves in as the group gets smaller.

With an orthographic camera, distance doesn't change what's in view, so it fits the group by orthographicSize instead. The camera keeps the distance the Body gave, and only backs away when a member would end up in front of the near plane. By default it never goes below the size from the lens, so give it one with Lens; with fitMode: 'rigid' it always uses exactly the size that fits.

It doesn't rotate the camera, so pair it with an Aim that already looks at the group. klipp.setSize keeps the viewport size in sync.

Settings

SettingDescriptionDefault
membersThe targets to keep in view. See below.
positionModeHow the group's center, which the camera lines up with, is computed: 'groupCenter' (center of the bounds) or 'groupAverage' (weighted average of positions).'groupCenter'
paddingExtra space around the members, in world units.0
fitMode'ceiling' never moves the camera closer than the Body placed it, or for an orthographic camera never goes below the size from the lens. 'rigid' always fits the group exactly.'ceiling'
framingModeWhich screen dimensions to fit: 'horizontal', 'vertical' or 'horizontalAndVertical'.'horizontalAndVertical'
minDistanceClosest the camera may get to the group, for a perspective camera.0
maxDistanceFarthest GroupFraming will move the camera away from the group, for a perspective camera.Infinity
minOrthographicSizeSmallest orthographicSize it fits the group with, for an orthographic camera.0
maxOrthographicSizeLargest orthographicSize it fits the group with, for an orthographic camera.Infinity
dampingSeconds to catch up with the right distance or size. See Damping.0
maxSpeedMaximum catch-up speed, in units per second.Infinity
screenPositionShifts the view without moving the camera, for example to leave room for UI. [0, 0] is centered.[0, 0]
debugReact only. Draws the padding on screen. See Debugging.false

Members

FieldDescriptionDefault
targetthree.js and React. The member. See Targets.
position, extentCore only. The member's position and size, which you keep up to date.
weightInfluence on the center in 'groupAverage' mode.1
radiusthree.js and React. The member's radius, so its edge stays in view instead of its center.0
sizethree.js and React. The member's box size, used instead of radius.measured for meshes
resolvedCore only. Set to false to skip the member, for example while it isn't loaded yet.true

Usage

Members are plain data you keep up to date:

import { vec3 } from 'math';
import { GroupFramingExtension, groupFraming } from '@pmndrs/klipp';

const a = groupFraming.createMember();
const b = groupFraming.createMember();
b.extent.radius = 2;
camera.addExtension(new GroupFramingExtension([a, b], { padding: 1, damping: 0.5 }));

// every frame, before klipp.update
vec3.copy(a.position, aPosition);
vec3.copy(b.position, bPosition);