cameraRigs.package
This package holds more than this page lists. See all of its files in its world.
README.md
camera_rig
In a world that installs the Controllers library (
world.installLibrary({ world = "034d26cf-dc3d-4123-abad-3944f97c6a60", as = "controllers" })), every package name on this page is written with@controllers::in front of it, andasset.list("component")lists each one under that name.
Shared internals for the camera rigs — the parts every follow camera needs before it gets to have a personality.
The built-in rigs (chase, mount, cinematic, birds_eye,
third_person_follow, orbital_follow and the rest) resolve their follow
target the same way, read where it is drawn the same way, ease toward their
framing measured from the subject the same way, turn a look-around the same
way, avoid geometry the same way, and write their pose the same way. Only the
placement of the camera relative to the subject differs, so everything else
lives here and the rigs stay small enough to read in one screen.
local CameraRig = require("cameraRigs.camera_rig")
local follow = CameraRig.newTrail()
function update(dt)
local target = CameraRig.resolveFollow(public.entity.id, public.follow)
if target == nil then return end
local tx, ty, tz = CameraRig.drawnPosition(target)
if tx == nil then return end
local px, py, pz = tx, ty + public.height, tz
local h, radius = CameraRig.ringAt(public.rings, public.orbitT)
local cx = px + math.sin(public.yaw) * radius
local cy = py + h
local cz = pz + math.cos(public.yaw) * radius
cx, cy, cz = CameraRig.pullIn(px, py, pz, cx, cy, cz, 0.3,
CameraRig.pullInExclude(target, public.cameraIgnore))
cx, cy, cz = CameraRig.trail(follow, tx, ty, tz, cx, cy, cz,
public.followTau, public.followTau, public.followTau, dt)
CameraRig.applyPose(public.entity.id, cx, cy, cz, px, py, pz)
end
Exports
CameraRig.damp(current, goal, tau, dt) -> number— exponential convergence towardgoal.tauis the time constant in seconds: the smaller it is the tighter the camera tracks, and0snaps. Frame-rate independent, so a hitched 100 ms frame and six clean 16 ms frames land in the same place, and no frame length overshoots the goal. Damp each component separately.CameraRig.drawnPosition(id),CameraRig.drawnForward(id),CameraRig.drawnPose(id) -> (px, py, pz, qx, qy, qz, qw): where a subject is drawn this frame, which way it faces there, and its whole drawn pose. A body the simulation moves is drawn blended between physics ticks; a rig placed from these holds it steady on screen at any frame rate.drawnPoseis what a rig riding the body (a seat, a turret) places itself in the body's frame from.CameraRig.newTrail()andCameraRig.trail(state, subjectX, subjectY, subjectZ, goalX, goalY, goalZ, tauX, tauY, tauZ, dt) -> (x, y, z): ease the camera toward its goal measured as an OFFSET from the subject. The subject's own motion carries the camera at any speed, so a camera framed 6 m behind stays 6 m behind at 5 m/s and at 200 m/s, and the time constants soften only a change of the framing. Damping the camera's world position instead leaves itspeed x taubehind a moving subject.CameraRig.carryTrail(state, frame, fromX, fromY, fromZ, toX, toY, toZ): carry a change of the framing into the trail on this frame, so the camera stands at the new framing at once; what the trail was still closing turns about up with the framing's heading.CameraRig.lookIsDirect(class)says which looks a rig carries this way: the mouse's ("kbm", the device class a look control'sonInputnames as its fourth argument), whose movement the view follows one to one. A stick's or a finger's look eases through the damping.CameraRig.tauOf(remaining) -> number: the time constant of a per-second blend field (the share of a gap left after one second), fordampandtrail.CameraRig.newLook()andCameraRig.lookAround(state, dx, dy, limits, dt) -> (yaw, pitch): a look-around turned by the look control withinlimits({ sensitivity, maxYaw, minPitch, maxPitch, returnDelay, returnTime, invertY? }, radians and seconds) that comes back to restreturnDelayseconds after the input stops. Yaw is positive right, pitch positive up.CameraRig.newPullIn()andCameraRig.pullInEased(state, pivotX, pivotY, pivotZ, camX, camY, camZ, radius, exclude, outTime, dt, frame?) -> (x, y, z):pullInthat never cuts. Sweeps of the line ahead, from where the pivot is moving to (no farther than its own path is clear, so a subject falling to the ground is swept from the ground) and along where the line is turning over the next 0.8 s, bring the camera in ahead of what is about to come between it and its subject, at the speed that has it clear when the line gets there; something no sweep could see coming brings it in on the frame it blocks the view, so no frame is drawn from behind geometry. What the sweeps ahead found stays held for the time ahead they last found it at, so frames of uneven length (a long frame the subject's motion comes late to, then the short one it comes in) neither ease the camera back out nor cut it in, and motion that comes in a long frame (a walk that starts in one) is swept ahead at once. It eases back out overoutTimeonce the way is clear. The camera stands no nearer the pivot than 0.4 m while there is room round it: a line with less room turns the camera round the pivot aboutframe's up (the world's when left out) to the nearest turn that has the room, and back onto the line once the line has it. A pivot touching a collider is swept with a thinner sphere, and one nearer it than the thinnest sphere has room for by a ray; a pivot inside one holds the camera as far out as it stood. A camera standing at its framing moves with that framing, nearer or farther, on the frame it moves. A rig whose framed point lags its subject sweeps from the subject's own pivot, so the sweep finds what stands between the subject and the camera.CameraRig.newFade(),CameraRig.thinOccluders(state, aimX, aimY, aimZ, camX, camY, camZ, exclude, limits) -> { string },CameraRig.excludingThin(exclude, thin) -> { string },CameraRig.fadeOccluders(state, thin, camX, camY, camZ, aimX, aimY, aimZ, subjectId, subjectX, subjectY, subjectZ, limits, dt),CameraRig.fadeReport(state)andCameraRig.clearFade(state): a follow camera fading what fills its view in place of its subject.thinOccludersnames the colliders round the framing line no wider thanlimits.widthacross it at their narrowest and no deeper along it (pillars, poles, railings); a rig handsexcludingThin(exclude, thin)topullInEasedso they do not pull the camera in, thenfadeOccludersfades the ones on the line from the camera to the aim point or the middle of the subject's body tolimits.occluder, and the subject's body by how near the camera stands to the aim point (limits.bodyat 0.4 m, none pastlimits.bodyDistance), each eased overlimits.time. A fade leaves that share of the entity's and its descendants' pixels undrawn in this engine's camera views (visuals.setCameraFade); shadow views draw it whole.clearFadetakes every fade back at once.CameraRig.pullInAim(fromX, fromY, fromZ, framedX, framedY, framedZ, heldX, heldY, heldZ, aimX, aimY, aimZ) -> (x, y, z): the point a camerapullInEasedstood looks at: the aim, brought toward the sweep's start in step with how far the camera came in and moved with the camera where it stands turned round the pivot, so the camera looks along the line its framing looks along and faces its subject even when it stands nearer the subject than the point a lagging rig frames.CameraRig.newTracking(),CameraRig.track(state, subjectX, subjectY, subjectZ, frame, limits, dt, airborne?) -> (x, y, z)andCameraRig.snapTracking(state, x, y, z): the point a follow rig frames, closing on its subject overlimits.horizontalLag(across the ground offrame) andlimits.verticalLag(along its up), held withinlimits.maxHorizontalLag/limits.maxVerticalLaghowever fast the subject goes. The same call measures the subject's velocity (CameraRig.trackingVelocity) and eases a lead along its motion,limits.leadTimeseconds of it held tolimits.maxLeadand eased overlimits.leadSmoothing(CameraRig.trackingLead). A subject that moves more than 50 m in one frame was placed there, and tracking starts over on it. Whileairborneis true andlimits.jumpBandis above 0, the point holds its height as the subject rises up tojumpBandabove it, and past the band it rises with the subject, which stands no farther above it than the band; a landing at another height is eased to oververticalLag, and a fall below the held height is followed as on the ground.CameraRig.airborne(subjectId)reads whether a subject is off the ground from itsCharacterControllerorMovementState.CameraRig.trackingReport(state, recentre)is what a rig'stracking()answers.CameraRig.newRecenter(),CameraRig.recenter(state, heading, goal, looked, weight, delay, time, dt, moving?) -> headingandCameraRig.requestRecenter(state): a heading that turns back towardgoalthe short way round once look input has stopped fordelayseconds (at once after a request; a negativedelayturns only on request), over a time constant oftime, its rate scaled byweight; a frame whose weight is 0 holds the heading and starts the wait over. Told whether the subject moves (moving, fromCameraRig.subjectMoving(frame, vx, vy, vz): faster than 0.5 m/s across the ground), a look the player made holds the heading for as long as the subject stands, and the wait starts once it moves off; a request ends that hold.CameraRig.recenterGoal(frame, subjectId, to, tracking)is the heading behind the subject's facing ("facing") or its motion ("motion"), andCameraRig.recenterWeight(frame, heading, goal, vx, vy, vz)the share of the rate that keeps a camera from turning a subject it steers round in circles: the square of the share of the facing that points away from the camera while the subject moves, 1 at rest.CameraRig.headingOf(frame, x, y, z)is the heading a rig looking along a direction holds.CameraRig.facingSource(subjectId) -> string?: whatvisibleForwardreads the subject's facing from (itsLocomotionorLocomotionFull, the anchor that turns and the direction the component declares along it), as one key, nil while the subject's own transform is its facing. The key changes when the facing becomes known (the component arrives, a body stands up under the character) or is declared anew (Locomotion.forwardorLocomotionFull.faceSignchanges), and holds while the body turns. A follow rig aims behind its subject when it binds it and again when this key changes, so a character whose facing arrives after the camera bound (an avatar that composes late) is framed from behind.CameraRig.ringAt(rings, t) -> (number, number)— height and radius of the orbit attalong the path through the three rings, whereringsis{ bottom = { height, radius }, center = ..., top = ... }.truns 0 at the bottom ring to 1 at the top, passing exactly through the centre ring at 0.5. The path is a curve through the three rings that turns no corner at the centre ring: it heads there halfway between the directions of its two stretches. Out-of-rangetclamps rather than extrapolating.CameraRig.ringLook(rings, aimHeight, radiusScale, t, angle) -> number: where along the path a vertical look ofangleradians puts the camera. Positive moves toward the top ring. Where the path runs within about 78 degrees of round the aim point, the view pitches by the look's angle, so a rig turns at one rate up and down and sideways. Where it heads more steeply toward or away from the aim point, the look moves the camera along it by five times the angle times the camera's distance from the aim point, the farthest a look moves it anywhere on the rings: a stretch with little or no pitch to turn takes a look the way any other stretch does, and rings authored a fraction of a degree apart move the camera alike. The look carries through a ring into the next stretch, a stretch between two rings that stand in one place takes none of it, and the result stays in 0..1.CameraRig.resolveFollow(selfId, followField) -> string?— the entity this rig follows. Takes the rig's ownfollowfield (an entity id or an entity reference) when it is set, and otherwise falls back to theCamera.followslot, which is the standard place every camera behavior reads its target from — that is what makes "swap the behavior, keep the follow target" work. Returnsnilwhen nothing is set AND when the resolved id has no live entity, which is the normal state for the first frames of play mode while the avatar is still spawning.CameraRig.pullIn(pivotX, pivotY, pivotZ, camX, camY, camZ, radius, exclude?) -> (number, number, number): the camera position moved in along the pivot-to-camera line until no collider is between the two. A sphere ofradiusis swept from the pivot, and every collider in the world stops it, whether or not anything draws it: an invisible walk bound and a trigger volume pull the camera in exactly as a wall does.radiusis also the distance kept off the surface the camera lands on;0turns the pull-in off and returns the input untouched.excludeis an entity id, or a list of them, whose colliders the sweep passes through: the subject the camera is looking at, plus anything the camera should see through. A pivot standing against a collider, so the sphere starts touching it, is swept again with half the radius and then a quarter, which slides along it, and a pivot nearer it than that is answered by a ray; a pivot inside a collider has no line out, and the camera position comes back untouched.CameraRig.pullInExclude(subjectId, ignore) -> { string }: the exclude list a rig handspullIn: the subject first, then every entity in the rig'scameraIgnorelist (ids or entity proxies).orbital_followandthird_person_followboth build theirs with it from theircameraIgnorefield. A rig written on this module gives its authors the same control by declaring acameraIgnorelist of its own (Field.list(Field.entityRef(nil, Sync), Sync)) and passing it here.CameraRig.applyPose(selfId, camX, camY, camZ, lookX, lookY, lookZ)— place the camera and aim it at a point. Both the pose and the aim point are in the camera entity's own transform frame: parent-relative when the camera is parented to its subject, world space when it is standalone. The write goes to the local transform, so transform propagation composes the world matrix fromparent_world * localinstead of being fought by a world-space write.